summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/restful/api/osd.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
commit483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch)
treee5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/pybind/mgr/restful/api/osd.py
parentInitial commit. (diff)
downloadceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.tar.xz
ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.zip
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/pybind/mgr/restful/api/osd.py')
-rw-r--r--src/pybind/mgr/restful/api/osd.py135
1 files changed, 135 insertions, 0 deletions
diff --git a/src/pybind/mgr/restful/api/osd.py b/src/pybind/mgr/restful/api/osd.py
new file mode 100644
index 00000000..8577fae9
--- /dev/null
+++ b/src/pybind/mgr/restful/api/osd.py
@@ -0,0 +1,135 @@
+from pecan import expose, request, response
+from pecan.rest import RestController
+
+from restful import common, context
+from restful.decorators import auth
+
+
+class OsdIdCommand(RestController):
+ def __init__(self, osd_id):
+ self.osd_id = osd_id
+
+
+ @expose(template='json')
+ @auth
+ def get(self, **kwargs):
+ """
+ Show implemented commands for the OSD id
+ """
+ osd = context.instance.get_osd_by_id(self.osd_id)
+
+ if not osd:
+ response.status = 500
+ return {'message': 'Failed to identify the OSD id "{}"'.format(self.osd_id)}
+
+ if osd['up']:
+ return common.OSD_IMPLEMENTED_COMMANDS
+ else:
+ return []
+
+
+ @expose(template='json')
+ @auth
+ def post(self, **kwargs):
+ """
+ Run the implemented command for the OSD id
+ """
+ command = request.json.get('command', None)
+
+ osd = context.instance.get_osd_by_id(self.osd_id)
+
+ if not osd:
+ response.status = 500
+ return {'message': 'Failed to identify the OSD id "{}"'.format(self.osd_id)}
+
+ if not osd['up'] or command not in common.OSD_IMPLEMENTED_COMMANDS:
+ response.status = 500
+ return {'message': 'Command "{}" not available'.format(command)}
+
+ return context.instance.submit_request([[{
+ 'prefix': 'osd ' + command,
+ 'who': str(self.osd_id)
+ }]], **kwargs)
+
+
+
+class OsdId(RestController):
+ def __init__(self, osd_id):
+ self.osd_id = osd_id
+ self.command = OsdIdCommand(osd_id)
+
+
+ @expose(template='json')
+ @auth
+ def get(self, **kwargs):
+ """
+ Show the information for the OSD id
+ """
+ osd = context.instance.get_osds(ids=[str(self.osd_id)])
+ if len(osd) != 1:
+ response.status = 500
+ return {'message': 'Failed to identify the OSD id "{}"'.format(self.osd_id)}
+
+ return osd[0]
+
+
+ @expose(template='json')
+ @auth
+ def patch(self, **kwargs):
+ """
+ Modify the state (up, in) of the OSD id or reweight it
+ """
+ args = request.json
+
+ commands = []
+
+ if 'in' in args:
+ if args['in']:
+ commands.append({
+ 'prefix': 'osd in',
+ 'ids': [str(self.osd_id)]
+ })
+ else:
+ commands.append({
+ 'prefix': 'osd out',
+ 'ids': [str(self.osd_id)]
+ })
+
+ if 'up' in args:
+ if args['up']:
+ response.status = 500
+ return {'message': "It is not valid to set a down OSD to be up"}
+ else:
+ commands.append({
+ 'prefix': 'osd down',
+ 'ids': [str(self.osd_id)]
+ })
+
+ if 'reweight' in args:
+ commands.append({
+ 'prefix': 'osd reweight',
+ 'id': self.osd_id,
+ 'weight': args['reweight']
+ })
+
+ return context.instance.submit_request([commands], **kwargs)
+
+
+
+class Osd(RestController):
+ @expose(template='json')
+ @auth
+ def get(self, **kwargs):
+ """
+ Show the information for all the OSDs
+ """
+ # Parse request args
+ # TODO Filter by ids
+ pool_id = kwargs.get('pool', None)
+
+ return context.instance.get_osds(pool_id)
+
+
+ @expose()
+ def _lookup(self, osd_id, *remainder):
+ return OsdId(int(osd_id)), remainder