Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / pybind / mgr / restful / api / osd.py
1 from pecan import expose, request, response
2 from pecan.rest import RestController
3
4 from restful import common, module
5 from restful.decorators import auth
6
7
8 class OsdIdCommand(RestController):
9     def __init__(self, osd_id):
10         self.osd_id = osd_id
11
12
13     @expose(template='json')
14     @auth
15     def get(self, **kwargs):
16         """
17         Show implemented commands for the OSD id
18         """
19         osd = module.instance.get_osd_by_id(self.osd_id)
20
21         if not osd:
22             response.status = 500
23             return {'message': 'Failed to identify the OSD id "%d"' % self.osd_id}
24
25         if osd['up']:
26             return common.OSD_IMPLEMENTED_COMMANDS
27         else:
28             return []
29
30
31     @expose(template='json')
32     @auth
33     def post(self, **kwargs):
34         """
35         Run the implemented command for the OSD id
36         """
37         command = request.json.get('command', None)
38
39         osd = module.instance.get_osd_by_id(self.osd_id)
40
41         if not osd:
42             response.status = 500
43             return {'message': 'Failed to identify the OSD id "%d"' % self.osd_id}
44
45         if not osd['up'] or command not in common.OSD_IMPLEMENTED_COMMANDS:
46             response.status = 500
47             return {'message': 'Command "%s" not available' % command}
48
49         return module.instance.submit_request([[{
50             'prefix': 'osd ' + command,
51             'who': str(self.osd_id)
52         }]], **kwargs)
53
54
55
56 class OsdId(RestController):
57     def __init__(self, osd_id):
58         self.osd_id = osd_id
59         self.command = OsdIdCommand(osd_id)
60
61
62     @expose(template='json')
63     @auth
64     def get(self, **kwargs):
65         """
66         Show the information for the OSD id
67         """
68         osd = module.instance.get_osds(ids=[str(self.osd_id)])
69         if len(osd) != 1:
70             response.status = 500
71             return {'message': 'Failed to identify the OSD id "%d"' % self.osd_id}
72
73         return osd[0]
74
75
76     @expose(template='json')
77     @auth
78     def patch(self, **kwargs):
79         """
80         Modify the state (up, in) of the OSD id or reweight it
81         """
82         args = request.json
83
84         commands = []
85
86         if 'in' in args:
87             if args['in']:
88                 commands.append({
89                     'prefix': 'osd in',
90                     'ids': [str(self.osd_id)]
91                 })
92             else:
93                 commands.append({
94                     'prefix': 'osd out',
95                     'ids': [str(self.osd_id)]
96                 })
97
98         if 'up' in args:
99             if args['up']:
100                 response.status = 500
101                 return {'message': "It is not valid to set a down OSD to be up"}
102             else:
103                 commands.append({
104                     'prefix': 'osd down',
105                     'ids': [str(self.osd_id)]
106                 })
107
108         if 'reweight' in args:
109             commands.append({
110                 'prefix': 'osd reweight',
111                 'id': self.osd_id,
112                 'weight': args['reweight']
113             })
114
115         return module.instance.submit_request([commands], **kwargs)
116
117
118
119 class Osd(RestController):
120     @expose(template='json')
121     @auth
122     def get(self, **kwargs):
123         """
124         Show the information for all the OSDs
125         """
126         # Parse request args
127         # TODO Filter by ids
128         pool_id = kwargs.get('pool', None)
129
130         return module.instance.get_osds(pool_id)
131
132
133     @expose()
134     def _lookup(self, osd_id, *remainder):
135         return OsdId(int(osd_id)), remainder