diff --git a/src/spaceone/inventory_v2/connector/collector_plugin_connector/collector_plugin_v1_connector.py b/src/spaceone/inventory_v2/connector/collector_plugin_connector/collector_plugin_v1_connector.py index 6e38391..8383c34 100644 --- a/src/spaceone/inventory_v2/connector/collector_plugin_connector/collector_plugin_v1_connector.py +++ b/src/spaceone/inventory_v2/connector/collector_plugin_connector/collector_plugin_v1_connector.py @@ -3,6 +3,7 @@ from typing import Generator from spaceone.core.connector.space_connector import SpaceConnector +from spaceone.core.error import ERROR_BASE from spaceone.inventory_v2.connector.collector_plugin_connector import ( BaseCollectorPluginConnector, @@ -48,12 +49,36 @@ def collect( yield self._convert_resource_data(resource_data) @staticmethod - def _convert_resource_data(resource_data: dict) -> dict: + def _convert_match_rule(resource_data: dict, resource_type: str) -> dict: + for rule_values in resource_data.get("match_rules", {}).values(): + for index, rule_value in enumerate(rule_values): + if rule_value == "cloud_service_id": + rule_values[index] = "asset_id" + elif rule_value == "cloud_service_type": + rule_values[index] = "asset_type_id" + elif rule_value == "cloud_service_group": + del rule_values[index] + elif rule_value == "reference.resource_id": + rule_values[index] = "resource_id" + elif rule_value == "group": + rule_values[index] = "asset_group_id" + elif rule_value == "name": + if resource_type == "inventory.AssetType": + rule_values[index] = "asset_type_id" + return resource_data + + def _convert_resource_data(self, resource_data: dict) -> dict: + + if "resource" in resource_data and "metadata" in resource_data["resource"]: + del resource_data["resource"]["metadata"] + _LOGGER.debug( f"[_convert_resource_data] before convert resource_data: {resource_data}" ) resource_type = resource_data.get("resource_type") + if resource_type == "inventory.Region": + pass if resource_type in ["inventory.CloudService", "inventory.CloudServiceType"]: if resource_type == "inventory.CloudService": @@ -70,31 +95,35 @@ def _convert_resource_data(resource_data: dict) -> dict: resource_type = resource_data.get("resource_type") # convert match rule - for rule_values in resource_data.get("match_rules", {}).values(): - for index, rule_value in enumerate(rule_values): - if rule_value == "cloud_service_id": - rule_values[index] = "asset_id" - elif rule_value == "cloud_service_type": - rule_values[index] = "asset_type_id" - elif rule_value == "cloud_service_group": - del rule_values[index] - elif rule_value == "reference.resource_id": - rule_values[index] = "resource_id" - elif rule_value == "group": - rule_values[index] = "asset_group_id" + resource_data = self._convert_match_rule(resource_data, resource_type) + # convert keywords [instance_size, instance_type, ] if _resource := resource_data.get("resource"): - _resource_v1 = {} + # del _resource["metadata"] + if "instance_size" in _resource: - _resource_v1["instance_size"] = _resource.pop("instance_size") + _resource["data"]["instance_size"] = _resource.pop("instance_size") if "instance_type" in _resource: - _resource_v1["instance_type"] = _resource.pop("instance_type") - del _resource["metadata"] + _resource["data"]["instance_type"] = _resource.pop("instance_type") + + if "region_code" in _resource: + _resource["region_id"] = ( + f"{_resource['provider']}-{_resource['region_code']}" + ) if resource_type == "inventory.Asset": - asset_type_id = f"{_resource['provider']}.{_resource['cloud_service_group']}.{_resource['cloud_service_type']}" + asset_type_id = f"{_resource['provider']}-{_resource['cloud_service_group']}-{_resource['cloud_service_type']}" _resource["asset_type_id"] = asset_type_id resource_data["asset_type_id"] = asset_type_id + + if "reference" in _resource: + _resource["resource_id"] = _resource["reference"].get( + "resource_id" + ) + _resource["external_link"] = _resource["reference"].get( + "external_link" + ) + elif resource_type == "inventory.AssetType": asset_type_id = f"at-{_resource['provider']}-{_resource['group']}-{_resource['name']}" @@ -105,9 +134,10 @@ def _convert_resource_data(resource_data: dict) -> dict: _resource["asset_type_id"] = asset_type_id resource_data["asset_type_id"] = asset_type_id resource_data["asset_groups"] = asset_groups - resource_data["icon"] = _resource.get("tags", {}).get("icon", "") - resource_data["resource"]["v1"] = _resource_v1 + resource_data["icon"] = resource_data.get("tags", {}).get( + "spaceone:icon", "" + ) _LOGGER.debug(f"[_convert_resource_data] resource_data: {resource_data}") diff --git a/src/spaceone/inventory_v2/error/collect_data.py b/src/spaceone/inventory_v2/error/collect_data.py new file mode 100644 index 0000000..ef740ee --- /dev/null +++ b/src/spaceone/inventory_v2/error/collect_data.py @@ -0,0 +1,13 @@ +from spaceone.core.error import * + + +class ERROR_NOT_ALLOW_PINNING_KEYS(ERROR_INVALID_ARGUMENT): + _message = "Pinning keys not allowed. (key={key})" + + +class ERROR_METADATA_DICT_TYPE(ERROR_INVALID_ARGUMENT): + _message = "The value of metadata's {key} must be a dict type." + + +class ERROR_METADATA_LIST_VALUE_TYPE(ERROR_INVALID_ARGUMENT): + _message = "The value of metadata's {key} must be a list type." diff --git a/src/spaceone/inventory_v2/interface/grpc/__init__.py b/src/spaceone/inventory_v2/interface/grpc/__init__.py index 7f77806..cf0f475 100644 --- a/src/spaceone/inventory_v2/interface/grpc/__init__.py +++ b/src/spaceone/inventory_v2/interface/grpc/__init__.py @@ -3,8 +3,10 @@ from .collector import Collector from .job import Job from .job_task import JobTask +from .metric import Metric from .namespace_group import NamespaceGroup from .namespace import Namespace +from .metric import Metric _all_ = ["app"] @@ -14,4 +16,5 @@ app.add_service(Job) app.add_service(JobTask) app.add_service(NamespaceGroup) -app.add_service(Namespace) \ No newline at end of file +app.add_service(Namespace) +app.add_service(Metric) \ No newline at end of file diff --git a/src/spaceone/inventory_v2/lib/rule_matcher.py b/src/spaceone/inventory_v2/lib/rule_matcher.py index 8d82cd5..b5dfd8a 100644 --- a/src/spaceone/inventory_v2/lib/rule_matcher.py +++ b/src/spaceone/inventory_v2/lib/rule_matcher.py @@ -36,10 +36,14 @@ def dict_key_int_parser(data: dict) -> dict: def make_query( key: str, rules: dict, resource: dict, domain_id: str, workspace_id: str ) -> dict: - _filter = [ - {"k": "domain_id", "v": domain_id, "o": "eq"}, - {"k": "workspace_id", "v": workspace_id, "o": "eq"}, - ] + resource_type = resource.get("resource_type") + _filter = [{"k": "domain_id", "v": domain_id, "o": "eq"}] + + if resource_type in ["inventory.Asset"]: + _filter.append({"k": "workspace_id", "v": workspace_id, "o": "eq"}) + else: + workspaces = list({workspace_id, "*"}) + _filter.append({"k": "workspace_id", "v": workspaces, "o": "in"}) for rule in rules[key]: value = find_data(resource, rule) diff --git a/src/spaceone/inventory_v2/manager/asset_manager.py b/src/spaceone/inventory_v2/manager/asset_manager.py index dc73da0..572920d 100644 --- a/src/spaceone/inventory_v2/manager/asset_manager.py +++ b/src/spaceone/inventory_v2/manager/asset_manager.py @@ -19,11 +19,9 @@ "name", "ip_addresses", "account", - "instance_type", - "instance_size", - "reference", - "region_code", - "ref_region", + "resource_id", + "external_link", + "region_id", "project_id", "data", ] @@ -116,6 +114,22 @@ def list_assets( **query, target=target, reference_filter=reference_filter ) + def analyze_assets( + self, + query: dict, + change_filter: bool = False, + domain_id: str = None, + reference_filter: dict = None, + ): + if change_filter: + query = self._change_filter_tags(query) + query = self._change_filter_project_group_id(query, domain_id) + + # Append Query for DELETED filter (Temporary Logic) + query = self._append_state_query(query) + + return self.asset_model.analyze(**query, reference_filter=reference_filter) + def _change_filter_tags(self, query: dict) -> dict: change_filter = [] diff --git a/src/spaceone/inventory_v2/manager/asset_type_manager.py b/src/spaceone/inventory_v2/manager/asset_type_manager.py index 9992124..87dd208 100644 --- a/src/spaceone/inventory_v2/manager/asset_type_manager.py +++ b/src/spaceone/inventory_v2/manager/asset_type_manager.py @@ -51,12 +51,12 @@ def delete_asset_type_by_vo(asset_type_vo: AssetType) -> None: def get_asset_type( self, - asset_Type_id: str, + asset_type_id: str, domain_id: str, workspace_id: str = None, ) -> AssetType: conditions = { - "asset_Type_id": asset_Type_id, + "asset_type_id": asset_type_id, "domain_id": domain_id, } diff --git a/src/spaceone/inventory_v2/manager/collecting_manager.py b/src/spaceone/inventory_v2/manager/collecting_manager.py index 5a6c5ef..c402e39 100644 --- a/src/spaceone/inventory_v2/manager/collecting_manager.py +++ b/src/spaceone/inventory_v2/manager/collecting_manager.py @@ -1,10 +1,13 @@ +import datetime import logging import time +from datetime import datetime from typing import Generator + from spaceone.core import utils from spaceone.core.manager import BaseManager + from spaceone.inventory_v2.lib.resource_manager import ResourceManager -from spaceone.inventory_v2.manager.asset_manager import AssetManager from spaceone.inventory_v2.manager.job_manager import JobManager from spaceone.inventory_v2.manager.job_task_manager import JobTaskManager from spaceone.inventory_v2.manager.collector_manager import CollectorManager @@ -13,8 +16,6 @@ CollectorPluginManager, ) -# from spaceone.inventory.manager.namespace_manager import NamespaceManager -# from spaceone.inventory.manager.metric_manager import MetricManager from spaceone.inventory_v2.model.job_task.database import JobTask from spaceone.inventory_v2.error import * from spaceone.inventory_v2.lib import rule_matcher @@ -50,9 +51,6 @@ def collecting_resources(self, params: dict) -> bool: 'token': 'str' } """ - from spaceone.core import model - - model.init_all(False) # set token to transaction meta token = params["token"] @@ -71,6 +69,8 @@ def collecting_resources(self, params: dict) -> bool: secret_data = params["secret_data"] plugin_info = params["plugin_info"] + print(secret_info) + if is_sub_task: _LOGGER.debug( f"[collecting_resources] start sub task: {job_task_id} " @@ -85,7 +85,7 @@ def collecting_resources(self, params: dict) -> bool: self.job_task_mgr.push_job_task(params) return True - job_task_vo = self.job_task_mgr.get(job_task_id, domain_id) + job_task_vo = self.job_task_mgr.get_job_task(job_task_id, domain_id) # add workspace_id to params from secret_info params["workspace_id"] = secret_info["workspace_id"] @@ -233,7 +233,11 @@ def _upsert_collecting_resources( total_count += 1 try: - if resource_type in ["inventory.Namespace", "inventory.Metric"]: + if resource_type in [ + "inventory.NamespaceGroup", + "inventory.Namespace", + "inventory.Metric", + ]: # self._upsert_metric_and_namespace(resource_data, params) # total_count -= 1 pass @@ -377,6 +381,7 @@ def _upsert_resource( request_data = resource_data.get("resource", {}) request_data["domain_id"] = domain_id request_data["workspace_id"] = workspace_id + request_data["last_collected_at"] = datetime.utcnow() service, manager = self._get_resource_map(resource_type) @@ -421,7 +426,11 @@ def _upsert_resource( try: match_resource, total_count = self._query_with_match_rules( - request_data, match_rules, domain_id, workspace_id, manager + request_data, + match_rules, + domain_id, + workspace_id, + manager, ) except ERROR_TOO_MANY_MATCH as e: @@ -474,6 +483,7 @@ def _upsert_resource( additional = self._set_error_addition_info( resource_type, total_count, request_data ) + # todo: refactoring job task error self.job_task_mgr.add_error( job_task_vo, e.error_code, e.message, additional ) @@ -590,8 +600,11 @@ def _query_with_match_rules( query = rule_matcher.make_query( order, match_rules, resource_data, domain_id, workspace_id ) + _LOGGER.debug(f"[_query_with_match_rules] query: {query}") match_resource, total_count = resource_manager.find_resources(query) + _LOGGER.debug(f"[_query_with_match_rules] match_resource: {match_resource}") + if total_count > 1: if data := resource_data.get("data"): raise ERROR_TOO_MANY_MATCH( diff --git a/src/spaceone/inventory_v2/manager/collector_rule_manager.py b/src/spaceone/inventory_v2/manager/collector_rule_manager.py index f45adcc..58f6e3c 100644 --- a/src/spaceone/inventory_v2/manager/collector_rule_manager.py +++ b/src/spaceone/inventory_v2/manager/collector_rule_manager.py @@ -83,15 +83,15 @@ def change_asset_data( custom_collector_rule_vos, ) = self._get_collector_rules(collector_id, domain_id) - cloud_service_data = self._apply_collector_rule_to_asset_data( + asset_data = self._apply_collector_rule_to_asset_data( asset_data, managed_collector_rule_vos, domain_id ) - cloud_service_data = self._apply_collector_rule_to_asset_data( - cloud_service_data, custom_collector_rule_vos, domain_id + asset_data = self._apply_collector_rule_to_asset_data( + asset_data, custom_collector_rule_vos, domain_id ) - return cloud_service_data + return asset_data def _apply_collector_rule_to_asset_data( self, asset_data: dict, collector_rule_vos: QuerySet, domain_id: str diff --git a/src/spaceone/inventory_v2/manager/identity_manager.py b/src/spaceone/inventory_v2/manager/identity_manager.py index 4f1c7f1..62e1d5a 100644 --- a/src/spaceone/inventory_v2/manager/identity_manager.py +++ b/src/spaceone/inventory_v2/manager/identity_manager.py @@ -21,28 +21,6 @@ def __init__(self, *args, **kwargs): token=token, ) - def get_user(self, domain_id: str, user_id: str) -> dict: - system_token = config.get_global("TOKEN") - response = self.identity_conn.dispatch( - "User.list", - {"user_id": user_id, "state": "ENABLED"}, - x_domain_id=domain_id, - token=system_token, - ) - users_info = response.get("results", []) - if users_info: - return users_info[0] - else: - return {} - - def get_domain_name(self, domain_id: str) -> str: - system_token = config.get_global("TOKEN") - - domain_info = self.identity_conn.dispatch( - "Domain.get", {"domain_id": domain_id}, token=system_token - ) - return domain_info["name"] - def list_domains(self, params: dict) -> dict: system_token = config.get_global("TOKEN") return self.identity_conn.dispatch("Domain.list", params, token=system_token) @@ -107,6 +85,24 @@ def get_service_account_name_map(self, domain_id: str, workspace_id: str) -> dic ) return service_account_name_map + @cache.cacheable( + key="inventory:service-account:{domain_id}:{service_account_id}", expire=300 + ) + def get_service_account(self, service_account_id: str, domain_id: str) -> dict: + token = self.transaction.get_meta("token") + token_type = JWTUtil.get_value_from_token(token, "typ") + + if token_type == "SYSTEM_TOKEN": + return self.identity_conn.dispatch( + "ServiceAccount.get", + {"service_account_id": service_account_id}, + x_domain_id=domain_id, + ) + else: + return self.identity_conn.dispatch( + "ServiceAccount.get", {"service_account": service_account_id} + ) + def list_service_accounts(self, query: dict, domain_id: str) -> dict: if self.token_type == "SYSTEM_TOKEN": return self.identity_conn.dispatch( @@ -115,10 +111,16 @@ def list_service_accounts(self, query: dict, domain_id: str) -> dict: else: return self.identity_conn.dispatch("ServiceAccount.list", {"query": query}) - def get_project(self, project_id: str, domain_id: str): - if self.token_type == "SYSTEM_TOKEN": + @cache.cacheable(key="inventory:project:{domain_id}:{project_id}", expire=3600) + def get_project(self, project_id, domain_id) -> dict: + token = self.transaction.get_meta("token") + token_type = JWTUtil.get_value_from_token(token, "typ") + + if token_type == "SYSTEM_TOKEN": return self.identity_conn.dispatch( - "Project.get", {"project_id": project_id}, x_domain_id=domain_id + "Project.get", + {"project_id": project_id}, + x_domain_id=domain_id, ) else: return self.identity_conn.dispatch( diff --git a/src/spaceone/inventory_v2/manager/metric_manager.py b/src/spaceone/inventory_v2/manager/metric_manager.py index 6555f9d..da70b46 100644 --- a/src/spaceone/inventory_v2/manager/metric_manager.py +++ b/src/spaceone/inventory_v2/manager/metric_manager.py @@ -231,13 +231,11 @@ def analyze_resource( ) try: - if metric_vo.resource_type == "inventory.CloudService": - return self._analyze_cloud_service(query, domain_id) - elif metric_vo.resource_type.startswith("inventory.CloudService:"): + if metric_vo.resource_type == "inventory.Asset": + return self._analyze_assets(query, domain_id) + elif metric_vo.resource_type.startswith("inventory.Asset:"): cloud_service_type_key = metric_vo.resource_type.split(":")[-1] - return self._analyze_cloud_service( - query, domain_id, cloud_service_type_key - ) + return self._analyze_assets(query, domain_id, cloud_service_type_key) else: raise ERROR_NOT_SUPPORT_RESOURCE_TYPE(resource_type=resource_type) except Exception as e: @@ -281,13 +279,13 @@ def _append_datetime_filter( return query @staticmethod - def _analyze_cloud_service( + def _analyze_assets( query: dict, domain_id: str, - cloud_service_type_key: str = None, + asset_type_id: str = None, ) -> list: default_group_by = [ - "collection_info.service_account_id", + "service_account_id", "project_id", "workspace_id", ] @@ -306,24 +304,16 @@ def _analyze_cloud_service( query["group_by"] = changed_group_by query["filter"] = query.get("filter", []) query["filter"].append({"k": "domain_id", "v": domain_id, "o": "eq"}) + print(query) - if cloud_service_type_key: + if asset_type_id: try: - ( - provider, - cloud_service_group, - cloud_service_type, - ) = cloud_service_type_key.split(".") - query["filter"].append({"k": f"provider", "v": provider, "o": "eq"}) - query["filter"].append( - {"k": f"cloud_service_group", "v": cloud_service_group, "o": "eq"} - ) query["filter"].append( - {"k": f"cloud_service_type", "v": cloud_service_type, "o": "eq"} + {"k": "asset_type_id", "v": asset_type_id, "o": "eq"} ) except Exception as e: raise ERROR_NOT_SUPPORT_RESOURCE_TYPE( - resource_type=f"inventory.CloudService:{cloud_service_type_key}" + resource_type=f"inventory.Asset:{asset_type_id}" ) if "select" in query: diff --git a/src/spaceone/inventory_v2/model/asset/database.py b/src/spaceone/inventory_v2/model/asset/database.py index d21f4a8..8accf3b 100644 --- a/src/spaceone/inventory_v2/model/asset/database.py +++ b/src/spaceone/inventory_v2/model/asset/database.py @@ -20,8 +20,10 @@ class Asset(MongoModel): provider = StringField(max_length=255) account = StringField(max_length=255, default=None, null=True) region_id = StringField(max_length=40) + secret_id = StringField(max_length=40, default=None, null=True) asset_type_id = StringField(max_length=255) - collector_id = StringField(max_length=40) + service_account_id = StringField(max_length=40, default=None, null=True) + collector_id = StringField(max_length=40, default=None, null=True) project_id = StringField(max_length=40) workspace_id = StringField(max_length=40) domain_id = StringField(max_length=40) @@ -35,12 +37,15 @@ class Asset(MongoModel): "name", "data", "state", - "account", "ip_addresses", "tags", - "project_id", + "account", "region_id", "asset_type_id", + "secret_id", + "service_account_id", + "collector_id", + "project_id", "updated_at", "last_collected_at", "deleted_at", diff --git a/src/spaceone/inventory_v2/model/asset/request.py b/src/spaceone/inventory_v2/model/asset/request.py index 57b05ad..b90e578 100644 --- a/src/spaceone/inventory_v2/model/asset/request.py +++ b/src/spaceone/inventory_v2/model/asset/request.py @@ -10,21 +10,21 @@ class AssetCreateRequest(BaseModel): - asset_code: Union[str, None] + asset_id: Union[str, None] name: Union[str, None] provider: str asset_type: str ipaddresses: Union[List[str], None] account: Union[str, None] - instance_type: Union[str, None] - instance_size: Union[float, None] data: dict metadata: Union[dict, None] tags: Union[dict, None] - region: Union[str, None] + region_id: Union[str, None] + service_account_id: Union[str, None] project_id: str workspace_id: str domain_id: str + users_projects: Union[List[str], None] class AssetUpdateRequest(BaseModel): diff --git a/src/spaceone/inventory_v2/model/asset/response.py b/src/spaceone/inventory_v2/model/asset/response.py index 7563421..db704f0 100644 --- a/src/spaceone/inventory_v2/model/asset/response.py +++ b/src/spaceone/inventory_v2/model/asset/response.py @@ -7,18 +7,17 @@ class AssetResponse(BaseModel): asset_id: Union[str, None] - asset_code: Union[str, None] name: Union[str, None] provider: Union[str, None] ipaddresses: Union[List[str], None] account: Union[str, None] - instance_type: Union[str, None] - instance_size: Union[float, None] data: Union[dict, None] metadata: Union[dict, None] tags: Union[dict, None] - region: Union[str, None] + region_id: Union[str, None] asset_type_id: Union[str, None] + service_account_id: Union[str, None] + collector_id: Union[str, None] project_id: Union[str, None] workspace_id: Union[str, None] domain_id: Union[str, None] @@ -28,5 +27,5 @@ class AssetResponse(BaseModel): def dict(self, *args, **kwargs): data = super().dict(*args, **kwargs) data["created_at"] = utils.datetime_to_iso8601(data["created_at"]) - data["updated_at"] = utils.datetime_to_iso8601(data["updated_at"]) + data["updated_at"] = utils.datetime_to_iso8601(data.get("updated_at")) return data diff --git a/src/spaceone/inventory_v2/model/asset_type/database.py b/src/spaceone/inventory_v2/model/asset_type/database.py index 5118d8f..43c269b 100644 --- a/src/spaceone/inventory_v2/model/asset_type/database.py +++ b/src/spaceone/inventory_v2/model/asset_type/database.py @@ -5,10 +5,7 @@ class AssetType(MongoModel): asset_type_id = StringField(max_length=40, unique=True) - name = StringField( - max_length=255, - unique_with=["provider", "workspace_id", "domain_id"], - ) + name = StringField(max_length=255) description = StringField(max_length=255, default=None, null=True) icon = StringField(max_length=255, default=None, null=True) provider = StringField(max_length=255) diff --git a/src/spaceone/inventory_v2/model/metric/request.py b/src/spaceone/inventory_v2/model/metric/request.py index 3210c18..747cd22 100644 --- a/src/spaceone/inventory_v2/model/metric/request.py +++ b/src/spaceone/inventory_v2/model/metric/request.py @@ -83,5 +83,5 @@ class MetricSearchQueryRequest(BaseModel): class MetricStatQueryRequest(BaseModel): query: dict - workspace_id: Union[str, list, None] = None + workspace_id: Union[list, str, None] = None domain_id: str diff --git a/src/spaceone/inventory_v2/service/asset_service.py b/src/spaceone/inventory_v2/service/asset_service.py index f2dcd80..05402a5 100644 --- a/src/spaceone/inventory_v2/service/asset_service.py +++ b/src/spaceone/inventory_v2/service/asset_service.py @@ -22,9 +22,9 @@ "asset_id", "name", "ip_addresses", - "cloud_service_group", - "cloud_service_type", - "reference.resource_id", + "asset_type_id", + "region_id", + "resource_id", ] _LOGGER = logging.getLogger(__name__) @@ -58,18 +58,14 @@ def create(self, params: AssetCreateRequest) -> Union[AssetResponse, dict]: """ Args: params (dict): { - 'cloud_service_type': 'str', # required - 'cloud_service_group': 'str', # required + 'asset_id': 'str', # required 'provider': 'str', # required 'name': 'str', 'account': 'str', - 'instance_type': 'str', - 'instance_size': 'float', 'ip_addresses': 'list', 'data': 'dict', # required 'json_data': 'dict', 'metadata': 'dict', - 'reference': 'dict', 'tags': 'list or dict', 'region_code': 'str', 'project_id': 'str', # required @@ -110,7 +106,9 @@ def create_resource(self, params: dict) -> Asset: domain_id = params["domain_id"] workspace_id = params["workspace_id"] + secret_project_id = self.transaction.get_meta("secret.project_id") + provider = params["provider"] if instance_size := params.get("instance_size"): @@ -136,17 +134,12 @@ def create_resource(self, params: dict) -> Asset: elif secret_project_id: params["project_id"] = secret_project_id - # params["ref_cloud_service_type"] = self._make_cloud_service_type_key(params) - - if "region_id" in params: - params["ref_region"] = self._make_region_key( - domain_id, provider, params["region_code"] + if "service_account_id" in params: + self.identity_mgr.get_service_account( + params["service_account_id"], domain_id ) - if "metadata" in params: - params["metadata"] = self._convert_metadata(params["metadata"], provider) - - params["collection_info"] = self._get_collection_info() + params = self._set_metadata_info_from_transaction(params) asset_vo = self.asset_mgr.create_asset(params) @@ -226,10 +219,6 @@ def update_resource(self, params: dict) -> Asset: if "ip_addresses" in params and params["ip_addresses"] is None: del params["ip_addresses"] - if instance_size := params.get("instance_size"): - if not isinstance(instance_size, float): - raise ERROR_INVALID_PARAMETER_TYPE(key="instance_size", type="float") - if "tags" in params: params["tags"] = self._convert_tags_to_dict(params["tags"]) @@ -282,7 +271,7 @@ def update_resource(self, params: dict) -> Asset: else: del params["metadata"] - params["collection_info"] = self._get_collection_info() + params = self._set_metadata_info_from_transaction(params) params = self.asset_mgr.merge_data(params, old_asset_data) @@ -391,13 +380,6 @@ def list(self, params: AssetSearchQueryRequest): reference_filter=reference_filter, ) - @staticmethod - def _make_cloud_service_type_key(resource_data: dict) -> str: - return ( - f'{resource_data["domain_id"]}.{resource_data["workspace_id"]}.{resource_data["provider"]}.' - f'{resource_data["cloud_service_group"]}.{resource_data["cloud_service_type"]}' - ) - @staticmethod def _make_region_key(domain_id: str, provider: str, region_code: str) -> str: return f"{domain_id}.{provider}.{region_code}" @@ -406,17 +388,20 @@ def _make_region_key(domain_id: str, provider: str, region_code: str) -> str: def _convert_metadata(metadata: dict, provider: str) -> dict: return {provider: copy.deepcopy(metadata)} - def _get_collection_info(self) -> dict: + def _set_metadata_info_from_transaction(self, params: dict) -> dict: collector_id = self.transaction.get_meta("collector_id") secret_id = self.transaction.get_meta("secret.secret_id") service_account_id = self.transaction.get_meta("secret.service_account_id") - return { - "collector_id": collector_id, - "secret_id": secret_id, - "service_account_id": service_account_id, - "last_collected_at": datetime.utcnow(), - } + params.update( + { + "collector_id": collector_id, + "secret_id": secret_id, + "service_account_id": service_account_id, + } + ) + + return params @staticmethod def _convert_tags_to_dict(tags: Union[list, dict]) -> dict: diff --git a/src/spaceone/inventory_v2/service/asset_type_service.py b/src/spaceone/inventory_v2/service/asset_type_service.py index 23904c4..e5b2768 100644 --- a/src/spaceone/inventory_v2/service/asset_type_service.py +++ b/src/spaceone/inventory_v2/service/asset_type_service.py @@ -22,19 +22,10 @@ from spaceone.inventory_v2.model.asset_type.database import AssetType from spaceone.inventory_v2.model.asset_type.response import AssetTypeResponse -_KEYWORD_FILTER = [ - "asset_id", - "name", - "ip_addresses", - "cloud_service_group", - "cloud_service_type", - "reference.resource_id", -] +_KEYWORD_FILTER = ["asset_type_id", "name", "asset_group_id"] _LOGGER = logging.getLogger(__name__) -_KEYWORD_FILTER = ["asset_type_id", "name", "asset_group_id", "service_code"] - @authentication_handler @authorization_handler @@ -131,7 +122,7 @@ def update(self, params: dict) -> AssetType: return self.update_resource(params) - @check_required(["cloud_service_type_id", "workspace_id", "domain_id"]) + @check_required(["asset_type_id", "workspace_id", "domain_id"]) def update_resource(self, params: dict) -> AssetType: if json_metadata := params.get("json_metadata"): params["metadata"] = utils.load_json(json_metadata) @@ -149,11 +140,11 @@ def update_resource(self, params: dict) -> AssetType: params["updated_by"] = self.transaction.get_meta("collector_id") or "manual" domain_id = params["domain_id"] - cloud_svc_type_vo = self.asset_type_mgr.get_asset_type( + asset_type_vo = self.asset_type_mgr.get_asset_type( params["asset_type_id"], domain_id ) - return self.asset_type_mgr.update_asset_type_by_vo(params, cloud_svc_type_vo) + return self.asset_type_mgr.update_asset_type_by_vo(params, asset_type_vo) @transaction( permission="inventory:AssetType.write", @@ -173,10 +164,10 @@ def delete(self, params: dict) -> None: self.delete_resource(params) - @check_required(["cloud_service_type_id", "workspace_id", "domain_id"]) + @check_required(["asset_type_id", "workspace_id", "domain_id"]) def delete_resource(self, params: dict) -> None: asset_type_vo = self.asset_type_mgr.get_asset_type( - params["cloud_service_type_id"], params["domain_id"], params["workspace_id"] + params["asset_type_id"], params["domain_id"], params["workspace_id"] ) self.asset_type_mgr.delete_asset_type_by_vo(asset_type_vo) diff --git a/src/spaceone/inventory_v2/service/collector_service.py b/src/spaceone/inventory_v2/service/collector_service.py index 50befd2..b05f548 100644 --- a/src/spaceone/inventory_v2/service/collector_service.py +++ b/src/spaceone/inventory_v2/service/collector_service.py @@ -614,7 +614,7 @@ def _get_tasks( domain_id: str, collector_workspace_id: str = None, ) -> list: - secret_mgr: SecretManager = self.locator.get_manager(SecretManager) + secret_mgr = SecretManager() collector_plugin_mgr = CollectorPluginManager() tasks = [] @@ -629,6 +629,7 @@ def _get_tasks( for secret_id in secret_ids: secret_info = secret_mgr.get_secret(secret_id, domain_id) secret_data = secret_mgr.get_secret_data(secret_id, domain_id) + _task = { "plugin_info": plugin_info, "secret_info": secret_info, diff --git a/src/spaceone/inventory_v2/service/metric_service.py b/src/spaceone/inventory_v2/service/metric_service.py index 9fdeb9b..494ea10 100644 --- a/src/spaceone/inventory_v2/service/metric_service.py +++ b/src/spaceone/inventory_v2/service/metric_service.py @@ -283,6 +283,7 @@ def list(self, params: MetricSearchQueryRequest) -> Union[MetricsResponse, dict] permission="inventory-v2:Metric.read", role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"], ) + @change_value_by_rule("APPEND", "workspace_id", "*") @append_query_filter(["workspace_id", "domain_id"]) @append_keyword_filter(["metric_id", "name"]) @convert_model