Skip to content

Commit

Permalink
feat: Add new managed namespace group configurations for Common, AWS,…
Browse files Browse the repository at this point in the history
… Azure, Etc, and Google Cloud

Signed-off-by: kang2453 <[email protected]>
  • Loading branch information
kang2453 committed Dec 17, 2024
1 parent a7be44d commit 3ee9044
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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'
9 changes: 9 additions & 0 deletions src/spaceone/inventory_v2/manager/managed_resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
44 changes: 44 additions & 0 deletions src/spaceone/inventory_v2/manager/namespace_group_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__)

Expand Down Expand Up @@ -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,
Expand Down
42 changes: 39 additions & 3 deletions src/spaceone/inventory_v2/manager/namespace_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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

1 change: 1 addition & 0 deletions src/spaceone/inventory_v2/model/namespace/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion src/spaceone/inventory_v2/service/namespace_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 3ee9044

Please sign in to comment.