Skip to content

Commit

Permalink
feat: add new services namespacegroup, namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
kang2453 authored and ImMin5 committed Dec 10, 2024
1 parent 7663f6b commit 894552c
Show file tree
Hide file tree
Showing 20 changed files with 1,067 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/spaceone/inventory_v2/error/error_namespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from spaceone.core.error import *


class ERROR_RELATED_NAMESPACE_GROUP(ERROR_INVALID_ARGUMENT):
_message = (
"Related namespace_group is exist. (namespace_group_id = {namespace_group_id})"
)


class ERROR_NOT_ALLOWED_ADD_USER_TO_PUBLIC_PROJECT(ERROR_INVALID_ARGUMENT):
_message = "Not allowed to add user to public project."
24 changes: 24 additions & 0 deletions src/spaceone/inventory_v2/error/error_namespace_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

from spaceone.core.error import *


class ERROR_REQUIRED_PARAMETER(ERROR_INVALID_ARGUMENT):
_message = "Required parameter. (key = {key})"


class ERROR_RELATED_NAMESPACE_EXIST(ERROR_INVALID_ARGUMENT):
_message = "Related namespace is exist. (namespace_id = {namespace_id})"


class ERROR_RELATED_NAMESPACE_GROUP_EXIST(ERROR_INVALID_ARGUMENT):
_message = "Related namespace group is exist. (namespace_group_id = {namespace_group_id})"


class ERROR_NOT_ALLOWED_TO_CHANGE_PARENT_GROUP_TO_SUB_PROJECT_GROUP(ERROR_INVALID_ARGUMENT):
_message = "Not allowed to change parent group to sub namespace group. (namespace_group_id = {namespace_group_id})"


class ERROR_USER_NOT_IN_PROJECT_GROUP(ERROR_PERMISSION_DENIED):
_message = "{namespace_id} is not in namespace group."


4 changes: 4 additions & 0 deletions src/spaceone/inventory_v2/interface/grpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .collector import Collector
from .job import Job
from .job_task import JobTask
from .namespace_group import NamespaceGroup
from .namespace import Namespace
from .metric import Metric

_all_ = ["app"]
Expand All @@ -12,4 +14,6 @@
app.add_service(Collector)
app.add_service(Job)
app.add_service(JobTask)
app.add_service(NamespaceGroup)
app.add_service(Namespace)
app.add_service(Metric)
46 changes: 46 additions & 0 deletions src/spaceone/inventory_v2/interface/grpc/namespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from google.protobuf.json_format import ParseDict
from spaceone.api.inventory_v2.v1 import namespace_pb2, namespace_pb2_grpc
from spaceone.core.pygrpc import BaseAPI

from spaceone.inventory_v2.service.namespace_service import NamespaceService

class Namespace(BaseAPI, namespace_pb2_grpc.NamespaceServicer):

pb2 = namespace_pb2
pb2_grpc = namespace_pb2_grpc

def create(self, request, context):
params, metadata = self.parse_request(request, context)
namespace_svc = NamespaceService(metadata)
response: dict = namespace_svc.create(params)
return self.dict_to_message(response)

def update(self, request, context):
params, metadata = self.parse_request(request, context)
namespace_svc = NamespaceService(metadata)
response: dict = namespace_svc.update(params)
return self.dict_to_message(response)

def delete(self, request, context):
params, metadata = self.parse_request(request, context)
namespace_svc = NamespaceService(metadata)
response: dict = namespace_svc.delete(params)
return self.empty()

def get(self, request, context):
params, metadata = self.parse_request(request, context)
namespace_svc = NamespaceService(metadata)
response: dict = namespace_svc.get(params)
return self.dict_to_message(response)

def list(self, request, context):
params, metadata = self.parse_request(request, context)
namespace_svc = NamespaceService(metadata)
response: dict = namespace_svc.list(params)
return self.dict_to_message(response)

def stat(self, request, context):
params, metadata = self.parse_request(request, context)
namespace_svc = NamespaceService(metadata)
response: dict = namespace_svc.stat(params)
return self.dict_to_message(response)
47 changes: 47 additions & 0 deletions src/spaceone/inventory_v2/interface/grpc/namespace_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from google.protobuf.json_format import ParseDict
from spaceone.api.inventory_v2.v1 import namespace_group_pb2, namespace_group_pb2_grpc
from spaceone.core.pygrpc import BaseAPI

