diff --git a/src/spaceone/config/interface/grpc/__init__.py b/src/spaceone/config/interface/grpc/__init__.py index e23f1ee..46f8e12 100644 --- a/src/spaceone/config/interface/grpc/__init__.py +++ b/src/spaceone/config/interface/grpc/__init__.py @@ -1,11 +1,11 @@ from spaceone.core.pygrpc.server import GRPCServer from spaceone.config.interface.grpc.domain_config import DomainConfig -from spaceone.config.interface.grpc.workspace_config import WorkspaceConfig +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(WorkspaceConfig) app.add_service(UserConfig) +app.add_service(PublicConfig) diff --git a/src/spaceone/config/interface/grpc/public_config.py b/src/spaceone/config/interface/grpc/public_config.py new file mode 100644 index 0000000..0fd8702 --- /dev/null +++ b/src/spaceone/config/interface/grpc/public_config.py @@ -0,0 +1,51 @@ +from spaceone.api.config.v1 import public_config_pb2, public_config_pb2_grpc +from spaceone.core.pygrpc import BaseAPI + +from spaceone.config.service.public_config_service import PublicConfigService + + +class PublicConfig(BaseAPI, public_config_pb2_grpc.PublicConfigServicer): + pb2 = public_config_pb2 + pb2_grpc = public_config_pb2_grpc + + def create(self, request, context): + params, metadata = self.parse_request(request, context) + public_config_svc = PublicConfigService(metadata) + response: dict = public_config_svc.create(params) + return self.dict_to_message(response) + + def update(self, request, context): + params, metadata = self.parse_request(request, context) + public_config_svc = PublicConfigService(metadata) + response: dict = public_config_svc.update(params) + return self.dict_to_message(response) + + def set(self, request, context): + params, metadata = self.parse_request(request, context) + public_config_svc = PublicConfigService(metadata) + response: dict = public_config_svc.set(params) + return self.dict_to_message(response) + + def delete(self, request, context): + params, metadata = self.parse_request(request, context) + public_config_svc = PublicConfigService(metadata) + public_config_svc.delete(params) + return self.empty() + + def get(self, request, context): + params, metadata = self.parse_request(request, context) + public_config_svc = PublicConfigService(metadata) + response: dict = public_config_svc.get(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/workspace_config.py b/src/spaceone/config/interface/grpc/workspace_config.py deleted file mode 100644 index 9549dcc..0000000 --- a/src/spaceone/config/interface/grpc/workspace_config.py +++ /dev/null @@ -1,53 +0,0 @@ -from spaceone.api.config.v1 import workspace_config_pb2, workspace_config_pb2_grpc -from spaceone.core.pygrpc import BaseAPI - -from spaceone.config.service.workspace_config_service import WorkspaceConfigService -from spaceone.config.info.domain_config_info import * -from spaceone.config.info.common_info import * - - -class WorkspaceConfig(BaseAPI, workspace_config_pb2_grpc.WorkspaceConfigServicer): - pb2 = workspace_config_pb2 - pb2_grpc = workspace_config_pb2_grpc - - def create(self, request, context): - params, metadata = self.parse_request(request, context) - workspace_config_svc = WorkspaceConfigService(metadata) - response: dict = workspace_config_svc.create(params) - return self.dict_to_message(response) - - def update(self, request, context): - params, metadata = self.parse_request(request, context) - workspace_config_svc = WorkspaceConfigService(metadata) - response: dict = workspace_config_svc.update(params) - return self.dict_to_message(response) - - def set(self, request, context): - params, metadata = self.parse_request(request, context) - workspace_config_svc = WorkspaceConfigService(metadata) - response: dict = workspace_config_svc.set(params) - return self.dict_to_message(response) - - def delete(self, request, context): - params, metadata = self.parse_request(request, context) - workspace_config_svc = WorkspaceConfigService(metadata) - workspace_config_svc.delete(params) - return self.empty() - - def get(self, request, context): - params, metadata = self.parse_request(request, context) - workspace_config_svc = WorkspaceConfigService(metadata) - response: dict = workspace_config_svc.get(params) - return self.dict_to_message(response) - - def list(self, request, context): - params, metadata = self.parse_request(request, context) - workspace_config_svc = WorkspaceConfigService(metadata) - response: dict = workspace_config_svc.list(params) - return self.dict_to_message(response) - - def stat(self, request, context): - params, metadata = self.parse_request(request, context) - workspace_config_svc = WorkspaceConfigService(metadata) - response: dict = workspace_config_svc.stat(params) - return self.dict_to_message(response) diff --git a/src/spaceone/config/manager/identity_manager.py b/src/spaceone/config/manager/identity_manager.py new file mode 100644 index 0000000..bf5c885 --- /dev/null +++ b/src/spaceone/config/manager/identity_manager.py @@ -0,0 +1,39 @@ +import logging + +from spaceone.core import cache +from spaceone.core import config +from spaceone.core.manager import BaseManager +from spaceone.core.connector.space_connector import SpaceConnector +from spaceone.core.auth.jwt.jwt_util import JWTUtil + +_LOGGER = logging.getLogger(__name__) + + +class IdentityManager(BaseManager): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + token = self.transaction.get_meta("token") + self.token_type = JWTUtil.get_value_from_token(token, "typ") + self.identity_conn: SpaceConnector = self.locator.get_connector( + SpaceConnector, service="identity" + ) + + def check_workspace(self, workspace_id: str, domain_id: str) -> None: + system_token = config.get_global("TOKEN") + + self.identity_conn.dispatch( + "Workspace.check", + {"workspace_id": workspace_id, "domain_id": domain_id}, + token=system_token, + ) + + @cache.cacheable(key="inventory:project:{domain_id}:{project_id}", expire=600) + def get_project(self, project_id: str, domain_id: str): + if self.token_type == "SYSTEM_TOKEN": + return self.identity_conn.dispatch( + "Project.get", {"project_id": project_id}, x_domain_id=domain_id + ) + else: + return self.identity_conn.dispatch( + "Project.get", {"project_id": project_id} + ) diff --git a/src/spaceone/config/manager/public_config_manager.py b/src/spaceone/config/manager/public_config_manager.py new file mode 100644 index 0000000..25c10fe --- /dev/null +++ b/src/spaceone/config/manager/public_config_manager.py @@ -0,0 +1,66 @@ +import logging +from typing import Tuple, Union + +from mongoengine import QuerySet +from spaceone.core.manager import BaseManager + +from spaceone.config.model.public_config.database import PublicConfig + +_LOGGER = logging.getLogger(__name__) + + +class PublicConfigManager(BaseManager): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.public_config_model = PublicConfig + + def create_public_config(self, params: dict) -> PublicConfig: + def _rollback(vo: PublicConfig) -> None: + _LOGGER.info( + f"[create_public_config._rollback] " f"Delete public config : {vo.name}" + ) + vo.delete() + + public_config_vo: PublicConfig = self.public_config_model.create(params) + self.transaction.add_rollback(_rollback, public_config_vo) + + return public_config_vo + + def update_public_config_by_vo( + self, params: dict, public_config_vo: PublicConfig + ) -> PublicConfig: + def _rollback(old_data: dict): + _LOGGER.info( + f'[update_public_config_by_vo._rollback] Revert Data : {old_data["name"]}' + ) + public_config_vo.update(old_data) + + self.transaction.add_rollback(_rollback, public_config_vo.to_dict()) + + return public_config_vo.update(params) + + @staticmethod + def delete_public_config_by_vo(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 filter_public_configs(self, **conditions): + return self.public_config_model.filter(**conditions) + + def list_public_configs(self, query: dict) -> Tuple[QuerySet, int]: + return self.public_config_model.query(**query) + + def stat_public_configs(self, query: dict) -> dict: + return self.public_config_model.stat(**query) diff --git a/src/spaceone/config/manager/workspace_config_manager.py b/src/spaceone/config/manager/workspace_config_manager.py deleted file mode 100644 index 1eba90a..0000000 --- a/src/spaceone/config/manager/workspace_config_manager.py +++ /dev/null @@ -1,56 +0,0 @@ -import logging -from typing import Tuple - -from mongoengine import QuerySet -from spaceone.core.manager import BaseManager - -from spaceone.config.model.workspace_config.database import WorkspaceConfig - -_LOGGER = logging.getLogger(__name__) - - -class WorkspaceConfigManager(BaseManager): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.workspace_config_model = WorkspaceConfig - - def create_workspace_config(self, params: dict) -> WorkspaceConfig: - def _rollback(vo: WorkspaceConfig) -> None: - _LOGGER.info( - f"[create_workspace_config._rollback] " f"Delete workspace config : {vo.name}" - ) - vo.delete() - - workspace_config_vo: WorkspaceConfig = self.workspace_config_model.create(params) - self.transaction.add_rollback(_rollback, workspace_config_vo) - - return workspace_config_vo - - def update_workspace_config_by_vo( - self, params: dict, workspace_config_vo: WorkspaceConfig - ) -> WorkspaceConfig: - def _rollback(old_data: dict): - _LOGGER.info( - f'[update_workspace_config_by_vo._rollback] Revert Data : {old_data["name"]}' - ) - workspace_config_vo.update(old_data) - - self.transaction.add_rollback(_rollback, workspace_config_vo.to_dict()) - - return workspace_config_vo.update(params) - - @staticmethod - def delete_workspace_config_by_vo(workspace_config_vo: WorkspaceConfig) -> None: - workspace_config_vo.delete() - - def get_workspace_config(self, name: str, workspace_id: str, domain_id: str) -> WorkspaceConfig: - return self.workspace_config_model.get(name=name, workspace_id=workspace_id, domain_id=domain_id) - - def filter_workspace_configs(self, **conditions): - return self.workspace_config_model.filter(**conditions) - - def list_workspace_configs(self, query: dict) -> Tuple[QuerySet, int]: - return self.workspace_config_model.query(**query) - - def stat_workspace_configs(self, query: dict) -> dict: - return self.workspace_config_model.stat(**query) diff --git a/src/spaceone/config/model/__init__.py b/src/spaceone/config/model/__init__.py index a161c73..b5548c7 100644 --- a/src/spaceone/config/model/__init__.py +++ b/src/spaceone/config/model/__init__.py @@ -1,3 +1,3 @@ from spaceone.config.model.user_config_model import UserConfig -from spaceone.config.model.workspace_config.database import WorkspaceConfig from spaceone.config.model.domain_config_model import DomainConfig +from spaceone.config.model.public_config.database import PublicConfig diff --git a/src/spaceone/config/model/workspace_config/__init__.py b/src/spaceone/config/model/public_config/__init__.py similarity index 100% rename from src/spaceone/config/model/workspace_config/__init__.py rename to src/spaceone/config/model/public_config/__init__.py diff --git a/src/spaceone/config/model/workspace_config/database.py b/src/spaceone/config/model/public_config/database.py similarity index 60% rename from src/spaceone/config/model/workspace_config/database.py rename to src/spaceone/config/model/public_config/database.py index 49496d0..575446a 100644 --- a/src/spaceone/config/model/workspace_config/database.py +++ b/src/spaceone/config/model/public_config/database.py @@ -3,10 +3,12 @@ from spaceone.core.model.mongo_model import MongoModel -class WorkspaceConfig(MongoModel): - name = StringField(max_length=255, unique_with=["domain_id", "workspace_id"]) +class PublicConfig(MongoModel): + name = StringField(max_length=255, unique_with=["domain_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) domain_id = StringField(max_length=40) created_at = DateTimeField(auto_now_add=True) @@ -14,11 +16,12 @@ class WorkspaceConfig(MongoModel): meta = { "updatable_fields": ["name", "data", "tags", "updated_at"], - "minimal_fields": ["name"], + "minimal_fields": ["name", "resource_group"], + "change_query_keys": {"user_projects": "project_id"}, "ordering": ["name"], "indexes": [ "name", - "workspace_id", "domain_id", + "workspace_id", ], } diff --git a/src/spaceone/config/model/public_config/request.py b/src/spaceone/config/model/public_config/request.py new file mode 100644 index 0000000..e36c794 --- /dev/null +++ b/src/spaceone/config/model/public_config/request.py @@ -0,0 +1,76 @@ +from typing import Union, Literal +from pydantic import BaseModel + +__all__ = [ + "PublicConfigCreateRequest", + "PublicConfigUpdateRequest", + "PublicConfigSetRequest", + "PublicConfigDeleteRequest", + "PublicConfigGetRequest", + "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 + + +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 + 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 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/workspace_config/response.py b/src/spaceone/config/model/public_config/response.py similarity index 66% rename from src/spaceone/config/model/workspace_config/response.py rename to src/spaceone/config/model/public_config/response.py index 8ae101e..feed02d 100644 --- a/src/spaceone/config/model/workspace_config/response.py +++ b/src/spaceone/config/model/public_config/response.py @@ -4,13 +4,17 @@ from spaceone.core import utils -__all__ = ["WorkspaceConfigResponse", "WorkspaceConfigsResponse"] +from spaceone.config.model.public_config.request import ResourceGroup +__all__ = ["PublicConfigResponse", "PublicConfigsResponse"] -class WorkspaceConfigResponse(BaseModel): + +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 @@ -23,6 +27,6 @@ def dict(self, *args, **kwargs): return data -class WorkspaceConfigsResponse(BaseModel): - results: List[WorkspaceConfigResponse] = [] +class PublicConfigsResponse(BaseModel): + results: List[PublicConfigResponse] = [] total_count: int diff --git a/src/spaceone/config/model/workspace_config/request.py b/src/spaceone/config/model/workspace_config/request.py deleted file mode 100644 index beabd28..0000000 --- a/src/spaceone/config/model/workspace_config/request.py +++ /dev/null @@ -1,61 +0,0 @@ -from typing import Union -from pydantic import BaseModel - -__all__ = [ - "WorkspaceConfigCreateRequest", - "WorkspaceConfigUpdateRequest", - "WorkspaceConfigSetRequest", - "WorkspaceConfigDeleteRequest", - "WorkspaceConfigGetRequest", - "WorkspaceConfigSearchQueryRequest", - "WorkspaceConfigQueryRequest", -] - - -class WorkspaceConfigCreateRequest(BaseModel): - name: str - data: dict - tags: Union[dict, None] = None - workspace_id: str - domain_id: str - - -class WorkspaceConfigUpdateRequest(BaseModel): - name: str - data: Union[dict, None] = None - tags: Union[dict, None] = None - workspace_id: str - domain_id: str - - -class WorkspaceConfigSetRequest(BaseModel): - name: str - data: str - tags: Union[dict, None] = None - workspace_id: str - domain_id: str - - -class WorkspaceConfigDeleteRequest(BaseModel): - name: str - workspace_id: str - domain_id: str - - -class WorkspaceConfigGetRequest(BaseModel): - name: str - workspace_id: str - domain_id: str - - -class WorkspaceConfigSearchQueryRequest(BaseModel): - query: Union[dict, None] = None - name: Union[str, None] = None - workspace_id: str - domain_id: str - - -class WorkspaceConfigQueryRequest(BaseModel): - query: dict - workspace_id: str - domain_id: str diff --git a/src/spaceone/config/service/public_config_service.py b/src/spaceone/config/service/public_config_service.py new file mode 100644 index 0000000..646b371 --- /dev/null +++ b/src/spaceone/config/service/public_config_service.py @@ -0,0 +1,246 @@ +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 * + +_LOGGER = logging.getLogger(__name__) + + +@authentication_handler +@authorization_handler +@mutation_handler +@event_handler +class PublicConfigService(BaseService): + 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"]) + @convert_model + def create(self, params: PublicConfigCreateRequest) -> Union[PublicConfigResponse, dict]: + """Create workspace config + + Args: + params (dict): { + '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 + } + + Returns: + public_config_vo (object) + """ + + public_config_vo = self.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"]) + @convert_model + def update(self, params: PublicConfigUpdateRequest) -> Union[PublicConfigResponse, dict]: + """Update public config + + Args: + params (dict): { + '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 + } + + Returns: + domain_config_vo (object) + """ + + domain_id = params.domain_id + workspace_id = params.workspace_id + project_id = params.user_projects or params.project_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) + + 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.write", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) + @convert_model + def delete(self, params: PublicConfigDeleteRequest) -> None: + """Delete workspace 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 + } + + Returns: + None + """ + + workspace_id = params.workspace_id + project_id = params.project_id or params.user_projects + + 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) + + @transaction(permission="config:PublicConfig.read", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) + @convert_model + def get(self, params: PublicConfigGetRequest) -> Union[PublicConfigResponse, dict]: + """Get workspace 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 + } + + Returns: + public_config_vo (object) + """ + + public_config_vo = self.public_config_mgr.get_public_config(params.name, params.domain_id, params.workspace_id, + params.user_projects) + + return PublicConfigResponse(**public_config_vo.to_dict()) + + @transaction(permission="config:PublicConfig.read", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) + @append_query_filter(["name", "domain_id"]) + @append_keyword_filter(["name"]) + @change_value_by_rule("APPEND", "workspace_id", "*") + @change_value_by_rule("APPEND", "project_id", "*") + @convert_model + def list(self, params: PublicConfigSearchQueryRequest) -> Union[PublicConfigsResponse, dict]: + """List workspace configs + + Args: + params (dict): { + 'query': 'dict (spaceone.api.core.v2.Query)' + 'name': 'str', + 'user_projects': 'list', + 'workspace_id': 'str', # injected from auth + 'domain_id': 'str', # injected from auth + } + + Returns: + public_configs_vos (objects) + total_count (int) + """ + + query = params.query or {} + public_config_vos, total_count = self.public_config_mgr.list_public_configs(query) + workspaces_info = [public_config_vo.to_dict() for public_config_vo in public_config_vos] + return PublicConfigsResponse(results=workspaces_info, total_count=total_count) + + @transaction(permission="config:PublicConfig.read", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) + @append_query_filter(["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/workspace_config_service.py b/src/spaceone/config/service/workspace_config_service.py deleted file mode 100644 index db3b28d..0000000 --- a/src/spaceone/config/service/workspace_config_service.py +++ /dev/null @@ -1,184 +0,0 @@ -import logging -from typing import Union - -from spaceone.core.service import * - -from spaceone.config.model.workspace_config.response import * -from spaceone.config.model.workspace_config.request import * -from spaceone.config.manager.workspace_config_manager import WorkspaceConfigManager - -_LOGGER = logging.getLogger(__name__) - - -@authentication_handler -@authorization_handler -@mutation_handler -@event_handler -class WorkspaceConfigService(BaseService): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.workspace_config_mgr = WorkspaceConfigManager() - - @transaction(permission="config:WorkspaceConfig.write", role_types=["WORKSPACE_OWNER"]) - @convert_model - def create(self, params: WorkspaceConfigCreateRequest) -> Union[WorkspaceConfigResponse, dict]: - """Create workspace config - - Args: - params (dict): { - 'name': 'str', # required - 'data': 'dict', # required - 'tags': 'dict', - 'workspace_id': 'str' # injected from auth - 'domain_id': 'str' # injected from auth - } - - Returns: - workspace_config_vo (object) - """ - - workspace_config_vo = self.workspace_config_mgr.create_workspace_config(params.dict()) - - return WorkspaceConfigResponse(**workspace_config_vo.to_dict()) - - @transaction(permission="config:WorkspaceConfig.write", role_types=["WORKSPACE_OWNER"]) - @convert_model - def update(self, params: WorkspaceConfigUpdateRequest) -> Union[WorkspaceConfigResponse, dict]: - """Update workspace config - - Args: - params (dict): { - 'name': 'str', # required - 'data': 'dict', - 'tags': 'dict', - 'workspace_id': 'str' # injected from auth - 'domain_id': 'str' # injected from auth - } - - Returns: - domain_config_vo (object) - """ - - workspace_config_vo = self.workspace_config_mgr.get_workspace_config(params.name, params.workspace_id, - params.domain_id) - - workspace_config_vo = self.workspace_config_mgr.update_workspace_config_by_vo(params.dict(exclude_unset=True), - workspace_config_vo) - - return WorkspaceConfigResponse(**workspace_config_vo.to_dict()) - - @transaction(permission="config:WorkspaceConfig.write", role_types=["WORKSPACE_OWNER"]) - @convert_model - def set(self, params: WorkspaceConfigSetRequest) -> Union[WorkspaceConfigResponse, 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: - workspace_config_vo (object) - """ - - workspace_config_vos = self.workspace_config_mgr.filter_workspace_configs( - name=params.name, workspace_id=params.workspace_id, domain_id=params.domain_id - ) - - if workspace_config_vos.count() == 0: - workspace_config_vo = self.workspace_config_mgr.create_workspace_config(params.dict()) - else: - workspace_config_vo = self.workspace_config_mgr.update_workspace_config_by_vo( - params.dict(exclude_unset=True), workspace_config_vos[0] - ) - return WorkspaceConfigResponse(**workspace_config_vo.to_dict()) - - @transaction(permission="config:WorkspaceConfig.write", role_types=["WORKSPACE_OWNER"]) - @convert_model - def delete(self, params: WorkspaceConfigDeleteRequest) -> None: - """Delete workspace config - - Args: - params (dict): { - 'name': 'str', # required - 'workspace_id': 'str' # injected from auth - 'domain_id': 'str' # injected from auth - } - - Returns: - None - """ - workspace_config_vo = self.workspace_config_mgr.get_workspace_config(params.name, params.workspace_id, - params.domain_id) - self.workspace_config_mgr.delete_workspace_config_by_vo(workspace_config_vo) - - @transaction(permission="config:WorkspaceConfig.read", role_types=["WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) - @check_required(["name", "domain_id"]) - def get(self, params: WorkspaceConfigGetRequest) -> Union[WorkspaceConfigResponse, dict]: - """Get workspace config - - Args: - params (dict): { - 'name': 'str', # required - 'workspace_id': 'str' # injected from auth - 'domain_id': 'str' # injected from auth - } - - Returns: - workspace_config_vo (object) - """ - - workspace_config_vo = self.workspace_config_mgr.get_workspace_config(params.name, params.workspace_id, - params.domain_id) - - return WorkspaceConfigResponse(**workspace_config_vo.to_dict()) - - @transaction(permission="config:WorkspaceConfig.read", role_types=["WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) - @append_query_filter(["name", "workspace_id", "domain_id"]) - @append_keyword_filter(["name"]) - @convert_model - def list(self, params: WorkspaceConfigSearchQueryRequest) -> Union[WorkspaceConfigsResponse, dict]: - """List workspace configs - - Args: - params (dict): { - 'query': 'dict (spaceone.api.core.v2.Query)' - 'name': 'str', - 'workspace_id': 'str', # injected from auth - 'domain_id': 'str', # injected from auth - } - - Returns: - workspace_configs_vos (objects) - total_count (int) - """ - - query = params.query or {} - workspace_config_vos, total_count = self.workspace_config_mgr.list_workspace_configs(query) - workspaces_info = [workspace_config_vo.to_dict() for workspace_config_vo in workspace_config_vos] - return WorkspaceConfigsResponse(results=workspaces_info, total_count=total_count) - - @transaction(permission="config:WorkspaceConfig.read", role_types=["WORKSPACE_OWNER", "WORKSPACE_MEMBER"]) - @append_query_filter(["workspace_id", "domain_id"]) - @append_keyword_filter(["name"]) - @convert_model - def stat(self, params: WorkspaceConfigQueryRequest) -> 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.workspace_config_mgr.stat_workspace_configs(query)