Skip to content

Commit

Permalink
Merge pull request #218 from ImMin5/feature-cost-router
Browse files Browse the repository at this point in the history
Fix reset api for data source account
  • Loading branch information
ImMin5 authored May 2, 2024
2 parents 00a74a9 + 91fe1b1 commit f54f330
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 42 deletions.
4 changes: 4 additions & 0 deletions src/spaceone/cost_analysis/manager/cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def _rollback(vo: Cost):
if v_workspace_id:
params["workspace_id"] = v_workspace_id

_LOGGER.debug(
f"[create_cost] v-workspace_id:{v_workspace_id}, v-workspace_id: {workspace_id}"
)

params = self.data_source_rule_mgr.change_cost_data(params, workspace_id)

cost_vo: Cost = self.cost_model.create(params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ def get_workspace_id_from_account_id(
if use_account_routing:
account_match_key_value = plugin_info_metadata.get("account_match_key")
if account_match_key_value:
ds_account_vo = self.get_data_source_account(
data_source_id, account_match_key_value, domain_id
ds_account_vos = self.filter_data_source_accounts(
data_source_id=data_source_id,
account_id=account_match_key_value,
domain_id=domain_id,
)

workspace_id = ds_account_vo.workspace_id
v_workspace_id = ds_account_vo.v_workspace_id
if ds_account_vos.count() > 0:
ds_account_vo = ds_account_vos[0]
workspace_id = ds_account_vo.workspace_id
v_workspace_id = ds_account_vo.v_workspace_id

return workspace_id, v_workspace_id

Expand Down
34 changes: 17 additions & 17 deletions src/spaceone/cost_analysis/manager/data_source_rule_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def delete_data_source_rule_by_vo(data_source_rule_vo):
data_source_rule_vo.delete()

def get_data_source_rule(
self, data_source_rule_id: str, domain_id: str, workspace_id: str = None
self, data_source_rule_id: str, domain_id: str, workspace_id: str = None
):
conditions = {
"data_source_rule_id": data_source_rule_id,
Expand Down Expand Up @@ -106,11 +106,11 @@ def change_cost_data(self, cost_data: dict, workspace_id: str = None) -> dict:
return cost_data

def _apply_data_source_rule_to_cost_data(
self,
cost_data: dict,
data_source_rule_vos: QuerySet,
domain_id: str,
workspace_id: str = None,
self,
cost_data: dict,
data_source_rule_vos: QuerySet,
domain_id: str,
workspace_id: str = None,
):
for data_source_rule_vo in data_source_rule_vos:
is_match = self._change_cost_data_by_rule(cost_data, data_source_rule_vo)
Expand All @@ -128,11 +128,11 @@ def _apply_data_source_rule_to_cost_data(
return cost_data

def _change_cost_data_with_actions(
self,
cost_data: dict,
actions: dict,
domain_id: str,
workspace_id: str = None,
self,
cost_data: dict,
actions: dict,
domain_id: str,
workspace_id: str = None,
):
for action, value in actions.items():
if action == "change_project" and value:
Expand All @@ -148,7 +148,7 @@ def _change_cost_data_with_actions(
)
if project_info:
cost_data["project_id"] = project_info["project_id"]
if not workspace_id:
if cost_data.get("workspace_id") is None:
cost_data["workspace_id"] = project_info["workspace_id"]

elif action == "match_service_account" and value:
Expand All @@ -164,7 +164,7 @@ def _change_cost_data_with_actions(
"service_account_id"
]
cost_data["project_id"] = service_account_info.get("project_id")
if not workspace_id:
if cost_data.get("workspace_id") is None:
cost_data["workspace_id"] = service_account_info.get(
"workspace_id"
)
Expand All @@ -176,11 +176,11 @@ def _change_cost_data_with_actions(
return cost_data

def _get_service_account(
self, target_key, target_value, domain_id: str, workspace_id: str = None
self, target_key, target_value, domain_id: str, workspace_id: str = None
):
if (
f"service-account:{domain_id}:{target_key}:{target_value}"
in self._service_account_info
f"service-account:{domain_id}:{target_key}:{target_value}"
in self._service_account_info
):
return self._service_account_info[
f"service-account:{domain_id}:{target_key}:{target_value}"
Expand Down Expand Up @@ -212,7 +212,7 @@ def _get_service_account(
return service_account_info

def _get_project(
self, target_key, target_value, domain_id: str, workspace_id: str = None
self, target_key, target_value, domain_id: str, workspace_id: str = None
):
if f"project:{domain_id}:{target_key}:{target_value}" in self._project_info:
return self._project_info[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class DataSourceAccountUpdateRequest(BaseModel):

class DataSourceAccountResetRequest(BaseModel):
data_source_id: str
account_id: Union[str, None] = None
workspace_id: Union[str, None] = None
domain_id: str

Expand Down
44 changes: 24 additions & 20 deletions src/spaceone/cost_analysis/service/data_source_account_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,23 @@ def update(
permission="cost-analysis:DataSourceAccount.write",
role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER"],
)
@convert_model
def reset(
self, params: DataSourceAccountResetRequest
) -> Union[DataSourceAccountsResponse, dict]:
"""Reset data source account
Args:
params (dict): {
'data_source_id': 'str', # required
'account_id': 'str', # optional
'workspace_id': 'str', # injected from auth
'domain_id': 'str' # injected from auth
}
Returns:
dict
"""
data_source_id = params.data_source_id
account_id = params.account_id
workspace_id = params.workspace_id
domain_id = params.domain_id

Expand All @@ -93,40 +96,41 @@ def reset(
data_source_id, domain_id, workspace_id
)

query = {
"filter": [
{"k": "data_source_id", "v": data_source_id, "o": "eq"},
{"k": "domain_id", "v": domain_id, "o": "eq"},
]
conditions = {
"data_source_id": data_source_id,
"domain_id": domain_id,
}
if data_source_vo.resource_group == "WORKSPACE":
query["filter"].append({"k": "workspace_id", "v": workspace_id, "o": "eq"})

(
data_source_account_vos,
total_count,
) = self.data_source_account_mgr.list_data_source_accounts(query)

if account_id:
conditions["account_id"] = account_id
if workspace_id:
conditions["workspace_id"] = workspace_id

updated_data_source_account_vos = []
data_source_account_vos = (
self.data_source_account_mgr.filter_data_source_accounts(**conditions)
)
for data_source_account_vo in data_source_account_vos:
update_params = {
"project_id": None,
"service_account_id": None,
"is_sync": False,
"updated_at": datetime.utcnow(),
}
if data_source_vo.resource_group == "DOMAIN":
update_params["workspace_id"] = None
self.data_source_account_mgr.update_data_source_account_by_vo(
update_params, data_source_account_vo
data_source_account_vo = (
self.data_source_account_mgr.update_data_source_account_by_vo(
update_params, data_source_account_vo
)
)
updated_data_source_account_vos.append(data_source_account_vo)

data_source_accounts_info = [
data_source_account_vo.to_dict()
for data_source_account_vo in data_source_account_vos
updated_data_source_account_vo.to_dict()
for updated_data_source_account_vo in updated_data_source_account_vos
]

return DataSourceAccountsResponse(
results=data_source_accounts_info, total_count=total_count
results=data_source_accounts_info,
total_count=len(data_source_accounts_info),
)

@transaction(
Expand Down

0 comments on commit f54f330

Please sign in to comment.