from spaceone.inventory_v2.service.namespace_group_service import NamespaceGroupService


class NamespaceGroup(BaseAPI, namespace_group_pb2_grpc.NamespaceGroupServicer):

pb2 = namespace_group_pb2
pb2_grpc = namespace_group_pb2_grpc

def create(self, request, context):
params, metadata = self.parse_request(request, context)
namespacegroup_svc = NamespaceGroupService(metadata)
response: dict = namespacegroup_svc.create(params)
return self.dict_to_message(response)

def update(self, request, context):
params, metadata = self.parse_request(request, context)
namespacegroup_svc = NamespaceGroupService(metadata)
response: dict = namespacegroup_svc.update(params)
return self.dict_to_message(response)

def delete(self, request, context):
params, metadata = self.parse_request(request, context)
namespacegroup_svc = NamespaceGroupService(metadata)
response: dict = namespacegroup_svc.delete(params)
return self.empty()

def get(self, request, context):
params, metadata = self.parse_request(request, context)
namespacegroup_svc = NamespaceGroupService(metadata)
response: dict = namespacegroup_svc.get(params)
return self.dict_to_message(response)

def list(self, request, context):
params, metadata = self.parse_request(request, context)
namespacegroup_svc = NamespaceGroupService(metadata)
response: dict = namespacegroup_svc.list(params)
return self.dict_to_message(response)

def stat(self, request, context):
params, metadata = self.parse_request(request, context)
namespacegroup_svc = NamespaceGroupService(metadata)
response: dict = namespacegroup_svc.stat(params)
return self.dict_to_message(response)
2 changes: 2 additions & 0 deletions src/spaceone/inventory_v2/manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
from spaceone.inventory_v2.lib.resource_manager import ResourceManager
from spaceone.inventory_v2.manager.region_manager import RegionManager
from spaceone.inventory_v2.manager.collecting_manager import CollectingManager
from spaceone.inventory_v2.manager.namespace_group_manager import NamespaceGroupManager
from spaceone.inventory_v2.manager.namespace_manager import NamespaceManager
120 changes: 120 additions & 0 deletions src/spaceone/inventory_v2/manager/namespace_group_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import logging
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


from spaceone.core.manager import BaseManager
from spaceone.inventory_v2.error.error_namespace_group import *
from spaceone.inventory_v2.manager.namespace_manager import NamespaceManager
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

_LOGGER = logging.getLogger(__name__)


class NamespaceGroupManager(BaseManager):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.namespace_group_model = NamespaceGroup
self.namespace_model = Namespace

def create_namespace_group(self, params: dict) -> NamespaceGroup:
def _rollback(vo: NamespaceGroup):
_LOGGER.info(
f"[ROLLBACK] Delete namespace_group : {vo.name} ({vo.namespace_group_id})"
)
vo.delete()

if "namespace_group_id" not in params:
params["namespace_group_id"] = utils.generate_id("nsg")
if "is_managed" not in params:
params["is_managed"] = False

namespace_group_vo= self.namespace_group_model.create(params)
self.transaction.add_rollback(_rollback, namespace_group_vo)

return namespace_group_vo

def update_namespace_group_by_vo(
self,
params: dict,
namespace_group_vo: NamespaceGroup
) -> NamespaceGroup:
def _rollback(old_data):
_LOGGER.info(
f'[ROLLBACK] Revert Data : {old_data["name"]} ({old_data["namespace_group_id"]})'
)
namespace_group_vo.update(old_data)

self.transaction.add_rollback(_rollback, namespace_group_vo.to_dict())
return namespace_group_vo.update(params)

# @staticmethod
def delete_namespace_group_by_vo(
self,
namespace_group_vo: NamespaceGroup
) -> None:

namespace_mgr = NamespaceManager()
namespace_vos = namespace_mgr.filter_namespaces(
namespace_group_id=namespace_group_vo.namespace_group_id,
domain_id = namespace_group_vo.domain_id,
)

for namespace_vo in namespace_vos:
raise ERROR_RELATED_NAMESPACE_EXIST(namespace_id=namespace_vo.namespace_id)

namespace_group_vo.delete()

