1 from typing
import Dict
, List
, Optional
4 from ceph
.deployment
.service_spec
import ServiceSpec
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
15 def service_task(name
, metadata
, wait_for
=2.0):
16 return Task("service/{}".format(name
), metadata
, wait_for
)
19 @APIRouter('/service', Scope
.HOSTS
)
20 @APIDoc("Service Management API", "Service")
21 class Service(RESTController
):
25 def known_types(self
) -> List
[str]:
27 Get a list of known service types, e.g. 'alertmanager',
28 'node-exporter', 'osd' or 'rgw'.
30 return ServiceSpec
.KNOWN_SERVICE_TYPES
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
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
)
47 raise cherrypy
.HTTPError(404, 'Service {} not found'.format(service_name
))
48 return services
[0].to_json()
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
]
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
64 :param service_spec: The service specification as JSON.
65 :param service_name: The service name, e.g. 'alertmanager'.
69 OrchClient
.instance().services
.apply(service_spec
, no_overwrite
=True)
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
78 :param service_spec: The service specification as JSON.
79 :param service_name: The service name, e.g. 'alertmanager'.
83 OrchClient
.instance().services
.apply(service_spec
, no_overwrite
=False)
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):
91 :param service_name: The service name, e.g. 'mds' or 'crash.foo'.
94 orch
= OrchClient
.instance()
95 orch
.services
.remove(service_name
)