]>
Commit | Line | Data |
---|---|---|
31f18b77 FG |
1 | from pecan import expose, request, response |
2 | from pecan.rest import RestController | |
3 | ||
11fdf7f2 | 4 | from restful import common, context |
31f18b77 FG |
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 | """ | |
11fdf7f2 | 19 | pool = context.instance.get_pool_by_id(self.pool_id) |
31f18b77 FG |
20 | |
21 | if not pool: | |
22 | response.status = 500 | |
11fdf7f2 | 23 | return {'message': 'Failed to identify the pool id "{}"'.format(self.pool_id)} |
31f18b77 FG |
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 | """ | |
11fdf7f2 | 37 | try: |
31f18b77 FG |
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 | |
11fdf7f2 | 44 | pool = context.instance.get_pool_by_id(self.pool_id) |
31f18b77 FG |
45 | if not pool: |
46 | response.status = 500 | |
11fdf7f2 | 47 | return {'message': 'Failed to identify the pool id "{}"'.format(self.pool_id)} |
31f18b77 FG |
48 | |
49 | # Check for invalid pool args | |
50 | invalid = common.invalid_pool_args(args) | |
51 | if invalid: | |
52 | response.status = 500 | |
11fdf7f2 | 53 | return {'message': 'Invalid arguments found: "{}"'.format(invalid)} |
31f18b77 FG |
54 | |
55 | # Schedule the update request | |
11fdf7f2 | 56 | return context.instance.submit_request(common.pool_update_commands(pool['pool_name'], args), **kwargs) |
31f18b77 FG |
57 | |
58 | ||
59 | @expose(template='json') | |
60 | @auth | |
61 | def delete(self, **kwargs): | |
62 | """ | |
63 | Remove the pool data for the pool id | |
64 | """ | |
11fdf7f2 | 65 | pool = context.instance.get_pool_by_id(self.pool_id) |
31f18b77 FG |
66 | |
67 | if not pool: | |
68 | response.status = 500 | |
11fdf7f2 | 69 | return {'message': 'Failed to identify the pool id "{}"'.format(self.pool_id)} |
31f18b77 | 70 | |
11fdf7f2 | 71 | return context.instance.submit_request([[{ |
31f18b77 FG |
72 | 'prefix': 'osd pool delete', |
73 | 'pool': pool['pool_name'], | |
74 | 'pool2': pool['pool_name'], | |
11fdf7f2 | 75 | 'yes_i_really_really_mean_it': True |
31f18b77 FG |
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 | """ | |
11fdf7f2 | 87 | pools = context.instance.get('osd_map')['pools'] |
31f18b77 FG |
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 | |
11fdf7f2 | 128 | return {'message': 'Invalid arguments found: "{}"'.format(invalid)} |
31f18b77 FG |
129 | |
130 | # Schedule the creation and update requests | |
11fdf7f2 | 131 | return context.instance.submit_request( |
31f18b77 FG |
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 |