diff --git a/Dockerfile b/Dockerfile index 1843bb9..35a89e8 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 @@ -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 ["grpc", "spaceone.dashboard", "-m", "/opt"] +CMD ["run", "grpc-server", "spaceone.dashboard", "-m", "/opt"] diff --git a/deploy/helm/Chart.yaml b/deploy/helm/Chart.yaml index c5da2e0..a8340b5 100644 --- a/deploy/helm/Chart.yaml +++ b/deploy/helm/Chart.yaml @@ -15,7 +15,7 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 1.3.10 +version: 1.3.11 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to diff --git a/deploy/helm/config/config.yaml b/deploy/helm/config/config.yaml index d9eef9a..98f3f9f 100644 --- a/deploy/helm/config/config.yaml +++ b/deploy/helm/config/config.yaml @@ -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,14 +29,4 @@ GLOBAL: handlers: file: type: file - filename: /var/log/spaceone/dashboard.log - - HANDLERS: - authentication: - - backend: spaceone.core.handler.authentication_handler.AuthenticationGRPCHandler - uri: grpc+ssl://identity.dev.spaceone.dev:443/v1/Domain/get_public_key - authorization: - - backend: spaceone.core.handler.authorization_handler.AuthorizationGRPCHandler - uri: grpc+ssl://identity.dev.spaceone.dev:443/v1/Authorization/verify - mutation: - - backend: spaceone.core.handler.mutation_handler.SpaceONEMutationHandler \ No newline at end of file + filename: /var/log/spaceone/dashboard.log \ No newline at end of file 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..e212adc 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 @@ -85,7 +85,7 @@ spec: {{- end }} volumes: - - name: default-conf + - name: default-conf configMap: name: {{ .Values.name }}-conf - name: database-conf diff --git a/deploy/helm/templates/deployment-worker.yaml b/deploy/helm/templates/deployment-worker.yaml index bb30a1d..157239e 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 "_" }}', '-m', '/opt'] + command: ['spaceone', 'run', 'scheduler', 'spaceone.{{ regexReplaceAll "-" .Values.name "_" }}', '-m', '/opt'] volumeMounts: - name: default-conf mountPath: /opt/spaceone/{{ .Values.name }}/config/config.yaml diff --git a/src/setup.py b/src/setup.py index cfc4b74..da0c390 100644 --- a/src/setup.py +++ b/src/setup.py @@ -17,20 +17,15 @@ from setuptools import setup, find_packages setup( - name='spaceone-dashboard', - version=os.environ.get('PACKAGE_VERSION'), - description='SpaceONE dashboard service', - long_description='', - url='https://www.spaceone.dev/', - author='MEGAZONE SpaceONE Team', - author_email='admin@spaceone.dev', - license='Apache License 2.0', + name="spaceone-dashboard", + version=os.environ.get("PACKAGE_VERSION"), + description="SpaceONE dashboard 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', - 'boto3' - ], + install_requires=["spaceone-core", "spaceone-api", "mongoengine", "boto3"], zip_safe=False, ) diff --git a/src/spaceone/__init__.py b/src/spaceone/__init__.py index 0260537..8db66d3 100644 --- a/src/spaceone/__init__.py +++ b/src/spaceone/__init__.py @@ -1 +1 @@ -__path__ = __import__('pkgutil').extend_path(__path__, __name__) \ No newline at end of file +__path__ = __import__("pkgutil").extend_path(__path__, __name__) diff --git a/src/spaceone/dashboard/conf/global_conf.py b/src/spaceone/dashboard/conf/global_conf.py index 4f59aa9..e08a6d2 100644 --- a/src/spaceone/dashboard/conf/global_conf.py +++ b/src/spaceone/dashboard/conf/global_conf.py @@ -1,55 +1,53 @@ # Database Settings DATABASES = { - 'default': { - 'db': 'dashboard', - 'host': 'localhost', - 'port': 27017, - 'username': '', - 'password': '' + "default": { + "db": "dashboard", + "host": "localhost", + "port": 27017, + "username": "", + "password": "", } } # Cache Settings CACHES = { - 'default': { + "default": { # Redis Example # 'backend': 'spaceone.core.cache.redis_cache.RedisCache', # 'host': '', # 'port': 6379, - # 'db': 0 } } # Handler Configuration HANDLERS = { - 'authentication': [ + "authentication": [ # Default Authentication Handler # { # 'backend': 'spaceone.core.handler.authentication_handler.AuthenticationGRPCHandler', # 'uri': 'grpc://identity:50051/v1/Domain/get_public_key' # } ], - 'authorization': [ + "authorization": [ # Default Authorization Handler # { # 'backend': 'spaceone.core.handler.authorization_handler.AuthorizationGRPCHandler', # 'uri': 'grpc://identity:50051/v1/Authorization/verify' # } ], - 'mutation': [], - 'event': [] + "mutation": [], + "event": [], } # Connector Settings CONNECTORS = { - 'SpaceConnector': { - 'backend': 'spaceone.core.connector.space_connector.SpaceConnector', - 'endpoints': { - 'identity': 'grpc://identity:50051', - } + "SpaceConnector": { + "backend": "spaceone.core.connector.space_connector:SpaceConnector", + "endpoints": { + "identity": "grpc://identity:50051", + }, } } # Log Settings -LOG = { -} +LOG = {} diff --git a/src/spaceone/dashboard/conf/proto_conf.py b/src/spaceone/dashboard/conf/proto_conf.py deleted file mode 100644 index e0d5776..0000000 --- a/src/spaceone/dashboard/conf/proto_conf.py +++ /dev/null @@ -1,5 +0,0 @@ -PROTO = { - 'spaceone.dashboard.interface.grpc.v1.domain_dashboard': ['DomainDashboard'], - 'spaceone.dashboard.interface.grpc.v1.project_dashboard': ['ProjectDashboard'], - 'spaceone.dashboard.interface.grpc.v1.custom_widget': ['CustomWidget'] -} diff --git a/src/spaceone/dashboard/error/__init__.py b/src/spaceone/dashboard/error/__init__.py index ad759ab..fbee51c 100644 --- a/src/spaceone/dashboard/error/__init__.py +++ b/src/spaceone/dashboard/error/__init__.py @@ -1 +1 @@ -from spaceone.dashboard.error.common_dashboard import * \ No newline at end of file +from spaceone.dashboard.error.dashboard import * diff --git a/src/spaceone/dashboard/error/common_dashboard.py b/src/spaceone/dashboard/error/dashboard.py similarity index 56% rename from src/spaceone/dashboard/error/common_dashboard.py rename to src/spaceone/dashboard/error/dashboard.py index 9df732e..1a8d41b 100644 --- a/src/spaceone/dashboard/error/common_dashboard.py +++ b/src/spaceone/dashboard/error/dashboard.py @@ -2,4 +2,4 @@ class ERROR_LATEST_VERSION(ERROR_INVALID_ARGUMENT): - _message = 'Do not remove latest version. (version = {version})' + _message = "Do not remove latest version. (version = {version})" diff --git a/src/spaceone/dashboard/info/__init__.py b/src/spaceone/dashboard/info/__init__.py index c6e6114..0b8c7f2 100644 --- a/src/spaceone/dashboard/info/__init__.py +++ b/src/spaceone/dashboard/info/__init__.py @@ -1,6 +1,3 @@ +from spaceone.dashboard.info.dashboard_info import * +from spaceone.dashboard.info.dashboard_version_info import * from spaceone.dashboard.info.common_info import * -from spaceone.dashboard.info.domain_dashboard_info import * -from spaceone.dashboard.info.domain_dashboard_version_info import * -from spaceone.dashboard.info.project_dashboard_info import * -from spaceone.dashboard.info.project_dashboard_version_info import * -from spaceone.dashboard.info.custom_widget_info import * \ No newline at end of file diff --git a/src/spaceone/dashboard/info/common_info.py b/src/spaceone/dashboard/info/common_info.py index 89bf2a3..a47f60d 100644 --- a/src/spaceone/dashboard/info/common_info.py +++ b/src/spaceone/dashboard/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/dashboard/info/custom_widget_info.py b/src/spaceone/dashboard/info/custom_widget_info.py deleted file mode 100644 index bb264e2..0000000 --- a/src/spaceone/dashboard/info/custom_widget_info.py +++ /dev/null @@ -1,36 +0,0 @@ -import functools -from spaceone.api.dashboard.v1 import custom_widget_pb2 -from spaceone.core.pygrpc.message_type import * -from spaceone.core import utils -from spaceone.dashboard.model.custom_widget_model import CustomWidget - -__all__ = ['CustomWidgetInfo', 'CustomWidgetsInfo'] - - -def CustomWidgetInfo(custom_widget_vo: CustomWidget, minimal=False): - info = { - 'custom_widget_id': custom_widget_vo.custom_widget_id, - 'widget_name': custom_widget_vo.widget_name, - 'title': custom_widget_vo.title, - 'version': custom_widget_vo.version, - 'user_id': custom_widget_vo.user_id, - 'domain_id': custom_widget_vo.domain_id - } - - if not minimal: - info.update({ - 'options': change_struct_type(custom_widget_vo.options), - 'settings': change_struct_type(custom_widget_vo.settings), - 'inherit_options': change_struct_type(custom_widget_vo.inherit_options), - 'labels': change_list_value_type(custom_widget_vo.labels), - 'tags': change_struct_type(custom_widget_vo.tags), - 'created_at': utils.datetime_to_iso8601(custom_widget_vo.created_at), - 'updated_at': utils.datetime_to_iso8601(custom_widget_vo.updated_at) - }) - - return custom_widget_pb2.CustomWidgetInfo(**info) - - -def CustomWidgetsInfo(custom_widget_vos, total_count, **kwargs): - return custom_widget_pb2.CustomWidgetsInfo(results=list( - map(functools.partial(CustomWidgetInfo, **kwargs), custom_widget_vos)), total_count=total_count) diff --git a/src/spaceone/dashboard/info/dashboard_info.py b/src/spaceone/dashboard/info/dashboard_info.py new file mode 100644 index 0000000..45e7ff9 --- /dev/null +++ b/src/spaceone/dashboard/info/dashboard_info.py @@ -0,0 +1,46 @@ +import functools +from spaceone.api.dashboard.v1 import dashboard_pb2 +from spaceone.core.pygrpc.message_type import * +from spaceone.core import utils +from spaceone.dashboard.model.dashboard_model import Dashboard + +__all__ = ["DashboardInfo", "DashboardsInfo"] + + +def DashboardInfo(dashboard_vo: Dashboard, minimal=False): + info = { + "dashboard_id": dashboard_vo.dashboard_id, + "name": dashboard_vo.name, + "dashboard_type": dashboard_vo.dashboard_type, + "version": dashboard_vo.version, + "labels": change_list_value_type(dashboard_vo.labels), + "resource_group": dashboard_vo.resource_group, + "user_id": dashboard_vo.user_id, + "project_id": dashboard_vo.project_id, + "workspace_id": dashboard_vo.workspace_id, + "domain_id": dashboard_vo.domain_id, + } + + if not minimal: + info.update( + { + "layouts": change_list_value_type(dashboard_vo.layouts) + if dashboard_vo.layouts + else None, + "variables": change_struct_type(dashboard_vo.variables), + "settings": change_struct_type(dashboard_vo.settings), + "variables_schema": change_struct_type(dashboard_vo.variables_schema), + "tags": change_struct_type(dashboard_vo.tags), + "created_at": utils.datetime_to_iso8601(dashboard_vo.created_at), + "updated_at": utils.datetime_to_iso8601(dashboard_vo.updated_at), + } + ) + + return dashboard_pb2.DashboardInfo(**info) + + +def DashboardsInfo(dashboard_vos, total_count, **kwargs): + return dashboard_pb2.DashboardsInfo( + results=list(map(functools.partial(DashboardInfo, **kwargs), dashboard_vos)), + total_count=total_count, + ) diff --git a/src/spaceone/dashboard/info/dashboard_version_info.py b/src/spaceone/dashboard/info/dashboard_version_info.py new file mode 100644 index 0000000..45c25eb --- /dev/null +++ b/src/spaceone/dashboard/info/dashboard_version_info.py @@ -0,0 +1,52 @@ +import functools +from spaceone.api.dashboard.v1 import dashboard_pb2 +from spaceone.core.pygrpc.message_type import * +from spaceone.core import utils +from spaceone.dashboard.model import DashboardVersion + +__all__ = ["DashboardVersionInfo", "DashboardVersionsInfo"] + + +def DashboardVersionInfo( + dashboard_version_vo: DashboardVersion, minimal=False, latest_version=None +): + info = { + "dashboard_id": dashboard_version_vo.dashboard_id, + "version": dashboard_version_vo.version, + "created_at": utils.datetime_to_iso8601(dashboard_version_vo.created_at), + "domain_id": dashboard_version_vo.domain_id, + } + + if latest_version: + if latest_version == dashboard_version_vo.version: + info.update({"latest": True}) + else: + info.update({"latest": False}) + + if not minimal: + info.update( + { + "layouts": change_list_value_type(dashboard_version_vo.layouts) + if dashboard_version_vo.layouts + else None, + "variables": change_struct_type(dashboard_version_vo.variables), + "settings": change_struct_type(dashboard_version_vo.settings), + "variables_schema": change_struct_type( + dashboard_version_vo.variables_schema + ), + } + ) + + return dashboard_pb2.DashboardVersionInfo(**info) + + +def DashboardVersionsInfo(dashboard_version_vos, total_count, **kwargs): + return dashboard_pb2.DashboardVersionsInfo( + results=list( + map( + functools.partial(DashboardVersionInfo, **kwargs), + dashboard_version_vos, + ) + ), + total_count=total_count, + ) diff --git a/src/spaceone/dashboard/info/domain_dashboard_info.py b/src/spaceone/dashboard/info/domain_dashboard_info.py deleted file mode 100644 index 1a89857..0000000 --- a/src/spaceone/dashboard/info/domain_dashboard_info.py +++ /dev/null @@ -1,38 +0,0 @@ -import functools -from spaceone.api.dashboard.v1 import domain_dashboard_pb2 -from spaceone.core.pygrpc.message_type import * -from spaceone.core import utils -from spaceone.dashboard.model.domain_dashboard_model import DomainDashboard - -__all__ = ['DomainDashboardInfo', 'DomainDashboardsInfo'] - - -def DomainDashboardInfo(domain_dashboard_vo: DomainDashboard, minimal=False): - info = { - 'domain_dashboard_id': domain_dashboard_vo.domain_dashboard_id, - 'name': domain_dashboard_vo.name, - 'viewers': domain_dashboard_vo.viewers, - 'version': domain_dashboard_vo.version, - 'labels': change_list_value_type(domain_dashboard_vo.labels), - 'user_id': domain_dashboard_vo.user_id, - 'domain_id': domain_dashboard_vo.domain_id - } - - if not minimal: - info.update({ - 'layouts': change_list_value_type( - domain_dashboard_vo.layouts) if domain_dashboard_vo.layouts else None, - 'variables': change_struct_type(domain_dashboard_vo.variables), - 'settings': change_struct_type(domain_dashboard_vo.settings), - 'variables_schema': change_struct_type(domain_dashboard_vo.variables_schema), - 'tags': change_struct_type(domain_dashboard_vo.tags), - 'created_at': utils.datetime_to_iso8601(domain_dashboard_vo.created_at), - 'updated_at': utils.datetime_to_iso8601(domain_dashboard_vo.updated_at) - }) - - return domain_dashboard_pb2.DomainDashboardInfo(**info) - - -def DomainDashboardsInfo(domain_dashboard_vos, total_count, **kwargs): - return domain_dashboard_pb2.DomainDashboardsInfo(results=list( - map(functools.partial(DomainDashboardInfo, **kwargs), domain_dashboard_vos)), total_count=total_count) diff --git a/src/spaceone/dashboard/info/domain_dashboard_version_info.py b/src/spaceone/dashboard/info/domain_dashboard_version_info.py deleted file mode 100644 index af618da..0000000 --- a/src/spaceone/dashboard/info/domain_dashboard_version_info.py +++ /dev/null @@ -1,43 +0,0 @@ -import functools -from spaceone.api.dashboard.v1 import domain_dashboard_pb2 -from spaceone.core.pygrpc.message_type import * -from spaceone.core import utils -from spaceone.dashboard.model import DomainDashboardVersion - -__all__ = ['DomainDashboardVersionInfo', 'DomainDashboardVersionsInfo'] - - -def DomainDashboardVersionInfo(domain_dashboard_version_vo: DomainDashboardVersion, minimal=False, latest_version=None): - info = { - 'domain_dashboard_id': domain_dashboard_version_vo.domain_dashboard_id, - 'version': domain_dashboard_version_vo.version, - 'created_at': utils.datetime_to_iso8601(domain_dashboard_version_vo.created_at), - 'domain_id': domain_dashboard_version_vo.domain_id - } - - if latest_version: - if latest_version == domain_dashboard_version_vo.version: - info.update({ - 'latest': True - }) - else: - info.update({ - 'latest': False - }) - - if not minimal: - info.update({ - 'layouts': change_list_value_type( - domain_dashboard_version_vo.layouts) if domain_dashboard_version_vo.layouts else None, - 'variables': change_struct_type(domain_dashboard_version_vo.variables), - 'settings': change_struct_type(domain_dashboard_version_vo.settings), - 'variables_schema': change_struct_type(domain_dashboard_version_vo.variables_schema) - }) - - return domain_dashboard_pb2.DomainDashboardVersionInfo(**info) - - -def DomainDashboardVersionsInfo(domain_dashboard_version_vos, total_count, **kwargs): - return domain_dashboard_pb2.DomainDashboardVersionsInfo(results=list( - map(functools.partial(DomainDashboardVersionInfo, **kwargs), domain_dashboard_version_vos)), - total_count=total_count) diff --git a/src/spaceone/dashboard/info/project_dashboard_info.py b/src/spaceone/dashboard/info/project_dashboard_info.py deleted file mode 100644 index 6ec1e36..0000000 --- a/src/spaceone/dashboard/info/project_dashboard_info.py +++ /dev/null @@ -1,39 +0,0 @@ -import functools -from spaceone.api.dashboard.v1 import project_dashboard_pb2 -from spaceone.core.pygrpc.message_type import * -from spaceone.core import utils -from spaceone.dashboard.model.project_dashboard_model import ProjectDashboard - -__all__ = ['ProjectDashboardInfo', 'ProjectDashboardsInfo'] - - -def ProjectDashboardInfo(project_dashboard_vo: ProjectDashboard, minimal=False): - info = { - 'project_dashboard_id': project_dashboard_vo.project_dashboard_id, - 'name': project_dashboard_vo.name, - 'viewers': project_dashboard_vo.viewers, - 'version': project_dashboard_vo.version, - 'labels': change_list_value_type(project_dashboard_vo.labels), - 'project_id': project_dashboard_vo.project_id, - 'user_id': project_dashboard_vo.user_id, - 'domain_id': project_dashboard_vo.domain_id - } - - if not minimal: - info.update({ - 'layouts': change_list_value_type( - project_dashboard_vo.layouts) if project_dashboard_vo.layouts else None, - 'variables': change_struct_type(project_dashboard_vo.variables), - 'settings': change_struct_type(project_dashboard_vo.settings), - 'variables_schema': change_struct_type(project_dashboard_vo.variables_schema), - 'tags': change_struct_type(project_dashboard_vo.tags), - 'created_at': utils.datetime_to_iso8601(project_dashboard_vo.created_at), - 'updated_at': utils.datetime_to_iso8601(project_dashboard_vo.updated_at) - }) - - return project_dashboard_pb2.ProjectDashboardInfo(**info) - - -def ProjectDashboardsInfo(project_dashboard_vos, total_count, **kwargs): - return project_dashboard_pb2.ProjectDashboardsInfo(results=list( - map(functools.partial(ProjectDashboardInfo, **kwargs), project_dashboard_vos)), total_count=total_count) diff --git a/src/spaceone/dashboard/info/project_dashboard_version_info.py b/src/spaceone/dashboard/info/project_dashboard_version_info.py deleted file mode 100644 index 52c8aa6..0000000 --- a/src/spaceone/dashboard/info/project_dashboard_version_info.py +++ /dev/null @@ -1,44 +0,0 @@ -import functools -from spaceone.api.dashboard.v1 import project_dashboard_pb2 -from spaceone.core.pygrpc.message_type import * -from spaceone.core import utils -from spaceone.dashboard.model import ProjectDashboardVersion - -__all__ = ['ProjectDashboardVersionInfo', 'ProjectDashboardVersionsInfo'] - - -def ProjectDashboardVersionInfo(project_dashboard_version_vo: ProjectDashboardVersion, minimal=False, - latest_version=None): - info = { - 'project_dashboard_id': project_dashboard_version_vo.project_dashboard_id, - 'version': project_dashboard_version_vo.version, - 'created_at': utils.datetime_to_iso8601(project_dashboard_version_vo.created_at), - 'domain_id': project_dashboard_version_vo.domain_id - } - - if latest_version: - if latest_version == project_dashboard_version_vo.version: - info.update({ - 'latest': True - }) - else: - info.update({ - 'latest': False - }) - - if not minimal: - info.update({ - 'layouts': change_list_value_type( - project_dashboard_version_vo.layouts) if project_dashboard_version_vo.layouts else None, - 'variables': change_struct_type(project_dashboard_version_vo.variables), - 'settings': change_struct_type(project_dashboard_version_vo.settings), - 'variables_schema': change_struct_type(project_dashboard_version_vo.variables_schema) - }) - - return project_dashboard_pb2.ProjectDashboardVersionInfo(**info) - - -def ProjectDashboardVersionsInfo(project_dashboard_version_vos, total_count, **kwargs): - return project_dashboard_pb2.ProjectDashboardVersionsInfo(results=list( - map(functools.partial(ProjectDashboardVersionInfo, **kwargs), project_dashboard_version_vos)), - total_count=total_count) diff --git a/src/spaceone/dashboard/interface/grpc/__init__.py b/src/spaceone/dashboard/interface/grpc/__init__.py index e69de29..6114920 100644 --- a/src/spaceone/dashboard/interface/grpc/__init__.py +++ b/src/spaceone/dashboard/interface/grpc/__init__.py @@ -0,0 +1,7 @@ +from spaceone.core.pygrpc.server import GRPCServer +from spaceone.dashboard.interface.grpc.dashboard import Dashboard + +__all__ = ["app"] + +app = GRPCServer() +app.add_service(Dashboard) diff --git a/src/spaceone/dashboard/interface/grpc/dashboard.py b/src/spaceone/dashboard/interface/grpc/dashboard.py new file mode 100644 index 0000000..21b34fd --- /dev/null +++ b/src/spaceone/dashboard/interface/grpc/dashboard.py @@ -0,0 +1,116 @@ +from spaceone.api.dashboard.v1 import dashboard_pb2, dashboard_pb2_grpc +from spaceone.core.pygrpc import BaseAPI + + +class Dashboard(BaseAPI, dashboard_pb2_grpc.DashboardServicer): + pb2 = dashboard_pb2 + pb2_grpc = dashboard_pb2_grpc + + def create(self, request, context): + params, metadata = self.parse_request(request, context) + + with self.locator.get_service( + "DashboardService", metadata + ) as dashboard_service: + return self.locator.get_info( + "DashboardInfo", dashboard_service.create(params) + ) + + def update(self, request, context): + params, metadata = self.parse_request(request, context) + + with self.locator.get_service( + "DashboardService", metadata + ) as dashboard_service: + return self.locator.get_info( + "DashboardInfo", dashboard_service.update(params) + ) + + def delete(self, request, context): + params, metadata = self.parse_request(request, context) + + with self.locator.get_service( + "DashboardService", metadata + ) as dashboard_service: + dashboard_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( + "DashboardService", metadata + ) as dashboard_service: + return self.locator.get_info("DashboardInfo", dashboard_service.get(params)) + + def delete_version(self, request, context): + params, metadata = self.parse_request(request, context) + + with self.locator.get_service( + "DashboardService", metadata + ) as dashboard_service: + dashboard_service.delete_version(params) + return self.locator.get_info("EmptyInfo") + + def revert_version(self, request, context): + params, metadata = self.parse_request(request, context) + + with self.locator.get_service( + "DashboardService", metadata + ) as dashboard_service: + return self.locator.get_info( + "DashboardInfo", dashboard_service.revert_version(params) + ) + + def get_version(self, request, context): + params, metadata = self.parse_request(request, context) + + with self.locator.get_service( + "DashboardService", metadata + ) as dashboard_service: + return self.locator.get_info( + "DashboardVersionInfo", dashboard_service.get_version(params) + ) + + def list_versions(self, request, context): + params, metadata = self.parse_request(request, context) + + with self.locator.get_service( + "DashboardService", metadata + ) as dashboard_service: + ( + dashboard_version_vos, + total_count, + dashboard_version, + ) = dashboard_service.list_versions(params) + return self.locator.get_info( + "DashboardVersionsInfo", + dashboard_version_vos, + total_count, + minimal=self.get_minimal(params), + latest_version=dashboard_version, + ) + + def list(self, request, context): + params, metadata = self.parse_request(request, context) + + with self.locator.get_service( + "DashboardService", metadata + ) as dashboard_service: + dashboard_vos, total_count = dashboard_service.list(params) + return self.locator.get_info( + "DashboardsInfo", + dashboard_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( + "DashboardService", metadata + ) as dashboard_service: + return self.locator.get_info( + "StatisticsInfo", dashboard_service.stat(params) + ) diff --git a/src/spaceone/dashboard/interface/grpc/v1/__init__.py b/src/spaceone/dashboard/interface/grpc/v1/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/spaceone/dashboard/interface/grpc/v1/custom_widget.py b/src/spaceone/dashboard/interface/grpc/v1/custom_widget.py deleted file mode 100644 index 4e5358f..0000000 --- a/src/spaceone/dashboard/interface/grpc/v1/custom_widget.py +++ /dev/null @@ -1,48 +0,0 @@ -from spaceone.api.dashboard.v1 import custom_widget_pb2, custom_widget_pb2_grpc -from spaceone.core.pygrpc import BaseAPI - - -class CustomWidget(BaseAPI, custom_widget_pb2_grpc.CustomWidgetServicer): - pb2 = custom_widget_pb2 - pb2_grpc = custom_widget_pb2_grpc - - def create(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('CustomWidgetService', metadata) as custom_widget_service: - return self.locator.get_info('CustomWidgetInfo', custom_widget_service.create(params)) - - def update(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('CustomWidgetService', metadata) as custom_widget_service: - return self.locator.get_info('CustomWidgetInfo', custom_widget_service.update(params)) - - def delete(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('CustomWidgetService', metadata) as custom_widget_service: - custom_widget_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('CustomWidgetService', metadata) as custom_widget_service: - return self.locator.get_info('CustomWidgetInfo', custom_widget_service.get(params)) - - def list(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('CustomWidgetService', metadata) as custom_widget_service: - custom_widget_vos, total_count = custom_widget_service.list(params) - return self.locator.get_info('CustomWidgetsInfo', - custom_widget_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('CustomWidgetService', metadata) as custom_widget_service: - return self.locator.get_info('StatisticsInfo', custom_widget_service.stat(params)) diff --git a/src/spaceone/dashboard/interface/grpc/v1/domain_dashboard.py b/src/spaceone/dashboard/interface/grpc/v1/domain_dashboard.py deleted file mode 100644 index 643c46c..0000000 --- a/src/spaceone/dashboard/interface/grpc/v1/domain_dashboard.py +++ /dev/null @@ -1,77 +0,0 @@ -from spaceone.api.dashboard.v1 import domain_dashboard_pb2, domain_dashboard_pb2_grpc -from spaceone.core.pygrpc import BaseAPI - - -class DomainDashboard(BaseAPI, domain_dashboard_pb2_grpc.DomainDashboardServicer): - pb2 = domain_dashboard_pb2 - pb2_grpc = domain_dashboard_pb2_grpc - - def create(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('DomainDashboardService', metadata) as domain_dashboard_service: - return self.locator.get_info('DomainDashboardInfo', domain_dashboard_service.create(params)) - - def update(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('DomainDashboardService', metadata) as domain_dashboard_service: - return self.locator.get_info('DomainDashboardInfo', domain_dashboard_service.update(params)) - - def delete(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('DomainDashboardService', metadata) as domain_dashboard_service: - domain_dashboard_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('DomainDashboardService', metadata) as domain_dashboard_service: - return self.locator.get_info('DomainDashboardInfo', domain_dashboard_service.get(params)) - - def delete_version(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('DomainDashboardService', metadata) as domain_dashboard_service: - domain_dashboard_service.delete_version(params) - return self.locator.get_info('EmptyInfo') - - def revert_version(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('DomainDashboardService', metadata) as domain_dashboard_service: - return self.locator.get_info('DomainDashboardInfo', domain_dashboard_service.revert_version(params)) - - def get_version(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('DomainDashboardService', metadata) as domain_dashboard_service: - return self.locator.get_info('DomainDashboardVersionInfo', domain_dashboard_service.get_version(params)) - - def list_versions(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('DomainDashboardService', metadata) as domain_dashboard_service: - domain_dashboard_version_vos, total_count, dashboard_version = domain_dashboard_service.list_versions(params) - return self.locator.get_info('DomainDashboardVersionsInfo', - domain_dashboard_version_vos, - total_count, - minimal=self.get_minimal(params), latest_version=dashboard_version) - - def list(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('DomainDashboardService', metadata) as domain_dashboard_service: - domain_dashboard_vos, total_count = domain_dashboard_service.list(params) - return self.locator.get_info('DomainDashboardsInfo', - domain_dashboard_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('DomainDashboardService', metadata) as domain_dashboard_service: - return self.locator.get_info('StatisticsInfo', domain_dashboard_service.stat(params)) diff --git a/src/spaceone/dashboard/interface/grpc/v1/project_dashboard.py b/src/spaceone/dashboard/interface/grpc/v1/project_dashboard.py deleted file mode 100644 index 01e1275..0000000 --- a/src/spaceone/dashboard/interface/grpc/v1/project_dashboard.py +++ /dev/null @@ -1,78 +0,0 @@ -from spaceone.api.dashboard.v1 import project_dashboard_pb2, project_dashboard_pb2_grpc -from spaceone.core.pygrpc import BaseAPI - - -class ProjectDashboard(BaseAPI, project_dashboard_pb2_grpc.ProjectDashboardServicer): - pb2 = project_dashboard_pb2 - pb2_grpc = project_dashboard_pb2_grpc - - def create(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('ProjectDashboardService', metadata) as project_dashboard_service: - return self.locator.get_info('ProjectDashboardInfo', project_dashboard_service.create(params)) - - def update(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('ProjectDashboardService', metadata) as project_dashboard_service: - return self.locator.get_info('ProjectDashboardInfo', project_dashboard_service.update(params)) - - def delete(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('ProjectDashboardService', metadata) as project_dashboard_service: - project_dashboard_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('ProjectDashboardService', metadata) as project_dashboard_service: - return self.locator.get_info('ProjectDashboardInfo', project_dashboard_service.get(params)) - - def delete_version(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('ProjectDashboardService', metadata) as project_dashboard_service: - project_dashboard_service.delete_version(params) - return self.locator.get_info('EmptyInfo') - - def revert_version(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('ProjectDashboardService', metadata) as project_dashboard_service: - return self.locator.get_info('ProjectDashboardInfo', project_dashboard_service.revert_version(params)) - - def get_version(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('ProjectDashboardService', metadata) as project_dashboard_service: - return self.locator.get_info('ProjectDashboardVersionInfo', project_dashboard_service.get_version(params)) - - def list_versions(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('ProjectDashboardService', metadata) as project_dashboard_service: - project_dashboard_version_vos, total_count, dashboard_version = project_dashboard_service.list_versions( - params) - return self.locator.get_info('ProjectDashboardVersionsInfo', - project_dashboard_version_vos, - total_count, - minimal=self.get_minimal(params), latest_version=dashboard_version) - - def list(self, request, context): - params, metadata = self.parse_request(request, context) - - with self.locator.get_service('ProjectDashboardService', metadata) as project_dashboard_service: - project_dashboard_vos, total_count = project_dashboard_service.list(params) - return self.locator.get_info('ProjectDashboardsInfo', - project_dashboard_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('ProjectDashboardService', metadata) as project_dashboard_service: - return self.locator.get_info('StatisticsInfo', project_dashboard_service.stat(params)) diff --git a/src/spaceone/dashboard/manager/__init__.py b/src/spaceone/dashboard/manager/__init__.py index bf8fd73..0ad11a9 100644 --- a/src/spaceone/dashboard/manager/__init__.py +++ b/src/spaceone/dashboard/manager/__init__.py @@ -1,5 +1,3 @@ -from spaceone.dashboard.manager.domain_dashboard_manager import DomainDashboardManager -from spaceone.dashboard.manager.project_dashboard_manager import ProjectDashboardManager -from spaceone.dashboard.manager.domain_dashboard_version_manager import DomainDashboardVersionManager -from spaceone.dashboard.manager.project_dashboard_version_manager import ProjectDashboardVersionManager -from spaceone.dashboard.manager.custom_widget_manager import CustomWidgetManager \ No newline at end of file +from spaceone.dashboard.manager.dashboard_manager import DashboardManager +from spaceone.dashboard.manager.dashboard_version_manager import DashboardVersionManager +from spaceone.dashboard.manager.identity_manager import IdentityManager diff --git a/src/spaceone/dashboard/manager/custom_widget_manager.py b/src/spaceone/dashboard/manager/custom_widget_manager.py deleted file mode 100644 index 83235ba..0000000 --- a/src/spaceone/dashboard/manager/custom_widget_manager.py +++ /dev/null @@ -1,52 +0,0 @@ -import logging -from spaceone.core.manager import BaseManager -from spaceone.dashboard.model.custom_widget_model import CustomWidget - -_LOGGER = logging.getLogger(__name__) - - -class CustomWidgetManager(BaseManager): - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.custom_widget_model: CustomWidget = self.locator.get_model('CustomWidget') - - def create_custom_widget(self, params): - def _rollback(custom_widget_vo): - _LOGGER.info(f'[create_custom_widget._rollback] ' - f'Delete custom_widget_vo : {custom_widget_vo.title} ' - f'({custom_widget_vo.custom_widget_id})') - custom_widget_vo.delete() - - custom_widget_vo: CustomWidget = self.custom_widget_model.create(params) - self.transaction.add_rollback(_rollback, custom_widget_vo) - - return custom_widget_vo - - def update_custom_widget(self, params): - custom_widget_vo: CustomWidget = self.get_custom_widget(params['custom_widget_id'], params['domain_id']) - return self.update_custom_widget_by_vo(params, custom_widget_vo) - - def update_custom_widget_by_vo(self, params, custom_widget_vo): - def _rollback(old_data): - _LOGGER.info(f'[update_custom_widget_by_vo._rollback] Revert Data : ' - f'{old_data["custom_widget_id"]}') - custom_widget_vo.update(old_data) - - self.transaction.add_rollback(_rollback, custom_widget_vo.to_dict()) - return custom_widget_vo.update(params) - - def delete_custom_widget(self, custom_widget_id, domain_id): - custom_widget_vo: CustomWidget = self.get_custom_widget(custom_widget_id, domain_id) - custom_widget_vo.delete() - - def get_custom_widget(self, custom_widget_id, domain_id, only=None): - return self.custom_widget_model.get(custom_widget_id=custom_widget_id, domain_id=domain_id, only=only) - - def list_custom_widgets(self, query=None): - if query is None: - query = {} - return self.custom_widget_model.query(**query) - - def stat_custom_widgets(self, query): - return self.custom_widget_model.stat(**query) diff --git a/src/spaceone/dashboard/manager/dashboard_manager.py b/src/spaceone/dashboard/manager/dashboard_manager.py new file mode 100644 index 0000000..56a62c3 --- /dev/null +++ b/src/spaceone/dashboard/manager/dashboard_manager.py @@ -0,0 +1,87 @@ +import logging +from spaceone.core.manager import BaseManager +from spaceone.dashboard.model.dashboard_model import Dashboard + +_LOGGER = logging.getLogger(__name__) + + +class DashboardManager(BaseManager): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.dashboard_model: Dashboard = self.locator.get_model("Dashboard") + + def create_dashboard(self, params: dict) -> Dashboard: + def _rollback(vo: Dashboard) -> None: + _LOGGER.info( + f"[create_dashboard._rollback] " + f"Delete vo : {vo.name} " + f"({vo.dashboard_id})" + ) + vo.delete() + + dashboard_vo: Dashboard = self.dashboard_model.create(params) + self.transaction.add_rollback(_rollback, dashboard_vo) + + return dashboard_vo + + def update_dashboard(self, params: dict) -> Dashboard: + dashboard_vo: Dashboard = self.get_dashboard( + params["dashboard_id"], params["domain_id"] + ) + return self.update_dashboard_by_vo(params, dashboard_vo) + + def update_dashboard_by_vo( + self, params: dict, dashboard_vo: Dashboard + ) -> Dashboard: + def _rollback(old_data: dict) -> None: + _LOGGER.info( + f"[update_dashboard_by_vo._rollback] Revert Data : " + f'{old_data["dashboard_id"]}' + ) + dashboard_vo.update(old_data) + + self.transaction.add_rollback(_rollback, dashboard_vo.to_dict()) + return dashboard_vo.update(params) + + def delete_dashboard(self, dashboard_id: str, domain_id: str) -> None: + dashboard_vo: Dashboard = self.get_dashboard(dashboard_id, domain_id) + dashboard_vo.delete() + + def get_dashboard( + self, + dashboard_id: str, + domain_id: str, + workspace_id: str = None, + user_projects=None, + ) -> Dashboard: + conditions = {"dashboard_id": dashboard_id, "domain_id": domain_id} + + if workspace_id: + conditions["workspace_id"] = workspace_id + + if user_projects: + conditions["project_id"] = user_projects + + return self.dashboard_model.get(**conditions) + + def list_dashboards(self, query=None): + if query is None: + query = {} + return self.dashboard_model.query(**query) + + def stat_dashboards(self, query: dict) -> dict: + return self.dashboard_model.stat(**query) + + def increase_version(self, dashboard_vo: Dashboard) -> None: + def _rollback(vo: Dashboard): + _LOGGER.info( + f"[increase_version._rollback] Decrease Version : " f"{vo.dashboard_id}" + ) + vo.decrement("version") + + dashboard_vo.increment("version") + self.transaction.add_rollback(_rollback, dashboard_vo) + + @staticmethod + def delete_by_dashboard_vo(dashboard_vo: Dashboard) -> None: + dashboard_vo.delete() diff --git a/src/spaceone/dashboard/manager/dashboard_version_manager.py b/src/spaceone/dashboard/manager/dashboard_version_manager.py new file mode 100644 index 0000000..8da9cf2 --- /dev/null +++ b/src/spaceone/dashboard/manager/dashboard_version_manager.py @@ -0,0 +1,72 @@ +import logging +from spaceone.core.manager import BaseManager +from spaceone.dashboard.model import DashboardVersion +from spaceone.dashboard.model import Dashboard + +_LOGGER = logging.getLogger(__name__) + + +class DashboardVersionManager(BaseManager): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.dashboard_version_model: DashboardVersion = self.locator.get_model( + "DashboardVersion" + ) + + def create_version_by_dashboard_vo( + self, dashboard_vo: Dashboard, params: dict + ) -> DashboardVersion: + def _rollback(vo: DashboardVersion) -> None: + _LOGGER.info( + f"[create_dashboard_version._rollback] " + f"Delete dashboard_version_vo : {vo.version} " + f"({vo.dashboard_id})" + ) + vo.delete() + + new_params = { + "dashboard_id": dashboard_vo.dashboard_id, + "version": dashboard_vo.version, + "layouts": params.get("layouts") + if params.get("layouts") + else dashboard_vo.layouts, + "variables": params.get("variables") + if params.get("variables") + else dashboard_vo.variables, + "settings": params.get("settings") + if params.get("settings") + else dashboard_vo.settings, + "variables_schema": params.get("variables_schema") + if params.get("variables_schema") + else dashboard_vo.variables_schema, + "domain_id": dashboard_vo.domain_id, + } + + version_vo: DashboardVersion = self.dashboard_version_model.create(new_params) + self.transaction.add_rollback(_rollback, version_vo) + return version_vo + + def delete_version(self, dashboard_id: str, version: int, domain_id: str) -> None: + version_vo: DashboardVersion = self.get_version( + dashboard_id, version, domain_id + ) + version_vo.delete() + + @staticmethod + def delete_versions_by_dashboard_version_vos(domain_dashboard_version_vos): + domain_dashboard_version_vos.delete() + + def get_version(self, dashboard_id, version, domain_id): + return self.dashboard_version_model.get( + dashboard_id=dashboard_id, + version=version, + domain_id=domain_id, + ) + + def list_versions(self, query: dict = None) -> dict: + if query is None: + query = {} + return self.dashboard_version_model.query(**query) + + def filter_versions(self, **conditions) -> dict: + return self.dashboard_version_model.filter(**conditions) diff --git a/src/spaceone/dashboard/manager/domain_dashboard_manager.py b/src/spaceone/dashboard/manager/domain_dashboard_manager.py deleted file mode 100644 index aa9e0df..0000000 --- a/src/spaceone/dashboard/manager/domain_dashboard_manager.py +++ /dev/null @@ -1,66 +0,0 @@ -import logging -from spaceone.core.manager import BaseManager -from spaceone.dashboard.model.domain_dashboard_model import DomainDashboard - -_LOGGER = logging.getLogger(__name__) - - -class DomainDashboardManager(BaseManager): - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.domain_dashboard_model: DomainDashboard = self.locator.get_model('DomainDashboard') - - def create_domain_dashboard(self, params): - def _rollback(domain_dashboard_vo): - _LOGGER.info(f'[create_domain_dashboard._rollback] ' - f'Delete domain_dashboard_vo : {domain_dashboard_vo.name} ' - f'({domain_dashboard_vo.domain_dashboard_id})') - domain_dashboard_vo.delete() - - domain_dashboard_vo: DomainDashboard = self.domain_dashboard_model.create(params) - self.transaction.add_rollback(_rollback, domain_dashboard_vo) - - return domain_dashboard_vo - - def update_domain_dashboard(self, params): - domain_dashboard_vo: DomainDashboard = self.get_domain_dashboard(params['domain_dashboard_id'], - params['domain_id']) - return self.update_domain_dashboard_by_vo(params, domain_dashboard_vo) - - def update_domain_dashboard_by_vo(self, params, domain_dashboard_vo): - def _rollback(old_data): - _LOGGER.info(f'[update_domain_dashboard_by_vo._rollback] Revert Data : ' - f'{old_data["domain_dashboard_id"]}') - domain_dashboard_vo.update(old_data) - - self.transaction.add_rollback(_rollback, domain_dashboard_vo.to_dict()) - return domain_dashboard_vo.update(params) - - def delete_domain_dashboard(self, domain_dashboard_id, domain_id): - domain_dashboard_vo: DomainDashboard = self.get_domain_dashboard(domain_dashboard_id, domain_id) - domain_dashboard_vo.delete() - - def get_domain_dashboard(self, domain_dashboard_id, domain_id, only=None): - return self.domain_dashboard_model.get(domain_dashboard_id=domain_dashboard_id, domain_id=domain_id, only=only) - - def list_domain_dashboards(self, query=None): - if query is None: - query = {} - return self.domain_dashboard_model.query(**query) - - def stat_domain_dashboards(self, query): - return self.domain_dashboard_model.stat(**query) - - def increase_version(self, domain_dashboard_vo): - def _rollback(vo: DomainDashboard): - _LOGGER.info(f'[increase_version._rollback] Decrease Version : ' - f'{vo.domain_dashboard_id}') - vo.decrement('version') - - domain_dashboard_vo.increment('version') - self.transaction.add_rollback(_rollback, domain_dashboard_vo) - - @staticmethod - def delete_by_domain_dashboard_vo(domain_dashboard_vo): - domain_dashboard_vo.delete() diff --git a/src/spaceone/dashboard/manager/domain_dashboard_version_manager.py b/src/spaceone/dashboard/manager/domain_dashboard_version_manager.py deleted file mode 100644 index c060c32..0000000 --- a/src/spaceone/dashboard/manager/domain_dashboard_version_manager.py +++ /dev/null @@ -1,54 +0,0 @@ -import logging -from spaceone.core.manager import BaseManager -from spaceone.dashboard.model import DomainDashboardVersion - -_LOGGER = logging.getLogger(__name__) - - -class DomainDashboardVersionManager(BaseManager): - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.domain_dashboard_version_model: DomainDashboardVersion = self.locator.get_model('DomainDashboardVersion') - - def create_version_by_domain_dashboard_vo(self, domain_dashboard_vo, params): - def _rollback(version_vo): - _LOGGER.info(f'[create_domain_dashboard_version._rollback] ' - f'Delete domain_dashboard_version_vo : {version_vo.version} ' - f'({version_vo.domain_dashboard_id})') - version_vo.delete() - - new_params = { - 'domain_dashboard_id': domain_dashboard_vo.domain_dashboard_id, - 'version': domain_dashboard_vo.version, - 'layouts': params.get('layouts') if params.get('layouts') else domain_dashboard_vo.layouts, - 'variables': params.get('variables') if params.get( - 'variables') else domain_dashboard_vo.variables, - 'settings': params.get('settings') if params.get('settings') else domain_dashboard_vo.settings, - 'variables_schema': params.get('variables_schema') if params.get( - 'variables_schema') else domain_dashboard_vo.variables_schema, - 'domain_id': domain_dashboard_vo.domain_id - } - - version_vo: DomainDashboardVersion = self.domain_dashboard_version_model.create(new_params) - self.transaction.add_rollback(_rollback, version_vo) - return version_vo - - def delete_version(self, domain_dashboard_id, version, domain_id): - version_vo: DomainDashboardVersion = self.get_version(domain_dashboard_id, version, domain_id) - version_vo.delete() - - def delete_versions_by_domain_dashboard_version_vos(self, domain_dashboard_version_vos): - domain_dashboard_version_vos.delete() - - def get_version(self, domain_dashboard_id, version, domain_id, only=None): - return self.domain_dashboard_version_model.get(domain_dashboard_id=domain_dashboard_id, version=version, - domain_id=domain_id, only=only) - - def list_versions(self, query=None): - if query is None: - query = {} - return self.domain_dashboard_version_model.query(**query) - - def filter_versions(self, **conditions): - return self.domain_dashboard_version_model.filter(**conditions) diff --git a/src/spaceone/dashboard/manager/identity_manager.py b/src/spaceone/dashboard/manager/identity_manager.py new file mode 100644 index 0000000..00b5a9f --- /dev/null +++ b/src/spaceone/dashboard/manager/identity_manager.py @@ -0,0 +1,19 @@ +from spaceone.core.manager import BaseManager +from spaceone.core.connector.space_connector import SpaceConnector + + +class IdentityManager(BaseManager): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.identity_conn: SpaceConnector = self.locator.get_connector( + "SpaceConnector", service="identity" + ) + + def check_workspace(self, workspace_id: str, domain_id: str) -> None: + return self.identity_conn.dispatch( + "Workspace.check", + {"workspace_id": workspace_id, "domain_id": domain_id}, + ) + + def get_project(self, project_id: str) -> dict: + return self.identity_conn.dispatch("Project.get", {"project_id": project_id}) diff --git a/src/spaceone/dashboard/manager/project_dashboard_manager.py b/src/spaceone/dashboard/manager/project_dashboard_manager.py deleted file mode 100644 index 2cd28c3..0000000 --- a/src/spaceone/dashboard/manager/project_dashboard_manager.py +++ /dev/null @@ -1,67 +0,0 @@ -import logging -from spaceone.core.manager import BaseManager -from spaceone.dashboard.model.project_dashboard_model import ProjectDashboard - -_LOGGER = logging.getLogger(__name__) - - -class ProjectDashboardManager(BaseManager): - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.project_dashboard_model: ProjectDashboard = self.locator.get_model('ProjectDashboard') - - def create_project_dashboard(self, params): - def _rollback(project_dashboard_vo): - _LOGGER.info(f'[create_project_dashboard._rollback] ' - f'Delete project_dashboard_vo : {project_dashboard_vo.name} ' - f'({project_dashboard_vo.project_dashboard_id})') - project_dashboard_vo.delete() - - project_dashboard_vo: ProjectDashboard = self.project_dashboard_model.create(params) - self.transaction.add_rollback(_rollback, project_dashboard_vo) - - return project_dashboard_vo - - def update_project_dashboard(self, params): - project_dashboard_vo: ProjectDashboard = self.get_project_dashboard(params['project_dashboard_id'], - params['domain_id']) - return self.update_project_dashboard_by_vo(params, project_dashboard_vo) - - def update_project_dashboard_by_vo(self, params, project_dashboard_vo): - def _rollback(old_data): - _LOGGER.info(f'[update_project_dashboard_by_vo._rollback] Revert Data : ' - f'{old_data["project_dashboard_id"]}') - project_dashboard_vo.update(old_data) - - self.transaction.add_rollback(_rollback, project_dashboard_vo.to_dict()) - return project_dashboard_vo.update(params) - - def delete_project_dashboard(self, project_dashboard_id, domain_id): - project_dashboard_vo: ProjectDashboard = self.get_project_dashboard(project_dashboard_id, domain_id) - project_dashboard_vo.delete() - - def get_project_dashboard(self, project_dashboard_id, domain_id, only=None): - return self.project_dashboard_model.get(project_dashboard_id=project_dashboard_id, domain_id=domain_id, - only=only) - - def list_project_dashboards(self, query=None): - if query is None: - query = {} - return self.project_dashboard_model.query(**query) - - def stat_project_dashboards(self, query): - return self.project_dashboard_model.stat(**query) - - def increase_version(self, project_dashboard_vo): - def _rollback(vo: ProjectDashboard): - _LOGGER.info(f'[increase_version._rollback] Decrease Version : ' - f'{vo.project_dashboard_id}') - vo.decrement('version') - - project_dashboard_vo.increment('version') - self.transaction.add_rollback(_rollback, project_dashboard_vo) - - @staticmethod - def delete_by_project_dashboard_vo(project_dashboard_vo): - project_dashboard_vo.delete() diff --git a/src/spaceone/dashboard/manager/project_dashboard_version_manager.py b/src/spaceone/dashboard/manager/project_dashboard_version_manager.py deleted file mode 100644 index 92d7681..0000000 --- a/src/spaceone/dashboard/manager/project_dashboard_version_manager.py +++ /dev/null @@ -1,55 +0,0 @@ -import logging -from spaceone.core.manager import BaseManager -from spaceone.dashboard.model import ProjectDashboardVersion - -_LOGGER = logging.getLogger(__name__) - - -class ProjectDashboardVersionManager(BaseManager): - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.project_dashboard_version_model: ProjectDashboardVersion = self.locator.get_model( - 'ProjectDashboardVersion') - - def create_version_by_project_dashboard_vo(self, project_dashboard_vo, params): - def _rollback(version_vo): - _LOGGER.info(f'[create_project_dashboard_version._rollback] ' - f'Delete project_dashboard_version_vo : {version_vo.version} ' - f'({version_vo.project_dashboard_id})') - version_vo.delete() - - new_params = { - 'project_dashboard_id': project_dashboard_vo.project_dashboard_id, - 'version': project_dashboard_vo.version, - 'layouts': params.get('layouts') if params.get('layouts') else project_dashboard_vo.layouts, - 'variables': params.get('variables') if params.get( - 'variables') else project_dashboard_vo.variables, - 'settings': params.get('settings') if params.get('settings') else project_dashboard_vo.settings, - 'variables_schema': params.get('variables_schema') if params.get( - 'variables_schema') else project_dashboard_vo.variables_schema, - 'domain_id': project_dashboard_vo.domain_id - } - - version_vo: ProjectDashboardVersion = self.project_dashboard_version_model.create(new_params) - self.transaction.add_rollback(_rollback, version_vo) - return version_vo - - def delete_version(self, project_dashboard_id, version, domain_id): - version_vo: ProjectDashboardVersion = self.get_version(project_dashboard_id, version, domain_id) - version_vo.delete() - - def delete_versions_by_project_dashboard_version_vos(self, project_dashboard_version_vos): - project_dashboard_version_vos.delete() - - def get_version(self, project_dashboard_id, version, domain_id, only=None): - return self.project_dashboard_version_model.get(project_dashboard_id=project_dashboard_id, version=version, - domain_id=domain_id, only=only) - - def list_versions(self, query=None): - if query is None: - query = {} - return self.project_dashboard_version_model.query(**query) - - def filter_versions(self, **conditions): - return self.project_dashboard_version_model.filter(**conditions) diff --git a/src/spaceone/dashboard/model/__init__.py b/src/spaceone/dashboard/model/__init__.py index 2fc3269..b88d840 100644 --- a/src/spaceone/dashboard/model/__init__.py +++ b/src/spaceone/dashboard/model/__init__.py @@ -1,5 +1,2 @@ -from spaceone.dashboard.model.domain_dashboard_model import DomainDashboard -from spaceone.dashboard.model.project_dashboard_model import ProjectDashboard -from spaceone.dashboard.model.domain_dashboard_version_model import DomainDashboardVersion -from spaceone.dashboard.model.project_dashboard_version_model import ProjectDashboardVersion -from spaceone.dashboard.model.custom_widget_model import CustomWidget \ No newline at end of file +from spaceone.dashboard.model.dashboard_model import Dashboard +from spaceone.dashboard.model.dashboard_version_model import DashboardVersion diff --git a/src/spaceone/dashboard/model/custom_widget_model.py b/src/spaceone/dashboard/model/custom_widget_model.py deleted file mode 100644 index 9b42c15..0000000 --- a/src/spaceone/dashboard/model/custom_widget_model.py +++ /dev/null @@ -1,47 +0,0 @@ -from mongoengine import * - -from spaceone.core.model.mongo_model import MongoModel - - -class CustomWidget(MongoModel): - custom_widget_id = StringField(max_length=40, generate_id='custom-widget', unique=True) - widget_name = StringField(max_length=40) - title = StringField(max_length=255) - version = StringField(max_length=40) - options = DictField(default={}) - settings = DictField(default={}) - inherit_options = DictField(default={}) - labels = ListField(StringField()) - tags = DictField(default={}) - user_id = StringField(max_length=40) - domain_id = StringField(max_length=40) - created_at = DateTimeField(auto_now_add=True) - updated_at = DateTimeField(auto_now=True) - - meta = { - 'updatable_fields': [ - 'widget_name', - 'title', - 'options', - 'settings', - 'inherit_options', - 'labels', - 'tags' - ], - 'minimal_fields': [ - 'custom_widget_id', - 'widget_name', - 'title', - 'version', - 'user_id', - 'domain_id' - ], - 'ordering': ['title'], - 'indexes': [ - 'widget_name', - 'title', - 'version', - 'user_id', - 'domain_id' - ] - } diff --git a/src/spaceone/dashboard/model/dashboard_model.py b/src/spaceone/dashboard/model/dashboard_model.py new file mode 100644 index 0000000..49a32fa --- /dev/null +++ b/src/spaceone/dashboard/model/dashboard_model.py @@ -0,0 +1,85 @@ +from mongoengine import * +from spaceone.core.error import ERROR_NOT_UNIQUE + +from spaceone.core.model.mongo_model import MongoModel + + +class Dashboard(MongoModel): + dashboard_id = StringField(max_length=40, generate_id="dash", unique=True) + name = StringField(max_length=100) + dashboard_type = StringField(max_length=40, choices=("PUBLIC", "PRIVATE")) + version = IntField(default=1) + layouts = ListField(default=[]) + variables = DictField(default={}) + settings = DictField(default={}) + variables_schema = DictField(default={}) + labels = ListField(StringField()) + tags = DictField(default={}) + resource_group = StringField( + max_length=40, choices=("DOMAIN", "WORKSPACE", "PROJECT") + ) + user_id = StringField(max_length=40) + project_id = StringField(max_length=40) + workspace_id = StringField(max_length=40) + domain_id = StringField(max_length=40) + created_at = DateTimeField(auto_now_add=True) + updated_at = DateTimeField(auto_now=True) + + meta = { + "updatable_fields": [ + "name", + "layouts", + "variables", + "settings", + "variables_schema", + "labels", + "tags", + ], + "minimal_fields": [ + "dashboard_id", + "name", + "dashboard_type", + "version", + "resource_group", + "user_id", + "project_id", + "workspace_id", + "domain_id", + ], + "ordering": ["name"], + "indexes": [ + "name", + "dashboard_type", + "labels", + "resource_group", + "user_id", + "project_id", + "workspace_id", + "domain_id", + ], + } + + @classmethod + def create(cls, data): + dashboard_vos = cls.filter( + name=data["name"], user_id=data.get("user_id"), domain_id=data["domain_id"] + ) + + if dashboard_vos.count() > 0: + raise ERROR_NOT_UNIQUE(key="name", value=data["name"]) + return super().create(data) + + def update(self, data): + if "name" in data: + dashboard_vos = self.filter( + name=data["name"], + user_id=self.user_id, + domain_id=self.domain_id, + dashboard_id__ne=self.dashboard_id, + ) + + if dashboard_vos.count() > 0: + raise ERROR_NOT_UNIQUE(key="name", value=data["name"]) + else: + return super().update(data) + return super().update(data) diff --git a/src/spaceone/dashboard/model/dashboard_version_model.py b/src/spaceone/dashboard/model/dashboard_version_model.py new file mode 100644 index 0000000..e059db6 --- /dev/null +++ b/src/spaceone/dashboard/model/dashboard_version_model.py @@ -0,0 +1,26 @@ +from mongoengine import * + +from spaceone.core.model.mongo_model import MongoModel + + +class DashboardVersion(MongoModel): + dashboard_id = StringField(max_length=40) + version = IntField() + layouts = ListField(default=[]) + variables = DictField(default={}) + settings = DictField(default={}) + variables_schema = DictField(default={}) + domain_id = StringField(max_length=40) + created_at = DateTimeField(auto_now_add=True) + + meta = { + "updatable_fields": [], + "minimal_fields": [ + "dashboard_id", + "version", + "domain_id", + "created_at", + ], + "ordering": ["-version"], + "indexes": ["dashboard_id", "version", "domain_id", "created_at"], + } diff --git a/src/spaceone/dashboard/model/domain_dashboard_model.py b/src/spaceone/dashboard/model/domain_dashboard_model.py deleted file mode 100644 index f235efa..0000000 --- a/src/spaceone/dashboard/model/domain_dashboard_model.py +++ /dev/null @@ -1,68 +0,0 @@ -from mongoengine import * -from spaceone.core.error import ERROR_NOT_UNIQUE - -from spaceone.core.model.mongo_model import MongoModel - - -class DomainDashboard(MongoModel): - domain_dashboard_id = StringField(max_length=40, generate_id='domain-dash', unique=True) - name = StringField(max_length=100) - viewers = StringField(max_length=255, choices=('PUBLIC', 'PRIVATE')) - version = IntField(default=1) - layouts = ListField(default=[]) - variables = DictField(default={}) - settings = DictField(default={}) - variables_schema = DictField(default={}) - labels = ListField(StringField()) - tags = DictField(default={}) - user_id = StringField(max_length=40) - domain_id = StringField(max_length=40) - created_at = DateTimeField(auto_now_add=True) - updated_at = DateTimeField(auto_now=True) - - meta = { - 'updatable_fields': [ - 'name', - 'layouts', - 'variables', - 'settings', - 'variables_schema', - 'labels', - 'tags' - ], - 'minimal_fields': [ - 'domain_dashboard_id', - 'name', - 'viewers', - 'version', - 'user_id', - 'domain_id' - ], - 'ordering': ['name'], - 'indexes': [ - 'name', - 'viewers', - 'labels', - 'user_id', - 'domain_id' - ] - } - - @classmethod - def create(cls, data): - domain_dashboard_vos = cls.filter(name=data['name'], user_id=data['user_id'], domain_id=data['domain_id']) - print(domain_dashboard_vos) - if domain_dashboard_vos.count() > 0: - raise ERROR_NOT_UNIQUE(key='name', value=data['name']) - return super().create(data) - - def update(self, data): - if 'name' in data: - domain_dashboard_vos = self.filter(name=data['name'], user_id=self.user_id, domain_id=self.domain_id, - domain_dashboard_id__ne=self.domain_dashboard_id) - - if domain_dashboard_vos.count() > 0: - raise ERROR_NOT_UNIQUE(key='name', value=data['name']) - else: - return super().update(data) - return super().update(data) diff --git a/src/spaceone/dashboard/model/domain_dashboard_version_model.py b/src/spaceone/dashboard/model/domain_dashboard_version_model.py deleted file mode 100644 index 217533a..0000000 --- a/src/spaceone/dashboard/model/domain_dashboard_version_model.py +++ /dev/null @@ -1,31 +0,0 @@ -from mongoengine import * - -from spaceone.core.model.mongo_model import MongoModel - - -class DomainDashboardVersion(MongoModel): - domain_dashboard_id = StringField(max_length=40) - version = IntField() - layouts = ListField(default=[]) - variables = DictField(default={}) - settings = DictField(default={}) - variables_schema = DictField(default={}) - domain_id = StringField(max_length=40) - created_at = DateTimeField(auto_now_add=True) - - meta = { - 'updatable_fields': [], - 'minimal_fields': [ - 'domain_dashboard_id', - 'version', - 'domain_id', - 'created_at', - ], - 'ordering': ['-version'], - 'indexes': [ - 'domain_dashboard_id', - 'version', - 'domain_id', - 'created_at' - ] - } diff --git a/src/spaceone/dashboard/model/project_dashboard_model.py b/src/spaceone/dashboard/model/project_dashboard_model.py deleted file mode 100644 index 44dd3b6..0000000 --- a/src/spaceone/dashboard/model/project_dashboard_model.py +++ /dev/null @@ -1,75 +0,0 @@ -from mongoengine import * -from spaceone.core.error import ERROR_NOT_UNIQUE - -from spaceone.core.model.mongo_model import MongoModel - - -class ProjectDashboard(MongoModel): - project_dashboard_id = StringField(max_length=40, generate_id='project-dash', unique=True) - name = StringField(max_length=100) - viewers = StringField(max_length=255, choices=('PUBLIC', 'PRIVATE')) - version = IntField(default=1) - layouts = ListField(default=[]) - variables = DictField(default={}) - settings = DictField(default={}) - variables_schema = DictField(default={}) - labels = ListField(StringField()) - tags = DictField(default={}) - user_id = StringField(max_length=40) - project_id = StringField(max_length=40) - domain_id = StringField(max_length=40) - created_at = DateTimeField(auto_now_add=True) - updated_at = DateTimeField(auto_now=True) - - meta = { - 'updatable_fields': [ - 'name', - 'layouts', - 'variables', - 'settings', - 'variables_schema', - 'labels', - 'tags' - ], - 'minimal_fields': [ - 'project_dashboard_id', - 'name', - 'viewers', - 'version', - 'project_id', - 'user_id', - 'domain_id' - ], - 'change_query_keys': { - 'user_projects': 'project_id' - }, - 'ordering': ['name'], - 'indexes': [ - 'name', - 'viewers', - 'labels', - 'project_id', - 'user_id', - 'domain_id' - ] - } - - @classmethod - def create(cls, data): - project_dashboard_vos = cls.filter(name=data['name'], project_id=data['project_id'], - user_id=data['user_id'], domain_id=data['domain_id']) - if project_dashboard_vos.count() > 0: - raise ERROR_NOT_UNIQUE(key='name', value=data['name']) - return super().create(data) - - def update(self, data): - if 'name' in data: - project_dashboard_vos = self.filter(name=data['name'], project_id=self.project_id, - user_id=self.user_id, domain_id=self.domain_id, - project_dashboard_id__ne=self.project_dashboard_id) - - if project_dashboard_vos.count() > 0: - raise ERROR_NOT_UNIQUE(key='name', value=data['name']) - else: - return super().update(data) - return super().update(data) diff --git a/src/spaceone/dashboard/model/project_dashboard_version_model.py b/src/spaceone/dashboard/model/project_dashboard_version_model.py deleted file mode 100644 index 4d664dd..0000000 --- a/src/spaceone/dashboard/model/project_dashboard_version_model.py +++ /dev/null @@ -1,31 +0,0 @@ -from mongoengine import * - -from spaceone.core.model.mongo_model import MongoModel - - -class ProjectDashboardVersion(MongoModel): - project_dashboard_id = StringField(max_length=40) - version = IntField() - layouts = ListField(default=[]) - variables = DictField(default={}) - settings = DictField(default={}) - variables_schema = DictField(default={}) - domain_id = StringField(max_length=40) - created_at = DateTimeField(auto_now_add=True) - - meta = { - 'updatable_fields': [], - 'minimal_fields': [ - 'project_dashboard_id', - 'version', - 'domain_id', - 'created_at', - ], - 'ordering': ['-version'], - 'indexes': [ - 'project_dashboard_id', - 'version', - 'domain_id', - 'created_at' - ] - } diff --git a/src/spaceone/dashboard/service/__init__.py b/src/spaceone/dashboard/service/__init__.py index 9a2aff9..1ba3e9c 100644 --- a/src/spaceone/dashboard/service/__init__.py +++ b/src/spaceone/dashboard/service/__init__.py @@ -1,3 +1 @@ -from spaceone.dashboard.service.domain_dashboard_service import DomainDashboardService -from spaceone.dashboard.service.project_dashboard_service import ProjectDashboardService -from spaceone.dashboard.service.custom_widget_service import CustomWidgetService +from spaceone.dashboard.service.dashboard_service import DashboardService diff --git a/src/spaceone/dashboard/service/custom_widget_service.py b/src/spaceone/dashboard/service/custom_widget_service.py deleted file mode 100644 index 74f20fb..0000000 --- a/src/spaceone/dashboard/service/custom_widget_service.py +++ /dev/null @@ -1,161 +0,0 @@ -import copy -import logging - -from spaceone.core.service import * -from spaceone.dashboard.manager import CustomWidgetManager -from spaceone.dashboard.model import CustomWidget - -_LOGGER = logging.getLogger(__name__) - - -@authentication_handler -@authorization_handler -@mutation_handler -@event_handler -class CustomWidgetService(BaseService): - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.custom_widget_mgr: CustomWidgetManager = self.locator.get_manager('CustomWidgetManager') - - @transaction(append_meta={'authorization.scope': 'USER'}) - @check_required(['widget_name', 'title', 'options', 'domain_id']) - def create(self, params): - """Register widget - - Args: - params (dict): { - 'widget_name': 'str', - 'title': 'str', - 'options': 'dict', - 'settings': 'dict', - 'inherit_options': 'dict', - 'labels': 'list', - 'tags': 'dict', - 'domain_id': 'str' - } - - Returns: - custom_widget_vo (object) - """ - - params['user_id'] = self.transaction.get_meta('user_id') - - return self.custom_widget_mgr.create_custom_widget(params) - - @transaction(append_meta={'authorization.scope': 'USER'}) - @check_required(['custom_widget_id', 'domain_id']) - def update(self, params): - """Update widget - - Args: - params (dict): { - 'custom_widget_id': 'str', - 'title': 'str', - 'options': 'dict', - 'settings': 'dict', - 'inherit_option': 'dict', - 'labels': 'list', - 'tags': 'dict', - 'domain_id': 'str' - } - - Returns: - custom_widget_vo (object) - """ - custom_widget_id = params['custom_widget_id'] - domain_id = params['domain_id'] - - custom_widget_vo: CustomWidget = self.custom_widget_mgr.get_custom_widget(custom_widget_id, domain_id) - - if 'settings' in params: - params['settings'] = self._merge_settings(custom_widget_vo.settings, params['settings']) - - return self.custom_widget_mgr.update_custom_widget_by_vo(params, custom_widget_vo) - - @transaction(append_meta={'authorization.scope': 'USER'}) - @check_required(['custom_widget_id', 'domain_id']) - def delete(self, params): - """Deregister widget - - Args: - params (dict): { - 'custom_widget_id': 'str', - 'domain_id': 'str' - } - - Returns: - custom_widget_vo (object) - """ - return self.custom_widget_mgr.delete_custom_widget(params['custom_widget_id'], params['domain_id']) - - @transaction(append_meta={'authorization.scope': 'USER'}) - @check_required(['custom_widget_id', 'domain_id']) - def get(self, params): - """Get widget - - Args: - params (dict): { - 'custom_widget_id': 'str', - 'only': 'list', - 'domain_id': 'str' - } - - Returns: - custom_widget_vo (object) - """ - - return self.custom_widget_mgr.get_custom_widget(params['custom_widget_id'], params['domain_id'], - params.get('only')) - - @transaction(append_meta={'authorization.scope': 'USER'}) - @check_required(['domain_id']) - @append_query_filter(['custom_widget_id', 'widget_name', 'title', 'user_id', 'domain_id']) - @append_keyword_filter(['custom_widget_id', 'title']) - def list(self, params): - """List widget - - Args: - params (dict): { - 'custom_widget_id': 'str', - 'widget_name': 'str', - 'title': 'str', - 'user_id': 'str', - 'query': 'dict', - 'domain_id': 'str' - } - - Returns: - custom_widget_vos (object) - """ - query = params.get('query', {}) - return self.custom_widget_mgr.list_custom_widgets(query) - - @transaction(append_meta={'authorization.scope': 'USER'}) - @check_required(['query', 'domain_id']) - @append_query_filter(['domain_id']) - @append_keyword_filter(['custom_widget_id', 'title']) - def stat(self, params): - """ - Args: - params (dict): { - 'domain_id': 'str', - 'query': 'dict (spaceone.api.core.v1.StatisticsQuery)' - } - - Returns: - values (list) : 'list of statistics data' - - """ - query = params.get('query', {}) - return self.custom_widget_mgr.stat_custom_widgets(query) - - @staticmethod - def _merge_settings(old_settings, new_settings): - settings = copy.deepcopy(old_settings) - - if old_settings: - settings.update(new_settings) - return settings - else: - return new_settings diff --git a/src/spaceone/dashboard/service/dashboard_service.py b/src/spaceone/dashboard/service/dashboard_service.py new file mode 100644 index 0000000..7431525 --- /dev/null +++ b/src/spaceone/dashboard/service/dashboard_service.py @@ -0,0 +1,498 @@ +import copy +import logging + +from spaceone.core.service import * +from spaceone.dashboard.manager import ( + DashboardManager, + DashboardVersionManager, + IdentityManager, +) +from spaceone.dashboard.model import Dashboard, DashboardVersion +from spaceone.dashboard.error import * + +_LOGGER = logging.getLogger(__name__) + + +@authentication_handler +@authorization_handler +@mutation_handler +@event_handler +class DashboardService(BaseService): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.dashboard_mgr: DashboardManager = self.locator.get_manager( + "DashboardManager" + ) + self.version_mgr: DashboardVersionManager = self.locator.get_manager( + "DashboardVersionManager" + ) + self.identity_mgr: IdentityManager = self.locator.get_manager("IdentityManager") + + @transaction( + permission="dashboard:Dashboard.write", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"], + ) + @check_required(["name", "dashboard_type", "domain_id"]) + def create(self, params: dict) -> Dashboard: + """Register domain_dashboard + + Args: + params (dict): { + 'name': 'str', # required + 'dashboard_type': 'str', # required + 'layouts': 'list', + 'variables': 'dict', + 'settings': 'dict', + 'variables_schema': 'dict', + 'labels': 'list', + 'tags': 'dict', + 'resource_group': 'str', # required + 'project_id': 'str', + 'workspace_id': 'str', # injected from auth + 'domain_id': 'str' # injected from auth (required) + } + + Returns: + dashboard_vo (object) + """ + + resource_group = params["resource_group"] + project_id = params.get("project_id") + workspace_id = params.get("workspace_id") + domain_id = params["domain_id"] + + # Check permission by resource group + if resource_group == "PROJECT": + project_info = self.identity_mgr.get_project(project_id) + params["workspace_id"] = project_info["workspace_id"] + elif resource_group == "WORKSPACE": + self.identity_mgr.check_workspace(workspace_id, domain_id) + params["project_id"] = "*" + else: + params["workspace_id"] = "*" + params["project_id"] = "*" + + dashboard_vo = self.dashboard_mgr.create_dashboard(params) + + version_keys = ["layouts", "variables", "variables_schema"] + if any(set(version_keys) & set(params.keys())): + self.version_mgr.create_version_by_dashboard_vo(dashboard_vo, params) + + return dashboard_vo + + @transaction( + permission="dashboard:Dashboard.write", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"], + ) + @check_required(["dashboard_id", "domain_id"]) + def update(self, params): + """Update domain_dashboard + + Args: + params (dict): { + 'dashboard_id': 'str', # required + 'name': 'str', + 'layouts': 'list', + 'variables': 'dict', + 'settings': 'dict', + 'variables_schema': 'list', + 'labels': 'list', + 'tags': 'dict', + 'workspace_id': 'str', # injected from auth + 'domain_id': 'str' # injected from auth (required) + 'user_projects': 'list' # injected from auth + } + + Returns: + dashboard_vo (object) + """ + + dashboard_id = params["dashboard_id"] + domain_id = params["domain_id"] + workspace_id = params.get("workspace_id") + user_projects = params.get("user_projects") + user_id = self.transaction.get_meta("authorization.user_id") + + dashboard_vo: Dashboard = self.dashboard_mgr.get_dashboard( + dashboard_id, domain_id, workspace_id, user_projects + ) + + if "name" not in params: + params["name"] = dashboard_vo.name + + if dashboard_vo.dashboard_type == "PRIVATE" and dashboard_vo.user_id != user_id: + raise ERROR_PERMISSION_DENIED() + + if "settings" in params: + params["settings"] = self._merge_settings( + dashboard_vo.settings, params["settings"] + ) + + version_change_keys = ["layouts", "variables", "variables_schema"] + if self._has_version_key_in_params(dashboard_vo, params, version_change_keys): + self.dashboard_mgr.increase_version(dashboard_vo) + self.version_mgr.create_version_by_dashboard_vo(dashboard_vo, params) + + return self.dashboard_mgr.update_dashboard_by_vo(params, dashboard_vo) + + @transaction( + permission="dashboard:Dashboard.write", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"], + ) + @check_required(["dashboard_id", "domain_id"]) + def delete(self, params): + """Deregister domain_dashboard + + Args: + params (dict): { + 'dashboard_id': 'str', # required + 'workspace_id': 'str', # injected from auth + 'domain_id': 'str' # injected from auth (required) + 'user_projects': 'list' # injected from auth + } + + Returns: + None + """ + dashboard_id = params["dashboard_id"] + workspace_id = params.get("workspace_id") + domain_id = params["domain_id"] + user_projects = params.get("user_projects") + user_id = self.transaction.get_meta("authorization.user_id") + + dashboard_vo: Dashboard = self.dashboard_mgr.get_dashboard( + dashboard_id, domain_id, workspace_id, user_projects + ) + + if dashboard_vo.dashboard_type == "PRIVATE" and dashboard_vo.user_id != user_id: + raise ERROR_PERMISSION_DENIED() + + if domain_dashboard_version_vos := self.version_mgr.filter_versions( + dashboard_id=dashboard_vo.dashboard_id, domain_id=domain_id + ): + self.version_mgr.delete_versions_by_dashboard_version_vos( + domain_dashboard_version_vos + ) + + self.dashboard_mgr.delete_by_dashboard_vo(dashboard_vo) + + @transaction( + permission="dashboard:Dashboard.read", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"], + ) + @change_value_by_rule("APPEND", "workspace_id", "*") + @change_value_by_rule("APPEND", "user_projects", "*") + @check_required(["dashboard_id", "domain_id"]) + def get(self, params): + """Get domain_dashboard + + Args: + params (dict): { + 'dashboard_id': 'str', # required + 'workspace_id': 'str', # injected from auth + 'domain_id': 'str' # injected from auth (required) + 'user_projects': 'list', # injected from auth + } + + Returns: + dashboard_vo (object) + """ + dashboard_id = params["dashboard_id"] + domain_id = params["domain_id"] + workspace_id = params.get("workspace_id") + user_projects = params.get("user_projects") + user_id = self.transaction.get_meta("authorization.user_id") + + dashboard_vo = self.dashboard_mgr.get_dashboard( + dashboard_id, domain_id, workspace_id, user_projects + ) + + if dashboard_vo.dashboard_type == "PRIVATE" and dashboard_vo.user_id != user_id: + raise ERROR_PERMISSION_DENIED() + + return dashboard_vo + + @transaction( + permission="dashboard:Dashboard.write", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"], + ) + @check_required(["dashboard_id", "version", "domain_id"]) + def delete_version(self, params): + """delete version of domain dashboard + + Args: + params (dict): { + 'dashboard_id': 'str', # required + 'version': 'int', # required + 'workspace_id': 'str', # injected from auth + 'domain_id': 'str' # injected from auth (required) + 'user_projects': 'list', # injected from auth + } + + Returns: + None + """ + + dashboard_id = params["dashboard_id"] + version = params["version"] + domain_id = params["domain_id"] + workspace_id = params.get("workspace_id") + user_projects = params.get("user_projects") + user_id = self.transaction.get_meta("authorization.user_id") + + dashboard_vo = self.dashboard_mgr.get_dashboard( + dashboard_id, domain_id, workspace_id, user_projects + ) + + if dashboard_vo.dashboard_type == "PRIVATE" and dashboard_vo.user_id != user_id: + raise ERROR_PERMISSION_DENIED() + + current_version = dashboard_vo.version + if current_version == version: + raise ERROR_LATEST_VERSION(version=version) + + self.version_mgr.delete_version(dashboard_id, version, domain_id) + + @transaction( + permission="dashboard:Dashboard.write", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"], + ) + @check_required(["dashboard_id", "version", "domain_id"]) + def revert_version(self, params): + """Revert version of domain dashboard + + Args: + params (dict): { + 'dashboard_id': 'str', # required + 'version': 'int', # required + 'workspace_id': 'str', # injected from auth + 'domain_id': 'str' # injected from auth (required) + 'user_projects': 'list', # injected from auth + } + + Returns: + dashboard_vo (object) + """ + + dashboard_id = params["dashboard_id"] + version = params["version"] + domain_id = params["domain_id"] + workspace_id = params.get("workspace_id") + user_projects = params.get("user_projects") + user_id = self.transaction.get_meta("authorization.user_id") + + dashboard_vo: Dashboard = self.dashboard_mgr.get_dashboard( + dashboard_id, domain_id, workspace_id, user_projects + ) + + if dashboard_vo.dashboard_type == "PRIVATE" and dashboard_vo.user_id != user_id: + raise ERROR_PERMISSION_DENIED() + + version_vo: DashboardVersion = self.version_mgr.get_version( + dashboard_id, version, domain_id + ) + + params["layouts"] = version_vo.layouts + params["variables"] = version_vo.variables + params["variables_schema"] = version_vo.variables_schema + + self.dashboard_mgr.increase_version(dashboard_vo) + self.version_mgr.create_version_by_dashboard_vo(dashboard_vo, params) + + return self.dashboard_mgr.update_dashboard_by_vo(params, dashboard_vo) + + @transaction( + permission="dashboard:Dashboard.read", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"], + ) + @change_value_by_rule("APPEND", "workspace_id", "*") + @change_value_by_rule("APPEND", "user_projects", "*") + @check_required(["dashboard_id", "version", "domain_id"]) + def get_version(self, params): + """Get version of domain dashboard + + Args: + params (dict): { + 'dashboard_id': 'str', # required + 'version': 'int', # required + 'user_id': 'str', # injected from auth + 'workspace_id': 'str', # injected from auth + 'domain_id': 'str' # injected from auth (required) + 'user_projects': 'list', # injected from auth + } + + Returns: + domain_dashboard_version_vo (object) + """ + + dashboard_id = params["dashboard_id"] + version = params["version"] + domain_id = params["domain_id"] + workspace_id = params.get("workspace_id") + user_projects = params.get("user_projects") + user_id = self.transaction.get_meta("authorization.user_id") + + dashboard_vo: Dashboard = self.dashboard_mgr.get_dashboard( + dashboard_id, domain_id, workspace_id, user_projects + ) + + if dashboard_vo.dashboard_type == "PRIVATE" and dashboard_vo.user_id != user_id: + raise ERROR_PERMISSION_DENIED() + + return self.version_mgr.get_version(dashboard_id, version, domain_id) + + @transaction( + permission="dashboard:Dashboard.read", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"], + ) + @change_value_by_rule("APPEND", "workspace_id", "*") + @change_value_by_rule("APPEND", "user_projects", "*") + @check_required(["dashboard_id", "domain_id"]) + @append_query_filter(["dashboard_id", "version", "domain_id"]) + @append_keyword_filter(["dashboard_id", "version"]) + def list_versions(self, params): + """List versions of domain dashboard + + Args: + params (dict): { + 'dashboard_id': 'str', # required + 'query': 'dict (spaceone.api.core.v1.Query)' + 'version': 'int', + 'workspace_id': 'str', # injected from auth + 'domain_id': 'str' # injected from auth (required) + 'user_projects': 'list', # injected from auth + } + + Returns: + domain_dashboard_version_vos (object) + total_count + """ + dashboard_id = params["dashboard_id"] + domain_id = params["domain_id"] + workspace_id = params.get("workspace_id") + user_projects = params.get("user_projects") + user_id = self.transaction.get_meta("authorization.user_id") + + query = params.get("query", {}) + domain_dashboard_version_vos, total_count = self.version_mgr.list_versions( + query + ) + dashboard_vo = self.dashboard_mgr.get_dashboard( + dashboard_id, domain_id, workspace_id, user_projects + ) + + if dashboard_vo.dashboard_type == "PRIVATE" and dashboard_vo.user_id != user_id: + raise ERROR_PERMISSION_DENIED() + + return domain_dashboard_version_vos, total_count, dashboard_vo.version + + @transaction( + permission="dashboard:Dashboard.read", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"], + ) + @change_value_by_rule("APPEND", "workspace_id", "*") + @change_value_by_rule("APPEND", "user_projects", "*") + @check_required(["domain_id"]) + @append_query_filter(["dashboard_id", "name", "viewers", "domain_id"]) + @append_keyword_filter(["dashboard_id", "name"]) + def list(self, params): + """List public_dashboards + + Args: + params (dict): { + 'query': 'dict (spaceone.api.core.v1.Query)' + 'dashboard_id': 'str', + 'name': 'str', + 'dashboard_type': 'str', + 'workspace_id': 'str', + 'project_id': 'str', + 'domain_id': 'str', # injected from auth + 'user_projects': 'list', # injected from auth + } + + Returns: + domain_dashboard_vos (object) + total_count + """ + + user_id = self.transaction.get_meta("authorization.user_id") + query = params.get("query", {}) + + query["filter"] = query.get("filter", []) + query["filter"].append( + { + "k": "user_id", + "v": [user_id, None], + "o": "in", + } + ) + + return self.dashboard_mgr.list_dashboards(query) + + @transaction( + permission="dashboard:Dashboard.read", + role_types=["DOMAIN_ADMIN", "WORKSPACE_OWNER", "WORKSPACE_MEMBER"], + ) + @change_value_by_rule("APPEND", "workspace_id", "*") + @change_value_by_rule("APPEND", "user_projects", "*") + @check_required(["query", "domain_id"]) + @append_query_filter(["domain_id"]) + @append_keyword_filter(["dashboard_id", "name"]) + def stat(self, params): + """ + Args: + params (dict): { + 'query': 'dict (spaceone.api.core.v1.StatisticsQuery)' + 'workspace_id': 'str', # injected from auth + 'domain_id': 'str' # injected from auth (required) + 'user_projects': 'list', # injected from auth + } + + Returns: + values (list) : 'list of statistics data' + + """ + + user_id = self.transaction.get_meta("authorization.user_id") + query = params.get("query", {}) + + query["filter"] = query.get("filter", []) + query["filter"].append( + { + "k": "user_id", + "v": [user_id, None], + "o": "in", + } + ) + + return self.dashboard_mgr.stat_dashboards(query) + + @staticmethod + def _has_version_key_in_params( + dashboard_vo: Dashboard, params: dict, version_change_keys: list + ) -> bool: + layouts = dashboard_vo.layouts + variables = dashboard_vo.variables + variables_schema = dashboard_vo.variables_schema + + if any(key for key in params if key in version_change_keys): + if layouts_from_params := params.get("layouts"): + if layouts != layouts_from_params: + return True + if options_from_params := params.get("variables"): + if variables != options_from_params: + return True + if schema_from_params := params.get("variables_schema"): + if schema_from_params != variables_schema: + return True + return False + + @staticmethod + def _merge_settings(old_settings: dict, new_settings: dict) -> dict: + settings = copy.deepcopy(old_settings) + + if old_settings: + settings.update(new_settings) + return settings + else: + return new_settings diff --git a/src/spaceone/dashboard/service/domain_dashboard_service.py b/src/spaceone/dashboard/service/domain_dashboard_service.py deleted file mode 100644 index 2a94f81..0000000 --- a/src/spaceone/dashboard/service/domain_dashboard_service.py +++ /dev/null @@ -1,376 +0,0 @@ -import copy -import logging - -from spaceone.core.service import * -from spaceone.dashboard.manager import DomainDashboardManager, DomainDashboardVersionManager -from spaceone.dashboard.model import DomainDashboard, DomainDashboardVersion -from spaceone.dashboard.error import * - -_LOGGER = logging.getLogger(__name__) - - -@authentication_handler -@authorization_handler -@mutation_handler -@event_handler -class DomainDashboardService(BaseService): - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.domain_dashboard_mgr: DomainDashboardManager = self.locator.get_manager('DomainDashboardManager') - self.version_mgr: DomainDashboardVersionManager = self.locator.get_manager('DomainDashboardVersionManager') - - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['name', 'viewers', 'domain_id']) - def create(self, params): - """Register domain_dashboard - - Args: - params (dict): { - 'name': 'str', - 'viewers': 'str', - 'layouts': 'list', - 'variables': 'dict', - 'settings': 'dict', - 'variables_schema': 'dict', - 'labels': 'list', - 'tags': 'dict', - 'domain_id': 'str' - } - - Returns: - domain_dashboard_vo (object) - """ - - if params['viewers'] == 'PUBLIC': - params['user_id'] = None - else: - params['user_id'] = self.transaction.get_meta('user_id') - - domain_dashboard_vo = self.domain_dashboard_mgr.create_domain_dashboard(params) - - version_keys = ['layouts', 'variables', 'variables_schema'] - if any(set(version_keys) & set(params.keys())): - self.version_mgr.create_version_by_domain_dashboard_vo(domain_dashboard_vo, params) - - return domain_dashboard_vo - - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['domain_dashboard_id', 'domain_id']) - def update(self, params): - """Update domain_dashboard - - Args: - params (dict): { - 'domain_dashboard_id': 'str', - 'name': 'str', - 'layouts': 'list', - 'variables': 'dict', - 'settings': 'dict', - 'variables_schema': 'list', - 'labels': 'list', - 'tags': 'dict', - 'domain_id': 'str' - } - - Returns: - domain_dashboard_vo (object) - """ - - domain_dashboard_id = params['domain_dashboard_id'] - domain_id = params['domain_id'] - - domain_dashboard_vo: DomainDashboard = self.domain_dashboard_mgr.get_domain_dashboard(domain_dashboard_id, - domain_id) - - if 'name' not in params: - params['name'] = domain_dashboard_vo.name - - if domain_dashboard_vo.viewers == 'PRIVATE' and \ - domain_dashboard_vo.user_id != self.transaction.get_meta('user_id'): - raise ERROR_PERMISSION_DENIED() - - if 'settings' in params: - params['settings'] = self._merge_settings(domain_dashboard_vo.settings, params['settings']) - - version_change_keys = ['layouts', 'variables', 'variables_schema'] - if self._has_version_key_in_params(domain_dashboard_vo, params, version_change_keys): - self.domain_dashboard_mgr.increase_version(domain_dashboard_vo) - self.version_mgr.create_version_by_domain_dashboard_vo(domain_dashboard_vo, params) - - return self.domain_dashboard_mgr.update_domain_dashboard_by_vo(params, domain_dashboard_vo) - - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['domain_dashboard_id', 'domain_id']) - def delete(self, params): - """Deregister domain_dashboard - - Args: - params (dict): { - 'domain_dashboard_id': 'str', - 'domain_id': 'str' - } - - Returns: - None - """ - - domain_dashboard_vo: DomainDashboard = self.domain_dashboard_mgr.get_domain_dashboard( - params['domain_dashboard_id'], params['domain_id']) - - if domain_dashboard_vo.viewers == 'PRIVATE' and \ - domain_dashboard_vo.user_id != self.transaction.get_meta('user_id'): - raise ERROR_PERMISSION_DENIED() - - if domain_dashboard_version_vos := self.version_mgr.filter_versions( - domain_dashboard_id=domain_dashboard_vo.domain_dashboard_id): - self.version_mgr.delete_versions_by_domain_dashboard_version_vos(domain_dashboard_version_vos) - - self.domain_dashboard_mgr.delete_by_domain_dashboard_vo(domain_dashboard_vo) - - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['domain_dashboard_id', 'domain_id']) - def get(self, params): - """ Get domain_dashboard - - Args: - params (dict): { - 'domain_dashboard_id': 'str', - 'domain_id': 'str', - 'only': 'list - } - - Returns: - domain_dashboard_vo (object) - """ - domain_dashboard_id = params['domain_dashboard_id'] - domain_id = params['domain_id'] - - domain_dashboard_vo = self.domain_dashboard_mgr.get_domain_dashboard(domain_dashboard_id, domain_id, - params.get('only')) - - if domain_dashboard_vo.viewers == 'PRIVATE' and \ - domain_dashboard_vo.user_id != self.transaction.get_meta('user_id'): - raise ERROR_PERMISSION_DENIED() - - return domain_dashboard_vo - - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['domain_dashboard_id', 'version', 'domain_id']) - def delete_version(self, params): - """ delete version of domain dashboard - - Args: - params (dict): { - 'domain_dashboard_id': 'str', - 'version': 'int', - 'domain_id': 'str', - } - - Returns: - None - """ - - domain_dashboard_id = params['domain_dashboard_id'] - version = params['version'] - domain_id = params['domain_id'] - - domain_dashboard_vo = self.domain_dashboard_mgr.get_domain_dashboard(domain_dashboard_id, domain_id) - - if domain_dashboard_vo.viewers == 'PRIVATE' and \ - domain_dashboard_vo.user_id != self.transaction.get_meta('user_id'): - raise ERROR_PERMISSION_DENIED() - - current_version = domain_dashboard_vo.version - if current_version == version: - raise ERROR_LATEST_VERSION(version=version) - - self.version_mgr.delete_version(domain_dashboard_id, version, domain_id) - - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['domain_dashboard_id', 'version', 'domain_id']) - def revert_version(self, params): - """ Revert version of domain dashboard - - Args: - params (dict): { - 'domain_dashboard_id': 'str', - 'version': 'int', - 'domain_id': 'str', - } - - Returns: - domain_dashboard_vo (object) - """ - - domain_dashboard_id = params['domain_dashboard_id'] - version = params['version'] - domain_id = params['domain_id'] - - domain_dashboard_vo: DomainDashboard = self.domain_dashboard_mgr.get_domain_dashboard(domain_dashboard_id, - domain_id) - - if domain_dashboard_vo.viewers == 'PRIVATE' and \ - domain_dashboard_vo.user_id != self.transaction.get_meta('user_id'): - raise ERROR_PERMISSION_DENIED() - - version_vo: DomainDashboardVersion = self.version_mgr.get_version(domain_dashboard_id, version, domain_id) - - params['layouts'] = version_vo.layouts - params['variables'] = version_vo.variables - params['variables_schema'] = version_vo.variables_schema - - self.domain_dashboard_mgr.increase_version(domain_dashboard_vo) - self.version_mgr.create_version_by_domain_dashboard_vo(domain_dashboard_vo, params) - - return self.domain_dashboard_mgr.update_domain_dashboard_by_vo(params, domain_dashboard_vo) - - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['domain_dashboard_id', 'version', 'domain_id']) - def get_version(self, params): - """ Get version of domain dashboard - - Args: - params (dict): { - 'domain_dashboard_id': 'str', - 'version': 'int', - 'domain_id': 'str', - 'only': 'list - } - - Returns: - domain_dashboard_version_vo (object) - """ - - domain_dashboard_id = params['domain_dashboard_id'] - version = params['version'] - domain_id = params['domain_id'] - - domain_dashboard_vo: DomainDashboard = self.domain_dashboard_mgr.get_domain_dashboard(domain_dashboard_id, - domain_id) - - if domain_dashboard_vo.viewers == 'PRIVATE' and \ - domain_dashboard_vo.user_id != self.transaction.get_meta('user_id'): - raise ERROR_PERMISSION_DENIED() - - return self.version_mgr.get_version(domain_dashboard_id, version, domain_id, params.get('only')) - - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['domain_dashboard_id', 'domain_id']) - @append_query_filter(['domain_dashboard_id', 'version', 'domain_id']) - @append_keyword_filter(['domain_dashboard_id', 'version']) - def list_versions(self, params): - """ List versions of domain dashboard - - Args: - params (dict): { - 'domain_dashboard_id': 'str', - 'version': 'int', - 'domain_id': 'str', - 'query': 'dict (spaceone.api.core.v1.Query)' - } - - Returns: - domain_dashboard_version_vos (object) - total_count - """ - domain_dashboard_id = params['domain_dashboard_id'] - domain_id = params['domain_id'] - - query = params.get('query', {}) - domain_dashboard_version_vos, total_count = self.version_mgr.list_versions(query) - domain_dashboard_vo = self.domain_dashboard_mgr.get_domain_dashboard(domain_dashboard_id, domain_id) - - if domain_dashboard_vo.viewers == 'PRIVATE' and \ - domain_dashboard_vo.user_id != self.transaction.get_meta('user_id'): - raise ERROR_PERMISSION_DENIED() - - return domain_dashboard_version_vos, total_count, domain_dashboard_vo.version - - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['domain_id']) - @append_query_filter(['domain_dashboard_id', 'name', 'viewers', 'user_id', 'domain_id']) - @append_keyword_filter(['domain_dashboard_id', 'name']) - def list(self, params): - """ List public_dashboards - - Args: - params (dict): { - 'domain_dashboard_id': 'str', - 'name': 'str', - 'viewers': 'str', - 'user_id': 'str' - 'domain_id': 'str', - 'query': 'dict (spaceone.api.core.v1.Query)' - } - - Returns: - domain_dashboard_vos (object) - total_count - """ - - query = params.get('query', {}) - - query['filter'] = query.get('filter', []) - query['filter'].append({ - 'k': 'user_id', - 'v': [self.transaction.get_meta('user_id'), None], - 'o': 'in' - }) - - return self.domain_dashboard_mgr.list_domain_dashboards(query) - - @transaction(append_meta={'authorization.scope': 'DOMAIN'}) - @check_required(['query', 'domain_id']) - @append_query_filter(['domain_id']) - @append_keyword_filter(['domain_dashboard_id', 'name']) - def stat(self, params): - """ - Args: - params (dict): { - 'domain_id': 'str', - 'query': 'dict (spaceone.api.core.v1.StatisticsQuery)' - } - - Returns: - values (list) : 'list of statistics data' - - """ - query = params.get('query', {}) - - query['filter'] = query.get('filter', []) - query['filter'].append({ - 'k': 'user_id', - 'v': [self.transaction.get_meta('user_id'), None], - 'o': 'in' - }) - - return self.domain_dashboard_mgr.stat_domain_dashboards(query) - - @staticmethod - def _has_version_key_in_params(domain_dashboard_vo, params, version_change_keys): - layouts = domain_dashboard_vo.layouts - variables = domain_dashboard_vo.variables - variables_schema = domain_dashboard_vo.variables_schema - - if any(key for key in params if key in version_change_keys): - if layouts_from_params := params.get('layouts'): - if layouts != layouts_from_params: - return True - if options_from_params := params.get('variables'): - if variables != options_from_params: - return True - if schema_from_params := params.get('variables_schema'): - if schema_from_params != variables_schema: - return True - return False - - @staticmethod - def _merge_settings(old_settings, new_settings): - settings = copy.deepcopy(old_settings) - - if old_settings: - settings.update(new_settings) - return settings - else: - return new_settings diff --git a/src/spaceone/dashboard/service/project_dashboard_service.py b/src/spaceone/dashboard/service/project_dashboard_service.py deleted file mode 100644 index a03e9c1..0000000 --- a/src/spaceone/dashboard/service/project_dashboard_service.py +++ /dev/null @@ -1,377 +0,0 @@ -import copy -import logging - -from spaceone.core.service import * -from spaceone.dashboard.manager import ProjectDashboardManager, ProjectDashboardVersionManager -from spaceone.dashboard.model import ProjectDashboard, ProjectDashboardVersion -from spaceone.dashboard.error import * - -_LOGGER = logging.getLogger(__name__) - - -@authentication_handler -@authorization_handler -@mutation_handler -@event_handler -class ProjectDashboardService(BaseService): - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.project_dashboard_mgr: ProjectDashboardManager = self.locator.get_manager('ProjectDashboardManager') - self.version_mgr: ProjectDashboardVersionManager = self.locator.get_manager('ProjectDashboardVersionManager') - - @transaction(append_meta={'authorization.scope': 'PROJECT'}) - @check_required(['project_id', 'name', 'viewers', 'domain_id']) - def create(self, params): - """Register project_dashboard - - Args: - params (dict): { - 'project_id': 'str', - 'name': 'str', - 'viewers': 'str', - 'layouts': 'list', - 'variables': 'dict', - 'settings': 'dict', - 'variables_schema': 'dict', - 'labels': 'list', - 'tags': 'dict', - 'domain_id': 'str' - } - - Returns: - project_dashboard_vo (object) - """ - - if params['viewers'] == 'PUBLIC': - params['user_id'] = None - else: - params['user_id'] = self.transaction.get_meta('user_id') - - project_dashboard_vo = self.project_dashboard_mgr.create_project_dashboard(params) - - version_keys = ['layouts', 'variables', 'variables_schema'] - if any(set(version_keys) & set(params.keys())): - self.version_mgr.create_version_by_project_dashboard_vo(project_dashboard_vo, params) - - return project_dashboard_vo - - @transaction(append_meta={'authorization.scope': 'PROJECT'}) - @check_required(['project_dashboard_id', 'domain_id']) - def update(self, params): - """Update project_dashboard - - Args: - params (dict): { - 'project_dashboard_id': 'str', - 'name': 'str', - 'layouts': 'list', - 'variables': 'dict', - 'settings': 'dict', - 'variables_schema': 'dict', - 'labels': 'list', - 'tags': 'dict', - 'domain_id': 'str' - } - - Returns: - project_dashboard_vo (object) - """ - - project_dashboard_id = params['project_dashboard_id'] - domain_id = params['domain_id'] - - project_dashboard_vo: ProjectDashboard = self.project_dashboard_mgr.get_project_dashboard(project_dashboard_id, - domain_id) - - if project_dashboard_vo.viewers == 'PRIVATE' and \ - project_dashboard_vo.user_id != self.transaction.get_meta('user_id'): - raise ERROR_PERMISSION_DENIED() - - if 'settings' in params: - params['settings'] = self._merge_settings(project_dashboard_vo.settings, params['settings']) - - version_change_keys = ['layouts', 'variables', 'variables_schema'] - if self._has_version_key_in_params(project_dashboard_vo, params, version_change_keys): - self.project_dashboard_mgr.increase_version(project_dashboard_vo) - self.version_mgr.create_version_by_project_dashboard_vo(project_dashboard_vo, params) - - return self.project_dashboard_mgr.update_project_dashboard_by_vo(params, project_dashboard_vo) - - @transaction(append_meta={'authorization.scope': 'PROJECT'}) - @check_required(['project_dashboard_id', 'domain_id']) - def delete(self, params): - """Deregister project_dashboard - - Args: - params (dict): { - 'project_dashboard_id': 'str', - 'domain_id': 'str' - } - - Returns: - None - """ - - project_dashboard_vo: ProjectDashboard = self.project_dashboard_mgr.get_project_dashboard( - params['project_dashboard_id'], params['domain_id']) - - if project_dashboard_vo.viewers == 'PRIVATE' and \ - project_dashboard_vo.user_id != self.transaction.get_meta('user_id'): - raise ERROR_PERMISSION_DENIED() - - if project_dashboard_version_vos := self.version_mgr.filter_versions( - project_dashboard_id=project_dashboard_vo.project_dashboard_id): - self.version_mgr.delete_versions_by_project_dashboard_version_vos(project_dashboard_version_vos) - - self.project_dashboard_mgr.delete_by_project_dashboard_vo(project_dashboard_vo) - - @transaction(append_meta={'authorization.scope': 'PROJECT'}) - @check_required(['project_dashboard_id', 'domain_id']) - def get(self, params): - """ Get project_dashboard - - Args: - params (dict): { - 'project_dashboard_id': 'str', - 'domain_id': 'str', - 'only': 'list - } - - Returns: - project_dashboard_vo (object) - """ - - project_dashboard_id = params['project_dashboard_id'] - domain_id = params['domain_id'] - - project_dashboard_vo = self.project_dashboard_mgr.get_project_dashboard(project_dashboard_id, domain_id, - params.get('only')) - - if project_dashboard_vo.viewers == 'PRIVATE' and \ - project_dashboard_vo.user_id != self.transaction.get_meta('user_id'): - raise ERROR_PERMISSION_DENIED() - - return project_dashboard_vo - - @transaction(append_meta={'authorization.scope': 'PROJECT'}) - @check_required(['project_dashboard_id', 'version', 'domain_id']) - def delete_version(self, params): - """ Delete version of project dashboard - - Args: - params (dict): { - 'project_dashboard_id': 'str', - 'version': 'int', - 'domain_id': 'str', - } - - Returns: - None - """ - project_dashboard_id = params['project_dashboard_id'] - version = params['version'] - domain_id = params['domain_id'] - - project_dashboard_vo = self.project_dashboard_mgr.get_project_dashboard(project_dashboard_id, domain_id) - - if project_dashboard_vo.viewers == 'PRIVATE' and \ - project_dashboard_vo.user_id != self.transaction.get_meta('user_id'): - raise ERROR_PERMISSION_DENIED() - - current_version = project_dashboard_vo.version - if current_version == version: - raise ERROR_LATEST_VERSION(version=version) - - self.version_mgr.delete_version(project_dashboard_id, version, domain_id) - - @transaction(append_meta={'authorization.scope': 'PROJECT'}) - @check_required(['project_dashboard_id', 'version', 'domain_id']) - def revert_version(self, params): - """ Revert version of project dashboard - - Args: - params (dict): { - 'project_dashboard_id': 'str', - 'version': 'int', - 'domain_id': 'str', - } - - Returns: - project_dashboard_vo (object) - """ - project_dashboard_id = params['project_dashboard_id'] - version = params['version'] - domain_id = params['domain_id'] - - project_dashboard_vo: ProjectDashboard = self.project_dashboard_mgr.get_project_dashboard(project_dashboard_id, - domain_id) - - if project_dashboard_vo.viewers == 'PRIVATE' and \ - project_dashboard_vo.user_id != self.transaction.get_meta('user_id'): - raise ERROR_PERMISSION_DENIED() - - version_vo: ProjectDashboardVersion = self.version_mgr.get_version(project_dashboard_id, version, domain_id) - - params['layouts'] = version_vo.layouts - params['variables'] = version_vo.variables - params['variables_schema'] = version_vo.variables_schema - - self.project_dashboard_mgr.increase_version(project_dashboard_vo) - self.version_mgr.create_version_by_project_dashboard_vo(project_dashboard_vo, params) - - return self.project_dashboard_mgr.update_project_dashboard_by_vo(params, project_dashboard_vo) - - @transaction(append_meta={'authorization.scope': 'PROJECT'}) - @check_required(['project_dashboard_id', 'version', 'domain_id']) - def get_version(self, params): - """ Get version of project dashboard - - Args: - params (dict): { - 'project_dashboard_id': 'str', - 'version': 'int', - 'domain_id': 'str', - 'only': 'list - } - - Returns: - project_dashboard_version_vo (object) - """ - - project_dashboard_id = params['project_dashboard_id'] - version = params['version'] - domain_id = params['domain_id'] - - project_dashboard_vo: ProjectDashboard = self.project_dashboard_mgr.get_project_dashboard(project_dashboard_id, - domain_id) - - if project_dashboard_vo.viewers == 'PRIVATE' and \ - project_dashboard_vo.user_id != self.transaction.get_meta('user_id'): - raise ERROR_PERMISSION_DENIED() - - return self.version_mgr.get_version(project_dashboard_id, version, domain_id, params.get('only')) - - @transaction(append_meta={'authorization.scope': 'PROJECT'}) - @check_required(['project_dashboard_id', 'domain_id']) - @append_query_filter(['project_dashboard_id', 'version', 'domain_id']) - @append_keyword_filter(['project_dashboard_id', 'version']) - def list_versions(self, params): - """ List versions of project dashboard - - Args: - params (dict): { - 'project_dashboard_id': 'str', - 'version': 'int', - 'domain_id': 'str', - 'query': 'dict (spaceone.api.core.v1.Query)' - } - - Returns: - project_dashboard_version_vos (object) - total_count - """ - project_dashboard_id = params['project_dashboard_id'] - domain_id = params['domain_id'] - - query = params.get('query', {}) - project_dashboard_version_vos, total_count = self.version_mgr.list_versions(query) - project_dashboard_vo = self.project_dashboard_mgr.get_project_dashboard(project_dashboard_id, domain_id) - - if project_dashboard_vo.viewers == 'PRIVATE' and \ - project_dashboard_vo.user_id != self.transaction.get_meta('user_id'): - raise ERROR_PERMISSION_DENIED() - - return project_dashboard_version_vos, total_count, project_dashboard_vo.version - - @transaction(append_meta={'authorization.scope': 'PROJECT'}) - @check_required(['domain_id']) - @append_query_filter( - ['project_dashboard_id', 'project_id', 'name', 'viewers', 'user_id', 'domain_id', 'user_projects']) - @append_keyword_filter(['project_dashboard_id', 'name']) - def list(self, params): - """ List project_dashboards - - Args: - params (dict): { - 'project_dashboard_id': 'str', - 'project_id': 'str', - 'name': 'str', - 'viewers': 'str', - 'user_id': 'str' - 'domain_id': 'str', - 'query': 'dict (spaceone.api.core.v1.Query)', - 'user_projects': 'list', // from meta - } - - Returns: - project_dashboard_vos (object) - total_count - """ - - query = params.get('query', {}) - - query['filter'] = query.get('filter', []) - query['filter'].append({ - 'k': 'user_id', - 'v': [self.transaction.get_meta('user_id'), None], - 'o': 'in' - }) - - return self.project_dashboard_mgr.list_project_dashboards(query) - - @transaction(append_meta={'authorization.scope': 'PROJECT'}) - @check_required(['query', 'domain_id']) - @append_query_filter(['domain_id', 'user_projects']) - @append_keyword_filter(['project_dashboard_id', 'name']) - def stat(self, params): - """ - Args: - params (dict): { - 'domain_id': 'str', - 'query': 'dict (spaceone.api.core.v1.StatisticsQuery)', - 'user_projects': 'list', // from meta - } - - Returns: - values (list) : 'list of statistics data' - - """ - query = params.get('query', {}) - - query['filter'] = query.get('filter', []) - query['filter'].append({ - 'k': 'user_id', - 'v': [self.transaction.get_meta('user_id'), None], - 'o': 'in' - }) - - return self.project_dashboard_mgr.stat_project_dashboards(query) - - @staticmethod - def _has_version_key_in_params(domain_dashboard_vo, params, version_change_keys): - layouts = domain_dashboard_vo.layouts - variables = domain_dashboard_vo.variables - variables_schema = domain_dashboard_vo.variables_schema - - if any(key for key in params if key in version_change_keys): - if layouts_from_params := params.get('layouts'): - if layouts != layouts_from_params: - return True - if options_from_params := params.get('variables'): - if variables != options_from_params: - return True - if schema_from_params := params.get('variables_schema'): - if schema_from_params != variables_schema: - return True - return False - - @staticmethod - def _merge_settings(old_settings, new_settings): - settings = copy.deepcopy(old_settings) - - if old_settings: - settings.update(new_settings) - return settings - else: - return new_settings diff --git a/test/factory/__init__.py b/test/factory/__init__.py index 1bdd915..b1b91ab 100644 --- a/test/factory/__init__.py +++ b/test/factory/__init__.py @@ -1,3 +1,3 @@ from test.factory.domain_dashboard_factory import DomainDashboardFactory from test.factory.project_dashboard_factory import ProjectDashboardFactory -from test.factory.custom_widget_dashboard_factory import CustomWidgetFactory \ No newline at end of file +from test.factory.custom_widget_dashboard_factory import CustomWidgetFactory diff --git a/test/factory/custom_widget_dashboard_factory.py b/test/factory/custom_widget_dashboard_factory.py index c1d0602..d65b57b 100644 --- a/test/factory/custom_widget_dashboard_factory.py +++ b/test/factory/custom_widget_dashboard_factory.py @@ -8,19 +8,18 @@ class CustomWidgetFactory(factory.mongoengine.MongoEngineFactory): class Meta: model = CustomWidget - custom_widget_id = factory.LazyAttribute(lambda o: utils.generate_id('custom-widget')) + custom_widget_id = factory.LazyAttribute( + lambda o: utils.generate_id("custom-widget") + ) widget_name = factory.LazyAttribute(lambda o: utils.random_string()) title = factory.LazyAttribute(lambda o: utils.random_string()) - version = 'v1' + version = "v1" options = { - 'group_by': 'product', + "group_by": "product", } - inherit_options = { - 'group_by': {'enabled': True} - } - tags = {'type': 'test', - 'env': 'dev'} - user_id = 'cloudforet@gmail.com' - domain_id = factory.LazyAttribute(lambda o: utils.generate_id('domain')) - created_at = factory.Faker('date_time') - updated_at = factory.Faker('date_time') + inherit_options = {"group_by": {"enabled": True}} + tags = {"type": "test", "env": "dev"} + user_id = "cloudforet@gmail.com" + domain_id = factory.LazyAttribute(lambda o: utils.generate_id("domain")) + created_at = factory.Faker("date_time") + updated_at = factory.Faker("date_time") diff --git a/test/factory/domain_dashboard_factory.py b/test/factory/domain_dashboard_factory.py index 23597fe..9b5378c 100644 --- a/test/factory/domain_dashboard_factory.py +++ b/test/factory/domain_dashboard_factory.py @@ -8,22 +8,20 @@ class DomainDashboardFactory(factory.mongoengine.MongoEngineFactory): class Meta: model = DomainDashboard - domain_dashboard_id = factory.LazyAttribute(lambda o: utils.generate_id('domain-dash')) + domain_dashboard_id = factory.LazyAttribute( + lambda o: utils.generate_id("domain-dash") + ) name = factory.LazyAttribute(lambda o: utils.random_string()) layouts = [] - variables = { - 'group_by': 'product', - 'project_id': [] - } + variables = {"group_by": "product", "project_id": []} settings = { - 'date_range': {'enabled': True}, - 'currency': {'enabled': True}, + "date_range": {"enabled": True}, + "currency": {"enabled": True}, } variables_schema = {} - labels = ['a', 'b', 'c'] - tags = {'type': 'test', - 'env': 'dev'} - user_id = 'cloudforet@gmail.com' - domain_id = factory.LazyAttribute(lambda o: utils.generate_id('domain')) - created_at = factory.Faker('date_time') - updated_at = factory.Faker('date_time') + labels = ["a", "b", "c"] + tags = {"type": "test", "env": "dev"} + user_id = "cloudforet@gmail.com" + domain_id = factory.LazyAttribute(lambda o: utils.generate_id("domain")) + created_at = factory.Faker("date_time") + updated_at = factory.Faker("date_time") diff --git a/test/factory/domain_dashboard_version_factory.py b/test/factory/domain_dashboard_version_factory.py index 921b254..a6cfdba 100644 --- a/test/factory/domain_dashboard_version_factory.py +++ b/test/factory/domain_dashboard_version_factory.py @@ -9,15 +9,12 @@ class Meta: model = DomainDashboardVersion # domain_dashboard_id = factory.LazyAttribute(lambda o: utils.generate_id('domain-dash')) - layouts = [[{'name': 'widget1'}, {'name': 'widget2'}, {'name': 'widget3'}]] - variables = { - 'group_by': 'product', - 'project_id': [] - } + layouts = [[{"name": "widget1"}, {"name": "widget2"}, {"name": "widget3"}]] + variables = {"group_by": "product", "project_id": []} settings = { - 'date_range': {'enabled': True}, - 'currency': {'enabled': True}, + "date_range": {"enabled": True}, + "currency": {"enabled": True}, } - variables_schema = {'a': 'b'} - domain_id = factory.LazyAttribute(lambda o: utils.generate_id('domain')) - created_at = factory.Faker('date_time') + variables_schema = {"a": "b"} + domain_id = factory.LazyAttribute(lambda o: utils.generate_id("domain")) + created_at = factory.Faker("date_time") diff --git a/test/factory/project_dashboard_factory.py b/test/factory/project_dashboard_factory.py index a86b8b2..e9f8df9 100644 --- a/test/factory/project_dashboard_factory.py +++ b/test/factory/project_dashboard_factory.py @@ -8,23 +8,21 @@ class ProjectDashboardFactory(factory.mongoengine.MongoEngineFactory): class Meta: model = ProjectDashboard - project_dashboard_id = factory.LazyAttribute(lambda o: utils.generate_id('project-dash')) - project_id = factory.LazyAttribute(lambda o: utils.generate_id('project')) + project_dashboard_id = factory.LazyAttribute( + lambda o: utils.generate_id("project-dash") + ) + project_id = factory.LazyAttribute(lambda o: utils.generate_id("project")) name = factory.LazyAttribute(lambda o: utils.random_string()) layouts = [] - variables = { - 'group_by': 'product', - 'project_id': [] - } + variables = {"group_by": "product", "project_id": []} settings = { - 'date_range': {'enabled': True}, - 'currency': {'enabled': True}, + "date_range": {"enabled": True}, + "currency": {"enabled": True}, } variables_schema = {} - labels = ['a', 'b', 'c'] - tags = {'type': 'test', - 'env': 'dev'} - user_id = 'cloudforet@gmail.com' - domain_id = factory.LazyAttribute(lambda o: utils.generate_id('domain')) - created_at = factory.Faker('date_time') - updated_at = factory.Faker('date_time') + labels = ["a", "b", "c"] + tags = {"type": "test", "env": "dev"} + user_id = "cloudforet@gmail.com" + domain_id = factory.LazyAttribute(lambda o: utils.generate_id("domain")) + created_at = factory.Faker("date_time") + updated_at = factory.Faker("date_time") diff --git a/test/factory/project_dashboard_version_factory.py b/test/factory/project_dashboard_version_factory.py index 7d3050c..2cd8399 100644 --- a/test/factory/project_dashboard_version_factory.py +++ b/test/factory/project_dashboard_version_factory.py @@ -9,15 +9,12 @@ class Meta: model = ProjectDashboardVersion # project_dashboard_id = factory.LazyAttribute(lambda o: utils.generate_id('project-dash')) - layouts = [[{'name': 'widget1'}, {'name': 'widget2'}, {'name': 'widget3'}]] - variables = { - 'group_by': 'product', - 'project_id': [] - } + layouts = [[{"name": "widget1"}, {"name": "widget2"}, {"name": "widget3"}]] + variables = {"group_by": "product", "project_id": []} settings = { - 'date_range': {'enabled': True}, - 'currency': {'enabled': True}, + "date_range": {"enabled": True}, + "currency": {"enabled": True}, } - variables_schema = {'a': 'b'} - domain_id = factory.LazyAttribute(lambda o: utils.generate_id('domain')) - created_at = factory.Faker('date_time') + variables_schema = {"a": "b"} + domain_id = factory.LazyAttribute(lambda o: utils.generate_id("domain")) + created_at = factory.Faker("date_time") diff --git a/test/service/test_custom_widget.py b/test/service/test_custom_widget.py index 93fee56..2f28d5f 100644 --- a/test/service/test_custom_widget.py +++ b/test/service/test_custom_widget.py @@ -13,19 +13,17 @@ class TestCustomWidgetService(unittest.TestCase): - @classmethod def setUpClass(cls): - config.init_conf(package='spaceone.dashboard') + config.init_conf(package="spaceone.dashboard") config.set_service_config() config.set_global(MOCK_MODE=True) - connect('test', host='mongomock://localhost') + connect("test", host="mongomock://localhost") - cls.domain_id = utils.generate_id('domain') - cls.transaction = Transaction({ - 'service': 'dashboard', - 'api_class': 'CustomWidget' - }) + cls.domain_id = utils.generate_id("domain") + cls.transaction = Transaction( + {"service": "dashboard", "api_class": "CustomWidget"} + ) super().setUpClass() @classmethod @@ -35,92 +33,98 @@ def tearDownClass(cls) -> None: def tearDown(self, *args) -> None: print() - print('(tearDown) ==> Delete all data_sources') + print("(tearDown) ==> Delete all data_sources") custom_widget_vos = CustomWidget.objects.filter() custom_widget_vos.delete() def test_create_custom_widget(self): params = { - 'widget_name': 'widget-name', - 'title': 'test', - 'version': 'v1', - 'options': {'group_by': 'product'}, - 'inherit_options': {'a': {'enabled': True}}, - 'tags': {'type': 'test'}, - 'domain_id': 'domain-12345' + "widget_name": "widget-name", + "title": "test", + "version": "v1", + "options": {"group_by": "product"}, + "inherit_options": {"a": {"enabled": True}}, + "tags": {"type": "test"}, + "domain_id": "domain-12345", } - self.transaction.method = 'create' + self.transaction.method = "create" custom_widget_svc = CustomWidgetService(transaction=self.transaction) custom_widget_vo = custom_widget_svc.create(params.copy()) - print_data(custom_widget_vo.to_dict(), 'test_create_custom_widget') + print_data(custom_widget_vo.to_dict(), "test_create_custom_widget") CustomWidgetInfo(custom_widget_vo) self.assertIsInstance(custom_widget_vo, CustomWidget) - self.assertEqual(params['title'], custom_widget_vo.title) + self.assertEqual(params["title"], custom_widget_vo.title) def test_update_custom_widget(self): custom_widget_vo = CustomWidgetFactory(domain_id=self.domain_id) params = { - 'custom_widget_id': custom_widget_vo.custom_widget_id, - 'title': 'update widget test', - 'options': {'group_by': 'product2'}, - 'inherit_options': {'b': {'enabled': True}}, - 'tags': {'type': 'test from params'}, - 'domain_id': self.domain_id + "custom_widget_id": custom_widget_vo.custom_widget_id, + "title": "update widget test", + "options": {"group_by": "product2"}, + "inherit_options": {"b": {"enabled": True}}, + "tags": {"type": "test from params"}, + "domain_id": self.domain_id, } - self.transaction.method = 'update' + self.transaction.method = "update" custom_widget_svc = CustomWidgetService(transaction=self.transaction) updated_custom_widget_vo = custom_widget_svc.update(params.copy()) - print_data(updated_custom_widget_vo.to_dict(), 'test_update_custom_widget') + print_data(updated_custom_widget_vo.to_dict(), "test_update_custom_widget") self.assertIsInstance(updated_custom_widget_vo, CustomWidget) - self.assertEqual(params['title'], updated_custom_widget_vo.title) + self.assertEqual(params["title"], updated_custom_widget_vo.title) def test_get_custom_widget(self): custom_widget_vo = CustomWidgetFactory(domain_id=self.domain_id) params = { - 'custom_widget_id': custom_widget_vo.custom_widget_id, - 'domain_id': self.domain_id + "custom_widget_id": custom_widget_vo.custom_widget_id, + "domain_id": self.domain_id, } - self.transaction.method = 'get' + self.transaction.method = "get" custom_widget_svc = CustomWidgetService(transaction=self.transaction) get_custom_widget_vo = custom_widget_svc.get(params) - print_data(get_custom_widget_vo.to_dict(), 'test_get_custom_widget') + print_data(get_custom_widget_vo.to_dict(), "test_get_custom_widget") CustomWidgetInfo(get_custom_widget_vo) self.assertIsInstance(get_custom_widget_vo, CustomWidget) self.assertEqual(custom_widget_vo.title, get_custom_widget_vo.title) - self.assertEqual(custom_widget_vo.custom_widget_id, get_custom_widget_vo.custom_widget_id) + self.assertEqual( + custom_widget_vo.custom_widget_id, get_custom_widget_vo.custom_widget_id + ) def test_list_custom_widgets(self): - custom_widget_vos = CustomWidgetFactory.build_batch(10, domain_id=self.domain_id) + custom_widget_vos = CustomWidgetFactory.build_batch( + 10, domain_id=self.domain_id + ) list(map(lambda vo: vo.save(), custom_widget_vos)) print_data(custom_widget_vos[4].to_dict(), "5th custom_widget_vo") params = { - 'custom_widget_id': custom_widget_vos[4].custom_widget_id, - 'domain_id': self.domain_id + "custom_widget_id": custom_widget_vos[4].custom_widget_id, + "domain_id": self.domain_id, } - self.transaction.method = 'list' + self.transaction.method = "list" custom_widget_svc = CustomWidgetService(transaction=self.transaction) list_custom_widget_vos, total_count = custom_widget_svc.list(params) CustomWidgetsInfo(list_custom_widget_vos, total_count) self.assertEqual(len(list_custom_widget_vos), 1) - self.assertEqual(list_custom_widget_vos[0].custom_widget_id, params.get('custom_widget_id')) + self.assertEqual( + list_custom_widget_vos[0].custom_widget_id, params.get("custom_widget_id") + ) self.assertIsInstance(list_custom_widget_vos[0], CustomWidget) self.assertEqual(total_count, 1) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/test/service/test_domain_dashboard.py b/test/service/test_domain_dashboard.py index d17a426..8ffeee2 100644 --- a/test/service/test_domain_dashboard.py +++ b/test/service/test_domain_dashboard.py @@ -7,7 +7,9 @@ from parameterized import parameterized from spaceone.dashboard.info import DomainDashboardInfo, DomainDashboardsInfo -from spaceone.dashboard.info.domain_dashboard_version_info import DomainDashboardVersionsInfo +from spaceone.dashboard.info.domain_dashboard_version_info import ( + DomainDashboardVersionsInfo, +) from spaceone.dashboard.model import DomainDashboard, DomainDashboardVersion from spaceone.dashboard.service.domain_dashboard_service import DomainDashboardService from spaceone.dashboard.error import * @@ -17,19 +19,17 @@ class TestDomainDashboardService(unittest.TestCase): - @classmethod def setUpClass(cls): - config.init_conf(package='spaceone.dashboard') + config.init_conf(package="spaceone.dashboard") config.set_service_config() config.set_global(MOCK_MODE=True) - connect('test', host='mongomock://localhost') + connect("test", host="mongomock://localhost") - cls.domain_id = utils.generate_id('domain') - cls.transaction = Transaction({ - 'service': 'dashboard', - 'api_class': 'DomainDashboard' - }) + cls.domain_id = utils.generate_id("domain") + cls.transaction = Transaction( + {"service": "dashboard", "api_class": "DomainDashboard"} + ) super().setUpClass() @classmethod @@ -39,101 +39,108 @@ def tearDownClass(cls) -> None: def tearDown(self, *args) -> None: print() - print('(tearDown) ==> Delete all data_sources') + print("(tearDown) ==> Delete all data_sources") domain_dashboard_vos = DomainDashboard.objects.filter() domain_dashboard_vos.delete() - @parameterized.expand([['user_id', None], ['user_id', 'cloudforet@gmail.com']], name_func=key_value_name_func) + @parameterized.expand( + [["user_id", None], ["user_id", "cloudforet@gmail.com"]], + name_func=key_value_name_func, + ) def test_create_domain_dashboard(self, key, value): params = { - 'name': 'test', - 'viewers': 'PUBLIC', - 'domain_id': 'domain-12345', - 'variables': { - 'project_id': 'project-1234' + "name": "test", + "viewers": "PUBLIC", + "domain_id": "domain-12345", + "variables": {"project_id": "project-1234"}, + "settings": { + "date_range": {"enabled": False}, + "currency": {"enabled": False}, }, - 'settings': { - 'date_range': {'enabled': False}, - 'currency': {'enabled': False} - } } if key and value: params.update({key: value}) - self.transaction.method = 'create' - self.transaction.set_meta('user_id', 'cloudforet@gmail.com') + self.transaction.method = "create" + self.transaction.set_meta("user_id", "cloudforet@gmail.com") domain_dashboard_svc = DomainDashboardService(transaction=self.transaction) domain_dashboard_vo = domain_dashboard_svc.create(params.copy()) - print_data(domain_dashboard_vo.to_dict(), 'test_create_domain_dashboard') + print_data(domain_dashboard_vo.to_dict(), "test_create_domain_dashboard") DomainDashboardInfo(domain_dashboard_vo) self.assertIsInstance(domain_dashboard_vo, DomainDashboard) - self.assertEqual(params['name'], domain_dashboard_vo.name) - self.assertEqual(params['variables']['project_id'], - domain_dashboard_vo.variables.get('project_id')) + self.assertEqual(params["name"], domain_dashboard_vo.name) + self.assertEqual( + params["variables"]["project_id"], + domain_dashboard_vo.variables.get("project_id"), + ) def test_update_domain_dashboard(self): domain_dashboard_vo = DomainDashboardFactory(domain_id=self.domain_id) params = { - 'domain_dashboard_id': domain_dashboard_vo.domain_dashboard_id, - 'name': 'update domain dashboard test', - 'settings': { - 'date_range': {'enabled': False}, - 'currency': {'enabled': False} + "domain_dashboard_id": domain_dashboard_vo.domain_dashboard_id, + "name": "update domain dashboard test", + "settings": { + "date_range": {"enabled": False}, + "currency": {"enabled": False}, }, - 'tags': {'a': 'b'}, - 'domain_id': self.domain_id + "tags": {"a": "b"}, + "domain_id": self.domain_id, } - self.transaction.method = 'update' + self.transaction.method = "update" domain_dashboard_svc = DomainDashboardService(transaction=self.transaction) domain_dashboard_vo = domain_dashboard_svc.update(params.copy()) - print_data(domain_dashboard_vo.to_dict(), 'test_update_project_dashboard') + print_data(domain_dashboard_vo.to_dict(), "test_update_project_dashboard") self.assertIsInstance(domain_dashboard_vo, DomainDashboard) - self.assertEqual(params['name'], domain_dashboard_vo.name) + self.assertEqual(params["name"], domain_dashboard_vo.name) def test_update_domain_dashboard_permission_error(self): - domain_dashboard_vo = DomainDashboardFactory(domain_id=self.domain_id, - viewers='PRIVATE', - user_id='cloudforet2@gmail.com') + domain_dashboard_vo = DomainDashboardFactory( + domain_id=self.domain_id, viewers="PRIVATE", user_id="cloudforet2@gmail.com" + ) params = { - 'domain_dashboard_id': domain_dashboard_vo.domain_dashboard_id, - 'name': 'update domain dashboard test', - 'settings': { - 'date_range': {'enabled': False}, - 'currency': {'enabled': False} + "domain_dashboard_id": domain_dashboard_vo.domain_dashboard_id, + "name": "update domain dashboard test", + "settings": { + "date_range": {"enabled": False}, + "currency": {"enabled": False}, }, - 'tags': {'a': 'b'}, - 'domain_id': self.domain_id + "tags": {"a": "b"}, + "domain_id": self.domain_id, } - self.transaction.method = 'update' - self.transaction.set_meta('user_id', 'cloudforet@gmail.com') + self.transaction.method = "update" + self.transaction.set_meta("user_id", "cloudforet@gmail.com") domain_dashboard_svc = DomainDashboardService(transaction=self.transaction) with self.assertRaises(ERROR_PERMISSION_DENIED): domain_dashboard_svc.update(params.copy()) def test_delete_domain_dashboard(self): domain_dashboard_vo = DomainDashboardFactory(domain_id=self.domain_id) - domain_dashboard_version_vos = DomainDashboardVersionFactory.build_batch(5, - domain_dashboard_id=domain_dashboard_vo.domain_dashboard_id, - domain_id=self.domain_id) + domain_dashboard_version_vos = DomainDashboardVersionFactory.build_batch( + 5, + domain_dashboard_id=domain_dashboard_vo.domain_dashboard_id, + domain_id=self.domain_id, + ) list(map(lambda vo: vo.save(), domain_dashboard_version_vos)) for idx, domain_dashboard_version in enumerate(domain_dashboard_version_vos, 1): - print_data(domain_dashboard_version.to_dict(), f'{idx}th domain_dashboard_version') + print_data( + domain_dashboard_version.to_dict(), f"{idx}th domain_dashboard_version" + ) params = { - 'domain_dashboard_id': domain_dashboard_vo.domain_dashboard_id, - 'domain_id': domain_dashboard_vo.domain_id + "domain_dashboard_id": domain_dashboard_vo.domain_dashboard_id, + "domain_id": domain_dashboard_vo.domain_id, } - self.transaction.method = 'delete' + self.transaction.method = "delete" domain_dashboard_svc = DomainDashboardService(transaction=self.transaction) domain_dashboard_svc.delete(params.copy()) @@ -149,30 +156,30 @@ def test_delete_version(self): variables=domain_dashboard_vo.variables, version=1, variables_schema=domain_dashboard_vo.variables_schema, - domain_id=self.domain_id + domain_id=self.domain_id, ) # When updating domain_dashboard, version=2 is created params = { - 'domain_dashboard_id': domain_dashboard_vo.domain_dashboard_id, - 'name': 'update domain dashboard test', - 'layouts': [[{'name': 'widget4'}]], - 'settings': { - 'date_range': {'enabled': False}, - 'currency': {'enabled': False} + "domain_dashboard_id": domain_dashboard_vo.domain_dashboard_id, + "name": "update domain dashboard test", + "layouts": [[{"name": "widget4"}]], + "settings": { + "date_range": {"enabled": False}, + "currency": {"enabled": False}, }, - 'tags': {'a': 'b'}, - 'domain_id': self.domain_id + "tags": {"a": "b"}, + "domain_id": self.domain_id, } - self.transaction.method = 'update' + self.transaction.method = "update" domain_dashboard_svc = DomainDashboardService(transaction=self.transaction) domain_dashboard_vo = domain_dashboard_svc.update(params.copy()) - print_data(domain_dashboard_vo.to_dict(), 'test_update_project_dashboard') + print_data(domain_dashboard_vo.to_dict(), "test_update_project_dashboard") params = { - 'domain_dashboard_id': domain_dashboard_vo.domain_dashboard_id, - 'version': 1, - 'domain_id': domain_dashboard_vo.domain_id + "domain_dashboard_id": domain_dashboard_vo.domain_dashboard_id, + "version": 1, + "domain_id": domain_dashboard_vo.domain_id, } # After that, delete version=1 with the delete_version method @@ -181,13 +188,13 @@ def test_delete_version(self): def test_delete_latest_version(self): domain_dashboard_vo = DomainDashboardFactory(domain_id=self.domain_id) params = { - 'domain_dashboard_id': domain_dashboard_vo.domain_dashboard_id, - 'version': 1, - 'domain_id': domain_dashboard_vo.domain_id + "domain_dashboard_id": domain_dashboard_vo.domain_dashboard_id, + "version": 1, + "domain_id": domain_dashboard_vo.domain_id, } print(params) - self.transaction.method = 'delete_version' + self.transaction.method = "delete_version" domain_dashboard_svc = DomainDashboardService(transaction=self.transaction) with self.assertRaises(ERROR_LATEST_VERSION): domain_dashboard_svc.delete_version(params.copy()) @@ -201,41 +208,43 @@ def test_revert_version(self): variables=domain_dashboard_vo.variables, version=1, variables_schema=domain_dashboard_vo.variables_schema, - domain_id=self.domain_id + domain_id=self.domain_id, ) # When updating domain_dashboard, version=2 is created params = { - 'domain_dashboard_id': domain_dashboard_vo.domain_dashboard_id, - 'name': 'update domain dashboard test', - 'layouts': [[{'name': 'widget4'}]], - 'settings': { - 'date_range': {'enabled': False}, - 'currency': {'enabled': False} + "domain_dashboard_id": domain_dashboard_vo.domain_dashboard_id, + "name": "update domain dashboard test", + "layouts": [[{"name": "widget4"}]], + "settings": { + "date_range": {"enabled": False}, + "currency": {"enabled": False}, }, - 'tags': {'a': 'b'}, - 'domain_id': self.domain_id + "tags": {"a": "b"}, + "domain_id": self.domain_id, } - self.transaction.method = 'update' + self.transaction.method = "update" domain_dashboard_svc = DomainDashboardService(transaction=self.transaction) domain_dashboard_vo = domain_dashboard_svc.update(params.copy()) - print_data(domain_dashboard_vo.to_dict(), 'test_update_domain_dashboard') + print_data(domain_dashboard_vo.to_dict(), "test_update_domain_dashboard") params = { - 'domain_dashboard_id': domain_dashboard_vo.domain_dashboard_id, - 'version': 1, - 'domain_id': domain_dashboard_vo.domain_id + "domain_dashboard_id": domain_dashboard_vo.domain_dashboard_id, + "version": 1, + "domain_id": domain_dashboard_vo.domain_id, } # After that, revert version=1 with the revert_version method domain_dashboard_vo = domain_dashboard_svc.revert_version(params.copy()) - print_data(domain_dashboard_vo.to_dict(), 'test_reverted_domain_dashboard') + print_data(domain_dashboard_vo.to_dict(), "test_reverted_domain_dashboard") self.assertIsInstance(domain_dashboard_vo, DomainDashboard) self.assertEqual(domain_dashboard_vo.version, 3) self.assertEqual(domain_dashboard_vo.layouts, first_version.layouts) self.assertEqual(domain_dashboard_vo.variables, first_version.variables) - self.assertEqual(domain_dashboard_vo.variables_schema, first_version.variables_schema) + self.assertEqual( + domain_dashboard_vo.variables_schema, first_version.variables_schema + ) def test_get_version(self): domain_dashboard_vo = DomainDashboardFactory(domain_id=self.domain_id) @@ -245,96 +254,127 @@ def test_get_version(self): variables=domain_dashboard_vo.variables, version=1, variables_schema=domain_dashboard_vo.variables_schema, - domain_id=self.domain_id + domain_id=self.domain_id, ) params = { - 'domain_dashboard_id': domain_dashboard_vo.domain_dashboard_id, - 'version': 1, - 'domain_id': domain_dashboard_vo.domain_id + "domain_dashboard_id": domain_dashboard_vo.domain_dashboard_id, + "version": 1, + "domain_id": domain_dashboard_vo.domain_id, } - self.transaction.method = 'get_version' + self.transaction.method = "get_version" domain_dashboard_svc = DomainDashboardService(transaction=self.transaction) domain_dashboard_version_vo = domain_dashboard_svc.get_version(params.copy()) - print_data(domain_dashboard_version_vo.to_dict(), 'test_domain_dashboard_version_by_get_version') + print_data( + domain_dashboard_version_vo.to_dict(), + "test_domain_dashboard_version_by_get_version", + ) self.assertIsInstance(domain_dashboard_version_vo, DomainDashboardVersion) self.assertEqual(domain_dashboard_version_vo.version, 1) - self.assertEqual(domain_dashboard_version_vo.domain_dashboard_id, domain_dashboard_vo.domain_dashboard_id) - self.assertEqual(domain_dashboard_version_vo.layouts, domain_dashboard_vo.layouts) - self.assertEqual(domain_dashboard_version_vo.variables, domain_dashboard_vo.variables) - self.assertEqual(domain_dashboard_version_vo.variables_schema, - domain_dashboard_vo.variables_schema) + self.assertEqual( + domain_dashboard_version_vo.domain_dashboard_id, + domain_dashboard_vo.domain_dashboard_id, + ) + self.assertEqual( + domain_dashboard_version_vo.layouts, domain_dashboard_vo.layouts + ) + self.assertEqual( + domain_dashboard_version_vo.variables, domain_dashboard_vo.variables + ) + self.assertEqual( + domain_dashboard_version_vo.variables_schema, + domain_dashboard_vo.variables_schema, + ) def test_list_versions(self): domain_dashboard_vo = DomainDashboardFactory(domain_id=self.domain_id) - domain_dashboard_version_vos = DomainDashboardVersionFactory.build_batch(5, - domain_dashboard_id=domain_dashboard_vo.domain_dashboard_id, - domain_id=self.domain_id) + domain_dashboard_version_vos = DomainDashboardVersionFactory.build_batch( + 5, + domain_dashboard_id=domain_dashboard_vo.domain_dashboard_id, + domain_id=self.domain_id, + ) list(map(lambda vo: vo.save(), domain_dashboard_version_vos)) params = { - 'domain_dashboard_id': domain_dashboard_vo.domain_dashboard_id, - 'domain_id': domain_dashboard_vo.domain_id + "domain_dashboard_id": domain_dashboard_vo.domain_dashboard_id, + "domain_id": domain_dashboard_vo.domain_id, } - self.transaction.method = 'get' + self.transaction.method = "get" domain_dashboard_svc = DomainDashboardService(transaction=self.transaction) - list_domain_dashboard_version_vos, total_count, current_version = domain_dashboard_svc.list_versions( - params.copy()) + ( + list_domain_dashboard_version_vos, + total_count, + current_version, + ) = domain_dashboard_svc.list_versions(params.copy()) DomainDashboardVersionsInfo(list_domain_dashboard_version_vos, total_count) - for idx, domain_dashboard_version in enumerate(list_domain_dashboard_version_vos, 1): - print_data(domain_dashboard_version.to_dict(), f'{idx}th domain_dashboard_version') + for idx, domain_dashboard_version in enumerate( + list_domain_dashboard_version_vos, 1 + ): + print_data( + domain_dashboard_version.to_dict(), f"{idx}th domain_dashboard_version" + ) self.assertEqual(len(list_domain_dashboard_version_vos), 5) - self.assertIsInstance(list_domain_dashboard_version_vos[0], DomainDashboardVersion) + self.assertIsInstance( + list_domain_dashboard_version_vos[0], DomainDashboardVersion + ) self.assertEqual(total_count, 5) def test_get_domain_dashboard(self): domain_dashboard_vo = DomainDashboardFactory(domain_id=self.domain_id) params = { - 'domain_dashboard_id': domain_dashboard_vo.domain_dashboard_id, - 'domain_id': self.domain_id + "domain_dashboard_id": domain_dashboard_vo.domain_dashboard_id, + "domain_id": self.domain_id, } - self.transaction.method = 'get' + self.transaction.method = "get" domain_dashboard_svc = DomainDashboardService(transaction=self.transaction) get_domain_dashboard_vo = domain_dashboard_svc.get(params) - print_data(get_domain_dashboard_vo.to_dict(), 'test_get_domain_dashboard') + print_data(get_domain_dashboard_vo.to_dict(), "test_get_domain_dashboard") DomainDashboardInfo(get_domain_dashboard_vo) self.assertIsInstance(get_domain_dashboard_vo, DomainDashboard) self.assertEqual(domain_dashboard_vo.name, get_domain_dashboard_vo.name) - self.assertEqual(domain_dashboard_vo.domain_dashboard_id, get_domain_dashboard_vo.domain_dashboard_id) + self.assertEqual( + domain_dashboard_vo.domain_dashboard_id, + get_domain_dashboard_vo.domain_dashboard_id, + ) def test_list_domain_dashboards(self): - domain_dashboard_vos = DomainDashboardFactory.build_batch(10, domain_id=self.domain_id) + domain_dashboard_vos = DomainDashboardFactory.build_batch( + 10, domain_id=self.domain_id + ) list(map(lambda vo: vo.save(), domain_dashboard_vos)) print_data(domain_dashboard_vos[4].to_dict(), "5th domain_dashboard_vo") params = { - 'domain_dashboard_id': domain_dashboard_vos[4].domain_dashboard_id, - 'domain_id': self.domain_id + "domain_dashboard_id": domain_dashboard_vos[4].domain_dashboard_id, + "domain_id": self.domain_id, } - self.transaction.method = 'list' + self.transaction.method = "list" domain_dashboard_svc = DomainDashboardService(transaction=self.transaction) list_domain_dashboard_vos, total_count = domain_dashboard_svc.list(params) DomainDashboardsInfo(list_domain_dashboard_vos, total_count) self.assertEqual(len(list_domain_dashboard_vos), 1) - self.assertEqual(list_domain_dashboard_vos[0].domain_dashboard_id, params.get('domain_dashboard_id')) + self.assertEqual( + list_domain_dashboard_vos[0].domain_dashboard_id, + params.get("domain_dashboard_id"), + ) self.assertIsInstance(list_domain_dashboard_vos[0], DomainDashboard) self.assertEqual(total_count, 1) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/test/service/test_project_dashboard.py b/test/service/test_project_dashboard.py index d1c5f40..2b410cf 100644 --- a/test/service/test_project_dashboard.py +++ b/test/service/test_project_dashboard.py @@ -6,30 +6,36 @@ from spaceone.core.unittest.result import print_data from parameterized import parameterized -from spaceone.dashboard.info import ProjectDashboardInfo, ProjectDashboardsInfo, StatisticsInfo -from spaceone.dashboard.info.project_dashboard_version_info import ProjectDashboardVersionsInfo +from spaceone.dashboard.info import ( + ProjectDashboardInfo, + ProjectDashboardsInfo, + StatisticsInfo, +) +from spaceone.dashboard.info.project_dashboard_version_info import ( + ProjectDashboardVersionsInfo, +) from spaceone.dashboard.model import ProjectDashboard, ProjectDashboardVersion from spaceone.dashboard.service import ProjectDashboardService, DomainDashboardService from spaceone.dashboard.error import * from test.factory import ProjectDashboardFactory -from test.factory.project_dashboard_version_factory import ProjectDashboardVersionFactory +from test.factory.project_dashboard_version_factory import ( + ProjectDashboardVersionFactory, +) from test.lib import * class TestProjectDashboardService(unittest.TestCase): - @classmethod def setUpClass(cls): - config.init_conf(package='spaceone.dashboard') + config.init_conf(package="spaceone.dashboard") config.set_service_config() config.set_global(MOCK_MODE=True) - connect('test', host='mongomock://localhost') + connect("test", host="mongomock://localhost") - cls.domain_id = utils.generate_id('domain') - cls.transaction = Transaction({ - 'service': 'dashboard', - 'api_class': 'ProjectDashboard' - }) + cls.domain_id = utils.generate_id("domain") + cls.transaction = Transaction( + {"service": "dashboard", "api_class": "ProjectDashboard"} + ) super().setUpClass() @classmethod @@ -39,102 +45,112 @@ def tearDownClass(cls) -> None: def tearDown(self, *args) -> None: print() - print('(tearDown) ==> Delete all data_sources') + print("(tearDown) ==> Delete all data_sources") project_dashboard_vos = ProjectDashboard.objects.filter() project_dashboard_vos.delete() - @parameterized.expand([['user_id', None], ['user_id', 'cloudforet@gmail.com']], name_func=key_value_name_func) + @parameterized.expand( + [["user_id", None], ["user_id", "cloudforet@gmail.com"]], + name_func=key_value_name_func, + ) def test_create_project_dashboard(self, key, value): params = { - 'project_id': 'project-12345', - 'name': 'test', - 'viewers': 'PUBLIC', - 'domain_id': 'domain-12345', - 'variables': { - 'project_id': 'project-1234' + "project_id": "project-12345", + "name": "test", + "viewers": "PUBLIC", + "domain_id": "domain-12345", + "variables": {"project_id": "project-1234"}, + "settings": { + "date_range": {"enabled": False}, + "currency": {"enabled": False}, }, - 'settings': { - 'date_range': {'enabled': False}, - 'currency': {'enabled': False} - } } if key and value: params.update({key: value}) - self.transaction.method = 'create' - self.transaction.set_meta('user_id', 'cloudforet@gmail.com') + self.transaction.method = "create" + self.transaction.set_meta("user_id", "cloudforet@gmail.com") project_dashboard_svc = ProjectDashboardService(transaction=self.transaction) project_dashboard_vo = project_dashboard_svc.create(params.copy()) - print_data(project_dashboard_vo.to_dict(), 'test_create_project_dashboard') + print_data(project_dashboard_vo.to_dict(), "test_create_project_dashboard") ProjectDashboardInfo(project_dashboard_vo) self.assertIsInstance(project_dashboard_vo, ProjectDashboard) - self.assertEqual(params['name'], project_dashboard_vo.name) - self.assertEqual(params['variables']['project_id'], - project_dashboard_vo.variables.get('project_id')) + self.assertEqual(params["name"], project_dashboard_vo.name) + self.assertEqual( + params["variables"]["project_id"], + project_dashboard_vo.variables.get("project_id"), + ) def test_update_project_dashboard(self): project_dashboard_vo = ProjectDashboardFactory(domain_id=self.domain_id) params = { - 'project_dashboard_id': project_dashboard_vo.project_dashboard_id, - 'name': 'update project dashboard test', - 'settings': { - 'date_range': {'enabled': False}, - 'currency': {'enabled': False} + "project_dashboard_id": project_dashboard_vo.project_dashboard_id, + "name": "update project dashboard test", + "settings": { + "date_range": {"enabled": False}, + "currency": {"enabled": False}, }, - 'tags': {'a': 'b'}, - 'domain_id': self.domain_id + "tags": {"a": "b"}, + "domain_id": self.domain_id, } - self.transaction.method = 'update' + self.transaction.method = "update" project_dashboard_svc = ProjectDashboardService(transaction=self.transaction) project_dashboard_vo = project_dashboard_svc.update(params.copy()) - print_data(project_dashboard_vo.to_dict(), 'test_update_project_dashboard') + print_data(project_dashboard_vo.to_dict(), "test_update_project_dashboard") self.assertIsInstance(project_dashboard_vo, ProjectDashboard) - self.assertEqual(params['name'], project_dashboard_vo.name) + self.assertEqual(params["name"], project_dashboard_vo.name) def test_update_project_dashboard_permission_error(self): - project_dashboard_vo = ProjectDashboardFactory(domain_id=self.domain_id, - viewers='PRIVATE', - user_id='cloudforet2@gmail.com') + project_dashboard_vo = ProjectDashboardFactory( + domain_id=self.domain_id, viewers="PRIVATE", user_id="cloudforet2@gmail.com" + ) params = { - 'project_dashboard_id': project_dashboard_vo.project_dashboard_id, - 'name': 'update project dashboard test', - 'settings': { - 'date_range': {'enabled': False}, - 'currency': {'enabled': False} + "project_dashboard_id": project_dashboard_vo.project_dashboard_id, + "name": "update project dashboard test", + "settings": { + "date_range": {"enabled": False}, + "currency": {"enabled": False}, }, - 'tags': {'a': 'b'}, - 'domain_id': self.domain_id + "tags": {"a": "b"}, + "domain_id": self.domain_id, } - self.transaction.method = 'update' - self.transaction.set_meta('user_id', 'cloudforet@gmail.com') + self.transaction.method = "update" + self.transaction.set_meta("user_id", "cloudforet@gmail.com") project_dashboard_svc = ProjectDashboardService(transaction=self.transaction) with self.assertRaises(ERROR_PERMISSION_DENIED): project_dashboard_svc.update(params.copy()) def test_delete_project_dashboard(self): project_dashboard_vo = ProjectDashboardFactory(domain_id=self.domain_id) - project_dashboard_version_vos = ProjectDashboardVersionFactory.build_batch(5, - project_dashboard_id=project_dashboard_vo.project_dashboard_id, - domain_id=self.domain_id) + project_dashboard_version_vos = ProjectDashboardVersionFactory.build_batch( + 5, + project_dashboard_id=project_dashboard_vo.project_dashboard_id, + domain_id=self.domain_id, + ) list(map(lambda vo: vo.save(), project_dashboard_version_vos)) - for idx, project_dashboard_version in enumerate(project_dashboard_version_vos, 1): - print_data(project_dashboard_version.to_dict(), f'{idx}th project_dashboard_version') + for idx, project_dashboard_version in enumerate( + project_dashboard_version_vos, 1 + ): + print_data( + project_dashboard_version.to_dict(), + f"{idx}th project_dashboard_version", + ) params = { - 'project_dashboard_id': project_dashboard_vo.project_dashboard_id, - 'domain_id': project_dashboard_vo.domain_id + "project_dashboard_id": project_dashboard_vo.project_dashboard_id, + "domain_id": project_dashboard_vo.domain_id, } - self.transaction.method = 'delete' + self.transaction.method = "delete" project_dashboard_svc = ProjectDashboardService(transaction=self.transaction) project_dashboard_svc.delete(params.copy()) @@ -150,30 +166,30 @@ def test_delete_version(self): variables=project_dashboard_vo.variables, version=1, variables_schema=project_dashboard_vo.variables_schema, - domain_id=self.domain_id + domain_id=self.domain_id, ) # When updating domain_dashboard, version=2 is created params = { - 'project_dashboard_id': project_dashboard_vo.project_dashboard_id, - 'name': 'update domain dashboard test', - 'layouts': [[{'name': 'widget4'}]], - 'settings': { - 'date_range': {'enabled': False}, - 'currency': {'enabled': False} + "project_dashboard_id": project_dashboard_vo.project_dashboard_id, + "name": "update domain dashboard test", + "layouts": [[{"name": "widget4"}]], + "settings": { + "date_range": {"enabled": False}, + "currency": {"enabled": False}, }, - 'tags': {'a': 'b'}, - 'domain_id': self.domain_id + "tags": {"a": "b"}, + "domain_id": self.domain_id, } - self.transaction.method = 'update' + self.transaction.method = "update" project_dashboard_svc = ProjectDashboardService(transaction=self.transaction) project_dashboard_vo = project_dashboard_svc.update(params.copy()) - print_data(project_dashboard_vo.to_dict(), 'test_update_project_dashboard') + print_data(project_dashboard_vo.to_dict(), "test_update_project_dashboard") params = { - 'project_dashboard_id': project_dashboard_vo.project_dashboard_id, - 'version': 1, - 'domain_id': project_dashboard_vo.domain_id + "project_dashboard_id": project_dashboard_vo.project_dashboard_id, + "version": 1, + "domain_id": project_dashboard_vo.domain_id, } # After that, delete version=1 with the delete_version method @@ -182,12 +198,12 @@ def test_delete_version(self): def test_delete_latest_version(self): project_dashboard_vo = ProjectDashboardFactory(domain_id=self.domain_id) params = { - 'project_dashboard_id': project_dashboard_vo.project_dashboard_id, - 'version': 1, - 'domain_id': project_dashboard_vo.domain_id + "project_dashboard_id": project_dashboard_vo.project_dashboard_id, + "version": 1, + "domain_id": project_dashboard_vo.domain_id, } - self.transaction.method = 'delete_version' + self.transaction.method = "delete_version" project_dashboard_svc = ProjectDashboardService(transaction=self.transaction) with self.assertRaises(ERROR_LATEST_VERSION): project_dashboard_svc.delete_version(params.copy()) @@ -201,41 +217,43 @@ def test_revert_version(self): variables=project_dashboard_vo.variables, version=1, variables_schema=project_dashboard_vo.variables_schema, - domain_id=self.domain_id + domain_id=self.domain_id, ) # When updating domain_dashboard, version=2 is created params = { - 'project_dashboard_id': project_dashboard_vo.project_dashboard_id, - 'name': 'update project dashboard test', - 'layouts': [[{'name': 'widget4'}]], - 'settings': { - 'date_range': {'enabled': False}, - 'currency': {'enabled': False} + "project_dashboard_id": project_dashboard_vo.project_dashboard_id, + "name": "update project dashboard test", + "layouts": [[{"name": "widget4"}]], + "settings": { + "date_range": {"enabled": False}, + "currency": {"enabled": False}, }, - 'tags': {'a': 'b'}, - 'domain_id': self.domain_id + "tags": {"a": "b"}, + "domain_id": self.domain_id, } - self.transaction.method = 'update' + self.transaction.method = "update" project_dashboard_svc = ProjectDashboardService(transaction=self.transaction) project_dashboard_vo = project_dashboard_svc.update(params.copy()) - print_data(project_dashboard_vo.to_dict(), 'test_update_project_dashboard') + print_data(project_dashboard_vo.to_dict(), "test_update_project_dashboard") params = { - 'project_dashboard_id': project_dashboard_vo.project_dashboard_id, - 'version': 1, - 'domain_id': project_dashboard_vo.domain_id + "project_dashboard_id": project_dashboard_vo.project_dashboard_id, + "version": 1, + "domain_id": project_dashboard_vo.domain_id, } # After that, revert version=1 with the revert_version method project_dashboard_vo = project_dashboard_svc.revert_version(params.copy()) - print_data(project_dashboard_vo.to_dict(), 'test_reverted_project_dashboard') + print_data(project_dashboard_vo.to_dict(), "test_reverted_project_dashboard") self.assertIsInstance(project_dashboard_vo, ProjectDashboard) self.assertEqual(project_dashboard_vo.version, 3) self.assertEqual(project_dashboard_vo.layouts, first_version.layouts) self.assertEqual(project_dashboard_vo.variables, first_version.variables) - self.assertEqual(project_dashboard_vo.variables_schema, first_version.variables_schema) + self.assertEqual( + project_dashboard_vo.variables_schema, first_version.variables_schema + ) def test_get_version(self): project_dashboard_vo = ProjectDashboardFactory(domain_id=self.domain_id) @@ -245,96 +263,127 @@ def test_get_version(self): variables=project_dashboard_vo.variables, version=1, variables_schema=project_dashboard_vo.variables_schema, - domain_id=self.domain_id + domain_id=self.domain_id, ) params = { - 'project_dashboard_id': project_dashboard_vo.project_dashboard_id, - 'version': 1, - 'domain_id': project_dashboard_vo.domain_id + "project_dashboard_id": project_dashboard_vo.project_dashboard_id, + "version": 1, + "domain_id": project_dashboard_vo.domain_id, } - self.transaction.method = 'get_version' + self.transaction.method = "get_version" project_dashboard_svc = ProjectDashboardService(transaction=self.transaction) project_dashboard_version_vo = project_dashboard_svc.get_version(params.copy()) - print_data(project_dashboard_version_vo.to_dict(), 'test_project_dashboard_version_by_get_version') + print_data( + project_dashboard_version_vo.to_dict(), + "test_project_dashboard_version_by_get_version", + ) self.assertIsInstance(project_dashboard_version_vo, ProjectDashboardVersion) self.assertEqual(project_dashboard_version_vo.version, 1) - self.assertEqual(project_dashboard_version_vo.project_dashboard_id, project_dashboard_vo.project_dashboard_id) - self.assertEqual(project_dashboard_version_vo.layouts, project_dashboard_vo.layouts) - self.assertEqual(project_dashboard_version_vo.variables, project_dashboard_vo.variables) - self.assertEqual(project_dashboard_version_vo.variables_schema, - project_dashboard_vo.variables_schema) + self.assertEqual( + project_dashboard_version_vo.project_dashboard_id, + project_dashboard_vo.project_dashboard_id, + ) + self.assertEqual( + project_dashboard_version_vo.layouts, project_dashboard_vo.layouts + ) + self.assertEqual( + project_dashboard_version_vo.variables, project_dashboard_vo.variables + ) + self.assertEqual( + project_dashboard_version_vo.variables_schema, + project_dashboard_vo.variables_schema, + ) def test_list_versions(self): project_dashboard_vo = ProjectDashboardFactory(domain_id=self.domain_id) - project_dashboard_version_vos = ProjectDashboardVersionFactory.build_batch(5, - project_dashboard_id=project_dashboard_vo.project_dashboard_id, - domain_id=self.domain_id) + project_dashboard_version_vos = ProjectDashboardVersionFactory.build_batch( + 5, + project_dashboard_id=project_dashboard_vo.project_dashboard_id, + domain_id=self.domain_id, + ) list(map(lambda vo: vo.save(), project_dashboard_version_vos)) params = { - 'project_dashboard_id': project_dashboard_vo.project_dashboard_id, - 'domain_id': project_dashboard_vo.domain_id + "project_dashboard_id": project_dashboard_vo.project_dashboard_id, + "domain_id": project_dashboard_vo.domain_id, } - self.transaction.method = 'list_versions' + self.transaction.method = "list_versions" project_dashboard_svc = ProjectDashboardService(transaction=self.transaction) - list_project_dashboard_version_vos, total_count, current_version = project_dashboard_svc.list_versions( - params.copy()) + ( + list_project_dashboard_version_vos, + total_count, + current_version, + ) = project_dashboard_svc.list_versions(params.copy()) ProjectDashboardVersionsInfo(list_project_dashboard_version_vos, total_count) - for idx, domain_dashboard_version in enumerate(list_project_dashboard_version_vos, 1): - print_data(domain_dashboard_version.to_dict(), f'{idx}th project_dashboard_version') + for idx, domain_dashboard_version in enumerate( + list_project_dashboard_version_vos, 1 + ): + print_data( + domain_dashboard_version.to_dict(), f"{idx}th project_dashboard_version" + ) self.assertEqual(len(list_project_dashboard_version_vos), 5) - self.assertIsInstance(list_project_dashboard_version_vos[0], ProjectDashboardVersion) + self.assertIsInstance( + list_project_dashboard_version_vos[0], ProjectDashboardVersion + ) self.assertEqual(total_count, 5) def test_get_project_dashboard(self): project_dashboard_vo = ProjectDashboardFactory(domain_id=self.domain_id) params = { - 'project_dashboard_id': project_dashboard_vo.project_dashboard_id, - 'domain_id': self.domain_id + "project_dashboard_id": project_dashboard_vo.project_dashboard_id, + "domain_id": self.domain_id, } - self.transaction.method = 'get' + self.transaction.method = "get" project_dashboard_svc = ProjectDashboardService(transaction=self.transaction) get_project_dashboard_vo = project_dashboard_svc.get(params) - print_data(get_project_dashboard_vo.to_dict(), 'test_get_project_dashboard') + print_data(get_project_dashboard_vo.to_dict(), "test_get_project_dashboard") ProjectDashboardInfo(get_project_dashboard_vo) self.assertIsInstance(get_project_dashboard_vo, ProjectDashboard) self.assertEqual(project_dashboard_vo.name, get_project_dashboard_vo.name) - self.assertEqual(project_dashboard_vo.project_dashboard_id, get_project_dashboard_vo.project_dashboard_id) + self.assertEqual( + project_dashboard_vo.project_dashboard_id, + get_project_dashboard_vo.project_dashboard_id, + ) def test_list_project_dashboards(self): - project_dashboard_vos = ProjectDashboardFactory.build_batch(10, domain_id=self.domain_id) + project_dashboard_vos = ProjectDashboardFactory.build_batch( + 10, domain_id=self.domain_id + ) list(map(lambda vo: vo.save(), project_dashboard_vos)) print_data(project_dashboard_vos[4].to_dict(), "5th project_dashboard_vo") params = { - 'project_dashboard_id': project_dashboard_vos[4].project_dashboard_id, - 'domain_id': self.domain_id + "project_dashboard_id": project_dashboard_vos[4].project_dashboard_id, + "domain_id": self.domain_id, } - self.transaction.method = 'list' + self.transaction.method = "list" project_dashboard_svc = ProjectDashboardService(transaction=self.transaction) list_project_dashboard_vos, total_count = project_dashboard_svc.list(params) ProjectDashboardsInfo(list_project_dashboard_vos, total_count) self.assertEqual(len(list_project_dashboard_vos), 1) - self.assertEqual(list_project_dashboard_vos[0].project_dashboard_id, params.get('project_dashboard_id')) + self.assertEqual( + list_project_dashboard_vos[0].project_dashboard_id, + params.get("project_dashboard_id"), + ) self.assertIsInstance(list_project_dashboard_vos[0], ProjectDashboard) self.assertEqual(total_count, 1) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main()