From c54fdee8aa9a1f60ad16d66c268adb28e08376ab Mon Sep 17 00:00:00 2001 From: seolmin Date: Mon, 11 Dec 2023 18:27:39 +0900 Subject: [PATCH 1/6] feat: add app in interface --- src/spaceone/config/api/v1/__init__.py | 0 src/spaceone/config/api/v1/domain_config.py | 53 ------------------ src/spaceone/config/api/v1/user_config.py | 53 ------------------ src/spaceone/config/conf/proto_conf.py | 4 -- .../config/{api => interface}/__init__.py | 0 .../config/interface/grpc/__init__.py | 9 +++ .../config/interface/grpc/domain_config.py | 56 +++++++++++++++++++ .../config/interface/grpc/user_config.py | 55 ++++++++++++++++++ 8 files changed, 120 insertions(+), 110 deletions(-) delete mode 100644 src/spaceone/config/api/v1/__init__.py delete mode 100644 src/spaceone/config/api/v1/domain_config.py delete mode 100644 src/spaceone/config/api/v1/user_config.py delete mode 100644 src/spaceone/config/conf/proto_conf.py rename src/spaceone/config/{api => interface}/__init__.py (100%) create mode 100644 src/spaceone/config/interface/grpc/__init__.py create mode 100644 src/spaceone/config/interface/grpc/domain_config.py create mode 100644 src/spaceone/config/interface/grpc/user_config.py diff --git a/src/spaceone/config/api/v1/__init__.py b/src/spaceone/config/api/v1/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/spaceone/config/api/v1/domain_config.py b/src/spaceone/config/api/v1/domain_config.py deleted file mode 100644 index 2ad08f2..0000000 --- a/src/spaceone/config/api/v1/domain_config.py +++ /dev/null @@ -1,53 +0,0 @@ -from spaceone.api.config.v1 import domain_config_pb2, domain_config_pb2_grpc -from spaceone.core.pygrpc import BaseAPI - - -class DomainConfig(BaseAPI, domain_config_pb2_grpc.DomainConfigServicer): - - pb2 = domain_config_pb2 - pb2_grpc = domain_config_pb2_grpc - - 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)) - - 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)) - - 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)) - - 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') - - 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)) - - 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)) diff --git a/src/spaceone/config/api/v1/user_config.py b/src/spaceone/config/api/v1/user_config.py deleted file mode 100644 index 168b4ca..0000000 --- a/src/spaceone/config/api/v1/user_config.py +++ /dev/null @@ -1,53 +0,0 @@ -from spaceone.api.config.v1 import user_config_pb2, user_config_pb2_grpc -from spaceone.core.pygrpc import BaseAPI - - -class UserConfig(BaseAPI, user_config_pb2_grpc.UserConfigServicer): - - pb2 = user_config_pb2 - pb2_grpc = user_config_pb2_grpc - - 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)) - - 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)) - - 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)) - - 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') - - 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)) - - 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)) diff --git a/src/spaceone/config/conf/proto_conf.py b/src/spaceone/config/conf/proto_conf.py deleted file mode 100644 index 4ff0dc2..0000000 --- a/src/spaceone/config/conf/proto_conf.py +++ /dev/null @@ -1,4 +0,0 @@ -PROTO = { - 'spaceone.config.api.v1.user_config': ['UserConfig'], - 'spaceone.config.api.v1.domain_config': ['DomainConfig'] -} diff --git a/src/spaceone/config/api/__init__.py b/src/spaceone/config/interface/__init__.py similarity index 100% rename from src/spaceone/config/api/__init__.py rename to src/spaceone/config/interface/__init__.py diff --git a/src/spaceone/config/interface/grpc/__init__.py b/src/spaceone/config/interface/grpc/__init__.py new file mode 100644 index 0000000..27b25f4 --- /dev/null +++ b/src/spaceone/config/interface/grpc/__init__.py @@ -0,0 +1,9 @@ +from spaceone.core.pygrpc.server import GRPCServer +from spaceone.config.interface.grpc.domain_config import DomainConfig +from spaceone.config.interface.grpc.user_config import UserConfig + +_all_ = ["app"] + +app = GRPCServer() +app.add_service(DomainConfig) +app.add_service(UserConfig) diff --git a/src/spaceone/config/interface/grpc/domain_config.py b/src/spaceone/config/interface/grpc/domain_config.py new file mode 100644 index 0000000..d2541a9 --- /dev/null +++ b/src/spaceone/config/interface/grpc/domain_config.py @@ -0,0 +1,56 @@ +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): + + pb2 = domain_config_pb2 + pb2_grpc = domain_config_pb2_grpc + + 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)) + + 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)) + + 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)) + + 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) + + 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)) + + 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)) diff --git a/src/spaceone/config/interface/grpc/user_config.py b/src/spaceone/config/interface/grpc/user_config.py new file mode 100644 index 0000000..d94f7ca --- /dev/null +++ b/src/spaceone/config/interface/grpc/user_config.py @@ -0,0 +1,55 @@ +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): + pb2 = user_config_pb2 + pb2_grpc = user_config_pb2_grpc + + 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)) + + 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)) + + 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)) + + 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) + + 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)) + + 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)) From fbf645dda2c46a2894ea915a174e0a6a45f0e0e5 Mon Sep 17 00:00:00 2001 From: seolmin Date: Mon, 11 Dec 2023 18:28:45 +0900 Subject: [PATCH 2/6] build: fix Dockerfile for SpaceONE 2.0 --- Dockerfile | 4 ++-- deploy/helm/Chart.yaml | 2 +- deploy/helm/config/config.yaml | 7 ------- deploy/helm/templates/deployment-rest.yaml | 2 +- deploy/helm/templates/deployment-scheduler.yaml | 2 +- deploy/helm/templates/deployment-worker.yaml | 2 +- 6 files changed, 6 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index b82a60c..ed142cc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM cloudforet/python-core:1.12 +FROM cloudforet/python-core:2.0 ARG PACKAGE_VERSION ENV PYTHONUNBUFFERED 1 ENV SPACEONE_PORT 50051 @@ -21,4 +21,4 @@ RUN pip install --upgrade spaceone-api EXPOSE ${SPACEONE_PORT} ENTRYPOINT ["spaceone"] -CMD ["grpc", "spaceone.config", "-m", "/opt"] +CMD ["run","grpc-server", "spaceone.config", "-m", "/opt"] diff --git a/deploy/helm/Chart.yaml b/deploy/helm/Chart.yaml index a4ed4e4..f2dc3f5 100644 --- a/deploy/helm/Chart.yaml +++ b/deploy/helm/Chart.yaml @@ -4,6 +4,6 @@ description: SpaceONE config Helm chart for Kubernetes type: application -version: 1.3.9 +version: 1.3.10 appVersion: 1.x.y diff --git a/deploy/helm/config/config.yaml b/deploy/helm/config/config.yaml index 942c64d..fac5ccb 100644 --- a/deploy/helm/config/config.yaml +++ b/deploy/helm/config/config.yaml @@ -31,13 +31,6 @@ GLOBAL: file: type: file filename: /var/log/spaceone/config.log - HANDLERS: - authentication: - - backend: spaceone.core.handler.authentication_handler.AuthenticationGRPCHandler - uri: grpc://identity:50051/v1/Domain/get_public_key - authorization: - - backend: spaceone.core.handler.authorization_handler.AuthorizationGRPCHandler - uri: grpc://identity:50051/v1/Authorization/verify CONNECTORS: IdentityConnector: endpoint: diff --git a/deploy/helm/templates/deployment-rest.yaml b/deploy/helm/templates/deployment-rest.yaml index 78d243b..9272b22 100644 --- a/deploy/helm/templates/deployment-rest.yaml +++ b/deploy/helm/templates/deployment-rest.yaml @@ -43,7 +43,7 @@ spec: resources: {{- toYaml .Values.resources.rest | nindent 12 }} {{- end }} - command: ['spaceone', 'rest', 'spaceone.{{ regexReplaceAll "-" .Values.name "_" }}', '-p', '8000'] + command: ['spaceone', 'run', 'rest-server', 'spaceone.{{ regexReplaceAll "-" .Values.name "_" }}', '-p', '8000'] ports: - containerPort: 8000 volumeMounts: diff --git a/deploy/helm/templates/deployment-scheduler.yaml b/deploy/helm/templates/deployment-scheduler.yaml index 9ae9bf3..58b02ce 100644 --- a/deploy/helm/templates/deployment-scheduler.yaml +++ b/deploy/helm/templates/deployment-scheduler.yaml @@ -43,7 +43,7 @@ spec: resources: {{- toYaml .Values.resources.scheduler | nindent 12 }} {{- end }} - command: ['spaceone', 'scheduler', 'spaceone.{{ regexReplaceAll "-" .Values.name "_" }}'] + command: ['spaceone', 'run', 'scheduler', 'spaceone.{{ regexReplaceAll "-" .Values.name "_" }}'] volumeMounts: - name: default-conf mountPath: /opt/spaceone/{{ .Values.name }}/config/config.yaml diff --git a/deploy/helm/templates/deployment-worker.yaml b/deploy/helm/templates/deployment-worker.yaml index 9b68a5c..9da3710 100644 --- a/deploy/helm/templates/deployment-worker.yaml +++ b/deploy/helm/templates/deployment-worker.yaml @@ -43,7 +43,7 @@ spec: resources: {{- toYaml .Values.resources.worker | nindent 12 }} {{- end }} - command: ['spaceone', 'scheduler', 'spaceone.{{ regexReplaceAll "-" .Values.name "_" }}'] + command: ['spaceone', 'run', 'scheduler', 'spaceone.{{ regexReplaceAll "-" .Values.name "_" }}'] volumeMounts: - name: default-conf mountPath: /opt/spaceone/{{ .Values.name }}/config/config.yaml From cd0610d70b61ba416bfa9b11105c1fce75491aba Mon Sep 17 00:00:00 2001 From: seolmin Date: Tue, 12 Dec 2023 12:07:11 +0900 Subject: [PATCH 3/6] fix: add scope of transaction decorator in all methods --- src/spaceone/config/service/__init__.py | 2 - .../config/service/domain_config_service.py | 100 +++++++------ .../config/service/user_config_service.py | 141 ++++++++++-------- 3 files changed, 135 insertions(+), 108 deletions(-) diff --git a/src/spaceone/config/service/__init__.py b/src/spaceone/config/service/__init__.py index d48a372..e69de29 100644 --- a/src/spaceone/config/service/__init__.py +++ b/src/spaceone/config/service/__init__.py @@ -1,2 +0,0 @@ -from spaceone.config.service.user_config_service import UserConfigService -from spaceone.config.service.domain_config_service import DomainConfigService diff --git a/src/spaceone/config/service/domain_config_service.py b/src/spaceone/config/service/domain_config_service.py index de6f889..fcf8fad 100644 --- a/src/spaceone/config/service/domain_config_service.py +++ b/src/spaceone/config/service/domain_config_service.py @@ -1,7 +1,7 @@ import logging from spaceone.core.service import * -from spaceone.core import utils + from spaceone.config.manager.domain_config_manager import DomainConfigManager _LOGGER = logging.getLogger(__name__) @@ -12,22 +12,27 @@ @mutation_handler @event_handler class DomainConfigService(BaseService): + service = "config" + resource = "DomainConfig" + permission_group = "DOMAIN" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.domain_config_mgr: DomainConfigManager = self.locator.get_manager('DomainConfigManager') + self.domain_config_mgr: DomainConfigManager = self.locator.get_manager( + DomainConfigManager + ) - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['name', 'data', 'domain_id']) + @transaction(scope="domain_admin:write") + @check_required(["name", "data", "domain_id"]) def create(self, params): """Create domain config Args: params (dict): { - 'name': 'str', - 'data': 'dict', + 'name': 'str', # required + 'data': 'dict', # required 'tags': 'dict', - 'domain_id': 'str' + 'domain_id': 'str' # required } Returns: @@ -36,17 +41,17 @@ def create(self, params): return self.domain_config_mgr.create_domain_config(params) - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['name', 'domain_id']) + @transaction(scope="domain_admin:write") + @check_required(["name", "domain_id"]) def update(self, params): """Update domain config Args: params (dict): { - 'name': 'str', + 'name': 'str', # required 'data': 'dict', 'tags': 'dict', - 'domain_id': 'str' + 'domain_id': 'str' # required } Returns: @@ -55,79 +60,84 @@ def update(self, params): return self.domain_config_mgr.update_domain_config(params) - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['name', 'data', 'domain_id']) + @transaction(scope="domain_admin:write") + @check_required(["name", "data", "domain_id"]) def set(self, params): """Set domain config (create or update) Args: params (dict): { - 'name': 'str', - 'data': 'dict', + 'name': 'str', # required + 'data': 'dict', # required 'tags': 'dict', - 'domain_id': 'str' + 'domain_id': 'str' # required } Returns: domain_config_vo (object) """ - domain_id = params['domain_id'] + domain_id = params["domain_id"] - domain_config_vos = self.domain_config_mgr.filter_domain_configs(domain_id=domain_id, name=params['name']) + domain_config_vos = self.domain_config_mgr.filter_domain_configs( + domain_id=domain_id, name=params["name"] + ) if domain_config_vos.count() == 0: return self.domain_config_mgr.create_domain_config(params) else: - return self.domain_config_mgr.update_domain_config_by_vo(params, domain_config_vos[0]) + return self.domain_config_mgr.update_domain_config_by_vo( + params, domain_config_vos[0] + ) - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['name', 'domain_id']) + @transaction(scope="domain_admin:write") + @check_required(["name", "domain_id"]) def delete(self, params): """Delete domain config Args: params (dict): { - 'name': 'str', - 'domain_id': 'str' + 'name': 'str', # required + 'domain_id': 'str' # required } Returns: None """ - self.domain_config_mgr.delete_domain_config(params['name'], params['domain_id']) + self.domain_config_mgr.delete_domain_config(params["name"], params["domain_id"]) - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['name', 'domain_id']) + @transaction(scope="domain_admin:read") + @check_required(["name", "domain_id"]) def get(self, params): """Get domain config Args: params (dict): { - 'name': 'str', - 'domain_id': 'str', - 'only': 'list' + 'name': 'str', # required + 'domain_id': 'str' # required } Returns: domain_config_vo (object) """ - return self.domain_config_mgr.get_domain_config(params['name'], params['domain_id'], params.get('only')) + return self.domain_config_mgr.get_domain_config( + params["name"], params["domain_id"], params.get("only") + ) - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['domain_id']) - @append_query_filter(['name', 'domain_id']) - @append_keyword_filter(['name']) + @transaction(scope="domain_admin:read") + @check_required(["domain_id"]) + @append_query_filter(["name", "domain_id"]) + @append_keyword_filter(["name"]) def list(self, params): - """ List domain configs + """List domain configs Args: params (dict): { - 'name': 'str', - 'domain_id': 'str', 'query': 'dict (spaceone.api.core.v1.Query)' + 'name': 'str', + 'domain_id': 'str', # required } Returns: @@ -135,19 +145,19 @@ def list(self, params): total_count (int) """ - query = params.get('query', {}) + query = params.get("query", {}) return self.domain_config_mgr.list_domain_configs(query) - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['query', 'domain_id']) - @append_query_filter(['domain_id']) - @append_keyword_filter(['name']) + @transaction(scope="domain_admin:read") + @check_required(["query", "domain_id"]) + @append_query_filter(["domain_id"]) + @append_keyword_filter(["name"]) def stat(self, params): """ Args: params (dict): { - 'domain_id': 'str', - 'query': 'dict (spaceone.api.core.v1.StatisticsQuery)' + 'query': 'dict (spaceone.api.core.v1.StatisticsQuery)' # required + 'domain_id': 'str', # required } Returns: @@ -155,5 +165,5 @@ def stat(self, params): """ - query = params.get('query', {}) + query = params.get("query", {}) return self.domain_config_mgr.state_domain_configs(query) diff --git a/src/spaceone/config/service/user_config_service.py b/src/spaceone/config/service/user_config_service.py index 23a348d..35383ff 100644 --- a/src/spaceone/config/service/user_config_service.py +++ b/src/spaceone/config/service/user_config_service.py @@ -1,8 +1,8 @@ import logging -from spaceone.core.service import * -from spaceone.core import utils from spaceone.core.error import * +from spaceone.core.service import * + from spaceone.config.manager.user_config_manager import UserConfigManager _LOGGER = logging.getLogger(__name__) @@ -13,142 +13,161 @@ @mutation_handler @event_handler class UserConfigService(BaseService): + service = "config" + resource = "UserConfig" + permission_group = "USER" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.user_config_mgr: UserConfigManager = self.locator.get_manager('UserConfigManager') + self.user_config_mgr: UserConfigManager = self.locator.get_manager( + "UserConfigManager" + ) - @transaction(append_meta={'authorization.scope': 'USER'}) - @check_required(['name', 'data', 'domain_id']) + @transaction(scope="user:write") + @check_required(["name", "data", "domain_id", "user_id"]) def create(self, params): """Create user config Args: params (dict): { - 'name': 'str', - 'data': 'dict', + 'name': 'str', # required + 'data': 'dict', # required 'tags': 'dict', - 'domain_id': 'str' + 'domain_id': 'str'(meta), # required + 'user_id': 'str'(meta) # required } Returns: user_config_vo (object) """ - self._check_permission(params['domain_id']) + self._check_permission(params["domain_id"]) - params['user_id'] = self.transaction.get_meta('user_id') + params["user_id"] = self.transaction.get_meta("user_id") return self.user_config_mgr.create_user_config(params) - @transaction(append_meta={'authorization.scope': 'USER'}) - @check_required(['name', 'domain_id']) + @transaction(scope="user:write") + @check_required(["name", "domain_id", "user_id"]) def update(self, params): """Update user config Args: params (dict): { - 'name': 'str', + 'name': 'str', # required 'data': 'dict', 'tags': 'dict', - 'domain_id': 'str' + 'domain_id': 'str'(meta) # required + 'user_id': 'str'(meta) # required } Returns: user_config_vo (object) """ - self._check_permission(params['domain_id']) + self._check_permission(params["domain_id"]) - params['user_id'] = self.transaction.get_meta('user_id') + params["user_id"] = self.transaction.get_meta("user_id") return self.user_config_mgr.update_user_config(params) - @transaction(append_meta={'authorization.scope': 'USER'}) - @check_required(['name', 'data', 'domain_id']) + @transaction(scope="user:write") + @check_required(["name", "data", "domain_id", "user_id"]) def set(self, params): """Set user config (create or update) Args: params (dict): { - 'name': 'str', - 'data': 'dict', + 'name': 'str', # required + 'data': 'dict', # required 'tags': 'dict', - 'domain_id': 'str' + 'domain_id': 'str'(meta), # required + 'user_id': 'str'(meta) # required } Returns: user_config_vo (object) """ - self._check_permission(params['domain_id']) + self._check_permission(params["domain_id"]) - params['user_id'] = self.transaction.get_meta('user_id') + params["user_id"] = self.transaction.get_meta("user_id") - user_type = self.transaction.get_meta('authorization.user_type') - if user_type == 'DOMAIN_OWNER': + user_type = self.transaction.get_meta("authorization.user_type") + if user_type == "DOMAIN_OWNER": raise ERROR_PERMISSION_DENIED() - user_config_vos = self.user_config_mgr.filter_user_configs(domain_id=params['domain_id'], - user_id=params['user_id'], name=params['name']) + user_config_vos = self.user_config_mgr.filter_user_configs( + domain_id=params["domain_id"], + user_id=params["user_id"], + name=params["name"], + ) if user_config_vos.count() == 0: return self.user_config_mgr.create_user_config(params) else: - return self.user_config_mgr.update_user_config_by_vo(params, user_config_vos[0]) + return self.user_config_mgr.update_user_config_by_vo( + params, user_config_vos[0] + ) - @transaction(append_meta={'authorization.scope': 'USER'}) - @check_required(['name', 'domain_id']) + @transaction(scope="user:write") + @check_required(["name", "domain_id", "user_id"]) def delete(self, params): """Delete user config Args: params (dict): { - 'name': 'str', - 'domain_id': 'str' + 'name': 'str', # required + 'domain_id': 'str'(meta), # required + 'user_id': 'str'(meta) # required } Returns: None """ - self._check_permission(params['domain_id']) - user_id = self.transaction.get_meta('user_id') + self._check_permission(params["domain_id"]) + user_id = self.transaction.get_meta("user_id") - self.user_config_mgr.delete_user_config(params['name'], user_id, params['domain_id']) + self.user_config_mgr.delete_user_config( + params["name"], user_id, params["domain_id"] + ) - @transaction(append_meta={'authorization.scope': 'USER'}) - @check_required(['name', 'domain_id']) + @transaction(scope="user:read") + @check_required(["name", "domain_id", "user_id"]) def get(self, params): """Get user config Args: params (dict): { - 'name': 'str', - 'domain_id': 'str', - 'only': 'list' + 'name': 'str', # required + 'domain_id': 'str',(meta), # required + 'user_id': 'str'(meta) # required } Returns: user_config_vo (object) """ - user_id = self.transaction.get_meta('user_id') + user_id = self.transaction.get_meta("user_id") - return self.user_config_mgr.get_user_config(params['name'], user_id, params['domain_id'], params.get('only')) + return self.user_config_mgr.get_user_config( + params["name"], user_id, params["domain_id"], params.get("only") + ) - @transaction(append_meta={'authorization.scope': 'USER'}) - @check_required(['domain_id']) - @append_query_filter(['name', 'user_id', 'domain_id']) - @append_keyword_filter(['name']) + @transaction(scope="user:read") + @check_required(["domain_id"]) + @append_query_filter(["name", "user_id", "domain_id"]) + @append_keyword_filter(["name"]) def list(self, params): - """ List user configs + """List user configs Args: params (dict): { + 'query': 'dict (spaceone.api.core.v1.Query)', 'name': 'str', - 'domain_id': 'str', - 'query': 'dict (spaceone.api.core.v1.Query)' + 'domain_id': 'str'(meta), # required + 'user_id': 'str'(meta) # required } Returns: @@ -156,20 +175,20 @@ def list(self, params): total_count (int) """ - query = params.get('query', {}) + query = params.get("query", {}) return self.user_config_mgr.list_user_configs(query) - @transaction(append_meta={'authorization.scope': 'USER'}) - @check_required(['query', 'domain_id']) - @append_query_filter(['domain_id']) - @append_keyword_filter(['name']) + @transaction(scope="user:read") + @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): { - 'domain_id': 'str', - 'query': 'dict (spaceone.api.core.v1.StatisticsQuery)', - 'user_self': 'list', // from meta + 'query': 'dict (spaceone.api.core.v1.StatisticsQuery)', # required + 'domain_id': 'str'(meta), # required + 'user_id': 'str'(meta) # required } Returns: @@ -177,12 +196,12 @@ def stat(self, params): """ - query = params.get('query', {}) + query = params.get("query", {}) return self.user_config_mgr.state_user_configs(query) def _check_permission(self, request_domain_id): - user_type = self.transaction.get_meta('authorization.user_type') - user_domain_id = self.transaction.get_meta('domain_id') + user_type = self.transaction.get_meta("authorization.user_type") + user_domain_id = self.transaction.get_meta("domain_id") - if user_type == 'DOMAIN_OWNER' or request_domain_id != user_domain_id: + if user_type == "DOMAIN_OWNER" or request_domain_id != user_domain_id: raise ERROR_PERMISSION_DENIED() From 47d87e8e52e6484059716c71f06fd92861351a0d Mon Sep 17 00:00:00 2001 From: seolmin Date: Tue, 12 Dec 2023 12:11:23 +0900 Subject: [PATCH 4/6] fix: apply black formatter --- src/setup.py | 24 +- src/spaceone/__init__.py | 2 +- src/spaceone/config/__init__.py | 2 +- src/spaceone/config/conf/global_conf.py | 27 +-- src/spaceone/config/info/common_info.py | 2 +- .../config/info/domain_config_info.py | 28 ++- src/spaceone/config/info/user_config_info.py | 28 ++- .../config/interface/grpc/domain_config.py | 57 +++-- .../config/interface/grpc/user_config.py | 56 +++-- .../config/manager/domain_config_manager.py | 17 +- .../config/manager/user_config_manager.py | 21 +- .../config/model/domain_config_model.py | 23 +- .../config/model/user_config_model.py | 25 +- test/factory/domain_config_factory.py | 15 +- test/factory/user_config_factory.py | 17 +- test/service/test_domain_config_service.py | 207 +++++++--------- test/service/test_user_config_service.py | 229 ++++++++---------- 17 files changed, 376 insertions(+), 404 deletions(-) diff --git a/src/setup.py b/src/setup.py index 574f0bc..1e849cb 100644 --- a/src/setup.py +++ b/src/setup.py @@ -3,7 +3,7 @@ # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. -# You may obtain a copy of the License at +# You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # @@ -17,19 +17,15 @@ from setuptools import setup, find_packages setup( - name='spaceone-config', - version=os.environ.get('PACKAGE_VERSION'), - description='SpaceONE config service', - long_description='', - url='https://www.spaceone.dev/', - author='MEGAZONE SpaceONE Team', - author_email='admin@spaceone.dev', - license='Apache License 2.0', + name="spaceone-config", + version=os.environ.get("PACKAGE_VERSION"), + description="SpaceONE config service", + long_description="", + url="https://www.spaceone.dev/", + author="MEGAZONE SpaceONE Team", + author_email="admin@spaceone.dev", + license="Apache License 2.0", packages=find_packages(), - install_requires=[ - 'spaceone-core', - 'spaceone-api', - 'mongoengine' - ], + install_requires=["spaceone-core", "spaceone-api", "mongoengine"], zip_safe=False, ) diff --git a/src/spaceone/__init__.py b/src/spaceone/__init__.py index 69e3be5..8db66d3 100644 --- a/src/spaceone/__init__.py +++ b/src/spaceone/__init__.py @@ -1 +1 @@ -__path__ = __import__('pkgutil').extend_path(__path__, __name__) +__path__ = __import__("pkgutil").extend_path(__path__, __name__) diff --git a/src/spaceone/config/__init__.py b/src/spaceone/config/__init__.py index e5d1dce..dd65b02 100644 --- a/src/spaceone/config/__init__.py +++ b/src/spaceone/config/__init__.py @@ -1 +1 @@ -name = 'config' +name = "config" diff --git a/src/spaceone/config/conf/global_conf.py b/src/spaceone/config/conf/global_conf.py index 074f882..1845fb2 100644 --- a/src/spaceone/config/conf/global_conf.py +++ b/src/spaceone/config/conf/global_conf.py @@ -1,22 +1,21 @@ DATABASE_AUTO_CREATE_INDEX = True DATABASES = { - 'default': { - 'db': 'config', - 'host': 'localhost', - 'port': 27017, - 'username': '', - 'password': '' + "default": { + "db": "config", + "host": "localhost", + "port": 27017, + "username": "", + "password": "", } } CACHES = { - 'default': {}, - 'local': { - 'backend': 'spaceone.core.cache.local_cache.LocalCache', - 'max_size': 128, - 'ttl': 300 - } + "default": {}, + "local": { + "backend": "spaceone.core.cache.local_cache.LocalCache", + "max_size": 128, + "ttl": 300, + }, } -HANDLERS = { -} +HANDLERS = {} diff --git a/src/spaceone/config/info/common_info.py b/src/spaceone/config/info/common_info.py index 89bf2a3..a47f60d 100644 --- a/src/spaceone/config/info/common_info.py +++ b/src/spaceone/config/info/common_info.py @@ -1,7 +1,7 @@ from google.protobuf.empty_pb2 import Empty from spaceone.core.pygrpc.message_type import * -__all__ = ['EmptyInfo', 'StatisticsInfo'] +__all__ = ["EmptyInfo", "StatisticsInfo"] def EmptyInfo(): diff --git a/src/spaceone/config/info/domain_config_info.py b/src/spaceone/config/info/domain_config_info.py index f5281dd..e5ad9c3 100644 --- a/src/spaceone/config/info/domain_config_info.py +++ b/src/spaceone/config/info/domain_config_info.py @@ -4,26 +4,32 @@ from spaceone.core import utils from spaceone.config.model.domain_config_model import DomainConfig -__all__ = ['DomainConfigInfo', 'DomainConfigsInfo'] +__all__ = ["DomainConfigInfo", "DomainConfigsInfo"] def DomainConfigInfo(domain_config_vo: DomainConfig, minimal=False): info = { - 'name': domain_config_vo.name, + "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) - }) + 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) + 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 index e3bdffb..0dee9ca 100644 --- a/src/spaceone/config/info/user_config_info.py +++ b/src/spaceone/config/info/user_config_info.py @@ -4,27 +4,31 @@ from spaceone.core import utils from spaceone.config.model.user_config_model import UserConfig -__all__ = ['UserConfigInfo', 'UserConfigsInfo'] +__all__ = ["UserConfigInfo", "UserConfigsInfo"] def UserConfigInfo(user_config_vo: UserConfig, minimal=False): info = { - 'name': user_config_vo.name, + "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) - }) + 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) + 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/domain_config.py b/src/spaceone/config/interface/grpc/domain_config.py index d2541a9..d8690e1 100644 --- a/src/spaceone/config/interface/grpc/domain_config.py +++ b/src/spaceone/config/interface/grpc/domain_config.py @@ -6,51 +6,78 @@ class DomainConfig(BaseAPI, domain_config_pb2_grpc.DomainConfigServicer): - pb2 = domain_config_pb2 pb2_grpc = domain_config_pb2_grpc 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)) + with self.locator.get_service( + DomainConfigService, metadata + ) as domain_config_service: + return self.locator.get_info( + DomainConfigInfo, domain_config_service.create(params) + ) 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)) + with self.locator.get_service( + DomainConfigService, metadata + ) as domain_config_service: + return self.locator.get_info( + DomainConfigInfo, domain_config_service.update(params) + ) 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)) + with self.locator.get_service( + DomainConfigService, metadata + ) as domain_config_service: + return self.locator.get_info( + DomainConfigInfo, domain_config_service.set(params) + ) def delete(self, request, context): params, metadata = self.parse_request(request, context) - with self.locator.get_service(DomainConfigService, metadata) as domain_config_service: + with self.locator.get_service( + DomainConfigService, metadata + ) as domain_config_service: domain_config_service.delete(params) return self.locator.get_info(EmptyInfo) 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)) + with self.locator.get_service( + DomainConfigService, metadata + ) as domain_config_service: + return self.locator.get_info( + DomainConfigInfo, domain_config_service.get(params) + ) def list(self, request, context): params, metadata = self.parse_request(request, context) - with self.locator.get_service(DomainConfigService, metadata) as domain_config_service: + 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)) + 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)) + with self.locator.get_service( + DomainConfigService, metadata + ) as domain_config_service: + return self.locator.get_info( + StatisticsInfo, domain_config_service.stat(params) + ) diff --git a/src/spaceone/config/interface/grpc/user_config.py b/src/spaceone/config/interface/grpc/user_config.py index d94f7ca..4d49002 100644 --- a/src/spaceone/config/interface/grpc/user_config.py +++ b/src/spaceone/config/interface/grpc/user_config.py @@ -12,44 +12,72 @@ 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)) + with self.locator.get_service( + UserConfigService, metadata + ) as user_config_service: + return self.locator.get_info( + UserConfigInfo, user_config_service.create(params) + ) 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)) + with self.locator.get_service( + UserConfigService, metadata + ) as user_config_service: + return self.locator.get_info( + UserConfigInfo, user_config_service.update(params) + ) 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)) + with self.locator.get_service( + UserConfigService, metadata + ) as user_config_service: + return self.locator.get_info( + UserConfigInfo, user_config_service.set(params) + ) def delete(self, request, context): params, metadata = self.parse_request(request, context) - with self.locator.get_service(UserConfigService, metadata) as user_config_service: + with self.locator.get_service( + UserConfigService, metadata + ) as user_config_service: user_config_service.delete(params) return self.locator.get_info(EmptyInfo) 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)) + with self.locator.get_service( + UserConfigService, metadata + ) as user_config_service: + return self.locator.get_info( + UserConfigInfo, user_config_service.get(params) + ) def list(self, request, context): params, metadata = self.parse_request(request, context) - with self.locator.get_service(UserConfigService, metadata) as user_config_service: + 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)) + 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)) + with self.locator.get_service( + UserConfigService, metadata + ) as user_config_service: + return self.locator.get_info( + StatisticsInfo, user_config_service.stat(params) + ) diff --git a/src/spaceone/config/manager/domain_config_manager.py b/src/spaceone/config/manager/domain_config_manager.py index abdc43d..1311c18 100644 --- a/src/spaceone/config/manager/domain_config_manager.py +++ b/src/spaceone/config/manager/domain_config_manager.py @@ -7,15 +7,16 @@ 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 = self.locator.get_model("DomainConfig") def create_domain_config(self, params): def _rollback(domain_config_vo): - _LOGGER.info(f'[create_domain_config._rollback] ' - f'Delete domain config : {domain_config_vo.name}') + _LOGGER.info( + f"[create_domain_config._rollback] " + f"Delete domain config : {domain_config_vo.name}" + ) domain_config_vo.delete() domain_config_vo: DomainConfig = self.domain_config_model.create(params) @@ -24,12 +25,16 @@ def _rollback(domain_config_vo): return domain_config_vo def update_domain_config(self, params): - domain_config_vo: DomainConfig = self.get_domain_config(params['name'], params['domain_id']) + 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, domain_config_vo): def _rollback(old_data): - _LOGGER.info(f'[update_domain_config_by_vo._rollback] Revert Data : {old_data["name"]}') + _LOGGER.info( + f'[update_domain_config_by_vo._rollback] Revert Data : {old_data["name"]}' + ) domain_config_vo.update(old_data) self.transaction.add_rollback(_rollback, domain_config_vo.to_dict()) diff --git a/src/spaceone/config/manager/user_config_manager.py b/src/spaceone/config/manager/user_config_manager.py index ed612ee..a8703fd 100644 --- a/src/spaceone/config/manager/user_config_manager.py +++ b/src/spaceone/config/manager/user_config_manager.py @@ -7,15 +7,16 @@ 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 = self.locator.get_model("UserConfig") def create_user_config(self, params): def _rollback(user_config_vo): - _LOGGER.info(f'[create_user_config._rollback] ' - f'Delete config map : {user_config_vo.name}') + _LOGGER.info( + f"[create_user_config._rollback] " + f"Delete config map : {user_config_vo.name}" + ) user_config_vo.delete() user_config_vo: UserConfig = self.user_config_model.create(params) @@ -24,12 +25,16 @@ def _rollback(user_config_vo): return user_config_vo def update_user_config(self, params): - user_config_vo: UserConfig = self.get_user_config(params['name'], params['user_id'], params['domain_id']) + 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, user_config_vo): def _rollback(old_data): - _LOGGER.info(f'[update_user_config_by_vo._rollback] Revert Data : {old_data["name"]}') + _LOGGER.info( + f'[update_user_config_by_vo._rollback] Revert Data : {old_data["name"]}' + ) user_config_vo.update(old_data) self.transaction.add_rollback(_rollback, user_config_vo.to_dict()) @@ -41,7 +46,9 @@ def delete_user_config(self, name, user_id, domain_id): user_config_vo.delete() def get_user_config(self, name, user_id, domain_id, only=None): - return self.user_config_model.get(name=name, user_id=user_id, domain_id=domain_id, only=only) + return self.user_config_model.get( + name=name, user_id=user_id, domain_id=domain_id, only=only + ) def filter_user_configs(self, **conditions): return self.user_config_model.filter(**conditions) diff --git a/src/spaceone/config/model/domain_config_model.py b/src/spaceone/config/model/domain_config_model.py index dc9ef80..efd2e18 100644 --- a/src/spaceone/config/model/domain_config_model.py +++ b/src/spaceone/config/model/domain_config_model.py @@ -9,7 +9,7 @@ class DomainConfigTag(EmbeddedDocument): class DomainConfig(MongoModel): - name = StringField(max_length=255, unique_with='domain_id') + name = StringField(max_length=255, unique_with="domain_id") data = DictField() tags = DictField() domain_id = StringField(max_length=40) @@ -17,20 +17,11 @@ class DomainConfig(MongoModel): updated_at = DateTimeField(auto_now=True) meta = { - 'updatable_fields': [ - 'name', - 'data', - 'tags', - 'updated_at' + "updatable_fields": ["name", "data", "tags", "updated_at"], + "minimal_fields": ["name"], + "ordering": ["name"], + "indexes": [ + "name", + "domain_id", ], - 'minimal_fields': [ - 'name' - ], - 'ordering': [ - 'name' - ], - 'indexes': [ - 'name', - 'domain_id', - ] } diff --git a/src/spaceone/config/model/user_config_model.py b/src/spaceone/config/model/user_config_model.py index fb94910..fb446e1 100644 --- a/src/spaceone/config/model/user_config_model.py +++ b/src/spaceone/config/model/user_config_model.py @@ -4,7 +4,7 @@ class UserConfig(MongoModel): - name = StringField(max_length=255, unique_with=['user_id', 'domain_id']) + name = StringField(max_length=255, unique_with=["user_id", "domain_id"]) data = DictField() tags = DictField() user_id = StringField(max_length=40) @@ -13,21 +13,12 @@ class UserConfig(MongoModel): updated_at = DateTimeField(auto_now=True) meta = { - 'updatable_fields': [ - 'name', - 'data', - 'tags', - 'updated_at' + "updatable_fields": ["name", "data", "tags", "updated_at"], + "minimal_fields": ["name"], + "ordering": ["name"], + "indexes": [ + "name", + "user_id", + "domain_id", ], - 'minimal_fields': [ - 'name' - ], - 'ordering': [ - 'name' - ], - 'indexes': [ - 'name', - 'user_id', - 'domain_id', - ] } diff --git a/test/factory/domain_config_factory.py b/test/factory/domain_config_factory.py index 1046680..ca771f4 100644 --- a/test/factory/domain_config_factory.py +++ b/test/factory/domain_config_factory.py @@ -9,13 +9,8 @@ class Meta: model = DomainConfig name = factory.LazyAttribute(lambda o: utils.random_string()) - data = { - 'key': 'value', - 'key2': { - 'key3': 'value3' - } - } - tags = {'tag_key': 'tag_value'} - domain_id = utils.generate_id('domain') - updated_at = factory.Faker('date_time') - created_at = factory.Faker('date_time') + data = {"key": "value", "key2": {"key3": "value3"}} + tags = {"tag_key": "tag_value"} + domain_id = utils.generate_id("domain") + updated_at = factory.Faker("date_time") + created_at = factory.Faker("date_time") diff --git a/test/factory/user_config_factory.py b/test/factory/user_config_factory.py index e2a2f48..2af47c6 100644 --- a/test/factory/user_config_factory.py +++ b/test/factory/user_config_factory.py @@ -9,14 +9,9 @@ class Meta: model = UserConfig name = factory.LazyAttribute(lambda o: utils.random_string()) - data = { - 'key': 'value', - 'key2': { - 'key3': 'value3' - } - } - tags = {'tag_key': 'tag_value'} - domain_id = utils.generate_id('domain') - user_id = utils.generate_id('user') - updated_at = factory.Faker('date_time') - created_at = factory.Faker('date_time') + data = {"key": "value", "key2": {"key3": "value3"}} + tags = {"tag_key": "tag_value"} + domain_id = utils.generate_id("domain") + user_id = utils.generate_id("user") + updated_at = factory.Faker("date_time") + created_at = factory.Faker("date_time") diff --git a/test/service/test_domain_config_service.py b/test/service/test_domain_config_service.py index 91f82b6..29a0f82 100644 --- a/test/service/test_domain_config_service.py +++ b/test/service/test_domain_config_service.py @@ -16,17 +16,15 @@ class TestDomainConfigService(unittest.TestCase): - @classmethod def setUpClass(cls): - config.init_conf(package='spaceone.config') - connect('test', host='mongomock://localhost') - - cls.domain_id = utils.generate_id('domain') - cls.transaction = Transaction({ - 'service': 'config', - 'api_class': 'DomainConfig' - }) + config.init_conf(package="spaceone.config") + connect("test", host="mongomock://localhost") + + cls.domain_id = utils.generate_id("domain") + cls.transaction = Transaction( + {"service": "config", "api_class": "DomainConfig"} + ) super().setUpClass() @classmethod @@ -34,136 +32,117 @@ def tearDownClass(cls) -> None: super().tearDownClass() disconnect() - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def tearDown(self, *args) -> None: print() - print('(tearDown) ==> Delete all config maps') + print("(tearDown) ==> Delete all config maps") domain_config_vos = DomainConfig.objects.filter() domain_config_vos.delete() - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_create_domain_config(self, *args): params = { - 'name': 'inventory.server.metadata.view.table.layout', - 'data': { - 'key': 'value' - }, - 'tags': { - utils.random_string(): utils.random_string() - }, - 'domain_id': utils.generate_id('domain') + "name": "inventory.server.metadata.view.table.layout", + "data": {"key": "value"}, + "tags": {utils.random_string(): utils.random_string()}, + "domain_id": utils.generate_id("domain"), } - self.transaction.method = 'create' + self.transaction.method = "create" domain_config_svc = DomainConfigService(transaction=self.transaction) domain_config_vo = domain_config_svc.create(params.copy()) - print_data(domain_config_vo.to_dict(), 'test_create_domain_config') + print_data(domain_config_vo.to_dict(), "test_create_domain_config") DomainConfigInfo(domain_config_vo) self.assertIsInstance(domain_config_vo, DomainConfig) - self.assertEqual(params['name'], domain_config_vo.name) - self.assertEqual(params['data'], domain_config_vo.data) - self.assertEqual(params['tags'], domain_config_vo.tags) - self.assertEqual(params['domain_id'], domain_config_vo.domain_id) + self.assertEqual(params["name"], domain_config_vo.name) + self.assertEqual(params["data"], domain_config_vo.data) + self.assertEqual(params["tags"], domain_config_vo.tags) + self.assertEqual(params["domain_id"], domain_config_vo.domain_id) - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_update_domain_config(self, *args): new_domain_config_vo = DomainConfigFactory(domain_id=self.domain_id) params = { - 'name': new_domain_config_vo.name, - 'data': { - 'update_data_key': 'update_data_value' - }, - 'tags': { - 'update_key': 'update_value' - }, - 'domain_id': self.domain_id + "name": new_domain_config_vo.name, + "data": {"update_data_key": "update_data_value"}, + "tags": {"update_key": "update_value"}, + "domain_id": self.domain_id, } - self.transaction.method = 'update' + self.transaction.method = "update" domain_config_svc = DomainConfigService(transaction=self.transaction) domain_config_vo = domain_config_svc.update(params.copy()) - print_data(domain_config_vo.to_dict(), 'test_update_domain_config') + print_data(domain_config_vo.to_dict(), "test_update_domain_config") DomainConfigInfo(domain_config_vo) self.assertIsInstance(domain_config_vo, DomainConfig) - self.assertEqual(params['data'], domain_config_vo.data) - self.assertEqual(params['tags'], domain_config_vo.tags) - self.assertEqual(params['domain_id'], domain_config_vo.domain_id) + self.assertEqual(params["data"], domain_config_vo.data) + self.assertEqual(params["tags"], domain_config_vo.tags) + self.assertEqual(params["domain_id"], domain_config_vo.domain_id) - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_set_domain_config(self, *args): params = { - 'name': 'inventory.server.metadata.view.table.layout', - 'data': { - 'key': 'value' - }, - 'tags': { - utils.random_string(): utils.random_string() - }, - 'domain_id': utils.generate_id('domain') + "name": "inventory.server.metadata.view.table.layout", + "data": {"key": "value"}, + "tags": {utils.random_string(): utils.random_string()}, + "domain_id": utils.generate_id("domain"), } - self.transaction.method = 'set' + self.transaction.method = "set" domain_config_svc = DomainConfigService(transaction=self.transaction) domain_config_vo = domain_config_svc.create(params.copy()) - print_data(domain_config_vo.to_dict(), 'test_set_domain_config') + print_data(domain_config_vo.to_dict(), "test_set_domain_config") DomainConfigInfo(domain_config_vo) self.assertIsInstance(domain_config_vo, DomainConfig) - self.assertEqual(params['name'], domain_config_vo.name) - self.assertEqual(params['data'], domain_config_vo.data) - self.assertEqual(params['tags'], domain_config_vo.tags) - self.assertEqual(params['domain_id'], domain_config_vo.domain_id) + self.assertEqual(params["name"], domain_config_vo.name) + self.assertEqual(params["data"], domain_config_vo.data) + self.assertEqual(params["tags"], domain_config_vo.tags) + self.assertEqual(params["domain_id"], domain_config_vo.domain_id) - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_delete_domain_config(self, *args): new_domain_config_vo = DomainConfigFactory(domain_id=self.domain_id) - params = { - 'name': new_domain_config_vo.name, - 'domain_id': self.domain_id - } + params = {"name": new_domain_config_vo.name, "domain_id": self.domain_id} - self.transaction.method = 'delete' + self.transaction.method = "delete" domain_config_svc = DomainConfigService(transaction=self.transaction) result = domain_config_svc.delete(params.copy()) self.assertIsNone(result) - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_get_domain_config(self, *args): new_domain_config_vo = DomainConfigFactory(domain_id=self.domain_id) - params = { - 'name': new_domain_config_vo.name, - 'domain_id': self.domain_id - } + params = {"name": new_domain_config_vo.name, "domain_id": self.domain_id} - self.transaction.method = 'get' + self.transaction.method = "get" domain_config_svc = DomainConfigService(transaction=self.transaction) domain_config_vo = domain_config_svc.get(params.copy()) - print_data(domain_config_vo.to_dict(), 'test_get_domain_config') + print_data(domain_config_vo.to_dict(), "test_get_domain_config") DomainConfigInfo(domain_config_vo) self.assertIsInstance(domain_config_vo, DomainConfig) - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_list_domain_configs_by_name(self, *args): - domain_config_vos = DomainConfigFactory.build_batch(10, domain_id=self.domain_id) + domain_config_vos = DomainConfigFactory.build_batch( + 10, domain_id=self.domain_id + ) list(map(lambda vo: vo.save(), domain_config_vos)) - params = { - 'name': domain_config_vos[0].name, - 'domain_id': self.domain_id - } + params = {"name": domain_config_vos[0].name, "domain_id": self.domain_id} - self.transaction.method = 'list' + self.transaction.method = "list" domain_config_svc = DomainConfigService(transaction=self.transaction) domain_config_vos, total_count = domain_config_svc.list(params.copy()) DomainConfigsInfo(domain_config_vos, total_count) @@ -172,24 +151,20 @@ def test_list_domain_configs_by_name(self, *args): self.assertIsInstance(domain_config_vos[0], DomainConfig) self.assertEqual(total_count, 1) - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_list_domain_configs_by_tag(self, *args): - DomainConfigFactory(tags={'tag_key_1': 'tag_value_1'}, domain_id=self.domain_id) + DomainConfigFactory(tags={"tag_key_1": "tag_value_1"}, domain_id=self.domain_id) domain_config_vos = DomainConfigFactory.build_batch(9, domain_id=self.domain_id) list(map(lambda vo: vo.save(), domain_config_vos)) params = { - 'query': { - 'filter': [{ - 'k': 'tags.tag_key_1', - 'v': 'tag_value_1', - 'o': 'eq' - }] + "query": { + "filter": [{"k": "tags.tag_key_1", "v": "tag_value_1", "o": "eq"}] }, - 'domain_id': self.domain_id + "domain_id": self.domain_id, } - self.transaction.method = 'list' + self.transaction.method = "list" domain_config_svc = DomainConfigService(transaction=self.transaction) domain_config_vos, total_count = domain_config_svc.list(params.copy()) DomainConfigsInfo(domain_config_vos, total_count) @@ -198,63 +173,53 @@ def test_list_domain_configs_by_tag(self, *args): self.assertIsInstance(domain_config_vos[0], DomainConfig) self.assertEqual(total_count, 1) - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_stat_domain_configs(self, *args): - domain_config_vos = DomainConfigFactory.build_batch(10, domain_id=self.domain_id) + domain_config_vos = DomainConfigFactory.build_batch( + 10, domain_id=self.domain_id + ) list(map(lambda vo: vo.save(), domain_config_vos)) params = { - 'domain_id': self.domain_id, - 'query': { - 'aggregate': [{ - 'group': { - 'keys': [{ - 'key': 'name', - 'name': 'Name' - }], - 'fields': [{ - 'operator': 'count', - 'name': 'Count' - }] - } - }, { - 'sort': { - 'key': 'Count', - 'desc': True - } - }] - } + "domain_id": self.domain_id, + "query": { + "aggregate": [ + { + "group": { + "keys": [{"key": "name", "name": "Name"}], + "fields": [{"operator": "count", "name": "Count"}], + } + }, + {"sort": {"key": "Count", "desc": True}}, + ] + }, } - self.transaction.method = 'stat' + self.transaction.method = "stat" domain_config_svc = DomainConfigService(transaction=self.transaction) values = domain_config_svc.stat(params) StatisticsInfo(values) - print_data(values, 'test_stat_domain_configs') + print_data(values, "test_stat_domain_configs") - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_stat_domain_configs_distinct(self, *args): - domain_config_vos = DomainConfigFactory.build_batch(10, domain_id=self.domain_id) + domain_config_vos = DomainConfigFactory.build_batch( + 10, domain_id=self.domain_id + ) list(map(lambda vo: vo.save(), domain_config_vos)) params = { - 'domain_id': self.domain_id, - 'query': { - 'distinct': 'name', - 'page': { - 'start': 2, - 'limit': 3 - } - } + "domain_id": self.domain_id, + "query": {"distinct": "name", "page": {"start": 2, "limit": 3}}, } - self.transaction.method = 'stat' + self.transaction.method = "stat" domain_config_svc = DomainConfigService(transaction=self.transaction) values = domain_config_svc.stat(params) StatisticsInfo(values) - print_data(values, 'test_stat_domain_configs_distinct') + print_data(values, "test_stat_domain_configs_distinct") if __name__ == "__main__": diff --git a/test/service/test_user_config_service.py b/test/service/test_user_config_service.py index 14f9cbb..2b0af1a 100644 --- a/test/service/test_user_config_service.py +++ b/test/service/test_user_config_service.py @@ -16,18 +16,14 @@ class TestUserConfigService(unittest.TestCase): - @classmethod def setUpClass(cls): - config.init_conf(package='spaceone.config') - connect('test', host='mongomock://localhost') - - cls.domain_id = utils.generate_id('domain') - cls.user_id = utils.generate_id('user') - cls.transaction = Transaction({ - 'service': 'config', - 'api_class': 'UserConfig' - }) + config.init_conf(package="spaceone.config") + connect("test", host="mongomock://localhost") + + cls.domain_id = utils.generate_id("domain") + cls.user_id = utils.generate_id("user") + cls.transaction = Transaction({"service": "config", "api_class": "UserConfig"}) super().setUpClass() @classmethod @@ -35,147 +31,132 @@ def tearDownClass(cls) -> None: super().tearDownClass() disconnect() - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def tearDown(self, *args) -> None: print() - print('(tearDown) ==> Delete all config maps') + print("(tearDown) ==> Delete all config maps") user_config_vos = UserConfig.objects.filter() user_config_vos.delete() - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_create_user_config(self, *args): params = { - 'name': 'inventory.server.metadata.view.table.layout', - 'data': { - 'key': 'value' - }, - 'tags': { - utils.random_string(): utils.random_string() - }, - 'domain_id': self.domain_id + "name": "inventory.server.metadata.view.table.layout", + "data": {"key": "value"}, + "tags": {utils.random_string(): utils.random_string()}, + "domain_id": self.domain_id, } - self.transaction.method = 'create' - self.transaction.set_meta('user_id', self.user_id) - self.transaction.set_meta('domain_id', self.domain_id) + self.transaction.method = "create" + self.transaction.set_meta("user_id", self.user_id) + self.transaction.set_meta("domain_id", self.domain_id) user_config_svc = UserConfigService(transaction=self.transaction) user_config_vo = user_config_svc.create(params.copy()) - print_data(user_config_vo.to_dict(), 'test_create_user_config') + print_data(user_config_vo.to_dict(), "test_create_user_config") UserConfigInfo(user_config_vo) self.assertIsInstance(user_config_vo, UserConfig) - self.assertEqual(params['name'], user_config_vo.name) - self.assertEqual(params['data'], user_config_vo.data) - self.assertEqual(params['tags'], user_config_vo.tags) - self.assertEqual(params['domain_id'], user_config_vo.domain_id) + self.assertEqual(params["name"], user_config_vo.name) + self.assertEqual(params["data"], user_config_vo.data) + self.assertEqual(params["tags"], user_config_vo.tags) + self.assertEqual(params["domain_id"], user_config_vo.domain_id) - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_update_user_config(self, *args): - new_user_config_vo = UserConfigFactory(domain_id=self.domain_id, user_id=self.user_id) + new_user_config_vo = UserConfigFactory( + domain_id=self.domain_id, user_id=self.user_id + ) params = { - 'name': new_user_config_vo.name, - 'data': { - 'update_data_key': 'update_data_value' - }, - 'tags': { - 'update_key': 'update_value' - }, - 'domain_id': self.domain_id + "name": new_user_config_vo.name, + "data": {"update_data_key": "update_data_value"}, + "tags": {"update_key": "update_value"}, + "domain_id": self.domain_id, } - self.transaction.method = 'update' - self.transaction.set_meta('user_id', self.user_id) - self.transaction.set_meta('domain_id', self.domain_id) + self.transaction.method = "update" + self.transaction.set_meta("user_id", self.user_id) + self.transaction.set_meta("domain_id", self.domain_id) user_config_svc = UserConfigService(transaction=self.transaction) user_config_vo = user_config_svc.update(params.copy()) - print_data(user_config_vo.to_dict(), 'test_update_user_config') + print_data(user_config_vo.to_dict(), "test_update_user_config") UserConfigInfo(user_config_vo) self.assertIsInstance(user_config_vo, UserConfig) - self.assertEqual(params['data'], user_config_vo.data) - self.assertEqual(params['tags'], user_config_vo.tags) - self.assertEqual(params['domain_id'], user_config_vo.domain_id) + self.assertEqual(params["data"], user_config_vo.data) + self.assertEqual(params["tags"], user_config_vo.tags) + self.assertEqual(params["domain_id"], user_config_vo.domain_id) - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_set_user_config(self, *args): params = { - 'name': 'inventory.server.metadata.view.table.layout', - 'data': { - 'key': 'value' - }, - 'tags': { - utils.random_string(): utils.random_string() - }, - 'domain_id': self.domain_id + "name": "inventory.server.metadata.view.table.layout", + "data": {"key": "value"}, + "tags": {utils.random_string(): utils.random_string()}, + "domain_id": self.domain_id, } - self.transaction.method = 'set' - self.transaction.set_meta('user_id', self.user_id) - self.transaction.set_meta('domain_id', self.domain_id) + self.transaction.method = "set" + self.transaction.set_meta("user_id", self.user_id) + self.transaction.set_meta("domain_id", self.domain_id) user_config_svc = UserConfigService(transaction=self.transaction) user_config_vo = user_config_svc.create(params.copy()) - print_data(user_config_vo.to_dict(), 'test_set_user_config') + print_data(user_config_vo.to_dict(), "test_set_user_config") UserConfigInfo(user_config_vo) self.assertIsInstance(user_config_vo, UserConfig) - self.assertEqual(params['name'], user_config_vo.name) - self.assertEqual(params['data'], user_config_vo.data) - self.assertEqual(params['tags'], user_config_vo.tags) - self.assertEqual(params['domain_id'], user_config_vo.domain_id) + self.assertEqual(params["name"], user_config_vo.name) + self.assertEqual(params["data"], user_config_vo.data) + self.assertEqual(params["tags"], user_config_vo.tags) + self.assertEqual(params["domain_id"], user_config_vo.domain_id) - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_delete_user_config(self, *args): - new_user_config_vo = UserConfigFactory(domain_id=self.domain_id, user_id=self.user_id) + new_user_config_vo = UserConfigFactory( + domain_id=self.domain_id, user_id=self.user_id + ) - params = { - 'name': new_user_config_vo.name, - 'domain_id': self.domain_id - } + params = {"name": new_user_config_vo.name, "domain_id": self.domain_id} - self.transaction.method = 'delete' - self.transaction.set_meta('user_id', self.user_id) - self.transaction.set_meta('domain_id', self.domain_id) + self.transaction.method = "delete" + self.transaction.set_meta("user_id", self.user_id) + self.transaction.set_meta("domain_id", self.domain_id) user_config_svc = UserConfigService(transaction=self.transaction) result = user_config_svc.delete(params.copy()) self.assertIsNone(result) - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_get_user_config(self, *args): - new_user_config_vo = UserConfigFactory(domain_id=self.domain_id, user_id=self.user_id) + new_user_config_vo = UserConfigFactory( + domain_id=self.domain_id, user_id=self.user_id + ) - params = { - 'name': new_user_config_vo.name, - 'domain_id': self.domain_id - } + params = {"name": new_user_config_vo.name, "domain_id": self.domain_id} - self.transaction.method = 'get' - self.transaction.set_meta('user_id', self.user_id) - self.transaction.set_meta('domain_id', self.domain_id) + self.transaction.method = "get" + self.transaction.set_meta("user_id", self.user_id) + self.transaction.set_meta("domain_id", self.domain_id) user_config_svc = UserConfigService(transaction=self.transaction) user_config_vo = user_config_svc.get(params.copy()) - print_data(user_config_vo.to_dict(), 'test_get_user_config') + print_data(user_config_vo.to_dict(), "test_get_user_config") UserConfigInfo(user_config_vo) self.assertIsInstance(user_config_vo, UserConfig) - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_list_user_configs_by_name(self, *args): user_config_vos = UserConfigFactory.build_batch(10, domain_id=self.domain_id) list(map(lambda vo: vo.save(), user_config_vos)) - params = { - 'name': user_config_vos[0].name, - 'domain_id': self.domain_id - } + params = {"name": user_config_vos[0].name, "domain_id": self.domain_id} - self.transaction.method = 'list' - self.transaction.set_meta('user_id', 'test_user') + self.transaction.method = "list" + self.transaction.set_meta("user_id", "test_user") user_config_svc = UserConfigService(transaction=self.transaction) user_config_vos, total_count = user_config_svc.list(params.copy()) UserConfigsInfo(user_config_vos, total_count) @@ -184,24 +165,20 @@ def test_list_user_configs_by_name(self, *args): self.assertIsInstance(user_config_vos[0], UserConfig) self.assertEqual(total_count, 1) - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_list_user_configs_by_tag(self, *args): - UserConfigFactory(tags={'tag_key_1': 'tag_value_1'}, domain_id=self.domain_id) + UserConfigFactory(tags={"tag_key_1": "tag_value_1"}, domain_id=self.domain_id) user_config_vos = UserConfigFactory.build_batch(9, domain_id=self.domain_id) list(map(lambda vo: vo.save(), user_config_vos)) params = { - 'query': { - 'filter': [{ - 'k': 'tags.tag_key_1', - 'v': 'tag_value_1', - 'o': 'eq' - }] + "query": { + "filter": [{"k": "tags.tag_key_1", "v": "tag_value_1", "o": "eq"}] }, - 'domain_id': self.domain_id + "domain_id": self.domain_id, } - self.transaction.method = 'list' + self.transaction.method = "list" user_config_svc = UserConfigService(transaction=self.transaction) user_config_vos, total_count = user_config_svc.list(params.copy()) UserConfigsInfo(user_config_vos, total_count) @@ -210,63 +187,49 @@ def test_list_user_configs_by_tag(self, *args): self.assertIsInstance(user_config_vos[0], UserConfig) self.assertEqual(total_count, 1) - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_stat_user_configs(self, *args): user_config_vos = UserConfigFactory.build_batch(10, domain_id=self.domain_id) list(map(lambda vo: vo.save(), user_config_vos)) params = { - 'domain_id': self.domain_id, - 'query': { - 'aggregate': [{ - 'group': { - 'keys': [{ - 'key': 'name', - 'name': 'Name' - }], - 'fields': [{ - 'operator': 'count', - 'name': 'Count' - }] - } - }, { - 'sort': { - 'key': 'Count', - 'desc': True - } - }] - } + "domain_id": self.domain_id, + "query": { + "aggregate": [ + { + "group": { + "keys": [{"key": "name", "name": "Name"}], + "fields": [{"operator": "count", "name": "Count"}], + } + }, + {"sort": {"key": "Count", "desc": True}}, + ] + }, } - self.transaction.method = 'stat' + self.transaction.method = "stat" user_config_svc = UserConfigService(transaction=self.transaction) values = user_config_svc.stat(params) StatisticsInfo(values) - print_data(values, 'test_stat_user_configs') + print_data(values, "test_stat_user_configs") - @patch.object(MongoModel, 'connect', return_value=None) + @patch.object(MongoModel, "connect", return_value=None) def test_stat_user_configs_distinct(self, *args): user_config_vos = UserConfigFactory.build_batch(10, domain_id=self.domain_id) list(map(lambda vo: vo.save(), user_config_vos)) params = { - 'domain_id': self.domain_id, - 'query': { - 'distinct': 'name', - 'page': { - 'start': 2, - 'limit': 3 - } - } + "domain_id": self.domain_id, + "query": {"distinct": "name", "page": {"start": 2, "limit": 3}}, } - self.transaction.method = 'stat' + self.transaction.method = "stat" user_config_svc = UserConfigService(transaction=self.transaction) values = user_config_svc.stat(params) StatisticsInfo(values) - print_data(values, 'test_stat_user_configs_distinct') + print_data(values, "test_stat_user_configs_distinct") if __name__ == "__main__": From 71a0c97d46aa48dac07675cdb6295c4fcc5fd959 Mon Sep 17 00:00:00 2001 From: seolmin Date: Tue, 12 Dec 2023 12:11:54 +0900 Subject: [PATCH 5/6] fix: fix typo --- test/api/test_domain_config_api.py | 152 ++++++++++++++-------------- test/api/test_user_config_api.py | 153 ++++++++++++++--------------- 2 files changed, 144 insertions(+), 161 deletions(-) diff --git a/test/api/test_domain_config_api.py b/test/api/test_domain_config_api.py index d3d9a29..1e72c3f 100644 --- a/test/api/test_domain_config_api.py +++ b/test/api/test_domain_config_api.py @@ -13,12 +13,11 @@ from spaceone.core.locator import Locator from spaceone.core.pygrpc import BaseAPI from spaceone.api.config.v1 import domain_config_pb2 -from spaceone.config.api.v1.domain_config import DomainConfig +from spaceone.config.interface.grpc.domain_config import DomainConfig from test.factory.domain_config_factory import DomainConfigFactory class _MockDomainConfigService(BaseService): - def create(self, params): params = copy.deepcopy(params) @@ -44,17 +43,14 @@ def list(self, params): return DomainConfigFactory.build_batch(10, **params), 10 def stat(self, params): - return { - 'result': [{'name': utils.random_string(), 'config_count': 100}] - } + return {"result": [{"name": utils.random_string(), "config_count": 100}]} class TestDomainConfigAPI(unittest.TestCase): - @classmethod def setUpClass(cls): - config.init_conf(package='spaceone.config') - connect('test', host='mongomock://localhost') + config.init_conf(package="spaceone.config") + connect("test", host="mongomock://localhost") super().setUpClass() @classmethod @@ -62,142 +58,138 @@ def tearDownClass(cls) -> None: super().tearDownClass() disconnect() - @patch.object(BaseAPI, '__init__', return_value=None) - @patch.object(Locator, 'get_service', return_value=_MockDomainConfigService()) - @patch.object(BaseAPI, 'parse_request') + @patch.object(BaseAPI, "__init__", return_value=None) + @patch.object(Locator, "get_service", return_value=_MockDomainConfigService()) + @patch.object(BaseAPI, "parse_request") def test_create_domain_config(self, mock_parse_request, *args): params = { - 'name': utils.random_string(), - 'data': { - 'config_key': 'config_value' - }, - 'tags': { - utils.random_string(): utils.random_string() - }, - 'domain_id': utils.generate_id('domain') + "name": utils.random_string(), + "data": {"config_key": "config_value"}, + "tags": {utils.random_string(): utils.random_string()}, + "domain_id": utils.generate_id("domain"), } mock_parse_request.return_value = (params, {}) domain_config_servicer = DomainConfig() domain_config_info = domain_config_servicer.create({}, {}) - print_message(domain_config_info, 'test_create_domain_config') - domain_config_data = MessageToDict(domain_config_info, preserving_proto_field_name=True) + print_message(domain_config_info, "test_create_domain_config") + domain_config_data = MessageToDict( + domain_config_info, preserving_proto_field_name=True + ) self.assertIsInstance(domain_config_info, domain_config_pb2.DomainConfigInfo) - self.assertEqual(domain_config_info.name, params['name']) - self.assertDictEqual(MessageToDict(domain_config_info.data), params['data']) - self.assertDictEqual(domain_config_data['tags'], params['tags']) - self.assertEqual(domain_config_info.domain_id, params['domain_id']) - self.assertIsNotNone(getattr(domain_config_info, 'created_at', None)) - self.assertIsNotNone(getattr(domain_config_info, 'updated_at', None)) - - @patch.object(BaseAPI, '__init__', return_value=None) - @patch.object(Locator, 'get_service', return_value=_MockDomainConfigService()) - @patch.object(BaseAPI, 'parse_request') + self.assertEqual(domain_config_info.name, params["name"]) + self.assertDictEqual(MessageToDict(domain_config_info.data), params["data"]) + self.assertDictEqual(domain_config_data["tags"], params["tags"]) + self.assertEqual(domain_config_info.domain_id, params["domain_id"]) + self.assertIsNotNone(getattr(domain_config_info, "created_at", None)) + self.assertIsNotNone(getattr(domain_config_info, "updated_at", None)) + + @patch.object(BaseAPI, "__init__", return_value=None) + @patch.object(Locator, "get_service", return_value=_MockDomainConfigService()) + @patch.object(BaseAPI, "parse_request") def test_update_domain_config(self, mock_parse_request, *args): params = { - 'name': utils.random_string(), - 'data': { - 'update_config_key': 'update_config_value' - }, - 'tags': { - 'update_key': 'update_value' - }, - 'domain_id': utils.generate_id('domain') + "name": utils.random_string(), + "data": {"update_config_key": "update_config_value"}, + "tags": {"update_key": "update_value"}, + "domain_id": utils.generate_id("domain"), } mock_parse_request.return_value = (params, {}) domain_config_servicer = DomainConfig() domain_config_info = domain_config_servicer.update({}, {}) - print_message(domain_config_info, 'test_update_domain_config') - domain_config_data = MessageToDict(domain_config_info, preserving_proto_field_name=True) + print_message(domain_config_info, "test_update_domain_config") + domain_config_data = MessageToDict( + domain_config_info, preserving_proto_field_name=True + ) self.assertIsInstance(domain_config_info, domain_config_pb2.DomainConfigInfo) - self.assertDictEqual(MessageToDict(domain_config_info.data), params['data']) - self.assertDictEqual(domain_config_data['tags'], params['tags']) + self.assertDictEqual(MessageToDict(domain_config_info.data), params["data"]) + self.assertDictEqual(domain_config_data["tags"], params["tags"]) - @patch.object(BaseAPI, '__init__', return_value=None) - @patch.object(Locator, 'get_service', return_value=_MockDomainConfigService()) - @patch.object(BaseAPI, 'parse_request') + @patch.object(BaseAPI, "__init__", return_value=None) + @patch.object(Locator, "get_service", return_value=_MockDomainConfigService()) + @patch.object(BaseAPI, "parse_request") def test_set_domain_config(self, mock_parse_request, *args): params = { - 'name': utils.random_string(), - 'data': { - 'config_key': 'config_value' - }, - 'tags': { - utils.random_string(): utils.random_string() - }, - 'domain_id': utils.generate_id('domain') + "name": utils.random_string(), + "data": {"config_key": "config_value"}, + "tags": {utils.random_string(): utils.random_string()}, + "domain_id": utils.generate_id("domain"), } mock_parse_request.return_value = (params, {}) domain_config_servicer = DomainConfig() domain_config_info = domain_config_servicer.set({}, {}) - print_message(domain_config_info, 'test_set_domain_config') - domain_config_data = MessageToDict(domain_config_info, preserving_proto_field_name=True) + print_message(domain_config_info, "test_set_domain_config") + domain_config_data = MessageToDict( + domain_config_info, preserving_proto_field_name=True + ) self.assertIsInstance(domain_config_info, domain_config_pb2.DomainConfigInfo) - self.assertEqual(domain_config_info.name, params['name']) - self.assertDictEqual(MessageToDict(domain_config_info.data), params['data']) - self.assertDictEqual(domain_config_data['tags'], params['tags']) - self.assertEqual(domain_config_info.domain_id, params['domain_id']) - self.assertIsNotNone(getattr(domain_config_info, 'created_at', None)) - self.assertIsNotNone(getattr(domain_config_info, 'updated_at', None)) - - @patch.object(BaseAPI, '__init__', return_value=None) - @patch.object(Locator, 'get_service', return_value=_MockDomainConfigService()) - @patch.object(BaseAPI, 'parse_request') + self.assertEqual(domain_config_info.name, params["name"]) + self.assertDictEqual(MessageToDict(domain_config_info.data), params["data"]) + self.assertDictEqual(domain_config_data["tags"], params["tags"]) + self.assertEqual(domain_config_info.domain_id, params["domain_id"]) + self.assertIsNotNone(getattr(domain_config_info, "created_at", None)) + self.assertIsNotNone(getattr(domain_config_info, "updated_at", None)) + + @patch.object(BaseAPI, "__init__", return_value=None) + @patch.object(Locator, "get_service", return_value=_MockDomainConfigService()) + @patch.object(BaseAPI, "parse_request") def test_delete_domain_config(self, mock_parse_request, *args): mock_parse_request.return_value = ({}, {}) domain_config_servicer = DomainConfig() result = domain_config_servicer.delete({}, {}) - print_message(result, 'test_delete_domain_config') + print_message(result, "test_delete_domain_config") self.assertIsInstance(result, Empty) - @patch.object(BaseAPI, '__init__', return_value=None) - @patch.object(Locator, 'get_service', return_value=_MockDomainConfigService()) - @patch.object(BaseAPI, 'parse_request') + @patch.object(BaseAPI, "__init__", return_value=None) + @patch.object(Locator, "get_service", return_value=_MockDomainConfigService()) + @patch.object(BaseAPI, "parse_request") def test_get_domain_config(self, mock_parse_request, *args): mock_parse_request.return_value = ({}, {}) domain_config_servicer = DomainConfig() domain_config_info = domain_config_servicer.get({}, {}) - print_message(domain_config_info, 'test_get_domain_config') + print_message(domain_config_info, "test_get_domain_config") self.assertIsInstance(domain_config_info, domain_config_pb2.DomainConfigInfo) - @patch.object(BaseAPI, '__init__', return_value=None) - @patch.object(Locator, 'get_service', return_value=_MockDomainConfigService()) - @patch.object(BaseAPI, 'parse_request') + @patch.object(BaseAPI, "__init__", return_value=None) + @patch.object(Locator, "get_service", return_value=_MockDomainConfigService()) + @patch.object(BaseAPI, "parse_request") def test_list_domain_configs(self, mock_parse_request, *args): mock_parse_request.return_value = ({}, {}) domain_config_servicer = DomainConfig() domain_configs_info = domain_config_servicer.list({}, {}) - print_message(domain_configs_info, 'test_list_domain_configs') + print_message(domain_configs_info, "test_list_domain_configs") self.assertIsInstance(domain_configs_info, domain_config_pb2.DomainConfigsInfo) - self.assertIsInstance(domain_configs_info.results[0], domain_config_pb2.DomainConfigInfo) + self.assertIsInstance( + domain_configs_info.results[0], domain_config_pb2.DomainConfigInfo + ) self.assertEqual(domain_configs_info.total_count, 10) - @patch.object(BaseAPI, '__init__', return_value=None) - @patch.object(Locator, 'get_service', return_value=_MockDomainConfigService()) - @patch.object(BaseAPI, 'parse_request') + @patch.object(BaseAPI, "__init__", return_value=None) + @patch.object(Locator, "get_service", return_value=_MockDomainConfigService()) + @patch.object(BaseAPI, "parse_request") def test_stat_domain_configs(self, mock_parse_request, *args): mock_parse_request.return_value = ({}, {}) domain_config_servicer = DomainConfig() stat_info = domain_config_servicer.stat({}, {}) - print_message(stat_info, 'test_stat_domain_configs') + print_message(stat_info, "test_stat_domain_configs") if __name__ == "__main__": diff --git a/test/api/test_user_config_api.py b/test/api/test_user_config_api.py index 8df48a4..6e62ed4 100644 --- a/test/api/test_user_config_api.py +++ b/test/api/test_user_config_api.py @@ -13,19 +13,17 @@ from spaceone.core.locator import Locator from spaceone.core.pygrpc import BaseAPI from spaceone.api.config.v1 import user_config_pb2 -from spaceone.config.api.v1.user_config import UserConfig +from spaceone.config.interface.grpc.user_config import UserConfig from test.factory.user_config_factory import UserConfigFactory class _MockUserConfigService(BaseService): - def create(self, params): params = copy.deepcopy(params) return UserConfigFactory(**params) def update(self, params): - params = copy.deepcopy(params) return UserConfigFactory(**params) @@ -45,17 +43,14 @@ def list(self, params): return UserConfigFactory.build_batch(10, **params), 10 def stat(self, params): - return { - 'result': [{'name': utils.random_string(), 'config_count': 100}] - } + return {"result": [{"name": utils.random_string(), "config_count": 100}]} class TestUserConfigAPI(unittest.TestCase): - @classmethod def setUpClass(cls): - config.init_conf(package='spaceone.config') - connect('test', host='mongomock://localhost') + config.init_conf(package="spaceone.config") + connect("test", host="mongomock://localhost") super().setUpClass() @classmethod @@ -63,142 +58,138 @@ def tearDownClass(cls) -> None: super().tearDownClass() disconnect() - @patch.object(BaseAPI, '__init__', return_value=None) - @patch.object(Locator, 'get_service', return_value=_MockUserConfigService()) - @patch.object(BaseAPI, 'parse_request') + @patch.object(BaseAPI, "__init__", return_value=None) + @patch.object(Locator, "get_service", return_value=_MockUserConfigService()) + @patch.object(BaseAPI, "parse_request") def test_create_user_config(self, mock_parse_request, *args): params = { - 'name': utils.random_string(), - 'data': { - 'config_key': 'config_value' - }, - 'tags': { - utils.random_string(): utils.random_string() - }, - 'domain_id': utils.generate_id('domain') + "name": utils.random_string(), + "data": {"config_key": "config_value"}, + "tags": {utils.random_string(): utils.random_string()}, + "domain_id": utils.generate_id("domain"), } mock_parse_request.return_value = (params, {}) user_config_servicer = UserConfig() user_config_info = user_config_servicer.create({}, {}) - print_message(user_config_info, 'test_create_user_config') - user_config_data = MessageToDict(user_config_info, preserving_proto_field_name=True) + print_message(user_config_info, "test_create_user_config") + user_config_data = MessageToDict( + user_config_info, preserving_proto_field_name=True + ) self.assertIsInstance(user_config_info, user_config_pb2.UserConfigInfo) - self.assertEqual(user_config_info.name, params['name']) - self.assertDictEqual(MessageToDict(user_config_info.data), params['data']) - self.assertDictEqual(user_config_data['tags'], params['tags']) - self.assertEqual(user_config_info.domain_id, params['domain_id']) - self.assertIsNotNone(getattr(user_config_info, 'created_at', None)) - self.assertIsNotNone(getattr(user_config_info, 'updated_at', None)) - - @patch.object(BaseAPI, '__init__', return_value=None) - @patch.object(Locator, 'get_service', return_value=_MockUserConfigService()) - @patch.object(BaseAPI, 'parse_request') + self.assertEqual(user_config_info.name, params["name"]) + self.assertDictEqual(MessageToDict(user_config_info.data), params["data"]) + self.assertDictEqual(user_config_data["tags"], params["tags"]) + self.assertEqual(user_config_info.domain_id, params["domain_id"]) + self.assertIsNotNone(getattr(user_config_info, "created_at", None)) + self.assertIsNotNone(getattr(user_config_info, "updated_at", None)) + + @patch.object(BaseAPI, "__init__", return_value=None) + @patch.object(Locator, "get_service", return_value=_MockUserConfigService()) + @patch.object(BaseAPI, "parse_request") def test_update_user_config(self, mock_parse_request, *args): params = { - 'name': utils.random_string(), - 'data': { - 'update_config_key': 'update_config_value' - }, - 'tags': { - 'update_key': 'update_value' - }, - 'domain_id': utils.generate_id('domain') + "name": utils.random_string(), + "data": {"update_config_key": "update_config_value"}, + "tags": {"update_key": "update_value"}, + "domain_id": utils.generate_id("domain"), } mock_parse_request.return_value = (params, {}) user_config_servicer = UserConfig() user_config_info = user_config_servicer.update({}, {}) - print_message(user_config_info, 'test_update_user_config') - user_config_data = MessageToDict(user_config_info, preserving_proto_field_name=True) + print_message(user_config_info, "test_update_user_config") + user_config_data = MessageToDict( + user_config_info, preserving_proto_field_name=True + ) self.assertIsInstance(user_config_info, user_config_pb2.UserConfigInfo) - self.assertDictEqual(MessageToDict(user_config_info.data), params['data']) - self.assertDictEqual(user_config_data['tags'], params['tags']) + self.assertDictEqual(MessageToDict(user_config_info.data), params["data"]) + self.assertDictEqual(user_config_data["tags"], params["tags"]) - @patch.object(BaseAPI, '__init__', return_value=None) - @patch.object(Locator, 'get_service', return_value=_MockUserConfigService()) - @patch.object(BaseAPI, 'parse_request') + @patch.object(BaseAPI, "__init__", return_value=None) + @patch.object(Locator, "get_service", return_value=_MockUserConfigService()) + @patch.object(BaseAPI, "parse_request") def test_set_user_config(self, mock_parse_request, *args): params = { - 'name': utils.random_string(), - 'data': { - 'config_key': 'config_value' - }, - 'tags': { - utils.random_string(): utils.random_string() - }, - 'domain_id': utils.generate_id('domain') + "name": utils.random_string(), + "data": {"config_key": "config_value"}, + "tags": {utils.random_string(): utils.random_string()}, + "domain_id": utils.generate_id("domain"), } mock_parse_request.return_value = (params, {}) user_config_servicer = UserConfig() user_config_info = user_config_servicer.create({}, {}) - print_message(user_config_info, 'test_update_user_config') - user_config_data = MessageToDict(user_config_info, preserving_proto_field_name=True) + print_message(user_config_info, "test_update_user_config") + user_config_data = MessageToDict( + user_config_info, preserving_proto_field_name=True + ) self.assertIsInstance(user_config_info, user_config_pb2.UserConfigInfo) - self.assertEqual(user_config_info.name, params['name']) - self.assertDictEqual(MessageToDict(user_config_info.data), params['data']) - self.assertDictEqual(user_config_data['tags'], params['tags']) - self.assertEqual(user_config_info.domain_id, params['domain_id']) - self.assertIsNotNone(getattr(user_config_info, 'created_at', None)) - self.assertIsNotNone(getattr(user_config_info, 'updated_at', None)) - - @patch.object(BaseAPI, '__init__', return_value=None) - @patch.object(Locator, 'get_service', return_value=_MockUserConfigService()) - @patch.object(BaseAPI, 'parse_request') + self.assertEqual(user_config_info.name, params["name"]) + self.assertDictEqual(MessageToDict(user_config_info.data), params["data"]) + self.assertDictEqual(user_config_data["tags"], params["tags"]) + self.assertEqual(user_config_info.domain_id, params["domain_id"]) + self.assertIsNotNone(getattr(user_config_info, "created_at", None)) + self.assertIsNotNone(getattr(user_config_info, "updated_at", None)) + + @patch.object(BaseAPI, "__init__", return_value=None) + @patch.object(Locator, "get_service", return_value=_MockUserConfigService()) + @patch.object(BaseAPI, "parse_request") def test_delete_user_config(self, mock_parse_request, *args): mock_parse_request.return_value = ({}, {}) user_config_servicer = UserConfig() result = user_config_servicer.delete({}, {}) - print_message(result, 'test_delete_user_config') + print_message(result, "test_delete_user_config") self.assertIsInstance(result, Empty) - @patch.object(BaseAPI, '__init__', return_value=None) - @patch.object(Locator, 'get_service', return_value=_MockUserConfigService()) - @patch.object(BaseAPI, 'parse_request') + @patch.object(BaseAPI, "__init__", return_value=None) + @patch.object(Locator, "get_service", return_value=_MockUserConfigService()) + @patch.object(BaseAPI, "parse_request") def test_get_user_config(self, mock_parse_request, *args): mock_parse_request.return_value = ({}, {}) user_config_servicer = UserConfig() user_config_info = user_config_servicer.get({}, {}) - print_message(user_config_info, 'test_get_user_config') + print_message(user_config_info, "test_get_user_config") self.assertIsInstance(user_config_info, user_config_pb2.UserConfigInfo) - @patch.object(BaseAPI, '__init__', return_value=None) - @patch.object(Locator, 'get_service', return_value=_MockUserConfigService()) - @patch.object(BaseAPI, 'parse_request') + @patch.object(BaseAPI, "__init__", return_value=None) + @patch.object(Locator, "get_service", return_value=_MockUserConfigService()) + @patch.object(BaseAPI, "parse_request") def test_list_user_configs(self, mock_parse_request, *args): mock_parse_request.return_value = ({}, {}) user_config_servicer = UserConfig() user_configs_info = user_config_servicer.list({}, {}) - print_message(user_configs_info, 'test_list_user_configs') + print_message(user_configs_info, "test_list_user_configs") self.assertIsInstance(user_configs_info, user_config_pb2.UserConfigsInfo) - self.assertIsInstance(user_configs_info.results[0], user_config_pb2.UserConfigInfo) + self.assertIsInstance( + user_configs_info.results[0], user_config_pb2.UserConfigInfo + ) self.assertEqual(user_configs_info.total_count, 10) - @patch.object(BaseAPI, '__init__', return_value=None) - @patch.object(Locator, 'get_service', return_value=_MockUserConfigService()) - @patch.object(BaseAPI, 'parse_request') + @patch.object(BaseAPI, "__init__", return_value=None) + @patch.object(Locator, "get_service", return_value=_MockUserConfigService()) + @patch.object(BaseAPI, "parse_request") def test_stat_user_configs(self, mock_parse_request, *args): mock_parse_request.return_value = ({}, {}) user_config_servicer = UserConfig() stat_info = user_config_servicer.stat({}, {}) - print_message(stat_info, 'test_stat_user_configs') + print_message(stat_info, "test_stat_user_configs") if __name__ == "__main__": From 0f04e704f82cf60045fa69a73fbb5fdf3785e790 Mon Sep 17 00:00:00 2001 From: seolmin Date: Mon, 18 Dec 2023 01:58:47 +0900 Subject: [PATCH 6/6] refactor: change all for SpaceONE 2.0 --- Dockerfile | 5 +- deploy/helm/config/config.yaml | 22 +--- src/setup.py | 3 +- src/spaceone/config/conf/global_conf.py | 19 ++- .../config/manager/domain_config_manager.py | 28 ++--- .../config/manager/user_config_manager.py | 30 ++--- .../config/service/domain_config_service.py | 49 ++++---- .../config/service/user_config_service.py | 108 ++++++------------ 8 files changed, 118 insertions(+), 146 deletions(-) diff --git a/Dockerfile b/Dockerfile index ed142cc..59dfaad 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,8 @@ ENV PACKAGE_VERSION=$PACKAGE_VERSION COPY pkg/pip_requirements.txt pip_requirements.txt -RUN pip install --upgrade -r pip_requirements.txt +RUN pip install --upgrade pip && \ + pip install --upgrade -r pip_requirements.txt COPY src ${SRC_DIR} WORKDIR ${SRC_DIR} @@ -21,4 +22,4 @@ RUN pip install --upgrade spaceone-api EXPOSE ${SPACEONE_PORT} ENTRYPOINT ["spaceone"] -CMD ["run","grpc-server", "spaceone.config", "-m", "/opt"] +CMD ["run", "grpc-server", "spaceone.config", "-m", "/opt"] diff --git a/deploy/helm/config/config.yaml b/deploy/helm/config/config.yaml index fac5ccb..e3621d0 100644 --- a/deploy/helm/config/config.yaml +++ b/deploy/helm/config/config.yaml @@ -1,7 +1,7 @@ REMOTE_URL: -- file:///opt/spaceone/config/config/database.yaml -- file:///opt/spaceone/config/config/shared.yaml -- file:///opt/spaceone/config/config/application.yaml + - file:///opt/spaceone/config/config/database.yaml + - file:///opt/spaceone/config/config/shared.yaml + - file:///opt/spaceone/config/config/application.yaml GLOBAL: MAX_WORKERS: 1000 DATABASES: @@ -17,7 +17,6 @@ GLOBAL: backend: spaceone.core.cache.redis_cache.RedisCache host: redis port: 6379 - db: 1 encoding: utf-8 socket_timeout: 10 socket_connect_timeout: 10 @@ -30,17 +29,4 @@ GLOBAL: handlers: file: type: file - filename: /var/log/spaceone/config.log - CONNECTORS: - IdentityConnector: - endpoint: - v1: grpc://identity:50051 - SecretConnector: - endpoint: - v1: grpc://secret:50051 - PluginConnector: - endpoint: - v1: grpc://plugin:50051 - RepositoryConnector: - endpoint: - v1: grpc://repository:50051 + filename: /var/log/spaceone/config.log \ No newline at end of file diff --git a/src/setup.py b/src/setup.py index 1e849cb..528519f 100644 --- a/src/setup.py +++ b/src/setup.py @@ -14,6 +14,7 @@ # limitations under the License. import os + from setuptools import setup, find_packages setup( @@ -26,6 +27,6 @@ author_email="admin@spaceone.dev", license="Apache License 2.0", packages=find_packages(), - install_requires=["spaceone-core", "spaceone-api", "mongoengine"], + install_requires=["spaceone-core", "spaceone-api", "mongoengine", "mongomock"], zip_safe=False, ) diff --git a/src/spaceone/config/conf/global_conf.py b/src/spaceone/config/conf/global_conf.py index 1845fb2..2915b7e 100644 --- a/src/spaceone/config/conf/global_conf.py +++ b/src/spaceone/config/conf/global_conf.py @@ -1,3 +1,4 @@ +# Database Settings DATABASE_AUTO_CREATE_INDEX = True DATABASES = { "default": { @@ -9,6 +10,7 @@ } } +# Cache Settings CACHES = { "default": {}, "local": { @@ -18,4 +20,19 @@ }, } -HANDLERS = {} +# Handler Settings +HANDLERS = { + # "authentication": [{ + # "backend": "spaceone.core.handler.authentication_handler:SpaceONEAuthenticationHandler" + # }], + # "authorization": [{ + # "backend": "spaceone.core.handler.authorization_handler:SpaceONEAuthorizationHandler" + # }], + # "mutation": [{ + # "backend": "spaceone.core.handler.mutation_handler:SpaceONEMutationHandler" + # }], + # "event": [] +} + +# Connector Settings +CONNECTORS = {} diff --git a/src/spaceone/config/manager/domain_config_manager.py b/src/spaceone/config/manager/domain_config_manager.py index 1311c18..b4b46c1 100644 --- a/src/spaceone/config/manager/domain_config_manager.py +++ b/src/spaceone/config/manager/domain_config_manager.py @@ -1,6 +1,7 @@ import logging from spaceone.core.manager import BaseManager + from spaceone.config.model.domain_config_model import DomainConfig _LOGGER = logging.getLogger(__name__) @@ -11,27 +12,28 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.domain_config_model: DomainConfig = self.locator.get_model("DomainConfig") - def create_domain_config(self, params): - def _rollback(domain_config_vo): + def create_domain_config(self, params: dict) -> DomainConfig: + def _rollback(vo: DomainConfig) -> None: _LOGGER.info( - f"[create_domain_config._rollback] " - f"Delete domain config : {domain_config_vo.name}" + f"[create_domain_config._rollback] " f"Delete domain config : {vo.name}" ) - domain_config_vo.delete() + vo.delete() domain_config_vo: DomainConfig = self.domain_config_model.create(params) self.transaction.add_rollback(_rollback, domain_config_vo) return domain_config_vo - def update_domain_config(self, params): + 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, domain_config_vo): - def _rollback(old_data): + def update_domain_config_by_vo( + self, params: dict, domain_config_vo: DomainConfig + ) -> DomainConfig: + def _rollback(old_data: dict): _LOGGER.info( f'[update_domain_config_by_vo._rollback] Revert Data : {old_data["name"]}' ) @@ -41,17 +43,17 @@ def _rollback(old_data): return domain_config_vo.update(params) - def delete_domain_config(self, name, domain_id): + def delete_domain_config(self, name: str, domain_id: str) -> None: domain_config_vo: DomainConfig = self.get_domain_config(name, domain_id) domain_config_vo.delete() - def get_domain_config(self, name, domain_id, only=None): - return self.domain_config_model.get(name=name, domain_id=domain_id, only=only) + def get_domain_config(self, name: str, domain_id: str) -> DomainConfig: + return self.domain_config_model.get(name=name, domain_id=domain_id) - def filter_domain_configs(self, **conditions): + def filter_domain_configs(self, **conditions: dict): return self.domain_config_model.filter(**conditions) - def list_domain_configs(self, query={}): + def list_domain_configs(self, query: dict) -> dict: return self.domain_config_model.query(**query) def state_domain_configs(self, query): diff --git a/src/spaceone/config/manager/user_config_manager.py b/src/spaceone/config/manager/user_config_manager.py index a8703fd..a8ad3a0 100644 --- a/src/spaceone/config/manager/user_config_manager.py +++ b/src/spaceone/config/manager/user_config_manager.py @@ -1,6 +1,7 @@ import logging from spaceone.core.manager import BaseManager + from spaceone.config.model.user_config_model import UserConfig _LOGGER = logging.getLogger(__name__) @@ -11,27 +12,28 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.user_config_model: UserConfig = self.locator.get_model("UserConfig") - def create_user_config(self, params): - def _rollback(user_config_vo): + def create_user_config(self, params: dict) -> UserConfig: + def _rollback(vo: UserConfig): _LOGGER.info( - f"[create_user_config._rollback] " - f"Delete config map : {user_config_vo.name}" + f"[create_user_config._rollback] " f"Delete config map : {vo.name}" ) - user_config_vo.delete() + vo.delete() user_config_vo: UserConfig = self.user_config_model.create(params) self.transaction.add_rollback(_rollback, user_config_vo) return user_config_vo - def update_user_config(self, params): + 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, user_config_vo): - def _rollback(old_data): + def update_user_config_by_vo( + self, params: dict, user_config_vo: UserConfig + ) -> UserConfig: + def _rollback(old_data: dict): _LOGGER.info( f'[update_user_config_by_vo._rollback] Revert Data : {old_data["name"]}' ) @@ -41,20 +43,20 @@ def _rollback(old_data): return user_config_vo.update(params) - def delete_user_config(self, name, user_id, domain_id): + 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) user_config_vo.delete() - def get_user_config(self, name, user_id, domain_id, only=None): + def get_user_config(self, name: str, user_id: str, domain_id: str) -> UserConfig: return self.user_config_model.get( - name=name, user_id=user_id, domain_id=domain_id, only=only + name=name, user_id=user_id, domain_id=domain_id ) - def filter_user_configs(self, **conditions): + def filter_user_configs(self, **conditions: dict): return self.user_config_model.filter(**conditions) - def list_user_configs(self, query={}): + def list_user_configs(self, query: dict) -> dict: return self.user_config_model.query(**query) - def state_user_configs(self, query): + def state_user_configs(self, query: dict) -> dict: return self.user_config_model.stat(**query) diff --git a/src/spaceone/config/service/domain_config_service.py b/src/spaceone/config/service/domain_config_service.py index fcf8fad..4e404a7 100644 --- a/src/spaceone/config/service/domain_config_service.py +++ b/src/spaceone/config/service/domain_config_service.py @@ -3,6 +3,7 @@ from spaceone.core.service import * from spaceone.config.manager.domain_config_manager import DomainConfigManager +from spaceone.config.model import DomainConfig _LOGGER = logging.getLogger(__name__) @@ -12,19 +13,15 @@ @mutation_handler @event_handler class DomainConfigService(BaseService): - service = "config" - resource = "DomainConfig" - permission_group = "DOMAIN" - def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.domain_config_mgr: DomainConfigManager = self.locator.get_manager( DomainConfigManager ) - @transaction(scope="domain_admin:write") + @transaction(permission="config:DomainConfig.write", role_types=["DOMAIN_ADMIN"]) @check_required(["name", "data", "domain_id"]) - def create(self, params): + def create(self, params: dict) -> DomainConfig: """Create domain config Args: @@ -32,7 +29,7 @@ def create(self, params): 'name': 'str', # required 'data': 'dict', # required 'tags': 'dict', - 'domain_id': 'str' # required + 'domain_id': 'str' # injected from auth } Returns: @@ -41,9 +38,9 @@ def create(self, params): return self.domain_config_mgr.create_domain_config(params) - @transaction(scope="domain_admin:write") + @transaction(permission="config:DomainConfig.write", role_types=["DOMAIN_ADMIN"]) @check_required(["name", "domain_id"]) - def update(self, params): + def update(self, params: dict) -> DomainConfig: """Update domain config Args: @@ -51,7 +48,7 @@ def update(self, params): 'name': 'str', # required 'data': 'dict', 'tags': 'dict', - 'domain_id': 'str' # required + 'domain_id': 'str' # injected from auth } Returns: @@ -60,7 +57,7 @@ def update(self, params): return self.domain_config_mgr.update_domain_config(params) - @transaction(scope="domain_admin:write") + @transaction(permission="config:DomainConfig.write", role_types=["DOMAIN_ADMIN"]) @check_required(["name", "data", "domain_id"]) def set(self, params): """Set domain config (create or update) @@ -70,17 +67,15 @@ def set(self, params): 'name': 'str', # required 'data': 'dict', # required 'tags': 'dict', - 'domain_id': 'str' # required + 'domain_id': 'str' # injected from auth } Returns: domain_config_vo (object) """ - domain_id = params["domain_id"] - domain_config_vos = self.domain_config_mgr.filter_domain_configs( - domain_id=domain_id, name=params["name"] + name=params["name"], domain_id=params["domain_id"] ) if domain_config_vos.count() == 0: @@ -90,15 +85,15 @@ def set(self, params): params, domain_config_vos[0] ) - @transaction(scope="domain_admin:write") + @transaction(permission="config:DomainConfig.write", role_types=["DOMAIN_ADMIN"]) @check_required(["name", "domain_id"]) - def delete(self, params): + def delete(self, params: dict) -> None: """Delete domain config Args: params (dict): { 'name': 'str', # required - 'domain_id': 'str' # required + 'domain_id': 'str' # injected from auth } Returns: @@ -107,15 +102,15 @@ def delete(self, params): self.domain_config_mgr.delete_domain_config(params["name"], params["domain_id"]) - @transaction(scope="domain_admin:read") + @transaction(permission="config:DomainConfig.read", role_types=["DOMAIN_ADMIN"]) @check_required(["name", "domain_id"]) - def get(self, params): + def get(self, params: dict) -> DomainConfig: """Get domain config Args: params (dict): { 'name': 'str', # required - 'domain_id': 'str' # required + 'domain_id': 'str' # injected from auth } Returns: @@ -123,21 +118,21 @@ def get(self, params): """ return self.domain_config_mgr.get_domain_config( - params["name"], params["domain_id"], params.get("only") + params["name"], params["domain_id"] ) - @transaction(scope="domain_admin:read") + @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): + def list(self, params: dict) -> dict: """List domain configs Args: params (dict): { 'query': 'dict (spaceone.api.core.v1.Query)' 'name': 'str', - 'domain_id': 'str', # required + 'domain_id': 'str', # injected from auth } Returns: @@ -148,11 +143,11 @@ def list(self, params): query = params.get("query", {}) return self.domain_config_mgr.list_domain_configs(query) - @transaction(scope="domain_admin:read") + @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): + def stat(self, params: dict) -> dict: """ Args: params (dict): { diff --git a/src/spaceone/config/service/user_config_service.py b/src/spaceone/config/service/user_config_service.py index 35383ff..8dc87ea 100644 --- a/src/spaceone/config/service/user_config_service.py +++ b/src/spaceone/config/service/user_config_service.py @@ -1,9 +1,9 @@ import logging -from spaceone.core.error import * from spaceone.core.service import * from spaceone.config.manager.user_config_manager import UserConfigManager +from spaceone.config.model import UserConfig _LOGGER = logging.getLogger(__name__) @@ -13,19 +13,15 @@ @mutation_handler @event_handler class UserConfigService(BaseService): - service = "config" - resource = "UserConfig" - permission_group = "USER" - def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.user_config_mgr: UserConfigManager = self.locator.get_manager( "UserConfigManager" ) - @transaction(scope="user:write") - @check_required(["name", "data", "domain_id", "user_id"]) - def create(self, params): + @transaction(permission="config:UserConfig.write", role_types=["USER"]) + @check_required(["name", "data", "user_id", "domain_id"]) + def create(self, params: dict) -> UserConfig: """Create user config Args: @@ -33,74 +29,58 @@ def create(self, params): 'name': 'str', # required 'data': 'dict', # required 'tags': 'dict', - 'domain_id': 'str'(meta), # required - 'user_id': 'str'(meta) # required + 'user_id': 'str'(meta) # injected from auth + 'domain_id': 'str'(meta), # injected from auth } Returns: user_config_vo (object) """ - self._check_permission(params["domain_id"]) - - params["user_id"] = self.transaction.get_meta("user_id") - return self.user_config_mgr.create_user_config(params) - @transaction(scope="user:write") - @check_required(["name", "domain_id", "user_id"]) - def update(self, params): + @transaction(permission="config:UserConfig.write", role_types=["USER"]) + @check_required(["name", "user_id", "domain_id"]) + def update(self, params: dict) -> UserConfig: """Update user config Args: params (dict): { - 'name': 'str', # required + 'name': 'str', # required 'data': 'dict', 'tags': 'dict', - 'domain_id': 'str'(meta) # required - 'user_id': 'str'(meta) # required + 'domain_id': 'str' # injected from auth + 'user_id': 'str' # injected from auth } Returns: user_config_vo (object) """ - self._check_permission(params["domain_id"]) - - params["user_id"] = self.transaction.get_meta("user_id") - return self.user_config_mgr.update_user_config(params) - @transaction(scope="user:write") - @check_required(["name", "data", "domain_id", "user_id"]) - def set(self, params): + @transaction(permission="config:UserConfig.write", role_types=["USER"]) + @check_required(["name", "data", "user_id", "domain_id"]) + def set(self, params: dict) -> UserConfig: """Set user config (create or update) Args: params (dict): { - 'name': 'str', # required - 'data': 'dict', # required + 'name': 'str', # required + 'data': 'dict', # required 'tags': 'dict', - 'domain_id': 'str'(meta), # required - 'user_id': 'str'(meta) # required + 'user_id': 'str' # injected from auth + 'domain_id': 'str' # injected from auth } Returns: user_config_vo (object) """ - self._check_permission(params["domain_id"]) - - params["user_id"] = self.transaction.get_meta("user_id") - - user_type = self.transaction.get_meta("authorization.user_type") - if user_type == "DOMAIN_OWNER": - raise ERROR_PERMISSION_DENIED() - user_config_vos = self.user_config_mgr.filter_user_configs( - domain_id=params["domain_id"], - user_id=params["user_id"], name=params["name"], + user_id=params["user_id"], + domain_id=params["domain_id"], ) if user_config_vos.count() == 0: @@ -110,31 +90,28 @@ def set(self, params): params, user_config_vos[0] ) - @transaction(scope="user:write") - @check_required(["name", "domain_id", "user_id"]) + @transaction(permission="config:UserConfig.write", role_types=["USER"]) + @check_required(["name", "user_id", "domain_id"]) def delete(self, params): """Delete user config Args: params (dict): { - 'name': 'str', # required - 'domain_id': 'str'(meta), # required - 'user_id': 'str'(meta) # required + 'name': 'str', # required + 'user_id': 'str', # injected from auth + 'domain_id': 'str' # injected from auth } Returns: None """ - self._check_permission(params["domain_id"]) - user_id = self.transaction.get_meta("user_id") - self.user_config_mgr.delete_user_config( - params["name"], user_id, params["domain_id"] + params["name"], params["user_id"], params["domain_id"] ) - @transaction(scope="user:read") - @check_required(["name", "domain_id", "user_id"]) + @transaction(permission="config:UserConfig.read", role_types=["USER"]) + @check_required(["name", "user_id", "domain_id"]) def get(self, params): """Get user config @@ -149,25 +126,23 @@ def get(self, params): user_config_vo (object) """ - user_id = self.transaction.get_meta("user_id") - return self.user_config_mgr.get_user_config( - params["name"], user_id, params["domain_id"], params.get("only") + params["name"], params["user_id"], params["domain_id"] ) - @transaction(scope="user:read") - @check_required(["domain_id"]) + @transaction(permission="config:UserConfig.read", role_types=["USER"]) + @check_required(["user_id", "domain_id"]) @append_query_filter(["name", "user_id", "domain_id"]) @append_keyword_filter(["name"]) - def list(self, params): + def list(self, params: dict) -> dict: """List user configs Args: params (dict): { 'query': 'dict (spaceone.api.core.v1.Query)', 'name': 'str', - 'domain_id': 'str'(meta), # required - 'user_id': 'str'(meta) # required + 'user_id': 'str', # injected from auth + 'domain_id': 'str' # injected from auth } Returns: @@ -178,7 +153,7 @@ def list(self, params): query = params.get("query", {}) return self.user_config_mgr.list_user_configs(query) - @transaction(scope="user:read") + @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"]) @@ -187,8 +162,8 @@ def stat(self, params): Args: params (dict): { 'query': 'dict (spaceone.api.core.v1.StatisticsQuery)', # required - 'domain_id': 'str'(meta), # required - 'user_id': 'str'(meta) # required + 'user_id': 'str'(meta) # injected from auth + 'domain_id': 'str'(meta), # injected from auth } Returns: @@ -198,10 +173,3 @@ def stat(self, params): query = params.get("query", {}) return self.user_config_mgr.state_user_configs(query) - - def _check_permission(self, request_domain_id): - user_type = self.transaction.get_meta("authorization.user_type") - user_domain_id = self.transaction.get_meta("domain_id") - - if user_type == "DOMAIN_OWNER" or request_domain_id != user_domain_id: - raise ERROR_PERMISSION_DENIED()