Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / pybind / mgr / restful / api / pool.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 PoolId(RestController):
9     def __init__(self, pool_id):
10         self.pool_id = pool_id
11
12
13     @expose(template='json')
14     @auth
15     def get(self, **kwargs):
16         """
17         Show the information for the pool id
18         """
19         pool = module.instance.get_pool_by_id(self.pool_id)
20
21         if not pool:
22             response.status = 500
23             return {'message': 'Failed to identify the pool id "%d"' % self.pool_id}
24
25         # pgp_num is called pg_placement_num, deal with that
26         if 'pg_placement_num' in pool:
27             pool['pgp_num'] = pool.pop('pg_placement_num')
28         return pool
29
30
31     @expose(template='json')
32     @auth
33     def patch(self, **kwargs):
34         """
35         Modify the information for the pool id
36         """
37         try:
38             args = request.json
39         except ValueError:
40             response.status = 400
41             return {'message': 'Bad request: malformed JSON or wrong Content-Type'}
42
43         # Get the pool info for its name
44         pool = module.instance.get_pool_by_id(self.pool_id)
45         if not pool:
46             response.status = 500
47             return {'message': 'Failed to identify the pool id "%d"' % self.pool_id}
48
49         # Check for invalid pool args
50         invalid = common.invalid_pool_args(args)
51         if invalid:
52             response.status = 500
53             return {'message': 'Invalid arguments found: "%s"' % str(invalid)}
54
55         # Schedule the update request
56         return module.instance.submit_request(common.pool_update_commands(pool['pool_name'], args), **kwargs)
57
58
59     @expose(template='json')
60     @auth
61     def delete(self, **kwargs):
62         """
63         Remove the pool data for the pool id
64         """
65         pool = module.instance.get_pool_by_id(self.pool_id)
66
67         if not pool:
68             response.status = 500
69             return {'message': 'Failed to identify the pool id "%d"' % self.pool_id}
70
71         return module.instance.submit_request([[{
72             'prefix': 'osd pool delete',
73             'pool': pool['pool_name'],
74             'pool2': pool['pool_name'],
75             'sure': '--yes-i-really-really-mean-it'
76         }]], **kwargs)
77
78
79
80 class Pool(RestController):
81     @expose(template='json')
82     @auth
83     def get(self, **kwargs):
84         """
85         Show the information for all the pools
86         """
87         pools = module.instance.get('osd_map')['pools']
88
89         # pgp_num is called pg_placement_num, deal with that
90         for pool in pools:
91             if 'pg_placement_num' in pool:
92                 pool['pgp_num'] = pool.pop('pg_placement_num')
93
94         return pools
95
96
97     @expose(template='json')
98     @auth
99     def post(self, **kwargs):
100         """
101         Create a new pool
102         Requires name and pg_num dict arguments
103         """
104         args = request.json
105
106         # Check for the required arguments
107         pool_name = args.pop('name', None)
108         if pool_name is None:
109             response.status = 500
110             return {'message': 'You need to specify the pool "name" argument'}
111
112         pg_num = args.pop('pg_num', None)
113         if pg_num is None:
114             response.status = 500
115             return {'message': 'You need to specify the "pg_num" argument'}
116
117         # Run the pool create command first
118         create_command = {
119             'prefix': 'osd pool create',
120             'pool': pool_name,
121             'pg_num': pg_num
122         }
123
124         # Check for invalid pool args
125         invalid = common.invalid_pool_args(args)
126         if invalid:
127             response.status = 500
128             return {'message': 'Invalid arguments found: "%s"' % str(invalid)}
129
130         # Schedule the creation and update requests
131         return module.instance.submit_request(
132             [[create_command]] +
133             common.pool_update_commands(pool_name, args),
134             **kwargs
135         )
136
137
138     @expose()
139     def _lookup(self, pool_id, *remainder):
140         return PoolId(int(pool_id)), remainder