diff --git a/src/spaceone/config/info/__init__.py b/src/spaceone/config/info/__init__.py deleted file mode 100644 index 3154be3..0000000 --- a/src/spaceone/config/info/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from spaceone.config.info.common_info import * -from spaceone.config.info.user_config_info import * -from spaceone.config.info.domain_config_info import * diff --git a/src/spaceone/config/info/common_info.py b/src/spaceone/config/info/common_info.py deleted file mode 100644 index a47f60d..0000000 --- a/src/spaceone/config/info/common_info.py +++ /dev/null @@ -1,12 +0,0 @@ -from google.protobuf.empty_pb2 import Empty -from spaceone.core.pygrpc.message_type import * - -__all__ = ["EmptyInfo", "StatisticsInfo"] - - -def EmptyInfo(): - return Empty() - - -def StatisticsInfo(result): - return change_struct_type(result) diff --git a/src/spaceone/config/info/domain_config_info.py b/src/spaceone/config/info/domain_config_info.py deleted file mode 100644 index e5ad9c3..0000000 --- a/src/spaceone/config/info/domain_config_info.py +++ /dev/null @@ -1,35 +0,0 @@ -import functools -from spaceone.api.config.v1 import domain_config_pb2 -from spaceone.core.pygrpc.message_type import * -from spaceone.core import utils -from spaceone.config.model.domain_config_model import DomainConfig - -__all__ = ["DomainConfigInfo", "DomainConfigsInfo"] - - -def DomainConfigInfo(domain_config_vo: DomainConfig, minimal=False): - info = { - "name": domain_config_vo.name, - } - - if not minimal: - info.update( - { - "data": change_struct_type(domain_config_vo.data), - "tags": change_struct_type(domain_config_vo.tags), - "domain_id": domain_config_vo.domain_id, - "updated_at": utils.datetime_to_iso8601(domain_config_vo.updated_at), - "created_at": utils.datetime_to_iso8601(domain_config_vo.created_at), - } - ) - - return domain_config_pb2.DomainConfigInfo(**info) - - -def DomainConfigsInfo(domain_config_vos, total_count, **kwargs): - return domain_config_pb2.DomainConfigsInfo( - results=list( - map(functools.partial(DomainConfigInfo, **kwargs), domain_config_vos) - ), - total_count=total_count, - ) diff --git a/src/spaceone/config/info/user_config_info.py b/src/spaceone/config/info/user_config_info.py deleted file mode 100644 index 0dee9ca..0000000 --- a/src/spaceone/config/info/user_config_info.py +++ /dev/null @@ -1,34 +0,0 @@ -import functools -from spaceone.api.config.v1 import user_config_pb2 -from spaceone.core.pygrpc.message_type import * -from spaceone.core import utils -from spaceone.config.model.user_config_model import UserConfig - -__all__ = ["UserConfigInfo", "UserConfigsInfo"] - - -def UserConfigInfo(user_config_vo: UserConfig, minimal=False): - info = { - "name": user_config_vo.name, - } - - if not minimal: - info.update( - { - "data": change_struct_type(user_config_vo.data), - "tags": change_struct_type(user_config_vo.tags), - "user_id": user_config_vo.user_id, - "domain_id": user_config_vo.domain_id, - "updated_at": utils.datetime_to_iso8601(user_config_vo.updated_at), - "created_at": utils.datetime_to_iso8601(user_config_vo.created_at), - } - ) - - return user_config_pb2.UserConfigInfo(**info) - - -def UserConfigsInfo(user_config_vos, total_count, **kwargs): - return user_config_pb2.UserConfigsInfo( - results=list(map(functools.partial(UserConfigInfo, **kwargs), user_config_vos)), - total_count=total_count, - ) diff --git a/src/spaceone/config/interface/grpc/__init__.py b/src/spaceone/config/interface/grpc/__init__.py index 46f8e12..52c2e1a 100644 --- a/src/spaceone/config/interface/grpc/__init__.py +++ b/src/spaceone/config/interface/grpc/__init__.py @@ -1,11 +1,13 @@ from spaceone.core.pygrpc.server import GRPCServer +from spaceone.config.interface.grpc.user_config import UserConfig from spaceone.config.interface.grpc.domain_config import DomainConfig +from spaceone.config.interface.grpc.shared_config import SharedConfig from spaceone.config.interface.grpc.public_config import PublicConfig -from spaceone.config.interface.grpc.user_config import UserConfig _all_ = ["app"] app = GRPCServer() -app.add_service(DomainConfig) app.add_service(UserConfig) +app.add_service(DomainConfig) +app.add_service(SharedConfig) app.add_service(PublicConfig) diff --git a/src/spaceone/config/interface/grpc/domain_config.py b/src/spaceone/config/interface/grpc/domain_config.py index d8690e1..691318e 100644 --- a/src/spaceone/config/interface/grpc/domain_config.py +++ b/src/spaceone/config/interface/grpc/domain_config.py @@ -1,8 +1,7 @@ from spaceone.api.config.v1 import domain_config_pb2, domain_config_pb2_grpc from spaceone.core.pygrpc import BaseAPI + from spaceone.config.service.domain_config_service import DomainConfigService -from spaceone.config.info.domain_config_info import * -from spaceone.config.info.common_info import * class DomainConfig(BaseAPI, domain_config_pb2_grpc.DomainConfigServicer): @@ -11,73 +10,36 @@ class DomainConfig(BaseAPI, domain_config_pb2_grpc.DomainConfigServicer): def create(self, request, context): params, metadata = self.parse_request(request, context) - - with self.locator.get_service( - DomainConfigService, metadata - ) as domain_config_service: - return self.locator.get_info( - DomainConfigInfo, domain_config_service.create(params) - ) + domain_config_svc = DomainConfigService(metadata) + response: dict = domain_config_svc.create(params) + return self.dict_to_message(response) def update(self, request, context): params, metadata = self.parse_request(request, context) - - with self.locator.get_service( - DomainConfigService, metadata - ) as domain_config_service: - return self.locator.get_info( - DomainConfigInfo, domain_config_service.update(params) - ) + domain_config_svc = DomainConfigService(metadata) + response: dict = domain_config_svc.update(params) + return self.dict_to_message(response) def set(self, request, context): params, metadata = self.parse_request(request, context) - - with self.locator.get_service( - DomainConfigService, metadata - ) as domain_config_service: - return self.locator.get_info( - DomainConfigInfo, domain_config_service.set(params) - ) + domain_config_svc = DomainConfigService(metadata) + response: dict = domain_config_svc.set(params) + return self.dict_to_message(response) def delete(self, request, context): params, metadata = self.parse_request(request, context) - - with self.locator.get_service( - DomainConfigService, metadata - ) as domain_config_service: - domain_config_service.delete(params) - return self.locator.get_info(EmptyInfo) + domain_config_svc = DomainConfigService(metadata) + domain_config_svc.delete(params) + return self.empty() def get(self, request, context): params, metadata = self.parse_request(request, context) - - with self.locator.get_service( - DomainConfigService, metadata - ) as domain_config_service: - return self.locator.get_info( - DomainConfigInfo, domain_config_service.get(params) - ) + domain_config_svc = DomainConfigService(metadata) + response: dict = domain_config_svc.get(params) + return self.dict_to_message(response) def list(self, request, context): params, metadata = self.parse_request(request, context) - - with self.locator.get_service( - DomainConfigService, metadata - ) as domain_config_service: - domain_config_vos, total_count = domain_config_service.list(params) - return self.locator.get_info( - DomainConfigsInfo, - domain_config_vos, - total_count, - minimal=self.get_minimal(params), - ) - - def stat(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service( - DomainConfigService, metadata - ) as domain_config_service: - return self.locator.get_info( - StatisticsInfo, domain_config_service.stat(params) - ) + domain_config_svc = DomainConfigService(metadata) + response: dict = domain_config_svc.list(params) + return self.dict_to_message(response) diff --git a/src/spaceone/config/interface/grpc/public_config.py b/src/spaceone/config/interface/grpc/public_config.py index 35aaabf..a8c4b5f 100644 --- a/src/spaceone/config/interface/grpc/public_config.py +++ b/src/spaceone/config/interface/grpc/public_config.py @@ -38,20 +38,8 @@ def get(self, request, context): response: dict = public_config_svc.get(params) return self.dict_to_message(response) - def get_accessible_configs(self, request, context): - params, metadata = self.parse_request(request, context) - public_config_svc = PublicConfigService(metadata) - response: dict = public_config_svc.get_accessible_configs(params) - return self.dict_to_message(response) - def list(self, request, context): params, metadata = self.parse_request(request, context) public_config_svc = PublicConfigService(metadata) response: dict = public_config_svc.list(params) return self.dict_to_message(response) - - def stat(self, request, context): - params, metadata = self.parse_request(request, context) - public_config_svc = PublicConfigService(metadata) - response: dict = public_config_svc.stat(params) - return self.dict_to_message(response) diff --git a/src/spaceone/config/interface/grpc/shared_config.py b/src/spaceone/config/interface/grpc/shared_config.py new file mode 100644 index 0000000..e8f98b9 --- /dev/null +++ b/src/spaceone/config/interface/grpc/shared_config.py @@ -0,0 +1,39 @@ +from spaceone.api.config.v1 import shared_config_pb2, shared_config_pb2_grpc +from spaceone.core.pygrpc import BaseAPI + +from spaceone.config.service.shared_config_service import SharedConfigService + + +class SharedConfig(BaseAPI, shared_config_pb2_grpc.SharedConfigServicer): + pb2 = shared_config_pb2 + pb2_grpc = shared_config_pb2_grpc + + def create(self, request, context): + params, metadata = self.parse_request(request, context) + shared_config_svc = SharedConfigService(metadata) + response: dict = shared_config_svc.create(params) + return self.dict_to_message(response) + + def update(self, request, context): + params, metadata = self.parse_request(request, context) + shared_config_svc = SharedConfigService(metadata) + response: dict = shared_config_svc.update(params) + return self.dict_to_message(response) + + def delete(self, request, context): + params, metadata = self.parse_request(request, context) + shared_config_svc = SharedConfigService(metadata) + shared_config_svc.delete(params) + return self.empty() + + def get(self, request, context): + params, metadata = self.parse_request(request, context) + shared_config_svc = SharedConfigService(metadata) + response: dict = shared_config_svc.get(params) + return self.dict_to_message(response) + + def list(self, request, context): + params, metadata = self.parse_request(request, context) + shared_config_svc = SharedConfigService(metadata) + response: dict = shared_config_svc.list(params) + return self.dict_to_message(response) diff --git a/src/spaceone/config/interface/grpc/user_config.py b/src/spaceone/config/interface/grpc/user_config.py index 4d49002..252c87a 100644 --- a/src/spaceone/config/interface/grpc/user_config.py +++ b/src/spaceone/config/interface/grpc/user_config.py @@ -1,8 +1,7 @@ from spaceone.api.config.v1 import user_config_pb2, user_config_pb2_grpc from spaceone.core.pygrpc import BaseAPI + from spaceone.config.service.user_config_service import UserConfigService -from spaceone.config.info.user_config_info import * -from spaceone.config.info.common_info import * class UserConfig(BaseAPI, user_config_pb2_grpc.UserConfigServicer): @@ -11,73 +10,36 @@ class UserConfig(BaseAPI, user_config_pb2_grpc.UserConfigServicer): def create(self, request, context): params, metadata = self.parse_request(request, context) - - with self.locator.get_service( - UserConfigService, metadata - ) as user_config_service: - return self.locator.get_info( - UserConfigInfo, user_config_service.create(params) - ) + user_config_svc = UserConfigService(metadata) + response: dict = user_config_svc.create(params) + return self.dict_to_message(response) def update(self, request, context): params, metadata = self.parse_request(request, context) - - with self.locator.get_service( - UserConfigService, metadata - ) as user_config_service: - return self.locator.get_info( - UserConfigInfo, user_config_service.update(params) - ) + user_config_svc = UserConfigService(metadata) + response: dict = user_config_svc.update(params) + return self.dict_to_message(response) def set(self, request, context): params, metadata = self.parse_request(request, context) - - with self.locator.get_service( - UserConfigService, metadata - ) as user_config_service: - return self.locator.get_info( - UserConfigInfo, user_config_service.set(params) - ) + user_config_svc = UserConfigService(metadata) + response: dict = user_config_svc.set(params) + return self.dict_to_message(response) def delete(self, request, context): params, metadata = self.parse_request(request, context) - - with self.locator.get_service( - UserConfigService, metadata - ) as user_config_service: - user_config_service.delete(params) - return self.locator.get_info(EmptyInfo) + user_config_svc = UserConfigService(metadata) + user_config_svc.delete(params) + return self.empty() def get(self, request, context): params, metadata = self.parse_request(request, context) - - with self.locator.get_service( - UserConfigService, metadata - ) as user_config_service: - return self.locator.get_info( - UserConfigInfo, user_config_service.get(params) - ) + user_config_svc = UserConfigService(metadata) + response: dict = user_config_svc.get(params) + return self.dict_to_message(response) def list(self, request, context): params, metadata = self.parse_request(request, context) - - with self.locator.get_service( - UserConfigService, metadata - ) as user_config_service: - user_config_vos, total_count = user_config_service.list(params) - return self.locator.get_info( - UserConfigsInfo, - user_config_vos, - total_count, - minimal=self.get_minimal(params), - ) - - def stat(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service( - UserConfigService, metadata - ) as user_config_service: - return self.locator.get_info( - StatisticsInfo, user_config_service.stat(params) - ) + user_config_svc = UserConfigService(metadata) + response: dict = user_config_svc.list(params) + return self.dict_to_message(response) diff --git a/src/spaceone/config/manager/domain_config_manager.py b/src/spaceone/config/manager/domain_config_manager.py index b4b46c1..f19c97e 100644 --- a/src/spaceone/config/manager/domain_config_manager.py +++ b/src/spaceone/config/manager/domain_config_manager.py @@ -1,8 +1,10 @@ import logging +from typing import Tuple +from mongoengine import QuerySet from spaceone.core.manager import BaseManager -from spaceone.config.model.domain_config_model import DomainConfig +from spaceone.config.model.domain_config.database import DomainConfig _LOGGER = logging.getLogger(__name__) @@ -10,10 +12,10 @@ class DomainConfigManager(BaseManager): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.domain_config_model: DomainConfig = self.locator.get_model("DomainConfig") + self.domain_config_model = DomainConfig def create_domain_config(self, params: dict) -> DomainConfig: - def _rollback(vo: DomainConfig) -> None: + def _rollback(vo: DomainConfig): _LOGGER.info( f"[create_domain_config._rollback] " f"Delete domain config : {vo.name}" ) @@ -24,12 +26,6 @@ def _rollback(vo: DomainConfig) -> None: return domain_config_vo - def update_domain_config(self, params: dict) -> DomainConfig: - domain_config_vo: DomainConfig = self.get_domain_config( - params["name"], params["domain_id"] - ) - return self.update_domain_config_by_vo(params, domain_config_vo) - def update_domain_config_by_vo( self, params: dict, domain_config_vo: DomainConfig ) -> DomainConfig: @@ -43,18 +39,19 @@ def _rollback(old_data: dict): return domain_config_vo.update(params) - def delete_domain_config(self, name: str, domain_id: str) -> None: - domain_config_vo: DomainConfig = self.get_domain_config(name, domain_id) + def delete_domain_config_by_vo(self, domain_config_vo: DomainConfig) -> None: domain_config_vo.delete() def get_domain_config(self, name: str, domain_id: str) -> DomainConfig: - return self.domain_config_model.get(name=name, domain_id=domain_id) + return self.domain_config_model.get( + name=name, domain_id=domain_id + ) - def filter_domain_configs(self, **conditions: dict): + def filter_domain_configs(self, **conditions) -> QuerySet: return self.domain_config_model.filter(**conditions) - def list_domain_configs(self, query: dict) -> dict: + def list_domain_configs(self, query: dict) -> Tuple[QuerySet, int]: return self.domain_config_model.query(**query) - def state_domain_configs(self, query): + def stat_domain_configs(self, query: dict) -> dict: return self.domain_config_model.stat(**query) diff --git a/src/spaceone/config/manager/public_config_manager.py b/src/spaceone/config/manager/public_config_manager.py index 25c10fe..6781cf6 100644 --- a/src/spaceone/config/manager/public_config_manager.py +++ b/src/spaceone/config/manager/public_config_manager.py @@ -1,7 +1,7 @@ import logging -from typing import Tuple, Union - +from typing import Tuple from mongoengine import QuerySet + from spaceone.core.manager import BaseManager from spaceone.config.model.public_config.database import PublicConfig @@ -15,7 +15,7 @@ def __init__(self, *args, **kwargs): self.public_config_model = PublicConfig def create_public_config(self, params: dict) -> PublicConfig: - def _rollback(vo: PublicConfig) -> None: + def _rollback(vo: PublicConfig): _LOGGER.info( f"[create_public_config._rollback] " f"Delete public config : {vo.name}" ) @@ -27,7 +27,7 @@ def _rollback(vo: PublicConfig) -> None: return public_config_vo def update_public_config_by_vo( - self, params: dict, public_config_vo: PublicConfig + self, params: dict, public_config_vo: PublicConfig ) -> PublicConfig: def _rollback(old_data: dict): _LOGGER.info( @@ -39,24 +39,15 @@ def _rollback(old_data: dict): return public_config_vo.update(params) - @staticmethod - def delete_public_config_by_vo(public_config_vo: PublicConfig) -> None: + def delete_public_config_by_vo(self, public_config_vo: PublicConfig) -> None: public_config_vo.delete() - def get_public_config(self, name: str, domain_id: str, workspace_id: Union[list, str, None], - project_id: Union[list, str, None]) -> PublicConfig: - conditions = { - "name": name, - "domain_id": domain_id, - } - if workspace_id: - conditions["workspace_id"] = workspace_id - if project_id: - conditions["project_id"] = project_id - - return self.public_config_model.get(**conditions) + def get_public_config(self, name: str, domain_id: str) -> PublicConfig: + return self.public_config_model.get( + name=name, domain_id=domain_id + ) - def filter_public_configs(self, **conditions): + def filter_public_configs(self, **conditions) -> QuerySet: return self.public_config_model.filter(**conditions) def list_public_configs(self, query: dict) -> Tuple[QuerySet, int]: diff --git a/src/spaceone/config/manager/shared_config_manager.py b/src/spaceone/config/manager/shared_config_manager.py new file mode 100644 index 0000000..691297b --- /dev/null +++ b/src/spaceone/config/manager/shared_config_manager.py @@ -0,0 +1,69 @@ +import logging +from typing import Tuple, List +from mongoengine import QuerySet + +from spaceone.core.manager import BaseManager + +from spaceone.config.model.shared_config.database import SharedConfig + +_LOGGER = logging.getLogger(__name__) + + +class SharedConfigManager(BaseManager): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.shared_config_model = SharedConfig + + def create_shared_config(self, params: dict) -> SharedConfig: + def _rollback(vo: SharedConfig): + _LOGGER.info( + f"[create_shared_config._rollback] " f"Delete shared config : {vo.name}" + ) + vo.delete() + + shared_config_vo: SharedConfig = self.shared_config_model.create(params) + self.transaction.add_rollback(_rollback, shared_config_vo) + + return shared_config_vo + + def update_shared_config_by_vo( + self, params: dict, shared_config_vo: SharedConfig + ) -> SharedConfig: + def _rollback(old_data: dict): + _LOGGER.info( + f'[update_shared_config_by_vo._rollback] Revert Data : {old_data["name"]}' + ) + shared_config_vo.update(old_data) + + self.transaction.add_rollback(_rollback, shared_config_vo.to_dict()) + + return shared_config_vo.update(params) + + def delete_shared_config_by_vo(self, shared_config_vo: SharedConfig) -> None: + shared_config_vo.delete() + + def get_shared_config( + self, + name: str, + domain_id: str, + workspace_id: str = None, + user_projects: List[str] = None + ) -> SharedConfig: + conditions = {"name": name, "domain_id": domain_id} + + if workspace_id: + conditions["workspace_id"] = workspace_id + + if user_projects: + conditions["project_id"] = user_projects + + return self.shared_config_model.get(**conditions) + + def filter_shared_configs(self, **conditions) -> QuerySet: + return self.shared_config_model.filter(**conditions) + + def list_shared_configs(self, query: dict) -> Tuple[QuerySet, int]: + return self.shared_config_model.query(**query) + + def stat_shared_configs(self, query: dict) -> dict: + return self.shared_config_model.stat(**query) diff --git a/src/spaceone/config/manager/user_config_manager.py b/src/spaceone/config/manager/user_config_manager.py index a8ad3a0..4eda409 100644 --- a/src/spaceone/config/manager/user_config_manager.py +++ b/src/spaceone/config/manager/user_config_manager.py @@ -1,8 +1,10 @@ import logging +from typing import Tuple +from mongoengine import QuerySet from spaceone.core.manager import BaseManager -from spaceone.config.model.user_config_model import UserConfig +from spaceone.config.model.user_config.database import UserConfig _LOGGER = logging.getLogger(__name__) @@ -10,12 +12,12 @@ class UserConfigManager(BaseManager): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.user_config_model: UserConfig = self.locator.get_model("UserConfig") + self.user_config_model = UserConfig def create_user_config(self, params: dict) -> UserConfig: def _rollback(vo: UserConfig): _LOGGER.info( - f"[create_user_config._rollback] " f"Delete config map : {vo.name}" + f"[create_user_config._rollback] " f"Delete user config : {vo.name}" ) vo.delete() @@ -24,12 +26,6 @@ def _rollback(vo: UserConfig): return user_config_vo - def update_user_config(self, params: dict) -> UserConfig: - user_config_vo: UserConfig = self.get_user_config( - params["name"], params["user_id"], params["domain_id"] - ) - return self.update_user_config_by_vo(params, user_config_vo) - def update_user_config_by_vo( self, params: dict, user_config_vo: UserConfig ) -> UserConfig: @@ -43,20 +39,19 @@ def _rollback(old_data: dict): return user_config_vo.update(params) - def delete_user_config(self, name: str, user_id: str, domain_id: str) -> None: - user_config_vo: UserConfig = self.get_user_config(name, user_id, domain_id) + def delete_user_config_by_vo(self, user_config_vo: UserConfig) -> None: user_config_vo.delete() - def get_user_config(self, name: str, user_id: str, domain_id: str) -> UserConfig: + def get_user_config(self, name: str, domain_id: str, user_id: str) -> UserConfig: return self.user_config_model.get( - name=name, user_id=user_id, domain_id=domain_id + name=name, domain_id=domain_id, user_id=user_id ) - def filter_user_configs(self, **conditions: dict): + def filter_user_configs(self, **conditions) -> QuerySet: return self.user_config_model.filter(**conditions) - def list_user_configs(self, query: dict) -> dict: + def list_user_configs(self, query: dict) -> Tuple[QuerySet, int]: return self.user_config_model.query(**query) - def state_user_configs(self, query: dict) -> dict: + def stat_user_configs(self, query: dict) -> dict: return self.user_config_model.stat(**query) diff --git a/src/spaceone/config/model/__init__.py b/src/spaceone/config/model/__init__.py index b5548c7..e69de29 100644 --- a/src/spaceone/config/model/__init__.py +++ b/src/spaceone/config/model/__init__.py @@ -1,3 +0,0 @@ -from spaceone.config.model.user_config_model import UserConfig -from spaceone.config.model.domain_config_model import DomainConfig -from spaceone.config.model.public_config.database import PublicConfig diff --git a/src/spaceone/config/lib/__init__.py b/src/spaceone/config/model/domain_config/__init__.py similarity index 100% rename from src/spaceone/config/lib/__init__.py rename to src/spaceone/config/model/domain_config/__init__.py diff --git a/src/spaceone/config/model/domain_config_model.py b/src/spaceone/config/model/domain_config/database.py similarity index 67% rename from src/spaceone/config/model/domain_config_model.py rename to src/spaceone/config/model/domain_config/database.py index efd2e18..b9a3a3c 100644 --- a/src/spaceone/config/model/domain_config_model.py +++ b/src/spaceone/config/model/domain_config/database.py @@ -3,21 +3,16 @@ from spaceone.core.model.mongo_model import MongoModel -class DomainConfigTag(EmbeddedDocument): - key = StringField(max_length=255) - value = StringField(max_length=255) - - class DomainConfig(MongoModel): name = StringField(max_length=255, unique_with="domain_id") - data = DictField() - tags = DictField() + data = DictField(default=None) + tags = DictField(default=None) domain_id = StringField(max_length=40) created_at = DateTimeField(auto_now_add=True) updated_at = DateTimeField(auto_now=True) meta = { - "updatable_fields": ["name", "data", "tags", "updated_at"], + "updatable_fields": ["name", "data", "tags"], "minimal_fields": ["name"], "ordering": ["name"], "indexes": [ diff --git a/src/spaceone/config/model/domain_config/request.py b/src/spaceone/config/model/domain_config/request.py new file mode 100644 index 0000000..2d2721e --- /dev/null +++ b/src/spaceone/config/model/domain_config/request.py @@ -0,0 +1,48 @@ +from typing import Union +from pydantic import BaseModel + +__all__ = [ + "DomainConfigCreateRequest", + "DomainConfigUpdateRequest", + "DomainConfigSetRequest", + "DomainConfigDeleteRequest", + "DomainConfigGetRequest", + "DomainConfigSearchQueryRequest", +] + + +class DomainConfigCreateRequest(BaseModel): + name: str + data: dict + tags: Union[dict, None] = None + domain_id: str + + +class DomainConfigUpdateRequest(BaseModel): + name: str + data: Union[dict, None] = None + tags: Union[dict, None] = None + domain_id: str + + +class DomainConfigSetRequest(BaseModel): + name: str + data: dict + tags: Union[dict, None] = None + domain_id: str + + +class DomainConfigDeleteRequest(BaseModel): + name: str + domain_id: str + + +class DomainConfigGetRequest(BaseModel): + name: str + domain_id: str + + +class DomainConfigSearchQueryRequest(BaseModel): + query: Union[dict, None] = None + name: Union[str, None] = None + domain_id: str diff --git a/src/spaceone/config/model/domain_config/response.py b/src/spaceone/config/model/domain_config/response.py new file mode 100644 index 0000000..d74ffa5 --- /dev/null +++ b/src/spaceone/config/model/domain_config/response.py @@ -0,0 +1,27 @@ +from datetime import datetime +from typing import Union, List +from pydantic import BaseModel + +from spaceone.core import utils + +__all__ = ["DomainConfigResponse", "DomainConfigsResponse"] + + +class DomainConfigResponse(BaseModel): + name: Union[str, None] = None + data: Union[dict, None] = None + tags: Union[dict, None] = None + domain_id: Union[str, None] = None + created_at: Union[datetime, None] = None + updated_at: Union[datetime, None] = None + + def dict(self, *args, **kwargs): + data = super().dict(*args, **kwargs) + data["created_at"] = utils.datetime_to_iso8601(data["created_at"]) + data["updated_at"] = utils.datetime_to_iso8601(data.get("updated_at")) + return data + + +class DomainConfigsResponse(BaseModel): + results: List[DomainConfigResponse] + total_count: int diff --git a/src/spaceone/config/model/public_config/database.py b/src/spaceone/config/model/public_config/database.py index 1ddc9c4..e7fd4d5 100644 --- a/src/spaceone/config/model/public_config/database.py +++ b/src/spaceone/config/model/public_config/database.py @@ -4,24 +4,19 @@ class PublicConfig(MongoModel): - name = StringField(max_length=255, unique_with=["domain_id", "workspace_id", "project_id"]) - data = DictField() - tags = DictField() - resource_group = StringField(max_length=40, choices=("DOMAIN", "WORKSPACE", "PROJECT")) - project_id = StringField(max_length=40) - workspace_id = StringField(max_length=40) + name = StringField(max_length=255, unique_with="domain_id") + data = DictField(default=None) + tags = DictField(default=None) domain_id = StringField(max_length=40) created_at = DateTimeField(auto_now_add=True) updated_at = DateTimeField(auto_now=True) meta = { - "updatable_fields": ["name", "data", "tags", "updated_at"], - "minimal_fields": ["name", "resource_group"], - "change_query_keys": {"user_projects": "project_id"}, + "updatable_fields": ["name", "data", "tags"], + "minimal_fields": ["name"], "ordering": ["name"], "indexes": [ "name", "domain_id", - "workspace_id", ], } diff --git a/src/spaceone/config/model/public_config/request.py b/src/spaceone/config/model/public_config/request.py index 5ee5985..fbce8a2 100644 --- a/src/spaceone/config/model/public_config/request.py +++ b/src/spaceone/config/model/public_config/request.py @@ -1,4 +1,4 @@ -from typing import Union, Literal +from typing import Union from pydantic import BaseModel __all__ = [ @@ -7,22 +7,14 @@ "PublicConfigSetRequest", "PublicConfigDeleteRequest", "PublicConfigGetRequest", - "PublicConfigGetAccessibleConfigRequest", "PublicConfigSearchQueryRequest", - "PublicConfigQueryRequest", ] -ResourceGroup = Literal["DOMAIN", "WORKSPACE", "PROJECT"] - class PublicConfigCreateRequest(BaseModel): name: str data: dict tags: Union[dict, None] = None - resource_group: ResourceGroup - user_projects: Union[list, None] = None - project_id: Union[list, str, None] = None - workspace_id: Union[list, str, None] = None domain_id: str @@ -30,54 +22,27 @@ class PublicConfigUpdateRequest(BaseModel): name: str data: Union[dict, None] = None tags: Union[dict, None] = None - user_projects: Union[list, None] = None - project_id: Union[str, None] = None - workspace_id: Union[str, None] = None domain_id: str class PublicConfigSetRequest(BaseModel): name: str - data: str + data: dict tags: Union[dict, None] = None - user_projects: Union[list, None] = None - project_id: Union[str, None] = None - workspace_id: Union[str, None] = None domain_id: str class PublicConfigDeleteRequest(BaseModel): name: str - user_projects: Union[list, None] = None - project_id: Union[str, None] = None - workspace_id: Union[str, None] = None domain_id: str class PublicConfigGetRequest(BaseModel): name: str - user_projects: Union[list, None] = None - project_id: Union[list, str, None] = None - workspace_id: Union[list, str, None] = None - domain_id: str - - -class PublicConfigGetAccessibleConfigRequest(BaseModel): - query: Union[dict, None] = None - name: Union[str, None] = None domain_id: str class PublicConfigSearchQueryRequest(BaseModel): query: Union[dict, None] = None name: Union[str, None] = None - user_project: Union[list, None] = None - project_id: Union[list, str, None] = None - workspace_id: Union[list, str, None] = None - domain_id: str - - -class PublicConfigQueryRequest(BaseModel): - query: dict - workspace_id: str domain_id: str diff --git a/src/spaceone/config/model/public_config/response.py b/src/spaceone/config/model/public_config/response.py index feed02d..441f90f 100644 --- a/src/spaceone/config/model/public_config/response.py +++ b/src/spaceone/config/model/public_config/response.py @@ -4,8 +4,6 @@ from spaceone.core import utils -from spaceone.config.model.public_config.request import ResourceGroup - __all__ = ["PublicConfigResponse", "PublicConfigsResponse"] @@ -13,9 +11,6 @@ class PublicConfigResponse(BaseModel): name: Union[str, None] = None data: Union[dict, None] = None tags: Union[dict, None] = None - resource_group: Union[ResourceGroup, None] = None - project_id: Union[str, None] = None - workspace_id: Union[str, None] = None domain_id: Union[str, None] = None created_at: Union[datetime, None] = None updated_at: Union[datetime, None] = None @@ -28,5 +23,5 @@ def dict(self, *args, **kwargs): class PublicConfigsResponse(BaseModel): - results: List[PublicConfigResponse] = [] + results: List[PublicConfigResponse] total_count: int diff --git a/src/spaceone/config/model/shared_config/__init__.py b/src/spaceone/config/model/shared_config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/spaceone/config/model/shared_config/database.py b/src/spaceone/config/model/shared_config/database.py new file mode 100644 index 0000000..ca0737f --- /dev/null +++ b/src/spaceone/config/model/shared_config/database.py @@ -0,0 +1,28 @@ +from mongoengine import * + +from spaceone.core.model.mongo_model import MongoModel + + +class SharedConfig(MongoModel): + name = StringField(max_length=255, unique_with=["domain_id", "workspace_id", "project_id"]) + data = DictField(default=None) + tags = DictField(default=None) + resource_group = StringField(max_length=40, choices=("DOMAIN", "WORKSPACE", "PROJECT")) + project_id = StringField(max_length=40) + workspace_id = StringField(max_length=40) + domain_id = StringField(max_length=40) + created_at = DateTimeField(auto_now_add=True) + updated_at = DateTimeField(auto_now=True) + + meta = { + "updatable_fields": ["name", "data", "tags"], + "minimal_fields": ["name", "resource_group", "domain_id", "workspace_id", "project_id"], + "change_query_keys": {"user_projects": "project_id"}, + "ordering": ["name"], + "indexes": [ + "name", + "domain_id", + "workspace_id", + "project_id" + ], + } diff --git a/src/spaceone/config/model/shared_config/request.py b/src/spaceone/config/model/shared_config/request.py new file mode 100644 index 0000000..8859cce --- /dev/null +++ b/src/spaceone/config/model/shared_config/request.py @@ -0,0 +1,55 @@ +from typing import Union, Literal +from pydantic import BaseModel + +__all__ = [ + "SharedConfigCreateRequest", + "SharedConfigUpdateRequest", + "SharedConfigDeleteRequest", + "SharedConfigGetRequest", + "SharedConfigSearchQueryRequest", + "ResourceGroup", +] + +ResourceGroup = Literal["DOMAIN", "WORKSPACE", "PROJECT"] + + +class SharedConfigCreateRequest(BaseModel): + name: str + data: dict + tags: Union[dict, None] = None + resource_group: ResourceGroup + domain_id: str + workspace_id: Union[list, str, None] = None + project_id: Union[list, str, None] = None + + +class SharedConfigUpdateRequest(BaseModel): + name: str + data: Union[dict, None] = None + tags: Union[dict, None] = None + domain_id: str + workspace_id: Union[str, None] = None + user_projects: Union[list, None] = None + + +class SharedConfigDeleteRequest(BaseModel): + name: str + domain_id: str + workspace_id: Union[str, None] = None + user_projects: Union[list, None] = None + + +class SharedConfigGetRequest(BaseModel): + name: str + domain_id: str + workspace_id: Union[list, str, None] = None + user_projects: Union[list, None] = None + + +class SharedConfigSearchQueryRequest(BaseModel): + query: Union[dict, None] = None + name: Union[str, None] = None + domain_id: str + workspace_id: Union[list, str, None] = None + user_project: Union[list, None] = None + project_id: Union[str, None] = None diff --git a/src/spaceone/config/model/shared_config/response.py b/src/spaceone/config/model/shared_config/response.py new file mode 100644 index 0000000..9d0e282 --- /dev/null +++ b/src/spaceone/config/model/shared_config/response.py @@ -0,0 +1,32 @@ +from datetime import datetime +from typing import Union, List +from pydantic import BaseModel + +from spaceone.core import utils + +from spaceone.config.model.shared_config.request import ResourceGroup + +__all__ = ["SharedConfigResponse", "SharedConfigsResponse"] + + +class SharedConfigResponse(BaseModel): + name: Union[str, None] = None + data: Union[dict, None] = None + tags: Union[dict, None] = None + resource_group: Union[ResourceGroup, None] = None + domain_id: Union[str, None] = None + workspace_id: Union[str, None] = None + project_id: Union[str, None] = None + created_at: Union[datetime, None] = None + updated_at: Union[datetime, None] = None + + def dict(self, *args, **kwargs): + data = super().dict(*args, **kwargs) + data["created_at"] = utils.datetime_to_iso8601(data["created_at"]) + data["updated_at"] = utils.datetime_to_iso8601(data.get("updated_at")) + return data + + +class SharedConfigsResponse(BaseModel): + results: List[SharedConfigResponse] + total_count: int diff --git a/src/spaceone/config/model/user_config/__init__.py b/src/spaceone/config/model/user_config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/spaceone/config/model/user_config_model.py b/src/spaceone/config/model/user_config/database.py similarity index 81% rename from src/spaceone/config/model/user_config_model.py rename to src/spaceone/config/model/user_config/database.py index fb446e1..c7873ec 100644 --- a/src/spaceone/config/model/user_config_model.py +++ b/src/spaceone/config/model/user_config/database.py @@ -5,20 +5,20 @@ class UserConfig(MongoModel): name = StringField(max_length=255, unique_with=["user_id", "domain_id"]) - data = DictField() - tags = DictField() - user_id = StringField(max_length=40) + data = DictField(default=None) + tags = DictField(default=None) domain_id = StringField(max_length=40) + user_id = StringField(max_length=40) created_at = DateTimeField(auto_now_add=True) updated_at = DateTimeField(auto_now=True) meta = { - "updatable_fields": ["name", "data", "tags", "updated_at"], + "updatable_fields": ["name", "data", "tags"], "minimal_fields": ["name"], "ordering": ["name"], "indexes": [ "name", - "user_id", "domain_id", + "user_id", ], } diff --git a/src/spaceone/config/model/user_config/request.py b/src/spaceone/config/model/user_config/request.py new file mode 100644 index 0000000..f9d3457 --- /dev/null +++ b/src/spaceone/config/model/user_config/request.py @@ -0,0 +1,54 @@ +from typing import Union +from pydantic import BaseModel + +__all__ = [ + "UserConfigCreateRequest", + "UserConfigUpdateRequest", + "UserConfigSetRequest", + "UserConfigDeleteRequest", + "UserConfigGetRequest", + "UserConfigSearchQueryRequest", +] + + +class UserConfigCreateRequest(BaseModel): + name: str + data: dict + tags: Union[dict, None] = None + domain_id: str + user_id: str + + +class UserConfigUpdateRequest(BaseModel): + name: str + data: Union[dict, None] = None + tags: Union[dict, None] = None + domain_id: str + user_id: str + + +class UserConfigSetRequest(BaseModel): + name: str + data: dict + tags: Union[dict, None] = None + domain_id: str + user_id: str + + +class UserConfigDeleteRequest(BaseModel): + name: str + domain_id: str + user_id: str + + +class UserConfigGetRequest(BaseModel): + name: str + domain_id: str + user_id: str + + +class UserConfigSearchQueryRequest(BaseModel): + query: Union[dict, None] = None + name: Union[str, None] = None + domain_id: str + user_id: str diff --git a/src/spaceone/config/model/user_config/response.py b/src/spaceone/config/model/user_config/response.py new file mode 100644 index 0000000..aadb776 --- /dev/null +++ b/src/spaceone/config/model/user_config/response.py @@ -0,0 +1,28 @@ +from datetime import datetime +from typing import Union, List +from pydantic import BaseModel + +from spaceone.core import utils + +__all__ = ["UserConfigResponse", "UserConfigsResponse"] + + +class UserConfigResponse(BaseModel): + name: Union[str, None] = None + data: Union[dict, None] = None + tags: Union[dict, None] = None + domain_id: Union[str, None] = None + user_id: Union[str, None] = None + created_at: Union[datetime, None] = None + updated_at: Union[datetime, None] = None + + def dict(self, *args, **kwargs): + data = super().dict(*args, **kwargs) + data["created_at"] = utils.datetime_to_iso8601(data["created_at"]) + data["updated_at"] = utils.datetime_to_iso8601(data.get("updated_at")) + return data + + +class UserConfigsResponse(BaseModel): + results: List[UserConfigResponse] + total_count: int diff --git a/src/spaceone/config/service/domain_config_service.py b/src/spaceone/config/service/domain_config_service.py index 4e404a7..5304e04 100644 --- a/src/spaceone/config/service/domain_config_service.py +++ b/src/spaceone/config/service/domain_config_service.py @@ -1,9 +1,12 @@ import logging +from typing import Union from spaceone.core.service import * from spaceone.config.manager.domain_config_manager import DomainConfigManager -from spaceone.config.model import DomainConfig +from spaceone.config.model.domain_config.request import * +from spaceone.config.model.domain_config.response import * +from spaceone.config.model.domain_config.database import DomainConfig _LOGGER = logging.getLogger(__name__) @@ -15,150 +18,148 @@ class DomainConfigService(BaseService): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.domain_config_mgr: DomainConfigManager = self.locator.get_manager( - DomainConfigManager - ) + self.domain_config_mgr = DomainConfigManager() @transaction(permission="config:DomainConfig.write", role_types=["DOMAIN_ADMIN"]) - @check_required(["name", "data", "domain_id"]) - def create(self, params: dict) -> DomainConfig: + @convert_model + def create(self, params: DomainConfigCreateRequest) -> Union[DomainConfigResponse, dict]: """Create domain config Args: - params (dict): { - 'name': 'str', # required - 'data': 'dict', # required + params (DomainConfigCreateRequest): { + 'name': 'str', # required + 'data': 'dict', # required 'tags': 'dict', - 'domain_id': 'str' # injected from auth + 'domain_id': 'str', # injected from auth (required) } Returns: - domain_config_vo (object) + DomainConfigResponse: """ - return self.domain_config_mgr.create_domain_config(params) + domain_config_vo = self.domain_config_mgr.create_domain_config(params.dict()) + return DomainConfigResponse(**domain_config_vo.to_dict()) @transaction(permission="config:DomainConfig.write", role_types=["DOMAIN_ADMIN"]) - @check_required(["name", "domain_id"]) - def update(self, params: dict) -> DomainConfig: + @convert_model + def update(self, params: DomainConfigUpdateRequest) -> Union[DomainConfigResponse, dict]: """Update domain config Args: - params (dict): { - 'name': 'str', # required + params (DomainConfigUpdateRequest): { + 'name': 'str', # required 'data': 'dict', 'tags': 'dict', - 'domain_id': 'str' # injected from auth + 'domain_id': 'str', # injected from auth (required) } Returns: - domain_config_vo (object) + DomainConfigResponse: """ - return self.domain_config_mgr.update_domain_config(params) + domain_config_vo: DomainConfig = self.domain_config_mgr.get_domain_config( + params.name, params.domain_id + ) + + domain_config_vo = self.domain_config_mgr.update_domain_config_by_vo( + params.dict(exclude_unset=True), domain_config_vo + ) + + return DomainConfigResponse(**domain_config_vo.to_dict()) @transaction(permission="config:DomainConfig.write", role_types=["DOMAIN_ADMIN"]) - @check_required(["name", "data", "domain_id"]) - def set(self, params): + @convert_model + def set(self, params: DomainConfigSetRequest) -> Union[DomainConfigResponse, dict]: """Set domain config (create or update) Args: params (dict): { - 'name': 'str', # required - 'data': 'dict', # required + 'name': 'str', # required + 'data': 'dict', # required 'tags': 'dict', - 'domain_id': 'str' # injected from auth + 'domain_id': 'str', # injected from auth (required) } Returns: - domain_config_vo (object) + DomainConfigResponse: """ domain_config_vos = self.domain_config_mgr.filter_domain_configs( - name=params["name"], domain_id=params["domain_id"] + name=params.name, domain_id=params.domain_id ) if domain_config_vos.count() == 0: - return self.domain_config_mgr.create_domain_config(params) + domain_config_vo = self.domain_config_mgr.create_domain_config(params.dict()) else: - return self.domain_config_mgr.update_domain_config_by_vo( - params, domain_config_vos[0] + domain_config_vo = self.domain_config_mgr.update_domain_config_by_vo( + params.dict(exclude_unset=True), domain_config_vos[0] ) + return DomainConfigResponse(**domain_config_vo.to_dict()) + @transaction(permission="config:DomainConfig.write", role_types=["DOMAIN_ADMIN"]) - @check_required(["name", "domain_id"]) - def delete(self, params: dict) -> None: + @convert_model + def delete(self, params: DomainConfigDeleteRequest) -> None: """Delete domain config Args: params (dict): { - 'name': 'str', # required - 'domain_id': 'str' # injected from auth + 'name': 'str', # required + 'domain_id': 'str' # injected from auth (required) } Returns: None """ - self.domain_config_mgr.delete_domain_config(params["name"], params["domain_id"]) + domain_config_vo: DomainConfig = self.domain_config_mgr.get_domain_config( + params.name, params.domain_id + ) + + self.domain_config_mgr.delete_domain_config_by_vo(domain_config_vo) @transaction(permission="config:DomainConfig.read", role_types=["DOMAIN_ADMIN"]) - @check_required(["name", "domain_id"]) - def get(self, params: dict) -> DomainConfig: + @convert_model + def get(self, params: DomainConfigGetRequest) -> Union[DomainConfigResponse, dict]: """Get domain config Args: params (dict): { - 'name': 'str', # required - 'domain_id': 'str' # injected from auth + 'name': 'str', # required + 'domain_id': 'str', # injected from auth (required) } Returns: - domain_config_vo (object) + DomainConfigResponse: """ - return self.domain_config_mgr.get_domain_config( - params["name"], params["domain_id"] + domain_config_vo: DomainConfig = self.domain_config_mgr.get_domain_config( + params.name, params.domain_id ) + return DomainConfigResponse(**domain_config_vo.to_dict()) + @transaction(permission="config:DomainConfig.read", role_types=["DOMAIN_ADMIN"]) - @check_required(["domain_id"]) @append_query_filter(["name", "domain_id"]) @append_keyword_filter(["name"]) - def list(self, params: dict) -> dict: + @convert_model + def list(self, params: DomainConfigSearchQueryRequest) -> Union[DomainConfigsResponse, dict]: """List domain configs Args: params (dict): { - 'query': 'dict (spaceone.api.core.v1.Query)' + 'query': 'dict (spaceone.api.core.v1.Query)', 'name': 'str', - 'domain_id': 'str', # injected from auth + 'domain_id': 'str' # injected from auth (required) } Returns: - domain_config_vos (objects) - total_count (int) - """ - - query = params.get("query", {}) - return self.domain_config_mgr.list_domain_configs(query) - - @transaction(permission="config:DomainConfig.read", role_types=["DOMAIN_ADMIN"]) - @check_required(["query", "domain_id"]) - @append_query_filter(["domain_id"]) - @append_keyword_filter(["name"]) - def stat(self, params: dict) -> dict: - """ - Args: - params (dict): { - 'query': 'dict (spaceone.api.core.v1.StatisticsQuery)' # required - 'domain_id': 'str', # required - } - - Returns: - values (list) : 'list of statistics data' - + DomainConfigsResponse: """ - query = params.get("query", {}) - return self.domain_config_mgr.state_domain_configs(query) + query = params.query or {} + domain_configs_vos, total_count = self.domain_config_mgr.list_domain_configs(query) + domain_configs_info = [ + domain_config_vo.to_dict() for domain_config_vo in domain_configs_vos + ] + return DomainConfigsResponse(results=domain_configs_info, total_count=total_count) diff --git a/src/spaceone/config/service/public_config_service.py b/src/spaceone/config/service/public_config_service.py index bf104f3..bc95bec 100644 --- a/src/spaceone/config/service/public_config_service.py +++ b/src/spaceone/config/service/public_config_service.py @@ -1,14 +1,12 @@ import logging from typing import Union -from spaceone.core.error import ERROR_REQUIRED_PARAMETER from spaceone.core.service import * -from spaceone.config.manager.identity_manager import IdentityManager from spaceone.config.manager.public_config_manager import PublicConfigManager -from spaceone.config.model.public_config.database import PublicConfig -from spaceone.config.model.public_config.response import * from spaceone.config.model.public_config.request import * +from spaceone.config.model.public_config.response import * +from spaceone.config.model.public_config.database import PublicConfig _LOGGER = logging.getLogger(__name__) @@ -22,281 +20,146 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.public_config_mgr = PublicConfigManager() - @transaction(permission="config:PublicConfig.write", - role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) + @transaction(permission="config:PublicConfig.write", role_types=["DOMAIN_ADMIN"]) @convert_model def create(self, params: PublicConfigCreateRequest) -> Union[PublicConfigResponse, dict]: - """Create workspace config + """Create public config Args: - params (dict): { - 'name': 'str', # required - 'data': 'dict', # required + params (PublicConfigCreateRequest): { + 'name': 'str', # required + 'data': 'dict', # required 'tags': 'dict', - 'resource_group' : 'str', # required - 'users_project_id': 'str', # injected from auth - 'project_id': 'str' - 'workspace_id': 'str' # injected from auth - 'domain_id': 'str' # injected from auth + 'domain_id': 'str', # injected from auth (required) } Returns: - public_config_vo (object) + PublicConfigResponse: """ - public_config_vo = self.create_public_config(params.dict()) - + public_config_vo = self.public_config_mgr.create_public_config(params.dict()) return PublicConfigResponse(**public_config_vo.to_dict()) - @transaction(permission="config:PublicConfig.write", - role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) + @transaction(permission="config:PublicConfig.write", role_types=["DOMAIN_ADMIN"]) @convert_model def update(self, params: PublicConfigUpdateRequest) -> Union[PublicConfigResponse, dict]: """Update public config Args: - params (dict): { - 'name': 'str', # required + params (PublicConfigUpdateRequest): { + 'name': 'str', # required 'data': 'dict', 'tags': 'dict', - "user_projects": 'list', # injected from auth - 'project_id': 'str', - 'workspace_id': 'str' # injected from auth - 'domain_id': 'str' # injected from auth + 'domain_id': 'str', # injected from auth (required) } Returns: - domain_config_vo (object) + PublicConfigResponse: """ - domain_id = params.domain_id - workspace_id = params.workspace_id - project_id = params.user_projects or params.project_id + public_config_vo: PublicConfig = self.public_config_mgr.get_public_config( + params.name, params.domain_id + ) - public_config_vo = self.public_config_mgr.get_public_config(params.name, domain_id, workspace_id, project_id) - - public_config_vo = self.public_config_mgr.update_public_config_by_vo(params.dict(exclude_unset=True), - public_config_vo) + public_config_vo = self.public_config_mgr.update_public_config_by_vo( + params.dict(exclude_unset=True), public_config_vo + ) return PublicConfigResponse(**public_config_vo.to_dict()) - # @transaction(permission="config:PublicConfig.write", - # role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) - # @convert_model - # def set(self, params: PublicConfigSetRequest) -> Union[PublicConfigResponse, dict]: - # """Set domain config (create or update) - # - # Args: - # params (dict): { - # 'name': 'str', # required - # 'data': 'dict', # required - # 'tags': 'dict', - # 'workspace_id': 'str' # injected from auth - # 'domain_id': 'str' # injected from auth - # } - # - # Returns: - # public_config_vo (object) - # """ - # # need resource_group - # domain_id = params.domain_id - # workspace_id = params.workspace_id - # project_id = params.user_projects or params.project_id - # - # public_config_vos = self.public_config_mgr.filter_public_configs( - # name=params.name, domain_id=domain_id, workspace_id=workspace_id, project_id=project_id - # ) - # - # if public_config_vos.count() == 0: - # public_config_vo = self.create_public_config(params.dict()) - # else: - # public_config_vo = self.public_config_mgr.update_public_config_by_vo( - # params.dict(exclude_unset=True), public_config_vos[0] - # ) - # return PublicConfigResponse(**public_config_vo.to_dict()) - - @transaction(permission="config:PublicConfig.read", - role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) - @change_value_by_rule("APPEND", "workspace_id", "*") - @change_value_by_rule("APPEND", "project_id", "*") + @transaction(permission="config:PublicConfig.write", role_types=["DOMAIN_ADMIN"]) @convert_model - def get(self, params: PublicConfigGetRequest) -> Union[PublicConfigResponse, dict]: - """Get workspace config + def set(self, params: PublicConfigSetRequest) -> Union[PublicConfigResponse, dict]: + """Set public config (create or update) Args: params (dict): { - 'name': 'str', # required - 'user_projects': 'list', - 'project_id': 'str', - 'workspace_id': 'str' # injected from auth - 'domain_id': 'str' # injected from auth + 'name': 'str', # required + 'data': 'dict', # required + 'tags': 'dict', + 'domain_id': 'str', # injected from auth (required) } Returns: - public_config_vo (object) + PublicConfigResponse: """ - public_config_vo = self.public_config_mgr.get_public_config(params.name, params.domain_id, params.workspace_id, - params.user_projects) + public_config_vos = self.public_config_mgr.filter_public_configs( + name=params.name, domain_id=params.domain_id + ) + + if public_config_vos.count() == 0: + public_config_vo = self.public_config_mgr.create_public_config(params.dict()) + else: + public_config_vo = self.public_config_mgr.update_public_config_by_vo( + params.dict(exclude_unset=True), public_config_vos[0] + ) return PublicConfigResponse(**public_config_vo.to_dict()) - @transaction(permission="config:PublicConfig.read", role_types=["USER"]) + @transaction(permission="config:PublicConfig.write", role_types=["DOMAIN_ADMIN"]) @convert_model - def get_accessible_configs(self, params: PublicConfigGetAccessibleConfigRequest) -> Union[ - PublicConfigsResponse, dict]: - """Get accessible workspace config + def delete(self, params: PublicConfigDeleteRequest) -> None: + """Delete public config Args: params (dict): { - "query": "dict", - 'name': 'str', # required - - 'domain_id': 'str' # injected from auth + 'name': 'str', # required + 'domain_id': 'str' # injected from auth (required) } Returns: - public_config_vo (object) + None """ - query = params.query or {} - if "filter" not in query: - query["filter"] = [] - - query_filter = [ - { - "key": "domain_id", - "value": params.domain_id, - "operator": "eq" - }, - { - "key": "resource_group", - "value": "DOMAIN", - "operator": "eq" - } - ] - - query["filter"].extend(query_filter) + public_config_vo: PublicConfig = self.public_config_mgr.get_public_config( + params.name, params.domain_id + ) - if params.name: - query["filter"].append({ - "key": "name", - "value": params.name, - "operator": "eq" - }) - - _LOGGER.debug(f'[get_accessible_configs] query: {query}') - - public_config_vos, total_count = self.public_config_mgr.list_public_configs(query) - - public_configs_info = [public_config_vo.to_dict() for public_config_vo in public_config_vos] - - return PublicConfigsResponse(results=public_configs_info, total_count=total_count) + self.public_config_mgr.delete_public_config_by_vo(public_config_vo) - @transaction(permission="config:PublicConfig.write", - role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) + @transaction(exclude=["authentication", "authorization", "mutation"]) @convert_model - def delete(self, params: PublicConfigDeleteRequest) -> None: - """Delete workspace config + def get(self, params: PublicConfigGetRequest) -> Union[PublicConfigResponse, dict]: + """Get public config Args: params (dict): { - 'name': 'str', # required - 'user_projects': 'list', - 'project_id': 'str', - 'workspace_id': 'str' # injected from auth - 'domain_id': 'str' # injected from auth + 'name': 'str', # required + 'domain_id': 'str', # injected from auth (required) } Returns: - None + PublicConfigResponse: """ - workspace_id = params.workspace_id - project_id = params.project_id or params.user_projects + public_config_vo: PublicConfig = self.public_config_mgr.get_public_config( + params.name, params.domain_id + ) - public_config_vo = self.public_config_mgr.get_public_config(params.name, params.domain_id, workspace_id, - project_id) - self.public_config_mgr.delete_public_config_by_vo(public_config_vo) + return PublicConfigResponse(**public_config_vo.to_dict()) - @transaction(permission="config:PublicConfig.read", - role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) + @transaction(exclude=["authentication", "authorization", "mutation"]) + @append_query_filter(["name", "domain_id"]) @append_keyword_filter(["name"]) - @change_value_by_rule("APPEND", "workspace_id", "*") - @change_value_by_rule("APPEND", "project_id", "*") - @append_query_filter(["name", "workspace_id", "users_project", "domain_id"]) @convert_model def list(self, params: PublicConfigSearchQueryRequest) -> Union[PublicConfigsResponse, dict]: - """List workspace configs + """List public configs Args: params (dict): { - 'query': 'dict (spaceone.api.core.v2.Query)' + 'query': 'dict (spaceone.api.core.v1.Query)', 'name': 'str', - 'user_projects': 'list', - 'workspace_id': 'str', # injected from auth - 'domain_id': 'str', # injected from auth + 'domain_id': 'str' # injected from auth (required) } Returns: - public_configs_vos (objects) - total_count (int) + PublicConfigsResponse: """ query = params.query or {} - public_config_vos, total_count = self.public_config_mgr.list_public_configs(query) - public_configs_info = [public_config_vo.to_dict() for public_config_vo in public_config_vos] + public_configs_vos, total_count = self.public_config_mgr.list_public_configs(query) + public_configs_info = [ + public_config_vo.to_dict() for public_config_vo in public_configs_vos + ] return PublicConfigsResponse(results=public_configs_info, total_count=total_count) - - @transaction(permission="config:PublicConfig.read", - role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) - @change_value_by_rule("APPEND", "workspace_id", "*") - @change_value_by_rule("APPEND", "project_id", "*") - @append_query_filter(["users_project", "workspace_id", "domain_id"]) - @append_keyword_filter(["name"]) - @convert_model - def stat(self, params: PublicConfigQueryRequest) -> dict: - """ Stat workspace configs - Args: - params (dict): { - 'query': 'dict (spaceone.api.core.v1.StatisticsQuery)' # required - 'workspace_id': 'str', # injected from auth - 'domain_id': 'str', # required - } - - Returns: - values (list) : 'list of statistics data' - - """ - - query = params.query or {} - return self.public_config_mgr.stat_public_configs(query) - - def create_public_config(self, params: dict) -> PublicConfig: - domain_id = params["domain_id"] - resource_group = params["resource_group"] - workspace_id = params.get("workspace_id") - project_id = params.get("project_id") - - identity_mgr: IdentityManager = self.locator.get_manager(IdentityManager) - - if resource_group == 'DOMAIN': - params["workspace_id"] = "*" - params["project_id"] = "*" - elif resource_group == 'WORKSPACE': - if params.get("workspace_id") is None: - raise ERROR_REQUIRED_PARAMETER(key='workspace_id') - - identity_mgr.check_workspace(workspace_id, domain_id) - params["project_id"] = "*" - else: - if workspace_id is None: - raise ERROR_REQUIRED_PARAMETER(key='workspace_id') - if project_id is None: - raise ERROR_REQUIRED_PARAMETER(key='project_id') - - identity_mgr.check_workspace(workspace_id, domain_id) - identity_mgr.get_project(project_id, domain_id) - - public_config_vo = self.public_config_mgr.create_public_config(params) - return public_config_vo diff --git a/src/spaceone/config/service/shared_config_service.py b/src/spaceone/config/service/shared_config_service.py new file mode 100644 index 0000000..903d7f6 --- /dev/null +++ b/src/spaceone/config/service/shared_config_service.py @@ -0,0 +1,185 @@ +import logging +from typing import Union + +from spaceone.core.error import ERROR_REQUIRED_PARAMETER +from spaceone.core.service import * + +from spaceone.config.manager.identity_manager import IdentityManager +from spaceone.config.manager.shared_config_manager import SharedConfigManager +from spaceone.config.model.shared_config.response import * +from spaceone.config.model.shared_config.request import * + +_LOGGER = logging.getLogger(__name__) + + +@authentication_handler +@authorization_handler +@mutation_handler +@event_handler +class SharedConfigService(BaseService): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.shared_config_mgr = SharedConfigManager() + self.identity_mgr = IdentityManager() + + @transaction(permission="config:SharedConfig.write", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) + @convert_model + def create(self, params: SharedConfigCreateRequest) -> Union[SharedConfigResponse, dict]: + """Create shared config + + Args: + params (SharedConfigCreateRequest): { + 'name': 'str', # required + 'data': 'dict', # required + 'tags': 'dict', + 'resource_group' : 'str', # required + 'domain_id': 'str', # injected from auth (required) + 'workspace_id': 'str' # injected from auth + 'users_project_id': 'str', # injected from auth + 'project_id': 'str' + } + + Returns: + SharedConfigResponse: + """ + + if params.resource_group == "PROJECT": + if params.project_id is None: + raise ERROR_REQUIRED_PARAMETER(key='project_id') + + project_info = self.identity_mgr.get_project(params.project_id, params.domain_id) + params.workspace_id = project_info["workspace_id"] + elif params.resource_group == "WORKSPACE": + if params.workspace_id is None: + raise ERROR_REQUIRED_PARAMETER(key='workspace_id') + + self.identity_mgr.check_workspace(params.workspace_id, params.domain_id) + params.project_id = "*" + else: + params.workspace_id = "*" + params.project_id = "*" + + shared_config_vo = self.shared_config_mgr.create_shared_config(params.dict()) + + return SharedConfigResponse(**shared_config_vo.to_dict()) + + @transaction(permission="config:SharedConfig.write", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) + @convert_model + def update(self, params: SharedConfigUpdateRequest) -> Union[SharedConfigResponse, dict]: + """Update shared config + + Args: + params (dict): { + 'name': 'str', # required + 'data': 'dict', + 'tags': 'dict', + 'domain_id': 'str', # injected from auth (required) + 'workspace_id': 'str' # injected from auth + "user_projects": 'list', # injected from auth + } + + Returns: + domain_config_vo (object) + """ + + domain_id = params.domain_id + workspace_id = params.workspace_id + user_projects = params.user_projects + + shared_config_vo = self.shared_config_mgr.get_shared_config( + params.name, domain_id, workspace_id, user_projects + ) + + shared_config_vo = self.shared_config_mgr.update_shared_config_by_vo( + params.dict(exclude_unset=True), shared_config_vo + ) + + return SharedConfigResponse(**shared_config_vo.to_dict()) + + @transaction(permission="config:SharedConfig.write", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) + @convert_model + def delete(self, params: SharedConfigDeleteRequest) -> None: + """Delete shared config + + Args: + params (dict): { + 'name': 'str', # required + 'domain_id': 'str', # injected from auth (required) + 'workspace_id': 'str', # injected from auth + 'user_projects': 'list', # injected from auth + } + + Returns: + None + """ + + domain_id = params.domain_id + workspace_id = params.workspace_id + user_projects = params.user_projects + + shared_config_vo = self.shared_config_mgr.get_shared_config( + params.name, domain_id, workspace_id, user_projects + ) + self.shared_config_mgr.delete_shared_config_by_vo(shared_config_vo) + + @transaction(permission="config:SharedConfig.read", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) + @change_value_by_rule("APPEND", "workspace_id", "*") + @change_value_by_rule("APPEND", "user_projects", "*") + @convert_model + def get(self, params: SharedConfigGetRequest) -> Union[SharedConfigResponse, dict]: + """Get shared config + + Args: + params (dict): { + 'name': 'str', # required + 'domain_id': 'str', # injected from auth (required) + 'workspace_id': 'str', # injected from auth + 'user_projects': 'list', # injected from auth + } + + Returns: + SharedConfigResponse: + """ + + domain_id = params.domain_id + workspace_id = params.workspace_id + user_projects = params.user_projects + + shared_config_vo = self.shared_config_mgr.get_shared_config( + params.name, domain_id, workspace_id, user_projects + ) + + return SharedConfigResponse(**shared_config_vo.to_dict()) + + @transaction(permission="config:SharedConfig.read", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) + @change_value_by_rule("APPEND", "workspace_id", "*") + @change_value_by_rule("APPEND", "user_projects", "*") + @append_query_filter(["name", "domain_id", "workspace_id", "users_project", "project_id"]) + @append_keyword_filter(["name"]) + @convert_model + def list(self, params: SharedConfigSearchQueryRequest) -> Union[SharedConfigsResponse, dict]: + """List shared configs + + Args: + params (dict): { + 'query': 'dict (spaceone.api.core.v2.Query)' + 'name': 'str', + 'domain_id': 'str', # injected from auth (required) + 'workspace_id': 'str', # injected from auth + 'user_projects': 'list', # injected from auth + 'project_id': 'str', + } + + Returns: + SharedConfigsResponse: + """ + + query = params.query or {} + shared_config_vos, total_count = self.shared_config_mgr.list_shared_configs(query) + shared_configs_info = [shared_config_vo.to_dict() for shared_config_vo in shared_config_vos] + return SharedConfigsResponse(results=shared_configs_info, total_count=total_count) diff --git a/src/spaceone/config/service/user_config_service.py b/src/spaceone/config/service/user_config_service.py index 8dc87ea..7edd06e 100644 --- a/src/spaceone/config/service/user_config_service.py +++ b/src/spaceone/config/service/user_config_service.py @@ -1,9 +1,12 @@ import logging +from typing import Union from spaceone.core.service import * from spaceone.config.manager.user_config_manager import UserConfigManager -from spaceone.config.model import UserConfig +from spaceone.config.model.user_config.request import * +from spaceone.config.model.user_config.response import * +from spaceone.config.model.user_config.database import UserConfig _LOGGER = logging.getLogger(__name__) @@ -15,161 +18,154 @@ class UserConfigService(BaseService): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.user_config_mgr: UserConfigManager = self.locator.get_manager( - "UserConfigManager" - ) + self.user_config_mgr = UserConfigManager() @transaction(permission="config:UserConfig.write", role_types=["USER"]) - @check_required(["name", "data", "user_id", "domain_id"]) - def create(self, params: dict) -> UserConfig: + @convert_model + def create(self, params: UserConfigCreateRequest) -> Union[UserConfigResponse, dict]: """Create user config Args: - params (dict): { + params (UserConfigCreateRequest): { 'name': 'str', # required 'data': 'dict', # required 'tags': 'dict', - 'user_id': 'str'(meta) # injected from auth - 'domain_id': 'str'(meta), # injected from auth + 'domain_id': 'str', # injected from auth (required) + 'user_id': 'str', # injected from auth (required) } Returns: - user_config_vo (object) + UserConfigResponse: """ - return self.user_config_mgr.create_user_config(params) + user_config_vo = self.user_config_mgr.create_user_config(params.dict()) + return UserConfigResponse(**user_config_vo.to_dict()) @transaction(permission="config:UserConfig.write", role_types=["USER"]) - @check_required(["name", "user_id", "domain_id"]) - def update(self, params: dict) -> UserConfig: + @convert_model + def update(self, params: UserConfigUpdateRequest) -> Union[UserConfigResponse, dict]: """Update user config Args: - params (dict): { - 'name': 'str', # required + params (UserConfigUpdateRequest): { + 'name': 'str', # required 'data': 'dict', 'tags': 'dict', - 'domain_id': 'str' # injected from auth - 'user_id': 'str' # injected from auth + 'domain_id': 'str', # injected from auth (required) + 'user_id': 'str', # injected from auth (required) } Returns: - user_config_vo (object) + UserConfigResponse: """ - return self.user_config_mgr.update_user_config(params) + user_config_vo: UserConfig = self.user_config_mgr.get_user_config( + params.name, params.domain_id, params.user_id + ) + + user_config_vo = self.user_config_mgr.update_user_config_by_vo( + params.dict(exclude_unset=True), user_config_vo + ) + + return UserConfigResponse(**user_config_vo.to_dict()) @transaction(permission="config:UserConfig.write", role_types=["USER"]) - @check_required(["name", "data", "user_id", "domain_id"]) - def set(self, params: dict) -> UserConfig: + @convert_model + def set(self, params: UserConfigSetRequest) -> Union[UserConfigResponse, dict]: """Set user config (create or update) Args: params (dict): { - 'name': 'str', # required - 'data': 'dict', # required + 'name': 'str', # required + 'data': 'dict', # required 'tags': 'dict', - 'user_id': 'str' # injected from auth - 'domain_id': 'str' # injected from auth + 'domain_id': 'str', # injected from auth (required) + 'user_id': 'str', # injected from auth (required) } Returns: - user_config_vo (object) + UserConfigResponse: """ user_config_vos = self.user_config_mgr.filter_user_configs( - name=params["name"], - user_id=params["user_id"], - domain_id=params["domain_id"], + name=params.name, domain_id=params.domain_id, user_id=params.user_id ) if user_config_vos.count() == 0: - return self.user_config_mgr.create_user_config(params) + user_config_vo = self.user_config_mgr.create_user_config(params.dict()) else: - return self.user_config_mgr.update_user_config_by_vo( - params, user_config_vos[0] + user_config_vo = self.user_config_mgr.update_user_config_by_vo( + params.dict(exclude_unset=True), user_config_vos[0] ) + return UserConfigResponse(**user_config_vo.to_dict()) + @transaction(permission="config:UserConfig.write", role_types=["USER"]) - @check_required(["name", "user_id", "domain_id"]) - def delete(self, params): + @convert_model + def delete(self, params: UserConfigDeleteRequest) -> None: """Delete user config Args: params (dict): { 'name': 'str', # required - 'user_id': 'str', # injected from auth - 'domain_id': 'str' # injected from auth + 'domain_id': 'str' # injected from auth (required) + 'user_id': 'str', # injected from auth (required) } Returns: None """ - self.user_config_mgr.delete_user_config( - params["name"], params["user_id"], params["domain_id"] + user_config_vo: UserConfig = self.user_config_mgr.get_user_config( + params.name, params.domain_id, params.user_id ) + self.user_config_mgr.delete_user_config_by_vo(user_config_vo) + @transaction(permission="config:UserConfig.read", role_types=["USER"]) - @check_required(["name", "user_id", "domain_id"]) - def get(self, params): + @convert_model + def get(self, params: UserConfigGetRequest) -> Union[UserConfigResponse, dict]: """Get user config Args: params (dict): { 'name': 'str', # required - 'domain_id': 'str',(meta), # required - 'user_id': 'str'(meta) # required + 'domain_id': 'str', # injected from auth (required) + 'user_id': 'str', # injected from auth (required) } Returns: - user_config_vo (object) + UserConfigResponse: """ - return self.user_config_mgr.get_user_config( - params["name"], params["user_id"], params["domain_id"] + user_config_vo: UserConfig = self.user_config_mgr.get_user_config( + params.name, params.domain_id, params.user_id ) + return UserConfigResponse(**user_config_vo.to_dict()) + @transaction(permission="config:UserConfig.read", role_types=["USER"]) - @check_required(["user_id", "domain_id"]) - @append_query_filter(["name", "user_id", "domain_id"]) + @append_query_filter(["name", "domain_id", "user_id"]) @append_keyword_filter(["name"]) - def list(self, params: dict) -> dict: + @convert_model + def list(self, params: UserConfigSearchQueryRequest) -> Union[UserConfigsResponse, dict]: """List user configs Args: params (dict): { 'query': 'dict (spaceone.api.core.v1.Query)', 'name': 'str', - 'user_id': 'str', # injected from auth - 'domain_id': 'str' # injected from auth + 'domain_id': 'str' # injected from auth (required) + 'user_id': 'str', # injected from auth (required) } Returns: - user_config_vos (objects) - total_count (int) - """ - - query = params.get("query", {}) - return self.user_config_mgr.list_user_configs(query) - - @transaction(permission="config:UserConfig.read", role_types=["USER"]) - @check_required(["query", "domain_id", "user_id"]) - @append_query_filter(["domain_id", "user_id"]) - @append_keyword_filter(["name"]) - def stat(self, params): - """ - Args: - params (dict): { - 'query': 'dict (spaceone.api.core.v1.StatisticsQuery)', # required - 'user_id': 'str'(meta) # injected from auth - 'domain_id': 'str'(meta), # injected from auth - } - - Returns: - values (list) : 'list of statistics data' - + UserConfigsResponse: """ - query = params.get("query", {}) - return self.user_config_mgr.state_user_configs(query) + query = params.query or {} + user_configs_vos, total_count = self.user_config_mgr.list_user_configs(query) + user_configs_info = [ + user_config_vo.to_dict() for user_config_vo in user_configs_vos + ] + return UserConfigsResponse(results=user_configs_info, total_count=total_count)