-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathflavor_profiles.py
More file actions
237 lines (203 loc) · 10.2 KB
/
flavor_profiles.py
File metadata and controls
237 lines (203 loc) · 10.2 KB
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
# Copyright 2014 Rackspace
# Copyright 2016 Blue Box, an IBM Company
#
# 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 oslo_db import exception as odb_exceptions
from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import excutils
from oslo_utils import uuidutils
from pecan import request as pecan_request
from sqlalchemy.orm import exc as sa_exception
from wsme import types as wtypes
from wsmeext import pecan as wsme_pecan
from octavia.api.drivers import driver_factory
from octavia.api.drivers import utils as driver_utils
from octavia.api.v2.controllers import base
from octavia.api.v2.types import flavor_profile as profile_types
from octavia.common import constants
from octavia.common import exceptions
LOG = logging.getLogger(__name__)
class FlavorProfileController(base.BaseController):
RBAC_TYPE = constants.RBAC_FLAVOR_PROFILE
def __init__(self):
super().__init__()
@wsme_pecan.wsexpose(profile_types.FlavorProfileRootResponse, wtypes.text,
[wtypes.text], ignore_extra_args=True)
def get_one(self, id, fields=None):
"""Gets a flavor profile's detail."""
context = pecan_request.context.get('octavia_context')
self._auth_validate_action(context, context.project_id,
constants.RBAC_GET_ONE)
if id == constants.NIL_UUID:
raise exceptions.NotFound(resource='Flavor profile',
id=constants.NIL_UUID)
with context.session.begin():
flavor_profile = self._get_db_flavor_profile(context.session, id)
result = (
profile_types.FlavorProfileResponse.from_db_obj(flavor_profile)
)
if fields is not None:
result = self._filter_fields([result], fields)[0]
return profile_types.FlavorProfileRootResponse(flavorprofile=result)
@wsme_pecan.wsexpose(profile_types.FlavorProfilesRootResponse,
[wtypes.text], ignore_extra_args=True)
def get_all(self, fields=None):
"""Lists all flavor profiles."""
pcontext = pecan_request.context
context = pcontext.get('octavia_context')
self._auth_validate_action(context, context.project_id,
constants.RBAC_GET_ALL)
with context.session.begin():
flavor_profiles, links = (
self.repositories.flavor_profile.get_all_orm(
context.session,
pagination_helper=pcontext.get(constants.PAGINATION_HELPER)
)
)
result = [
profile_types.FlavorProfileResponse.from_db_obj(flavor_profile)
for flavor_profile in flavor_profiles
]
if fields is not None:
result = self._filter_fields(result, fields)
return profile_types.FlavorProfilesRootResponse(
flavorprofiles=result, flavorprofile_links=links)
@wsme_pecan.wsexpose(profile_types.FlavorProfileRootResponse,
body=profile_types.FlavorProfileRootPOST,
status_code=201)
def post(self, flavor_profile_):
"""Creates a flavor Profile."""
flavorprofile = flavor_profile_.flavorprofile
context = pecan_request.context.get('octavia_context')
self._auth_validate_action(context, context.project_id,
constants.RBAC_POST)
# Do a basic JSON validation on the metadata
try:
flavor_data_dict = jsonutils.loads(flavorprofile.flavor_data)
except Exception as e:
raise exceptions.InvalidOption(
value=flavorprofile.flavor_data,
option=constants.FLAVOR_DATA) from e
# Validate that the provider driver supports the metadata
driver = driver_factory.get_driver(flavorprofile.provider_name)
driver_utils.call_provider(driver.name, driver.validate_flavor,
flavor_data_dict)
context.session.begin()
try:
flavorprofile_dict = flavorprofile.to_dict(render_unsets=True)
flavorprofile_dict['id'] = uuidutils.generate_uuid()
db_flavor_profile = self.repositories.flavor_profile.create(
context.session, **flavorprofile_dict)
context.session.commit()
except odb_exceptions.DBDuplicateEntry as e:
context.session.rollback()
raise exceptions.IDAlreadyExists() from e
except Exception:
with excutils.save_and_reraise_exception():
context.session.rollback()
result = (
profile_types.FlavorProfileResponse.from_db_obj(db_flavor_profile)
)
return profile_types.FlavorProfileRootResponse(flavorprofile=result)
def _validate_update_fp(self, context, id, flavorprofile):
if flavorprofile.name is None:
raise exceptions.InvalidOption(value=None, option=constants.NAME)
if flavorprofile.provider_name is None:
raise exceptions.InvalidOption(value=None,
option=constants.PROVIDER_NAME)
if flavorprofile.flavor_data is None:
raise exceptions.InvalidOption(value=None,
option=constants.FLAVOR_DATA)
# Don't allow changes to the flavor_data or provider_name if it
# is in use.
if (not isinstance(flavorprofile.flavor_data, wtypes.UnsetType) or
not isinstance(flavorprofile.provider_name, wtypes.UnsetType)):
if self.repositories.flavor.count(context.session,
flavor_profile_id=id) > 0:
raise exceptions.ObjectInUse(object='Flavor profile', id=id)
@wsme_pecan.wsexpose(profile_types.FlavorProfileRootResponse,
wtypes.text, status_code=200,
body=profile_types.FlavorProfileRootPUT)
def put(self, id, flavor_profile_):
"""Updates a flavor Profile."""
flavorprofile = flavor_profile_.flavorprofile
context = pecan_request.context.get('octavia_context')
self._auth_validate_action(context, context.project_id,
constants.RBAC_PUT)
context.session.begin()
try:
self._validate_update_fp(context, id, flavorprofile)
if id == constants.NIL_UUID:
raise exceptions.NotFound(resource='Flavor profile',
id=constants.NIL_UUID)
if not isinstance(flavorprofile.flavor_data, wtypes.UnsetType):
# Do a basic JSON validation on the metadata
try:
flavor_data_dict = (
jsonutils.loads(flavorprofile.flavor_data)
)
except Exception as e:
raise exceptions.InvalidOption(
value=flavorprofile.flavor_data,
option=constants.FLAVOR_DATA) from e
if isinstance(flavorprofile.provider_name, wtypes.UnsetType):
db_flavor_profile = self._get_db_flavor_profile(
context.session,
id
)
provider_driver = db_flavor_profile.provider_name
else:
provider_driver = flavorprofile.provider_name
# Validate that the provider driver supports the metadata
driver = driver_factory.get_driver(provider_driver)
driver_utils.call_provider(driver.name, driver.validate_flavor,
flavor_data_dict)
flavorprofile_dict = flavorprofile.to_dict(render_unsets=False)
if flavorprofile_dict:
self.repositories.flavor_profile.update(context.session, id,
**flavorprofile_dict)
context.session.commit()
except Exception:
with excutils.save_and_reraise_exception():
context.session.rollback()
# Force SQL alchemy to query the DB, otherwise we get inconsistent
# results
context.session.expire_all()
with context.session.begin():
flavor_profile = self._get_db_flavor_profile(context.session, id)
result = (
profile_types.FlavorProfileResponse.from_db_obj(flavor_profile)
)
return profile_types.FlavorProfileRootResponse(flavorprofile=result)
@wsme_pecan.wsexpose(None, wtypes.text, status_code=204)
def delete(self, flavor_profile_id):
"""Deletes a Flavor Profile"""
context = pecan_request.context.get('octavia_context')
self._auth_validate_action(context, context.project_id,
constants.RBAC_DELETE)
if flavor_profile_id == constants.NIL_UUID:
raise exceptions.NotFound(resource='Flavor profile',
id=constants.NIL_UUID)
# Don't allow it to be deleted if it is in use by a flavor
with context.session.begin():
if self.repositories.flavor.count(
context.session, flavor_profile_id=flavor_profile_id) > 0:
raise exceptions.ObjectInUse(object='Flavor profile',
id=flavor_profile_id)
try:
self.repositories.flavor_profile.delete(context.session,
id=flavor_profile_id)
except sa_exception.NoResultFound as e:
raise exceptions.NotFound(
resource='Flavor profile', id=flavor_profile_id) from e