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
|
# Copyright 2017 Huawei Technologies Co.,LTD.
# 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.
import pecan
from pecan import rest
from six.moves import http_client
import wsme
from wsme import types as wtypes
from cyborg.api.controllers import base
from cyborg.api.controllers import link
from cyborg.api.controllers.v1 import types
from cyborg.api import expose
from cyborg.common import policy
from cyborg import objects
from cyborg.api.controllers.v1 import utils as api_utils
from cyborg.common import exception
class Accelerator(base.APIBase):
"""API representation of a accelerator.
This class enforces type checking and value constraints, and converts
between the internal object model and the API representation of
a accelerator.
"""
uuid = types.uuid
"""The UUID of the accelerator"""
name = wtypes.text
"""The name of the accelerator"""
description = wtypes.text
"""The description of the accelerator"""
project_id = types.uuid
"""The project UUID of the accelerator"""
user_id = types.uuid
"""The user UUID of the accelerator"""
device_type = wtypes.text
"""The device type of the accelerator"""
acc_type = wtypes.text
"""The type of the accelerator"""
acc_capability = wtypes.text
"""The capability of the accelerator"""
vendor_id = wtypes.text
"""The vendor id of the accelerator"""
product_id = wtypes.text
"""The product id of the accelerator"""
remotable = wtypes.IntegerType()
"""Whether the accelerator is remotable"""
links = wsme.wsattr([link.Link], readonly=True)
"""A list containing a self link"""
def __init__(self, **kwargs):
super(Accelerator, self).__init__(**kwargs)
self.fields = []
for field in objects.Accelerator.fields:
self.fields.append(field)
setattr(self, field, kwargs.get(field, wtypes.Unset))
@classmethod
def convert_with_links(cls, obj_acc):
api_acc = cls(**obj_acc.as_dict())
url = pecan.request.public_url
api_acc.links = [
link.Link.make_link('self', url, 'accelerators', api_acc.uuid),
link.Link.make_link('bookmark', url, 'accelerators', api_acc.uuid,
bookmark=True)
]
return api_acc
class AcceleratorCollection(base.APIBase):
"""API representation of a collection of accelerators."""
accelerators = [Accelerator]
"""A list containing accelerator objects"""
@classmethod
def convert_with_links(cls, obj_accs):
collection = cls()
collection.accelerators = [Accelerator.convert_with_links(obj_acc)
for obj_acc in obj_accs]
return collection
class AcceleratorPatchType(types.JsonPatchType):
_api_base = Accelerator
@staticmethod
def internal_attrs():
defaults = types.JsonPatchType.internal_attrs()
return defaults + ['/project_id', '/user_id', '/device_type',
'/acc_type', '/acc_capability', '/vendor_id',
'/product_id', '/remotable']
class AcceleratorsControllerBase(rest.RestController):
_resource = None
def _get_resource(self, uuid):
self._resource = objects.Accelerator.get(pecan.request.context, uuid)
return self._resource
class AcceleratorsController(AcceleratorsControllerBase):
"""REST controller for Accelerators."""
@policy.authorize_wsgi("cyborg:accelerator", "create", False)
@expose.expose(Accelerator, body=types.jsontype,
status_code=http_client.CREATED)
def post(self, acc):
"""Create a new accelerator.
:param acc: an accelerator within the request body.
"""
context = pecan.request.context
obj_acc = objects.Accelerator(context, **acc)
new_acc = pecan.request.conductor_api.accelerator_create(
context, obj_acc )
# Set the HTTP Location Header
pecan.response.location = link.build_url('accelerators', new_acc.uuid)
return Accelerator.convert_with_links(new_acc)
@policy.authorize_wsgi("cyborg:accelerator", "get")
@expose.expose(Accelerator, types.uuid)
def get_one(self, uuid):
"""Retrieve information about the given accelerator.
:param uuid: UUID of an accelerator.
"""
obj_acc = self._resource or self._get_resource(uuid)
return Accelerator.convert_with_links(obj_acc)
@expose.expose(AcceleratorCollection, int, types.uuid, wtypes.text,
wtypes.text, types.boolean)
def get_all(self, limit=None, marker=None, sort_key='id', sort_dir='asc',
all_tenants=None):
"""Retrieve a list of accelerators.
:param limit: Optional, to determinate the maximum number of
accelerators to return.
:param marker: Optional, to display a list of accelerators after this
marker.
:param sort_key: Optional, to sort the returned accelerators list by
this specified key value.
:param sort_dir: Optional, to return a list of accelerators with this
sort direction.
:param all_tenants: Optional, allows administrators to see the
accelerators owned by all tenants, otherwise only
the accelerators associated with the calling
tenant are included in the response.
"""
context = pecan.request.context
project_only = True
if context.is_admin and all_tenants:
project_only = False
marker_obj = None
if marker:
marker_obj = objects.Accelerator.get(context, marker)
obj_accs = objects.Accelerator.list(context, limit, marker_obj,
sort_key, sort_dir, project_only)
return AcceleratorCollection.convert_with_links(obj_accs)
@policy.authorize_wsgi("cyborg:accelerator", "update")
@expose.expose(Accelerator, types.uuid, body=[AcceleratorPatchType])
def patch(self, uuid, patch):
"""Update an accelerator.
:param uuid: UUID of an accelerator.
:param patch: a json PATCH document to apply to this accelerator.
"""
obj_acc = self._resource or self._get_resource(uuid)
try:
api_acc = Accelerator(
**api_utils.apply_jsonpatch(obj_acc.as_dict(), patch))
except api_utils.JSONPATCH_EXCEPTIONS as e:
raise exception.PatchError(patch=patch, reason=e)
# Update only the fields that have changed
for field in objects.Accelerator.fields:
try:
patch_val = getattr(api_acc, field)
except AttributeError:
# Ignore fields that aren't exposed in the API
continue
if patch_val == wtypes.Unset:
patch_val = None
if obj_acc[field] != patch_val:
obj_acc[field] = patch_val
context = pecan.request.context
new_acc = pecan.request.conductor_api.accelerator_update(context,
obj_acc)
return Accelerator.convert_with_links(new_acc)
@policy.authorize_wsgi("cyborg:accelerator", "delete")
@expose.expose(None, types.uuid, status_code=http_client.NO_CONTENT)
def delete(self, uuid):
"""Delete an accelerator.
:param uuid: UUID of an accelerator.
"""
obj_acc = self._resource or self._get_resource(uuid)
context = pecan.request.context
pecan.request.conductor_api.accelerator_delete(context, obj_acc)
|