]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/controllers/service.py
d3ba882a1d92633ab143fc86df0008f0751e6657
[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 ..exceptions import DashboardException
7 from ..security import Scope
8 from ..services.exception import handle_orchestrator_error
9 from ..services.orchestrator import OrchClient, OrchFeature
10 from . import APIDoc, APIRouter, CreatePermission, DeletePermission, Endpoint, \
11 ReadPermission, RESTController, Task
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 def list(self, service_name: Optional[str] = None) -> List[dict]:
34 orch = OrchClient.instance()
35 return [service.to_dict() for service in orch.services.list(service_name=service_name)]
36
37 @raise_if_no_orchestrator([OrchFeature.SERVICE_LIST])
38 def get(self, service_name: str) -> List[dict]:
39 orch = OrchClient.instance()
40 services = orch.services.get(service_name)
41 if not services:
42 raise cherrypy.HTTPError(404, 'Service {} not found'.format(service_name))
43 return services[0].to_json()
44
45 @RESTController.Resource('GET')
46 @raise_if_no_orchestrator([OrchFeature.DAEMON_LIST])
47 def daemons(self, service_name: str) -> List[dict]:
48 orch = OrchClient.instance()
49 daemons = orch.services.list_daemons(service_name=service_name)
50 return [d.to_dict() for d in daemons]
51
52 @CreatePermission
53 @raise_if_no_orchestrator([OrchFeature.SERVICE_CREATE])
54 @handle_orchestrator_error('service')
55 @service_task('create', {'service_name': '{service_name}'})
56 def create(self, service_spec: Dict, service_name: str): # pylint: disable=W0613
57 """
58 :param service_spec: The service specification as JSON.
59 :param service_name: The service name, e.g. 'alertmanager'.
60 :return: None
61 """
62 try:
63 orch = OrchClient.instance()
64 orch.services.apply(service_spec)
65 except (ValueError, TypeError) as e:
66 raise DashboardException(e, component='service')
67
68 @DeletePermission
69 @raise_if_no_orchestrator([OrchFeature.SERVICE_DELETE])
70 @handle_orchestrator_error('service')
71 @service_task('delete', {'service_name': '{service_name}'})
72 def delete(self, service_name: str):
73 """
74 :param service_name: The service name, e.g. 'mds' or 'crash.foo'.
75 :return: None
76 """
77 orch = OrchClient.instance()
78 orch.services.remove(service_name)