diff --git a/src/spaceone/dashboard/manager/cost_analysis_manager.py b/src/spaceone/dashboard/manager/cost_analysis_manager.py index 076b603..55e8bbe 100644 --- a/src/spaceone/dashboard/manager/cost_analysis_manager.py +++ b/src/spaceone/dashboard/manager/cost_analysis_manager.py @@ -12,3 +12,6 @@ def __init__(self, *args, **kwargs): def analyze_cost(self, params: dict) -> dict: return self.cost_analysis_conn.dispatch("Cost.analyze", params) + + def list_data_sources(self, params: dict) -> dict: + return self.cost_analysis_conn.dispatch("DataSource.list", params) diff --git a/src/spaceone/dashboard/manager/inventory_manager.py b/src/spaceone/dashboard/manager/inventory_manager.py index b099144..5d45382 100644 --- a/src/spaceone/dashboard/manager/inventory_manager.py +++ b/src/spaceone/dashboard/manager/inventory_manager.py @@ -12,3 +12,6 @@ def __init__(self, *args, **kwargs): def analyze_metric_data(self, params: dict) -> dict: return self.inventory_conn.dispatch("MetricData.analyze", params) + + def list_metrics(self, params: dict) -> dict: + return self.inventory_conn.dispatch("Metric.list", params) diff --git a/src/spaceone/dashboard/service/private_data_table_service.py b/src/spaceone/dashboard/service/private_data_table_service.py index 7bd0fdc..9617ce1 100644 --- a/src/spaceone/dashboard/service/private_data_table_service.py +++ b/src/spaceone/dashboard/service/private_data_table_service.py @@ -13,6 +13,7 @@ from spaceone.dashboard.manager.data_table_manager.data_transformation_manager import ( DataTransformationManager, ) +from spaceone.dashboard.manager.cost_analysis_manager import CostAnalysisManager from spaceone.dashboard.model.private_data_table.request import * from spaceone.dashboard.model.private_data_table.response import * from spaceone.dashboard.model.private_data_table.database import PrivateDataTable @@ -74,6 +75,13 @@ def add_data_table(self, params_dict: dict) -> dict: user_id, ) + if source_type == "COST": + if plugin_id := options.get("COST", {}).get("plugin_id"): + data_source_id = self._get_data_source_id_from_plugin_id(plugin_id) + options["COST"]["data_source_id"] = data_source_id + params_dict["options"] = options + del params_dict["options"]["COST"]["plugin_id"] + ds_mgr = DataSourceManager( "PRIVATE", source_type, @@ -99,6 +107,23 @@ def add_data_table(self, params_dict: dict) -> dict: return pri_data_table_vo.to_dict() + @staticmethod + def _get_data_source_id_from_plugin_id(plugin_id: str) -> str: + cost_mgr = CostAnalysisManager() + params = { + "query": { + "filter": [{"k": "plugin_info.plugin_id", "v": plugin_id, "o": "eq"}], + } + } + data_sources_info = cost_mgr.list_data_sources(params) + if data_sources_info.get("total_count", 0) == 0: + raise ERROR_INVALID_PARAMETER( + key="options.COST.plugin_id", + reason=f"Invalid plugin_id: {plugin_id}", + ) + data_source_info = data_sources_info.get("results")[0] + return data_source_info.get("data_source_id") + @transaction( permission="dashboard:PrivateDataTable.write", role_types=["USER"], diff --git a/src/spaceone/dashboard/service/public_data_table_service.py b/src/spaceone/dashboard/service/public_data_table_service.py index 09827e4..1fd9d0b 100644 --- a/src/spaceone/dashboard/service/public_data_table_service.py +++ b/src/spaceone/dashboard/service/public_data_table_service.py @@ -11,6 +11,7 @@ from spaceone.dashboard.manager.data_table_manager.data_transformation_manager import ( DataTransformationManager, ) +from spaceone.dashboard.manager.cost_analysis_manager import CostAnalysisManager from spaceone.dashboard.model.public_data_table.request import * from spaceone.dashboard.model.public_data_table.response import * from spaceone.dashboard.model.public_data_table.database import PublicDataTable @@ -75,6 +76,13 @@ def add_data_table(self, params_dict: dict) -> dict: user_projects, ) + if source_type == "COST": + if plugin_id := options.get("COST", {}).get("plugin_id"): + data_source_id = self._get_data_source_id_from_plugin_id(plugin_id) + options["COST"]["data_source_id"] = data_source_id + params_dict["options"] = options + del params_dict["options"]["COST"]["plugin_id"] + ds_mgr = DataSourceManager( "PUBLIC", source_type, @@ -103,6 +111,23 @@ def add_data_table(self, params_dict: dict) -> dict: return pub_data_table_vo.to_dict() + @staticmethod + def _get_data_source_id_from_plugin_id(plugin_id: str) -> str: + cost_mgr = CostAnalysisManager() + params = { + "query": { + "filter": [{"k": "plugin_info.plugin_id", "v": plugin_id, "o": "eq"}], + } + } + data_sources_info = cost_mgr.list_data_sources(params) + if data_sources_info.get("total_count", 0) == 0: + raise ERROR_INVALID_PARAMETER( + key="options.COST.plugin_id", + reason=f"Invalid plugin_id: {plugin_id}", + ) + data_source_info = data_sources_info.get("results")[0] + return data_source_info.get("data_source_id") + @transaction( permission="dashboard:PublicDataTable.write", role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"],