]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/controllers/service.py
bump version to 18.2.4-pve3
[ceph.git] / ceph / src / pybind / mgr / dashboard / controllers / service.py
1 from typing import Dict, List, Optional
2
3 import cherrypy
4 from ceph.deployment.service_spec import ServiceSpec
5
6 from ..security import Scope
7 from ..services.exception import handle_custom_error, handle_orchestrator_error
8 from ..services.orchestrator import OrchClient, OrchFeature
9 from . import APIDoc, APIRouter, CreatePermission, DeletePermission, Endpoint, \
10 ReadPermission, RESTController, Task, UpdatePermission
11 from ._version import APIVersion
12 from .orchestrator import raise_if_no_orchestrator
13
14
15 def service_task(name, metadata, wait_for=2.0):
16 return Task("service/{}".format(name), metadata, wait_for)
17
18
19 @APIRouter('/service', Scope.HOSTS)
20 @APIDoc("Service Management API", "Service")
21 class Service(RESTController):
22
23 @Endpoint()
24 @ReadPermission
25 def known_types(self) -> List[str]:
26 """
27 Get a list of known service types, e.g. 'alertmanager',
28 'node-exporter', 'osd' or 'rgw'.
29 """
30 return ServiceSpec.KNOWN_SERVICE_TYPES
31
32 @raise_if_no_orchestrator([OrchFeature.SERVICE_LIST])
33 @RESTController.MethodMap(version=APIVersion(2, 0)) # type: ignore
34 def list(self, service_name: Optional[str] = None, offset: int = 0, limit: int = 5,
35 search: str = '', sort: str = '+service_name') -> List[dict]:
36 orch = OrchClient.instance()
37 services, count = orch.services.list(service_name=service_name, offset=int(offset),
38 limit=int(limit), search=search, sort=sort)
39 cherrypy.response.headers['X-Total-Count'] = count
40 return services
41
42 @raise_if_no_orchestrator([OrchFeature.SERVICE_LIST])
43 def get(self, service_name: str) -> List[dict]:
44 orch = OrchClient.instance()
45 services = orch.services.get(service_name)
46 if not services:
47 raise cherrypy.HTTPError(404, 'Service {} not found'.format(service_name))
48 return services[0].to_json()
49
50 @RESTController.Resource('GET')
51 @raise_if_no_orchestrator([OrchFeature.DAEMON_LIST])
52 def daemons(self, service_name: str) -> List[dict]:
53 orch = OrchClient.instance()
54 daemons = orch.services.list_daemons(service_name=service_name)
55 return [d.to_dict() for d in daemons]
56
57 @CreatePermission
58 @handle_custom_error('service', exceptions=(ValueError, TypeError))
59 @raise_if_no_orchestrator([OrchFeature.SERVICE_CREATE])
60 @handle_orchestrator_error('service')
61 @service_task('create', {'service_name': '{service_name}'})
62 def create(self, service_spec: Dict, service_name: str): # pylint: disable=W0613
63 """
64 :param service_spec: The service specification as JSON.
65 :param service_name: The service name, e.g. 'alertmanager'.
66 :return: None
67 """
68
69 OrchClient.instance().services.apply(service_spec, no_overwrite=True)
70
71 @UpdatePermission
72 @handle_custom_error('service', exceptions=(ValueError, TypeError))
73 @raise_if_no_orchestrator([OrchFeature.SERVICE_CREATE])
74 @handle_orchestrator_error('service')
75 @service_task('edit', {'service_name': '{service_name}'})
76 def set(self, service_spec: Dict, service_name: str): # pylint: disable=W0613
77 """
78 :param service_spec: The service specification as JSON.
79 :param service_name: The service name, e.g. 'alertmanager'.
80 :return: None
81 """
82
83 OrchClient.instance().services.apply(service_spec, no_overwrite=False)
84
85 @DeletePermission
86 @raise_if_no_orchestrator([OrchFeature.SERVICE_DELETE])
87 @handle_orchestrator_error('service')
88 @service_task('delete', {'service_name': '{service_name}'})
89 def delete(self, service_name: str):
90 """
91 :param service_name: The service name, e.g. 'mds' or 'crash.foo'.
92 :return: None
93 """
94 orch = OrchClient.instance()
95 orch.services.remove(service_name)