Skip to content

Commit 3d829b1

Browse files
author
Jongmin Kim
authored
Merge pull request #69 from whdalsrnt/master
feat: add dashboard template APIs
2 parents c15e0f3 + a790dd3 commit 3d829b1

21 files changed

+634
-62
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from spaceone.repository.error.plugin import *
2+
from spaceone.repository.error.dashboard_template import *
23
from spaceone.repository.error.repository import *
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from spaceone.core.error import *
2+
3+
4+
class ERROR_NO_DASHBOARD_TEMPLATE(ERROR_INVALID_ARGUMENT):
5+
_message = 'Dashboard Template does not exists. (template_id = {template_id})'
6+
7+
8+
class ERROR_MANAGED_REPOSITORY_NOT_SUPPORT_LIST(ERROR_INVALID_ARGUMENT):
9+
_message = 'Managed Repository does not support list method.'

src/spaceone/repository/error/plugin.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ class ERROR_NO_PLUGIN(ERROR_INVALID_ARGUMENT):
55
_message = 'Plugin does not exists. (plugin_id = {plugin_id})'
66

77

8-
class ERROR_INVALID_IMAGE_FORMAT(ERROR_INVALID_ARGUMENT):
9-
_message = 'Image format is <repository name>/<image name> in {name}'
10-
11-
128
class ERROR_INVALID_IMAGE_NAME_FORMAT(ERROR_INVALID_ARGUMENT):
139
_message = 'Image name format is [a-zA-Z0-9\-]+ in {name}'
1410

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from spaceone.repository.info.common_info import *
22
from spaceone.repository.info.plugin_info import *
3+
from spaceone.repository.info.dashboard_template_info import *
34
from spaceone.repository.info.repository_info import *
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import functools
2+
from spaceone.api.repository.v1 import dashboard_template_pb2, repository_pb2
3+
from spaceone.core.pygrpc.message_type import *
4+
from spaceone.core import utils
5+
6+
__all__ = ["DashboardTemplateInfo", "DashboardTemplatesInfo"]
7+
8+
9+
def DashboardTemplateInfo(template_info: dict, minimal=False):
10+
info = {
11+
"template_id": template_info.get("template_id"),
12+
"name": template_info.get("name"),
13+
"state": template_info.get("state"),
14+
"template_type": template_info.get("template_type"),
15+
}
16+
17+
if not minimal:
18+
info.update(
19+
{
20+
"dashboards": change_list_value_type(template_info.get("dashboards")),
21+
"tags": change_struct_type(template_info.get("tags")),
22+
"labels": change_list_value_type(template_info.get("labels")),
23+
"domain_id": template_info.get("domain_id"),
24+
"created_at": utils.datetime_to_iso8601(template_info.get("created_at")),
25+
"updated_at": utils.datetime_to_iso8601(template_info.get("updated_at")),
26+
}
27+
)
28+
29+
if repository_info := template_info.get("repository_info"):
30+
info.update(
31+
{
32+
"repository_info": repository_pb2.RepositoryInfo(
33+
repository_id=repository_info.get("repository_id"),
34+
name=repository_info.get("name"),
35+
repository_type=repository_info.get("repository_type"),
36+
)
37+
}
38+
)
39+
40+
return dashboard_template_pb2.DashboardTemplateInfo(**info)
41+
42+
43+
def DashboardTemplatesInfo(templates_info, total_count, **kwargs):
44+
return dashboard_template_pb2.DashboardTemplatesInfo(
45+
results=list(map(functools.partial(DashboardTemplateInfo, **kwargs), templates_info)),
46+
total_count=total_count,
47+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from spaceone.core.pygrpc.server import GRPCServer
22
from .repository import Repository
33
from .plugin import Plugin
4+
from .dashboard_template import DashboardTemplate
45

56

67
_all_ = ['app']
78

89
app = GRPCServer()
910
app.add_service(Repository)
1011
app.add_service(Plugin)
12+
app.add_service(DashboardTemplate)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from spaceone.api.repository.v1 import dashboard_template_pb2, dashboard_template_pb2_grpc
2+
from spaceone.core.pygrpc import BaseAPI
3+
4+
5+
class DashboardTemplate(BaseAPI, dashboard_template_pb2_grpc.DashboardTemplateServicer):
6+
7+
pb2 = dashboard_template_pb2
8+
pb2_grpc = dashboard_template_pb2_grpc
9+
10+
def register(self, request, context):
11+
params, metadata = self.parse_request(request, context)
12+
with self.locator.get_service('DashboardTemplateService', metadata) as template_svc:
13+
template_data = template_svc.register(params)
14+
return self.locator.get_info('DashboardTemplateInfo', template_data)
15+
16+
def update(self, request, context):
17+
params, metadata = self.parse_request(request, context)
18+
with self.locator.get_service('DashboardTemplateService', metadata) as template_svc:
19+
template_data = template_svc.update(params)
20+
return self.locator.get_info('DashboardTemplateInfo', template_data)
21+
22+
def deregister(self, request, context):
23+
params, metadata = self.parse_request(request, context)
24+
with self.locator.get_service('DashboardTemplateService', metadata) as template_svc:
25+
template_svc.deregister(params)
26+
return self.locator.get_info('EmptyInfo')
27+
28+
def enable(self, request, context):
29+
params, metadata = self.parse_request(request, context)
30+
with self.locator.get_service('DashboardTemplateService', metadata) as template_svc:
31+
template_data = template_svc.enable(params)
32+
return self.locator.get_info('DashboardTemplateInfo', template_data)
33+
34+
def disable(self, request, context):
35+
params, metadata = self.parse_request(request, context)
36+
with self.locator.get_service('DashboardTemplateService', metadata) as template_svc:
37+
template_data = template_svc.disable(params)
38+
return self.locator.get_info('DashboardTemplateInfo', template_data)
39+
40+
def get(self, request, context):
41+
params, metadata = self.parse_request(request, context)
42+
with self.locator.get_service('DashboardTemplateService', metadata) as template_svc:
43+
template_data = template_svc.get(params)
44+
return self.locator.get_info('DashboardTemplateInfo', template_data)
45+
46+
def list(self, request, context):
47+
params, metadata = self.parse_request(request, context)
48+
with self.locator.get_service('DashboardTemplateService', metadata) as template_svc:
49+
templates_data, total_count = template_svc.list(params)
50+
return self.locator.get_info('DashboardTemplatesInfo', templates_data, total_count, minimal=self.get_minimal(params))
51+

src/spaceone/repository/manager/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@
1212
from spaceone.repository.manager.plugin_manager.remote_plugin_manager import (
1313
RemotePluginManager,
1414
)
15+
16+
from spaceone.repository.manager.dashboard_template_manager.local_dashboard_template_manager import (
17+
LocalDashboardTemplateManager,
18+
)
19+
from spaceone.repository.manager.dashboard_template_manager.remote_dashboard_template_manager import (
20+
RemoteDashboardTemplateManager,
21+
)
22+
from spaceone.repository.manager.dashboard_template_manager.managed_dashboard_template_manager import (
23+
ManagedDashboardTemplateManager,
24+
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import logging
2+
from abc import abstractmethod
3+
4+
from spaceone.core.manager import BaseManager
5+
6+
__all__ = ["DashboardTemplateManager"]
7+
_LOGGER = logging.getLogger(__name__)
8+
9+
10+
class DashboardTemplateManager(BaseManager):
11+
def create_template(self, params):
12+
pass
13+
14+
def update_template(self, params):
15+
pass
16+
17+
def enable_template(self, template_id, domain_id):
18+
pass
19+
20+
def disable_template(self, template_id, domain_id):
21+
pass
22+
23+
def delete_template(self, template_id, domain_id):
24+
pass
25+
26+
@abstractmethod
27+
def get_template(self, repo_info: dict, template_id, domain_id):
28+
pass
29+
30+
@abstractmethod
31+
def list_templates(self, repo_info: dict, query: dict, params: dict):
32+
pass
33+
34+
@staticmethod
35+
def change_response(
36+
info: dict, repo_info: dict = None
37+
) -> dict:
38+
if repo_info:
39+
info["repository_info"] = {
40+
"repository_id": repo_info["repository_id"],
41+
"name": repo_info["name"],
42+
"repository_type": repo_info["repository_type"],
43+
}
44+
45+
return info
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import logging
2+
3+
from spaceone.repository.model.dashboard_template_model import DashboardTemplate
4+
from spaceone.repository.manager.dashboard_template_manager import DashboardTemplateManager
5+
from spaceone.repository.manager.repository_manager import RepositoryManager
6+
7+
__all__ = ["LocalDashboardTemplateManager"]
8+
9+
_LOGGER = logging.getLogger(__name__)
10+
11+
12+
class LocalDashboardTemplateManager(DashboardTemplateManager):
13+
def __init__(self, *args, **kwargs):
14+
super().__init__(*args, **kwargs)
15+
self.template_model: DashboardTemplate = self.locator.get_model("DashboardTemplate")
16+
self.repo_info = RepositoryManager.get_repositories(repository_type="LOCAL")[0]
17+
18+
def create_template(self, params: dict):
19+
def _rollback(vo: DashboardTemplate):
20+
vo.delete()
21+
22+
template_vo: DashboardTemplate = self.template_model.create(params)
23+
self.transaction.add_rollback(_rollback, template_vo)
24+
25+
template_info = template_vo.to_dict()
26+
return self.change_response(template_info, self.repo_info)
27+
28+
def update_template(self, params):
29+
template_vo = self.template_model.get(
30+
template_id=params["template_id"], domain_id=params["domain_id"]
31+
)
32+
return self.update_template_by_vo(params, template_vo)
33+
34+
def enable_template(self, template_id, domain_id):
35+
template_vo = self.template_model.get(template_id=template_id, domain_id=domain_id)
36+
return self.update_template_by_vo({"state": "ENABLED"}, template_vo)
37+
38+
def disable_template(self, template_id, domain_id):
39+
template_vo = self.template_model.get(template_id=template_id, domain_id=domain_id)
40+
return self.update_template_by_vo({"state": "DISABLED"}, template_vo)
41+
42+
def update_template_by_vo(self, params, template_vo):
43+
def _rollback(old_data):
44+
_LOGGER.info(
45+
f'[ROLLBACK] Revert DashboardTemplate Data : {old_data["name"]} ({old_data["template_id"]})'
46+
)
47+
template_vo.update(old_data)
48+
49+
self.transaction.add_rollback(_rollback, template_vo.to_dict())
50+
template_vo = template_vo.update(params)
51+
template_info = template_vo.to_dict()
52+
return self.change_response(template_info, self.repo_info)
53+
54+
def delete_template(self, template_id, domain_id):
55+
template_vo = self.template_model.get(template_id=template_id, domain_id=domain_id)
56+
template_vo.delete()
57+
58+
def get_template(self, repo_info: dict, template_id, domain_id: str):
59+
template_vo = self.template_model.get(template_id=template_id, domain_id=domain_id)
60+
61+
template_info = template_vo.to_dict()
62+
return self.change_response(template_info, self.repo_info)
63+
64+
def list_templates(self, repos_info: dict, query: dict, params: dict):
65+
domain_id = params.get("domain_id")
66+
keyword = query.get("keyword")
67+
query_filter = query.get("filter", [])
68+
query_filter_or = query.get("filter_or", [])
69+
query["filter"] = self._append_domain_filter(query_filter, domain_id)
70+
query["filter_or"] = self._append_keyword_filter(query_filter_or, keyword)
71+
if "repository" in query.get("only", []):
72+
query["only"].remove("repository")
73+
74+
template_vos, total_count = self.template_model.query(**query)
75+
results = []
76+
for template_vo in template_vos:
77+
template_info = template_vo.to_dict()
78+
results.append(self.change_response(template_info, repos_info))
79+
80+
return results, total_count
81+
82+
@staticmethod
83+
def _append_domain_filter(query_filter, domain_id=None):
84+
if domain_id:
85+
query_filter.append({"k": "domain_id", "v": domain_id, "o": "eq"})
86+
87+
return query_filter
88+
89+
@staticmethod
90+
def _append_keyword_filter(query_filter_or, keyword):
91+
if keyword:
92+
query_filter_or.append({"k": "name", "v": keyword, "o": "contain"})
93+
94+
return query_filter_or

0 commit comments

Comments
 (0)