summaryrefslogtreecommitdiffstats
path: root/compass-deck/api/v1/api.py
blob: 9dbc54891730540bf8b0202534f56c4f5a894ad7 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# Copyright 2014 Huawei Technologies Co. Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Define all the RestfulAPI entry points."""
import logging
import simplejson as json

from flask import Blueprint
from flask import request

from flask.ext.restful import Resource

from compass.api.exception import BadRequest
from compass.api.exception import Forbidden
from compass.api.exception import ItemNotFound
from compass.api.exception import Unauthorized
from compass.api.restfulAPI import CompassApi
from compass.api import utils

from compass.db import db_api
from compass.db.exception import InvalidParameter
from compass.db.exception import RecordNotExists


v1_app = Blueprint('v1_app', __name__)
api = CompassApi(v1_app)
PREFIX = '/v1.0'


@v1_app.route('/users', methods=['GET'])
def list_users():
    """List details of all users filtered by user email and admin role."""

    emails = request.args.getlist('email')
    is_admin = request.args.get('admin')
    filters = {}

    if emails:
        filters['email'] = emails

    if is_admin is not None:
        if is_admin == 'true':
            filters['is_admin'] = True
        elif is_admin == 'false':
            filters['is_admin'] = False

    users_list = db_api.user.list_users(filters)

    return utils.make_json_response(200, users_list)


class User(Resource):
    ENDPOINT = PREFIX + '/users'

    def get(self, user_id):
        """Get user's information for the specified ID."""
        try:
            user_data = db_api.user.get_user(user_id)
            logging.debug("user_data is===>%s", user_data)

        except RecordNotExists as ex:
            error_msg = ex.message
            raise ItemNotFound(error_msg)

        return utils.make_json_response(200, user_data)


class Adapter(Resource):
    ENDPOINT = PREFIX + "/adapters"

    def get(self, adapter_id):
        """Get information for a specified adapter."""

        try:
            adapter_info = db_api.adapter.get_adapter(adapter_id)
        except RecordNotExists as ex:
            error_msg = ex.message
            raise ItemNotFound(error_msg)

        return utils.make_json_response(200, adapter_info)


@v1_app.route('/adapters', methods=['GET'])
def list_adapters():
    """List details of all adapters filtered by the adapter name(s)."""

    names = request.args.getlist('name')
    filters = {}
    if names:
        filters['name'] = names

    adapters_list = db_api.adapter.list_adapters(filters)
    return utils.make_json_response(200, adapters_list)


@v1_app.route('/adapters/<int:adapter_id>/config-schema', methods=['GET'])
def get_adapter_config_schema(adapter_id):
    """Get the config schema for a specified adapter."""

    os_id = request.args.get("os-id", type=int)

    try:
        schema = db_api.adapter.get_adapter_config_schema(adapter_id, os_id)
    except RecordNotExists as ex:
        raise ItemNotFound(ex.message)

    return utils.make_json_response(200, schema)


@v1_app.route('/adapters/<int:adapter_id>/roles', methods=['GET'])
def get_adapter_roles(adapter_id):
    """Get roles for a specified adapter."""

    try:
        roles = db_api.adapter.get_adapter(adapter_id, True)
    except RecordNotExists as ex:
        raise ItemNotFound(ex.message)

    return utils.make_json_response(200, roles)


class Cluster(Resource):
    def get(self, cluster_id):
        """Get information for a specified cluster."""

        try:
            cluster_info = db_api.cluster.get_cluster(cluster_id)

        except RecordNotExists as ex:
            error_msg = ex.message
            raise ItemNotFound(error_msg)

        return utils.make_json_response(200, cluster_info)


@v1_app.route('/clusters/<int:cluster_id>/config', methods=['PUT', 'PATCH'])
def add_cluster_config(cluster_id):
    """Update the config information for a specified cluster."""
    config = json.loads(request.data)
    if not config:
        raise BadRequest("Config cannot be None!")

    root_elems = ['os_config', 'package_config']
    if len(config.keys()) != 1 or config.keys()[0] not in root_elems:
        error_msg = ("Config root elements must be either"
                     "'os_config' or 'package_config'")
        raise BadRequest(error_msg)

    result = None
    is_patch_method = request.method == 'PATCH'
    try:
        if "os_config" in config:
            result = db_api.cluster\
                           .update_cluster_config(cluster_id,
                                                  'os_config',
                                                  config,
                                                  patch=is_patch_method)
        elif "package_config" in config:
            result = db_api.cluster\
                           .update_cluster_config(cluster_id,
                                                  'package_config', config,
                                                  patch=is_patch_method)

    except InvalidParameter as ex:
        raise BadRequest(ex.message)

    except RecordNotExists as ex:
        raise ItemNotFound(ex.message)

    return utils.make_json_response(200, result)


api.add_resource(User,
                 '/users',
                 '/users/<int:user_id>')
api.add_resource(Adapter,
                 '/adapters',
                 '/adapters/<int:adapter_id>')
api.add_resource(Cluster,
                 '/clusters',
                 '/clusters/<int:cluster_id>')


@v1_app.errorhandler(ItemNotFound)
def handle_not_exist(error, failed_objs=None):
    """Handler of ItemNotFound Exception."""

    message = {'type': 'itemNotFound',
               'message': error.message}

    if failed_objs and isinstance(failed_objs, dict):
        message.update(failed_objs)

    return utils.make_json_response(404, message)


@v1_app.errorhandler(Unauthorized)
def handle_invalid_user(error, failed_objs=None):
    """Handler of Unauthorized Exception."""

    message = {'type': 'unathorized',
               'message': error.message}

    if failed_objs and isinstance(failed_objs, dict):
        message.update(failed_objs)

    return utils.make_json_response(401, message)


@v1_app.errorhandler(Forbidden)
def handle_no_permission(error, failed_objs=None):
    """Handler of Forbidden Exception."""

    message = {'type': 'Forbidden',
               'message': error.message}

    if failed_objs and isinstance(failed_objs, dict):
        message.update(failed_objs)

    return utils.make_json_response(403, message)


@v1_app.errorhandler(BadRequest)
def handle_bad_request(error, failed_objs=None):
    """Handler of badRequest Exception."""

    message = {'type': 'badRequest',
               'message': error.message}

    if failed_objs and isinstance(failed_objs, dict):
        message.update(failed_objs)

    return utils.make_json_response(400, message)


if __name__ == '__main__':
    v1_app.run(debug=True)