summaryrefslogtreecommitdiff
path: root/ironic/api/controllers/v1/chassis.py
blob: 03cf770c5e6fea16a79921864757085a008b7bfc (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Copyright 2013 Red Hat, Inc.
# All Rights Reserved.
#
#    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.

from http import client as http_client

from ironic_lib import metrics_utils
from oslo_utils import uuidutils
from pecan import rest

from ironic import api
from ironic.api.controllers import link
from ironic.api.controllers.v1 import collection
from ironic.api.controllers.v1 import node
from ironic.api.controllers.v1 import notification_utils as notify
from ironic.api.controllers.v1 import utils as api_utils
from ironic.api import method
from ironic.common import args
from ironic.common import exception
from ironic.common.i18n import _
from ironic.common import policy
from ironic import objects

METRICS = metrics_utils.get_metrics_logger(__name__)

CHASSIS_SCHEMA = {
    'type': 'object',
    'properties': {
        'uuid': {'type': ['string', 'null']},
        'extra': {'type': ['object', 'null']},
        'description': {'type': ['string', 'null'], 'maxLength': 255},
    },
    'additionalProperties': False,
}

CHASSIS_VALIDATOR = args.and_valid(
    args.schema(CHASSIS_SCHEMA),
    args.dict_valid(uuid=args.uuid)
)

DEFAULT_RETURN_FIELDS = ['uuid', 'description']


def convert_with_links(rpc_chassis, fields=None, sanitize=True):
    chassis = api_utils.object_to_dict(
        rpc_chassis,
        fields=('description', 'extra'),
        link_resource='chassis'
    )

    url = api.request.public_url
    chassis['nodes'] = [
        link.make_link('self',
                       url,
                       'chassis',
                       rpc_chassis.uuid + "/nodes"),
        link.make_link('bookmark',
                       url,
                       'chassis',
                       rpc_chassis.uuid + "/nodes",
                       bookmark=True)],

    if fields is not None:
        api_utils.check_for_invalid_fields(fields, chassis)

    if sanitize:
        api_utils.sanitize_dict(chassis, fields)
    return chassis


def list_convert_with_links(rpc_chassis_list, limit, url=None, fields=None,
                            **kwargs):
    return collection.list_convert_with_links(
        items=[convert_with_links(ch, fields=fields,
                                  sanitize=False)
               for ch in rpc_chassis_list],
        item_name='chassis',
        limit=limit,
        url=url,
        fields=fields,
        sanitize_func=api_utils.sanitize_dict,
        **kwargs
    )


class ChassisController(rest.RestController):
    """REST controller for Chassis."""

    nodes = node.NodesController()
    """Expose nodes as a sub-element of chassis"""

    # Set the flag to indicate that the requests to this resource are
    # coming from a top-level resource
    nodes.from_chassis = True

    _custom_actions = {
        'detail': ['GET'],
    }

    invalid_sort_key_list = ['extra']

    def _get_chassis_collection(self, marker, limit, sort_key, sort_dir,
                                resource_url=None, fields=None, detail=None):
        limit = api_utils.validate_limit(limit)
        sort_dir = api_utils.validate_sort_dir(sort_dir)
        marker_obj = None
        if marker:
            marker_obj = objects.Chassis.get_by_uuid(api.request.context,
                                                     marker)

        if sort_key in self.invalid_sort_key_list:
            raise exception.InvalidParameterValue(
                _("The sort_key value %(key)s is an invalid field for sorting")
                % {'key': sort_key})

        chassis = objects.Chassis.list(api.request.context, limit,
                                       marker_obj, sort_key=sort_key,
                                       sort_dir=sort_dir)
        parameters = {}
        if detail is not None:
            parameters['detail'] = detail

        return list_convert_with_links(chassis, limit,
                                       url=resource_url,
                                       fields=fields,
                                       sort_key=sort_key,
                                       sort_dir=sort_dir,
                                       **parameters)

    @METRICS.timer('ChassisController.get_all')
    @method.expose()
    @args.validate(marker=args.uuid, limit=args.integer, sort_key=args.string,
                   sort_dir=args.string, fields=args.string_list,
                   detail=args.boolean)
    def get_all(self, marker=None, limit=None, sort_key='id', sort_dir='asc',
                fields=None, detail=None):
        """Retrieve a list of chassis.

        :param marker: pagination marker for large data sets.
        :param limit: maximum number of resources to return in a single result.
                      This value cannot be larger than the value of max_limit
                      in the [api] section of the ironic configuration, or only
                      max_limit resources will be returned.
        :param sort_key: column to sort results by. Default: id.
        :param sort_dir: direction to sort. "asc" or "desc". Default: asc.
        :param fields: Optional, a list with a specified set of fields
            of the resource to be returned.
        """
        cdict = api.request.context.to_policy_values()
        policy.authorize('baremetal:chassis:get', cdict, cdict)

        api_utils.check_allow_specify_fields(fields)

        fields = api_utils.get_request_return_fields(fields, detail,
                                                     DEFAULT_RETURN_FIELDS)

        return self._get_chassis_collection(marker, limit, sort_key, sort_dir,
                                            fields=fields, detail=detail)

    @METRICS.timer('ChassisController.detail')
    @method.expose()
    @args.validate(marker=args.uuid, limit=args.integer, sort_key=args.string,
                   sort_dir=args.string)
    def detail(self, marker=None, limit=None, sort_key='id', sort_dir='asc'):
        """Retrieve a list of chassis with detail.

        :param marker: pagination marker for large data sets.
        :param limit: maximum number of resources to return in a single result.
                      This value cannot be larger than the value of max_limit
                      in the [api] section of the ironic configuration, or only
                      max_limit resources will be returned.
        :param sort_key: column to sort results by. Default: id.
        :param sort_dir: direction to sort. "asc" or "desc". Default: asc.
        """
        cdict = api.request.context.to_policy_values()
        policy.authorize('baremetal:chassis:get', cdict, cdict)

        # /detail should only work against collections
        parent = api.request.path.split('/')[:-1][-1]
        if parent != "chassis":
            raise exception.HTTPNotFound()

        resource_url = '/'.join(['chassis', 'detail'])
        return self._get_chassis_collection(marker, limit, sort_key, sort_dir,
                                            resource_url)

    @METRICS.timer('ChassisController.get_one')
    @method.expose()
    @args.validate(chassis_uuid=args.uuid, fields=args.string_list)
    def get_one(self, chassis_uuid, fields=None):
        """Retrieve information about the given chassis.

        :param chassis_uuid: UUID of a chassis.
        :param fields: Optional, a list with a specified set of fields
            of the resource to be returned.
        """
        cdict = api.request.context.to_policy_values()
        policy.authorize('baremetal:chassis:get', cdict, cdict)

        api_utils.check_allow_specify_fields(fields)
        rpc_chassis = objects.Chassis.get_by_uuid(api.request.context,
                                                  chassis_uuid)
        return convert_with_links(rpc_chassis, fields=fields)

    @METRICS.timer('ChassisController.post')
    @method.expose(status_code=http_client.CREATED)
    @method.body('chassis')
    @args.validate(chassis=CHASSIS_VALIDATOR)
    def post(self, chassis):
        """Create a new chassis.

        :param chassis: a chassis within the request body.
        """
        context = api.request.context
        cdict = context.to_policy_values()
        policy.authorize('baremetal:chassis:create', cdict, cdict)

        # NOTE(yuriyz): UUID is mandatory for notifications payload
        if not chassis.get('uuid'):
            chassis['uuid'] = uuidutils.generate_uuid()

        new_chassis = objects.Chassis(context, **chassis)
        notify.emit_start_notification(context, new_chassis, 'create')
        with notify.handle_error_notification(context, new_chassis, 'create'):
            new_chassis.create()
        notify.emit_end_notification(context, new_chassis, 'create')
        # Set the HTTP Location Header
        api.response.location = link.build_url('chassis', new_chassis.uuid)
        return convert_with_links(new_chassis)

    @METRICS.timer('ChassisController.patch')
    @method.expose()
    @method.body('patch')
    @args.validate(chassis_uuid=args.string, patch=args.patch)
    def patch(self, chassis_uuid, patch):
        """Update an existing chassis.

        :param chassis_uuid: UUID of a chassis.
        :param patch: a json PATCH document to apply to this chassis.
        """
        context = api.request.context
        cdict = context.to_policy_values()
        policy.authorize('baremetal:chassis:update', cdict, cdict)

        api_utils.patch_validate_allowed_fields(
            patch, CHASSIS_SCHEMA['properties'])

        rpc_chassis = objects.Chassis.get_by_uuid(context, chassis_uuid)
        chassis = api_utils.apply_jsonpatch(rpc_chassis.as_dict(), patch)

        api_utils.patched_validate_with_schema(
            chassis, CHASSIS_SCHEMA, CHASSIS_VALIDATOR)

        api_utils.patch_update_changed_fields(
            chassis, rpc_chassis, fields=objects.Chassis.fields,
            schema=CHASSIS_SCHEMA
        )

        notify.emit_start_notification(context, rpc_chassis, 'update')
        with notify.handle_error_notification(context, rpc_chassis, 'update'):
            rpc_chassis.save()
        notify.emit_end_notification(context, rpc_chassis, 'update')
        return convert_with_links(rpc_chassis)

    @METRICS.timer('ChassisController.delete')
    @method.expose(status_code=http_client.NO_CONTENT)
    @args.validate(chassis_uuid=args.uuid)
    def delete(self, chassis_uuid):
        """Delete a chassis.

        :param chassis_uuid: UUID of a chassis.
        """
        context = api.request.context
        cdict = context.to_policy_values()
        policy.authorize('baremetal:chassis:delete', cdict, cdict)

        rpc_chassis = objects.Chassis.get_by_uuid(context, chassis_uuid)
        notify.emit_start_notification(context, rpc_chassis, 'delete')
        with notify.handle_error_notification(context, rpc_chassis, 'delete'):
            rpc_chassis.destroy()
        notify.emit_end_notification(context, rpc_chassis, 'delete')