-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new services namespacegroup, namespace
- Loading branch information
Showing
20 changed files
with
1,067 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
src/spaceone/inventory_v2/interface/grpc/namespace_group.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/spaceone/inventory_v2/manager/namespace_group_manager.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from spaceone.inventory_v2.model.namespace import * |
Oops, something went wrong.