Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update metric_id type to enforce string requirement in MetricExa… #19

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

2 changes: 1 addition & 1 deletion src/spaceone/inventory_v2/model/metric_example/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class MetricExampleCreateRequest(BaseModel):
metric_id: Union[str, None] = None
metric_id: str
name: str
options: dict
tags: Union[dict, None] = {}
Expand Down
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
6 changes: 3 additions & 3 deletions src/spaceone/inventory_v2/service/namespace_group_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
6 changes: 4 additions & 2 deletions src/spaceone/inventory_v2/service/namespace_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,19 @@ 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",
"category",
"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
Expand All @@ -213,7 +214,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
Loading