summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/restful/api/pool.py
blob: 40de54eb957790f63d20b57aeddf06e4f1d69e78 (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
136
137
138
139
140
from pecan import expose, request, response
from pecan.rest import RestController

from restful import common, context
from restful.decorators import auth


class PoolId(RestController):
    def __init__(self, pool_id):
        self.pool_id = pool_id


    @expose(template='json')
    @auth
    def get(self, **kwargs):
        """
        Show the information for the pool id
        """
        pool = context.instance.get_pool_by_id(self.pool_id)

        if not pool:
            response.status = 500
            return {'message': 'Failed to identify the pool id "{}"'.format(self.pool_id)}

        # pgp_num is called pg_placement_num, deal with that
        if 'pg_placement_num' in pool:
            pool['pgp_num'] = pool.pop('pg_placement_num')
        return pool


    @expose(template='json')
    @auth
    def patch(self, **kwargs):
        """
        Modify the information for the pool id
        """
        try:
            args = request.json
        except ValueError:
            response.status = 400
            return {'message': 'Bad request: malformed JSON or wrong Content-Type'}

        # Get the pool info for its name
        pool = context.instance.get_pool_by_id(self.pool_id)
        if not pool:
            response.status = 500
            return {'message': 'Failed to identify the pool id "{}"'.format(self.pool_id)}

        # Check for invalid pool args
        invalid = common.invalid_pool_args(args)
        if invalid:
            response.status = 500
            return {'message': 'Invalid arguments found: "{}"'.format(invalid)}

        # Schedule the update request
        return context.instance.submit_request(common.pool_update_commands(pool['pool_name'], args), **kwargs)


    @expose(template='json')
    @auth
    def delete(self, **kwargs):
        """
        Remove the pool data for the pool id
        """
        pool = context.instance.get_pool_by_id(self.pool_id)

        if not pool:
            response.status = 500
            return {'message': 'Failed to identify the pool id "{}"'.format(self.pool_id)}

        return context.instance.submit_request([[{
            'prefix': 'osd pool delete',
            'pool': pool['pool_name'],
            'pool2': pool['pool_name'],
            'yes_i_really_really_mean_it': True
        }]], **kwargs)



class Pool(RestController):
    @expose(template='json')
    @auth
    def get(self, **kwargs):
        """
        Show the information for all the pools
        """
        pools = context.instance.get('osd_map')['pools']

        # pgp_num is called pg_placement_num, deal with that
        for pool in pools:
            if 'pg_placement_num' in pool:
                pool['pgp_num'] = pool.pop('pg_placement_num')

        return pools


    @expose(template='json')
    @auth
    def post(self, **kwargs):
        """
        Create a new pool
        Requires name and pg_num dict arguments
        """
        args = request.json

        # Check for the required arguments
        pool_name = args.pop('name', None)
        if pool_name is None:
            response.status = 500
            return {'message': 'You need to specify the pool "name" argument'}

        pg_num = args.pop('pg_num', None)
        if pg_num is None:
            response.status = 500
            return {'message': 'You need to specify the "pg_num" argument'}

        # Run the pool create command first
        create_command = {
            'prefix': 'osd pool create',
            'pool': pool_name,
            'pg_num': pg_num
        }

        # Check for invalid pool args
        invalid = common.invalid_pool_args(args)
        if invalid:
            response.status = 500
            return {'message': 'Invalid arguments found: "{}"'.format(invalid)}

        # Schedule the creation and update requests
        return context.instance.submit_request(
            [[create_command]] +
            common.pool_update_commands(pool_name, args),
            **kwargs
        )


    @expose()
    def _lookup(self, pool_id, *remainder):
        return PoolId(int(pool_id)), remainder