From a7be44d18c37aca95a3bba71848255427f10945b Mon Sep 17 00:00:00 2001 From: kang2453 Date: Tue, 17 Dec 2024 11:01:04 +0900 Subject: [PATCH 1/3] fix: Update metric_id type to enforce string requirement in MetricExampleCreateRequest Signed-off-by: kang2453 --- src/spaceone/inventory_v2/model/metric_example/request.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spaceone/inventory_v2/model/metric_example/request.py b/src/spaceone/inventory_v2/model/metric_example/request.py index 6016870..d5600ec 100644 --- a/src/spaceone/inventory_v2/model/metric_example/request.py +++ b/src/spaceone/inventory_v2/model/metric_example/request.py @@ -12,7 +12,7 @@ class MetricExampleCreateRequest(BaseModel): - metric_id: Union[str, None] = None + metric_id: str name: str options: dict tags: Union[dict, None] = {} From 3ee9044912b221b94233f014e4bca48480ceef50 Mon Sep 17 00:00:00 2001 From: kang2453 Date: Tue, 17 Dec 2024 16:30:02 +0900 Subject: [PATCH 2/3] feat: Add new managed namespace group configurations for Common, AWS, Azure, Etc, and Google Cloud Signed-off-by: kang2453 --- .../namespace/asset_change_history.yaml | 7 +++ .../namespace/asset_summary.yaml | 7 +++ .../namespace/iam_summary.yaml | 7 +++ .../managed_resource/namespace_group/aws.yaml | 5 +++ .../namespace_group/azure.yaml | 5 +++ .../namespace_group/common.yaml | 5 +++ .../managed_resource/namespace_group/etc.yaml | 5 +++ .../namespace_group/google_cloud.yaml | 5 +++ .../manager/managed_resource_manager.py | 9 ++++ .../manager/namespace_group_manager.py | 44 +++++++++++++++++++ .../inventory_v2/manager/namespace_manager.py | 42 ++++++++++++++++-- .../inventory_v2/model/namespace/database.py | 1 + .../model/namespace_group/database.py | 1 + .../inventory_v2/service/namespace_service.py | 3 +- 14 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 src/spaceone/inventory_v2/managed_resource/namespace/asset_change_history.yaml create mode 100644 src/spaceone/inventory_v2/managed_resource/namespace/asset_summary.yaml create mode 100644 src/spaceone/inventory_v2/managed_resource/namespace/iam_summary.yaml create mode 100644 src/spaceone/inventory_v2/managed_resource/namespace_group/aws.yaml create mode 100644 src/spaceone/inventory_v2/managed_resource/namespace_group/azure.yaml create mode 100644 src/spaceone/inventory_v2/managed_resource/namespace_group/common.yaml create mode 100644 src/spaceone/inventory_v2/managed_resource/namespace_group/etc.yaml create mode 100644 src/spaceone/inventory_v2/managed_resource/namespace_group/google_cloud.yaml diff --git a/src/spaceone/inventory_v2/managed_resource/namespace/asset_change_history.yaml b/src/spaceone/inventory_v2/managed_resource/namespace/asset_change_history.yaml new file mode 100644 index 0000000..c6633ac --- /dev/null +++ b/src/spaceone/inventory_v2/managed_resource/namespace/asset_change_history.yaml @@ -0,0 +1,7 @@ +--- +namespace_id: ns-managed-asset-change-history +name: Asset/ChangeHistory +category: ASSET +resource_group: "DOMAIN" +namespace_group_id: nsg-managed-common +version: '1.0' diff --git a/src/spaceone/inventory_v2/managed_resource/namespace/asset_summary.yaml b/src/spaceone/inventory_v2/managed_resource/namespace/asset_summary.yaml new file mode 100644 index 0000000..f320ada --- /dev/null +++ b/src/spaceone/inventory_v2/managed_resource/namespace/asset_summary.yaml @@ -0,0 +1,7 @@ +--- +namespace_id: ns-managed-asset-summary +name: Asset/Summary +category: ASSET +resource_group: "DOMAIN" +namespace_group_id: nsg-managed-common +version: '1.0' diff --git a/src/spaceone/inventory_v2/managed_resource/namespace/iam_summary.yaml b/src/spaceone/inventory_v2/managed_resource/namespace/iam_summary.yaml new file mode 100644 index 0000000..c612abd --- /dev/null +++ b/src/spaceone/inventory_v2/managed_resource/namespace/iam_summary.yaml @@ -0,0 +1,7 @@ +--- +namespace_id: ns-managed-iam-summary +name: IAM/Summary +category: IAM +resource_group: "DOMAIN" +namespace_group_id: nsg-managed-common +version: '1.0' diff --git a/src/spaceone/inventory_v2/managed_resource/namespace_group/aws.yaml b/src/spaceone/inventory_v2/managed_resource/namespace_group/aws.yaml new file mode 100644 index 0000000..b64aa43 --- /dev/null +++ b/src/spaceone/inventory_v2/managed_resource/namespace_group/aws.yaml @@ -0,0 +1,5 @@ +--- +namespace_group_id: nsg-managed-aws +name: AWS +icon: https://spaceone-custom-assets.s3.ap-northeast-2.amazonaws.com/console-assets/icons/aws.svg +version: '1.0' diff --git a/src/spaceone/inventory_v2/managed_resource/namespace_group/azure.yaml b/src/spaceone/inventory_v2/managed_resource/namespace_group/azure.yaml new file mode 100644 index 0000000..0c98ffa --- /dev/null +++ b/src/spaceone/inventory_v2/managed_resource/namespace_group/azure.yaml @@ -0,0 +1,5 @@ +--- +namespace_group_id: nsg-managed-azure +name: Azure +icon: https://spaceone-custom-assets.s3.ap-northeast-2.amazonaws.com/console-assets/icons/azure.svg +version: '1.0' diff --git a/src/spaceone/inventory_v2/managed_resource/namespace_group/common.yaml b/src/spaceone/inventory_v2/managed_resource/namespace_group/common.yaml new file mode 100644 index 0000000..d5bf196 --- /dev/null +++ b/src/spaceone/inventory_v2/managed_resource/namespace_group/common.yaml @@ -0,0 +1,5 @@ +--- +namespace_group_id: nsg-managed-common +name: Common +icon: https://spaceone-custom-assets.s3.ap-northeast-2.amazonaws.com/console-assets/icons/common.yaml +version: '1.0' diff --git a/src/spaceone/inventory_v2/managed_resource/namespace_group/etc.yaml b/src/spaceone/inventory_v2/managed_resource/namespace_group/etc.yaml new file mode 100644 index 0000000..cc925d2 --- /dev/null +++ b/src/spaceone/inventory_v2/managed_resource/namespace_group/etc.yaml @@ -0,0 +1,5 @@ +--- +namespace_group_id: nsg-managed-etc +name: Etc +icon: https://spaceone-custom-assets.s3.ap-northeast-2.amazonaws.com/console-assets/icons/etc.svg +version: '1.0' diff --git a/src/spaceone/inventory_v2/managed_resource/namespace_group/google_cloud.yaml b/src/spaceone/inventory_v2/managed_resource/namespace_group/google_cloud.yaml new file mode 100644 index 0000000..1175e4d --- /dev/null +++ b/src/spaceone/inventory_v2/managed_resource/namespace_group/google_cloud.yaml @@ -0,0 +1,5 @@ +--- +namespace_group_id: nsg-managed-google_cloud +name: Google Cloud +icon: https://spaceone-custom-assets.s3.ap-northeast-2.amazonaws.com/console-assets/icons/google_cloud.svg +version: '1.0' diff --git a/src/spaceone/inventory_v2/manager/managed_resource_manager.py b/src/spaceone/inventory_v2/manager/managed_resource_manager.py index b3a269c..256feca 100644 --- a/src/spaceone/inventory_v2/manager/managed_resource_manager.py +++ b/src/spaceone/inventory_v2/manager/managed_resource_manager.py @@ -7,11 +7,20 @@ _LOGGER = logging.getLogger(__name__) CURRENT_DIR = os.path.dirname(__file__) +_NAMESPACE_GROUP_DIR = os.path.join(CURRENT_DIR, "../managed_resource/namespace_group/") _NAMESPACE_DIR = os.path.join(CURRENT_DIR, "../managed_resource/namespace/") _METRIC_DIR = os.path.join(CURRENT_DIR, "../managed_resource/metric/") class ManagedResourceManager(BaseManager): + + def get_managed_namespace_groups(self) -> dict: + namespace_group_map = {} + for namespace_group_info in self._load_managed_resources(_NAMESPACE_GROUP_DIR): + namespace_group_map[namespace_group_info["namespace_group_id"]] = namespace_group_info + + return namespace_group_map + def get_managed_namespaces(self) -> dict: namespace_map = {} for namespace_info in self._load_managed_resources(_NAMESPACE_DIR): diff --git a/src/spaceone/inventory_v2/manager/namespace_group_manager.py b/src/spaceone/inventory_v2/manager/namespace_group_manager.py index 434379e..6f967f1 100644 --- a/src/spaceone/inventory_v2/manager/namespace_group_manager.py +++ b/src/spaceone/inventory_v2/manager/namespace_group_manager.py @@ -2,6 +2,7 @@ from operator import is_ from re import U from typing import Tuple, List, Optional, Union + from fastapi.background import P from mongoengine import QuerySet from spaceone.core import utils, cache @@ -13,6 +14,7 @@ from spaceone.inventory_v2.model.namespace_group.database import NamespaceGroup from spaceone.inventory_v2.model.namespace.database import Namespace from spaceone.inventory_v2.model.namespace.response import NamespaceResponse +from spaceone.inventory_v2.manager.managed_resource_manager import ManagedResourceManager _LOGGER = logging.getLogger(__name__) @@ -94,12 +96,54 @@ def filter_namespace_groups(self, **conditions) -> QuerySet: return self.namespace_group_model.filter(**conditions) def list_namespace_groups(self, query: dict, domain_id:str) -> Tuple[QuerySet, int]: + self.create_managed_namespace_group(domain_id) return self.namespace_group_model.query(**query) def stat_namespace_groups(self, query: dict) -> dict: result = self.namespace_group_model.stat(**query) return result if result is not None else {} + + # @cache.cacheable(key="inventory-v2:managed-namespace_group:{domain_id}:sync", expire=300) + def create_managed_namespace_group(self, domain_id:str) -> bool: + managed_resource_mgr = ManagedResourceManager() + + namespace_groups_vo = self.filter_namespace_groups( + domain_id=domain_id, + is_managed=True, + ) + installed_namespace_groups_version_map = {} + + for namespace_group_vo in namespace_groups_vo: + installed_namespace_groups_version_map[ + namespace_group_vo.namespace_group_id + ] = namespace_group_vo.version + + + managed_namespace_groups = managed_resource_mgr.get_managed_namespace_groups() + + for managed_nsg_id, managed_nsg_info in managed_namespace_groups.items(): + managed_nsg_info["domain_id"] = domain_id + managed_nsg_info["is_managed"] = True + managed_nsg_info["resource_group"] = "DOMAIN" + managed_nsg_info["workspace_id"] = "*" + + if ns_version := installed_namespace_groups_version_map.get(managed_nsg_id): + if ns_version != managed_nsg_info["version"]: + _LOGGER.debug( + f"[_create_managed_namespace_group] update managed namespace: {managed_nsg_id}" + ) + namespace_group_vo = self.get_namespace_group(managed_nsg_id, domain_id) + self.update_namespace_group_by_vo(managed_nsg_info, namespace_group_vo) + else: + _LOGGER.debug( + f"[_create_managed_namespace_group] create new managed namespace: {managed_nsg_id}" + ) + self.create_namespace_group(managed_nsg_info) + + return True + + # def get_namespaces_in_namespace_group( # self, # namespace_group_id: str, diff --git a/src/spaceone/inventory_v2/manager/namespace_manager.py b/src/spaceone/inventory_v2/manager/namespace_manager.py index 5d8afa4..84b51e5 100644 --- a/src/spaceone/inventory_v2/manager/namespace_manager.py +++ b/src/spaceone/inventory_v2/manager/namespace_manager.py @@ -6,7 +6,9 @@ from spaceone.core.manager import BaseManager from spaceone.inventory_v2.error.error_namespace import * +from spaceone.inventory_v2.model import namespace_group from spaceone.inventory_v2.model.namespace.database import Namespace +from spaceone.inventory_v2.manager.managed_resource_manager import ManagedResourceManager _LOGGER = logging.getLogger(__name__) @@ -70,11 +72,45 @@ def get_namespace( def filter_namespaces(self, **conditions) -> QuerySet: return self.namespace_model.filter(**conditions) - def list_namespaces(self, query: dict) -> Tuple[QuerySet, int]: + def list_namespaces(self, query: dict, domain_id: str) -> Tuple[QuerySet, int]: + # self.create_managed_namespace(domain_id) return self.namespace_model.query(**query) def stat_namespaces(self, query: dict) -> dict: return self.namespace_model.stat(**query) - - + @cache.cacheable(key="inventory-v2:managed-namespace:{domain_id}:sync", expire=300) + def create_managed_namespace(self, domain_id:str) -> bool: + managed_resource_mgr = ManagedResourceManager() + + namespace_vos = self.filter_namespaces(domain_id=domain_id, is_managed=True) + + installed_namespace_version_map = {} + for namespace_vo in namespace_vos: + installed_namespace_version_map[ + namespace_vo.namespace_id + ] = namespace_vo.version + + managed_namespace_map = managed_resource_mgr.get_managed_namespaces() + + for managed_ns_id, managed_ns_info in managed_namespace_map.items(): + managed_ns_info["domain_id"] = domain_id + managed_ns_info["is_managed"] = True + managed_ns_info["resource_group"] = "DOMAIN" + managed_ns_info["workspace_id"] = "*" + + if ns_version := installed_namespace_version_map.get(managed_ns_id): + if ns_version != managed_ns_info["version"]: + _LOGGER.debug( + f"[_create_managed_namespace] update managed namespace: {managed_ns_id}" + ) + namespace_vo = self.get_namespace(managed_ns_id, domain_id) + self.update_namespace_by_vo(managed_ns_info, namespace_vo) + else: + _LOGGER.debug( + f"[_create_managed_namespace] create new managed namespace: {managed_ns_id}" + ) + self.create_namespace(managed_ns_info) + + return True + diff --git a/src/spaceone/inventory_v2/model/namespace/database.py b/src/spaceone/inventory_v2/model/namespace/database.py index 3bc37cd..d0b5b55 100644 --- a/src/spaceone/inventory_v2/model/namespace/database.py +++ b/src/spaceone/inventory_v2/model/namespace/database.py @@ -15,6 +15,7 @@ class Namespace(MongoModel): namespace_group_id = StringField(max_length=40) workspace_id = StringField(max_length=40) domain_id = StringField(max_length=40) + version = StringField(max_length=40, default=None, null=True) created_at = DateTimeField(auto_now_add=True) updated_at = DateTimeField(auto_now=True) diff --git a/src/spaceone/inventory_v2/model/namespace_group/database.py b/src/spaceone/inventory_v2/model/namespace_group/database.py index 2d968d9..88ccc23 100644 --- a/src/spaceone/inventory_v2/model/namespace_group/database.py +++ b/src/spaceone/inventory_v2/model/namespace_group/database.py @@ -13,6 +13,7 @@ class NamespaceGroup(MongoModel): resource_group = StringField(max_length=40, choices=("DOMAIN", "WORKSPACE")) workspace_id = StringField(max_length=40) domain_id = StringField(max_length=40) + version = StringField(max_length=40, default=None, null=True) created_at = DateTimeField(auto_now_add=True) updated_at = DateTimeField(auto_now=True) diff --git a/src/spaceone/inventory_v2/service/namespace_service.py b/src/spaceone/inventory_v2/service/namespace_service.py index 3dafc0e..5bb16b2 100644 --- a/src/spaceone/inventory_v2/service/namespace_service.py +++ b/src/spaceone/inventory_v2/service/namespace_service.py @@ -213,7 +213,8 @@ def list( query = params.query or {} namespace_vos, total_count = self.namespace_mgr.list_namespaces( - query + query, + params.domain_id, ) namespaces_info = [namespace_vo.to_dict() for namespace_vo in namespace_vos] From a4e20a1b825f5162e06cd97ba85d7a49de983279 Mon Sep 17 00:00:00 2001 From: kang2453 Date: Tue, 17 Dec 2024 17:59:14 +0900 Subject: [PATCH 3/3] fix: Add new managed namespace for Common Signed-off-by: kang2453 --- src/spaceone/inventory_v2/manager/namespace_manager.py | 2 +- .../inventory_v2/service/namespace_group_service.py | 6 +++--- src/spaceone/inventory_v2/service/namespace_service.py | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/spaceone/inventory_v2/manager/namespace_manager.py b/src/spaceone/inventory_v2/manager/namespace_manager.py index 84b51e5..35ae949 100644 --- a/src/spaceone/inventory_v2/manager/namespace_manager.py +++ b/src/spaceone/inventory_v2/manager/namespace_manager.py @@ -73,7 +73,7 @@ def filter_namespaces(self, **conditions) -> QuerySet: return self.namespace_model.filter(**conditions) def list_namespaces(self, query: dict, domain_id: str) -> Tuple[QuerySet, int]: - # self.create_managed_namespace(domain_id) + self.create_managed_namespace(domain_id) return self.namespace_model.query(**query) def stat_namespaces(self, query: dict) -> dict: diff --git a/src/spaceone/inventory_v2/service/namespace_group_service.py b/src/spaceone/inventory_v2/service/namespace_group_service.py index 585c32a..8643e9a 100644 --- a/src/spaceone/inventory_v2/service/namespace_group_service.py +++ b/src/spaceone/inventory_v2/service/namespace_group_service.py @@ -174,15 +174,15 @@ def get(self, params: NamespaceGroupGetRequest) -> Union[NamespaceGroupResponse, permission="inventory-v2:NamespaceGroup.read", role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"], ) + @change_value_by_rule("APPEND", "workspace_id","*") @append_query_filter( [ "namespace_group_id", - "is_managed", + "workspace_id", "domain_id", ] ) @append_keyword_filter(["namespace_group_id", "name"]) - @change_value_by_rule("APPEND", "workspace_id","*") @convert_model def list( self, params: NamespaceGroupSearchQueryRequest @@ -205,7 +205,7 @@ def list( query = params.query or {} namespace_group_vos, total_count = self.namespace_group_mgr.list_namespace_groups( - query, params.domain_id, + query, params.domain_id ) namespaces_group_info = [namespace_group_vo.to_dict() for namespace_group_vo in namespace_group_vos] diff --git a/src/spaceone/inventory_v2/service/namespace_service.py b/src/spaceone/inventory_v2/service/namespace_service.py index 5bb16b2..5168cf8 100644 --- a/src/spaceone/inventory_v2/service/namespace_service.py +++ b/src/spaceone/inventory_v2/service/namespace_service.py @@ -178,6 +178,7 @@ def get(self, params: NamespaceGetRequest) -> Union[NamespaceResponse, dict]: permission="inventory-v2:Namespace.read", role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"], ) + @change_value_by_rule("APPEND", "workspace_id","*") @append_query_filter( [ "namespace_id", @@ -185,11 +186,11 @@ def get(self, params: NamespaceGetRequest) -> Union[NamespaceResponse, dict]: "is_managed", "resource_type", "namespace_group_id", + "workspace_id", "domain_id", ] ) @append_keyword_filter(["namespace_id", "name"]) - @change_value_by_rule("APPEND", "workspace_id","*") @convert_model def list( self, params: NamespaceSearchQueryRequest