From d9a0f4d07d25b7c2951efd0e4c515c8348789e1e Mon Sep 17 00:00:00 2001 From: ImMin5 Date: Thu, 10 Oct 2024 15:42:29 +0900 Subject: [PATCH] feat: modify set default unified cost config method Signed-off-by: ImMin5 --- .../cost_analysis/manager/config_manager.py | 51 ++++++++++++++++--- .../model/unified_cost/database.py | 1 - .../service/unified_cost_service.py | 1 - 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/spaceone/cost_analysis/manager/config_manager.py b/src/spaceone/cost_analysis/manager/config_manager.py index bd652036..f732becf 100644 --- a/src/spaceone/cost_analysis/manager/config_manager.py +++ b/src/spaceone/cost_analysis/manager/config_manager.py @@ -30,17 +30,31 @@ def get_unified_cost_config(self, domain_id: str) -> dict: params, token=system_token, x_domain_id=domain_id ) - unified_cost_config = {} - for config_info in response.get("results", []): - if data := config_info.get("data", {}): - unified_cost_config = data.get( - "unified_cost_config", self._get_default_unified_cost_config() - ) + results = response.get("results", []) + total_count = response.get("total_count", 0) + + if total_count > 0: + domain_config_data = results[0].get("data", {}) + unified_cost_config = domain_config_data.get("unified_cost_config", {}) + else: + domain_config_data = {} + unified_cost_config = {} + if not unified_cost_config: - unified_cost_config = self._get_default_unified_cost_config() + unified_cost_config = self._set_default_unified_cost_config( + domain_id, data=domain_config_data + ) return unified_cost_config + def set_domain_config(self, domain_id: str, name: str, data: dict) -> dict: + system_token = config.get_global("TOKEN") + params = {"name": name, "data": data} + + return self.config_conn.dispatch( + "DomainConfig.set", params, token=system_token, x_domain_id=domain_id + ) + def list_domain_configs( self, params: dict, token: str = None, x_domain_id: str = None ) -> dict: @@ -51,7 +65,6 @@ def list_domain_configs( x_domain_id=x_domain_id, ) - # todo: use method @staticmethod def _get_default_unified_cost_config() -> dict: default_unified_cost_config = { @@ -66,3 +79,25 @@ def _get_default_unified_cost_config() -> dict: "currency": "KRW", } return default_unified_cost_config + + def _set_default_unified_cost_config(self, domain_id: str, data: dict) -> dict: + default_unified_cost_config = self._get_default_unified_cost_config() + + try: + domain_config_name = _AUTH_CONFIG_KEYS[0] + data["unified_cost_config"] = default_unified_cost_config + + domain_config_info = self.set_domain_config( + domain_id, domain_config_name, data + ) + + default_unified_cost_config = domain_config_info["data"][ + "unified_cost_config" + ] + + except Exception as e: + _LOGGER.error( + f"Failed to set default unified cost config: {e}", exc_info=True + ) + + return default_unified_cost_config diff --git a/src/spaceone/cost_analysis/model/unified_cost/database.py b/src/spaceone/cost_analysis/model/unified_cost/database.py index 1b80e07c..6de8ff58 100644 --- a/src/spaceone/cost_analysis/model/unified_cost/database.py +++ b/src/spaceone/cost_analysis/model/unified_cost/database.py @@ -8,7 +8,6 @@ class UnifiedCost(MongoModel): max_length=40, generate_id="unified-cost", unique=True ) cost = DictField(default={}) - cost_report_name = StringField(max_length=255) billed_month = StringField(max_length=40) billed_year = StringField(max_length=20) exchange_date = StringField(max_length=40) diff --git a/src/spaceone/cost_analysis/service/unified_cost_service.py b/src/spaceone/cost_analysis/service/unified_cost_service.py index efd9de54..ae24468f 100644 --- a/src/spaceone/cost_analysis/service/unified_cost_service.py +++ b/src/spaceone/cost_analysis/service/unified_cost_service.py @@ -91,7 +91,6 @@ def run_unified_cost(self, params: dict): "month": 'str', (optional), } """ - domain_id = params["domain_id"] aggregation_month: Union[str, None] = params.get("month")