summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/restful/api/osd.py
blob: 8577fae98eb406733ee6832d1f1ad6aa5e1735f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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