From 91fe1b10db5eacde9f39df6200fcbad995d7b209 Mon Sep 17 00:00:00 2001 From: ImMin5 Date: Thu, 2 May 2024 17:42:22 +0900 Subject: [PATCH] fix: fix reset api for data source account --- .../cost_analysis/manager/cost_manager.py | 4 ++ .../manager/data_source_account_manager.py | 13 +++--- .../manager/data_source_rule_manager.py | 34 +++++++------- .../model/data_source_account/request.py | 1 + .../service/data_source_account_service.py | 44 ++++++++++--------- 5 files changed, 54 insertions(+), 42 deletions(-) diff --git a/src/spaceone/cost_analysis/manager/cost_manager.py b/src/spaceone/cost_analysis/manager/cost_manager.py index 712431ed..38dad06c 100644 --- a/src/spaceone/cost_analysis/manager/cost_manager.py +++ b/src/spaceone/cost_analysis/manager/cost_manager.py @@ -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) diff --git a/src/spaceone/cost_analysis/manager/data_source_account_manager.py b/src/spaceone/cost_analysis/manager/data_source_account_manager.py index 04482e24..0466a358 100644 --- a/src/spaceone/cost_analysis/manager/data_source_account_manager.py +++ b/src/spaceone/cost_analysis/manager/data_source_account_manager.py @@ -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 diff --git a/src/spaceone/cost_analysis/manager/data_source_rule_manager.py b/src/spaceone/cost_analysis/manager/data_source_rule_manager.py index 5bd41614..dcd55d98 100644 --- a/src/spaceone/cost_analysis/manager/data_source_rule_manager.py +++ b/src/spaceone/cost_analysis/manager/data_source_rule_manager.py @@ -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, @@ -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) @@ -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: @@ -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: @@ -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" ) @@ -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}" @@ -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[ diff --git a/src/spaceone/cost_analysis/model/data_source_account/request.py b/src/spaceone/cost_analysis/model/data_source_account/request.py index 289be4d5..4db218a5 100644 --- a/src/spaceone/cost_analysis/model/data_source_account/request.py +++ b/src/spaceone/cost_analysis/model/data_source_account/request.py @@ -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 diff --git a/src/spaceone/cost_analysis/service/data_source_account_service.py b/src/spaceone/cost_analysis/service/data_source_account_service.py index d51e92ef..42e4c694 100644 --- a/src/spaceone/cost_analysis/service/data_source_account_service.py +++ b/src/spaceone/cost_analysis/service/data_source_account_service.py @@ -71,6 +71,7 @@ def update( permission="cost-analysis:DataSourceAccount.write", role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER"], ) + @convert_model def reset( self, params: DataSourceAccountResetRequest ) -> Union[DataSourceAccountsResponse, dict]: @@ -78,6 +79,7 @@ def reset( Args: params (dict): { 'data_source_id': 'str', # required + 'account_id': 'str', # optional 'workspace_id': 'str', # injected from auth 'domain_id': 'str' # injected from auth } @@ -85,6 +87,7 @@ def reset( dict """ data_source_id = params.data_source_id + account_id = params.account_id workspace_id = params.workspace_id domain_id = params.domain_id @@ -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(