def get_namespace_group(
self,
namespace_group_id: str,
domain_id: str,
workspace_id: Union[list, str, None] = None,

) -> NamespaceGroup:
conditions:dict = {
"namespace_group_id": namespace_group_id,
"domain_id": domain_id,
}

if workspace_id:
conditions.update({"workspace_id": workspace_id})

return self.namespace_group_model.get(**conditions)


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]:
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 {}

# def get_namespaces_in_namespace_group(
# self,
# namespace_group_id: str,
# ) -> NamespaceResponse:

# namespace_vos = self.namespace_model.filter(
# namespace_group_id=namespace_group_id
# )
# namespaces = [namespace_vo.namespace_id for namespace_vo in namespace_vos]

# child_namespace_groups = self.namespace_group_model.filter(
# parent_group_id=namespace_group_id
# )
# for child_namespace_group in child_namespace_groups:
# parent_group_id = child_namespace_group.namespace_group_id
# namespaces.extend(
# self.get_namespaces_in_namespace_group(parent_group_id)
# )
# return list(set(namespaces))
79 changes: 79 additions & 0 deletions src/spaceone/inventory_v2/manager/namespace_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import logging
from typing import Tuple,List, Optional, Union
from anyio import Condition
from mongoengine import QuerySet

from spaceone.core.manager import BaseManager
from spaceone.inventory_v2.error.error_namespace import *
from spaceone.inventory_v2.model.namespace.database import Namespace

_LOGGER = logging.getLogger(__name__)


class NamespaceManager(BaseManager):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.namespace_model = Namespace


def create_namespace(self, params:dict) -> Namespace:
def _rollback(vo: Namespace):
_LOGGER.info(f"[ROLLBACK] Delete namespace : {vo.name} ({vo.namespace_id}) ({vo.namespace_group_id})")
vo.delete()

if "namespace_group_id" not in params:
raise ERROR_REQUIRED_PARAMETER(key="namespace_group_id")

namespace_vo: Namespace = self.namespace_model.create(params)
self.transaction.add_rollback(_rollback, namespace_vo)

return namespace_vo

def update_namespace_by_vo(
self,
params: dict,
namespace_vo: Namespace
) -> Namespace:
def _rollback(old_data):
_LOGGER.info(f'[ROLLBACK] Revert Data : {old_data["name"]} ({old_data["namespace_id"]}) ({old_data["namespace_group_id"]})')

namespace_vo.update(old_data)

self.transaction.add_rollback(_rollback, namespace_vo.to_dict())

return namespace_vo.update(params)

def delete_namespace_by_vo(
self,
namespace_vo: Namespace
) -> None:
namespace_vo.delete()

def get_namespace(
self,
namespace_id: str,
domain_id: str,
workspace_id: Union[list, str, None] = None,
) -> Namespace:

conditions: dict = {
"namespace_id": namespace_id,
"domain_id": domain_id
}

if workspace_id:
conditions.update({"workspace_id": workspace_id})

return self.namespace_model.get(**conditions)

def filter_namespaces(self, **conditions) -> QuerySet:
return self.namespace_model.filter(**conditions)

def list_namespaces(self, query: dict) -> Tuple[QuerySet, int]:
return self.namespace_model.query(**query)

def stat_namespaces(self, query: dict) -> dict:
return self.namespace_model.stat(**query)



4 changes: 3 additions & 1 deletion src/spaceone/inventory_v2/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
from spaceone.inventory_v2.model.collector.database import Collector
from spaceone.inventory_v2.model.collector_rule.database import CollectorRule
from spaceone.inventory_v2.model.collection_state.database import CollectionState
from spaceone.inventory_v2.model.namespace.database import Namespace
from spaceone.inventory_v2.model.namespace_group.database import NamespaceGroup
from spaceone.inventory_v2.model.metric.database import Metric
from spaceone.inventory_v2.model.metric_data.database import MetricData
from spaceone.inventory_v2.model.metric_example.database import MetricExample
from spaceone.inventory_v2.model.job.database import Job
from spaceone.inventory_v2.model.job_task.database import JobTask, JobTaskDetail
from spaceone.inventory_v2.model.job_task.database import JobTask, JobTaskDetail
1 change: 1 addition & 0 deletions src/spaceone/inventory_v2/model/namespace/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from spaceone.inventory_v2.model.namespace import *
Loading

0 comments on commit 894552c

Please sign in to comment.