Buckets:
| import collections | |
| import copy | |
| import netaddr | |
| from neutron_lib.api import attributes | |
| from neutron_lib.callbacks import events | |
| from neutron_lib.callbacks import registry | |
| from neutron_lib import exceptions | |
| from oslo_log import log as logging | |
| from oslo_policy import policy as oslo_policy | |
| from oslo_utils import excutils | |
| import webob.exc | |
| from neutron._i18n import _ | |
| from neutron.api import api_common | |
| from neutron.api.v2 import resource as wsgi_resource | |
| from neutron.common import constants as n_const | |
| from neutron.common import exceptions as n_exc | |
| from neutron.common import rpc as n_rpc | |
| from neutron.db import api as db_api | |
| from neutron import policy | |
| from neutron import quota | |
| from neutron.quota import resource_registry | |
| LOG = logging.getLogger(__name__) | |
| FAULT_MAP = {exceptions.NotFound: webob.exc.HTTPNotFound, exceptions.Conflict: webob.exc.HTTPConflict, exceptions.InUse: webob.exc.HTTPConflict, exceptions.BadRequest: webob.exc.HTTPBadRequest, exceptions.ServiceUnavailable: webob.exc.HTTPServiceUnavailable, exceptions.NotAuthorized: webob.exc.HTTPForbidden, netaddr.AddrFormatError: webob.exc.HTTPBadRequest, oslo_policy.PolicyNotAuthorized: webob.exc.HTTPForbidden} | |
| class Controller(object, ): | |
| LIST = 'list' | |
| SHOW = 'show' | |
| CREATE = 'create' | |
| UPDATE = 'update' | |
| DELETE = 'delete' | |
| def plugin(self): | |
| return self._plugin | |
| def resource(self): | |
| return self._resource | |
| def attr_info(self): | |
| return self._attr_info | |
| def member_actions(self): | |
| return self._member_actions | |
| def allow_pagination(self): | |
| return self._allow_pagination | |
| def allow_sorting(self): | |
| return self._allow_sorting | |
| def _init_policy_attrs(self): | |
| 'Create the list of attributes required by policy.\n\n If the attribute map contains a tenant_id policy, then include\n project_id to bring the resource into the brave new world.\n\n :return: sorted list of attributes required by policy\n\n ' | |
| policy_attrs = {name for (name, info) in self._attr_info.items() if info.get('required_by_policy')} | |
| if ('tenant_id' in policy_attrs): | |
| policy_attrs.add('project_id') | |
| return sorted(policy_attrs) | |
| def __init__(self, plugin, collection, resource, attr_info, allow_bulk=False, member_actions=None, parent=None, allow_pagination=False, allow_sorting=False): | |
| if (member_actions is None): | |
| member_actions = [] | |
| self._plugin = plugin | |
| self._collection = collection.replace('-', '_') | |
| self._resource = resource.replace('-', '_') | |
| self._attr_info = attr_info | |
| self._allow_bulk = allow_bulk | |
| self._allow_pagination = allow_pagination | |
| self._allow_sorting = allow_sorting | |
| self._native_bulk = self._is_native_bulk_supported() | |
| self._native_pagination = self._is_native_pagination_supported() | |
| self._native_sorting = self._is_native_sorting_supported() | |
| self._policy_attrs = self._init_policy_attrs() | |
| self._notifier = n_rpc.get_notifier('network') | |
| self._member_actions = member_actions | |
| self._primary_key = self._get_primary_key() | |
| if (self._allow_pagination and self._native_pagination): | |
| if (not self._native_sorting): | |
| raise exceptions.Invalid(_('Native pagination depend on native sorting')) | |
| if (not self._allow_sorting): | |
| LOG.info('Allow sorting is enabled because native pagination requires native sorting') | |
| self._allow_sorting = True | |
| self.parent = parent | |
| if parent: | |
| self._parent_id_name = ('%s_id' % parent['member_name']) | |
| parent_part = ('_%s' % parent['member_name']) | |
| else: | |
| self._parent_id_name = None | |
| parent_part = '' | |
| self._plugin_handlers = {self.LIST: ('get%s_%s' % (parent_part, self._collection)), self.SHOW: ('get%s_%s' % (parent_part, self._resource))} | |
| for action in [self.CREATE, self.UPDATE, self.DELETE]: | |
| self._plugin_handlers[action] = ('%s%s_%s' % (action, parent_part, self._resource)) | |
| def _get_primary_key(self, default_primary_key='id'): | |
| for (key, value) in self._attr_info.items(): | |
| if value.get('primary_key', False): | |
| return key | |
| return default_primary_key | |
| def _is_native_bulk_supported(self): | |
| native_bulk_attr_name = ('_%s__native_bulk_support' % self._plugin.__class__.__name__) | |
| return getattr(self._plugin, native_bulk_attr_name, False) | |
| def _is_native_pagination_supported(self): | |
| return api_common.is_native_pagination_supported(self._plugin) | |
| def _is_native_sorting_supported(self): | |
| return api_common.is_native_sorting_supported(self._plugin) | |
| def _exclude_attributes_by_policy(self, context, data): | |
| 'Identifies attributes to exclude according to authZ policies.\n\n Return a list of attribute names which should be stripped from the\n response returned to the user because the user is not authorized\n to see them.\n ' | |
| attributes_to_exclude = [] | |
| for attr_name in data.keys(): | |
| if (attr_name == 'project_id'): | |
| continue | |
| attr_data = self._attr_info.get(attr_name) | |
| if (attr_data and attr_data['is_visible']): | |
| if policy.check(context, ('%s:%s' % (self._plugin_handlers[self.SHOW], attr_name)), data, might_not_exist=True, pluralized=self._collection): | |
| continue | |
| attributes_to_exclude.append(attr_name) | |
| if (attr_name == 'tenant_id'): | |
| attributes_to_exclude.append('project_id') | |
| return attributes_to_exclude | |
| def _view(self, context, data, fields_to_strip=None): | |
| 'Build a view of an API resource.\n\n :param context: the neutron context\n :param data: the object for which a view is being created\n :param fields_to_strip: attributes to remove from the view\n\n :returns: a view of the object which includes only attributes\n visible according to API resource declaration and authZ policies.\n ' | |
| fields_to_strip = ((fields_to_strip or []) + self._exclude_attributes_by_policy(context, data)) | |
| return self._filter_attributes(data, fields_to_strip) | |
| def _filter_attributes(self, data, fields_to_strip=None): | |
| if (not fields_to_strip): | |
| return data | |
| return dict((item for item in data.items() if (item[0] not in fields_to_strip))) | |
| def _do_field_list(self, original_fields): | |
| fields_to_add = None | |
| if original_fields: | |
| fields_to_add = [attr for attr in self._policy_attrs if (attr not in original_fields)] | |
| original_fields.extend(self._policy_attrs) | |
| return (original_fields, fields_to_add) | |
| def __getattr__(self, name): | |
| if (name in self._member_actions): | |
| def _handle_action(request, id, **kwargs): | |
| arg_list = [request.context, id] | |
| policy.init() | |
| try: | |
| parent_id = kwargs.get(self._parent_id_name) | |
| resource = self._item(request, id, do_authz=True, field_list=None, parent_id=parent_id) | |
| except oslo_policy.PolicyNotAuthorized: | |
| msg = _('The resource could not be found.') | |
| raise webob.exc.HTTPNotFound(msg) | |
| body = kwargs.pop('body', None) | |
| if (body is not None): | |
| arg_list.append(body) | |
| policy.enforce(request.context, name, resource, pluralized=self._collection) | |
| ret_value = getattr(self._plugin, name)(*arg_list, **kwargs) | |
| resource_registry.set_resources_dirty(request.context) | |
| return ret_value | |
| return _handle_action | |
| else: | |
| raise AttributeError() | |
| def _get_pagination_helper(self, request): | |
| if (self._allow_pagination and self._native_pagination): | |
| return api_common.PaginationNativeHelper(request, self._primary_key) | |
| elif self._allow_pagination: | |
| return api_common.PaginationEmulatedHelper(request, self._primary_key) | |
| return api_common.NoPaginationHelper(request, self._primary_key) | |
| def _get_sorting_helper(self, request): | |
| if (self._allow_sorting and self._native_sorting): | |
| return api_common.SortingNativeHelper(request, self._attr_info) | |
| elif self._allow_sorting: | |
| return api_common.SortingEmulatedHelper(request, self._attr_info) | |
| return api_common.NoSortingHelper(request, self._attr_info) | |
| def _items(self, request, do_authz=False, parent_id=None): | |
| 'Retrieves and formats a list of elements of the requested entity.' | |
| (original_fields, fields_to_add) = self._do_field_list(api_common.list_args(request, 'fields')) | |
| filters = api_common.get_filters(request, self._attr_info, ['fields', 'sort_key', 'sort_dir', 'limit', 'marker', 'page_reverse']) | |
| kwargs = {'filters': filters, 'fields': original_fields} | |
| sorting_helper = self._get_sorting_helper(request) | |
| pagination_helper = self._get_pagination_helper(request) | |
| sorting_helper.update_args(kwargs) | |
| sorting_helper.update_fields(original_fields, fields_to_add) | |
| pagination_helper.update_args(kwargs) | |
| pagination_helper.update_fields(original_fields, fields_to_add) | |
| if parent_id: | |
| kwargs[self._parent_id_name] = parent_id | |
| obj_getter = getattr(self._plugin, self._plugin_handlers[self.LIST]) | |
| obj_list = obj_getter(request.context, **kwargs) | |
| obj_list = sorting_helper.sort(obj_list) | |
| obj_list = pagination_helper.paginate(obj_list) | |
| if do_authz: | |
| obj_list = [obj for obj in obj_list if policy.check(request.context, self._plugin_handlers[self.SHOW], obj, plugin=self._plugin, pluralized=self._collection)] | |
| fields_to_strip = (fields_to_add or []) | |
| if obj_list: | |
| fields_to_strip += self._exclude_attributes_by_policy(request.context, obj_list[0]) | |
| collection = {self._collection: [self._filter_attributes(obj, fields_to_strip=fields_to_strip) for obj in obj_list]} | |
| pagination_links = pagination_helper.get_links(obj_list) | |
| if pagination_links: | |
| collection[(self._collection + '_links')] = pagination_links | |
| resource_registry.resync_resource(request.context, self._resource, request.context.tenant_id) | |
| return collection | |
| def _item(self, request, id, do_authz=False, field_list=None, parent_id=None): | |
| 'Retrieves and formats a single element of the requested entity.' | |
| kwargs = {'fields': field_list} | |
| action = self._plugin_handlers[self.SHOW] | |
| if parent_id: | |
| kwargs[self._parent_id_name] = parent_id | |
| obj_getter = getattr(self._plugin, action) | |
| obj = obj_getter(request.context, id, **kwargs) | |
| if do_authz: | |
| policy.enforce(request.context, action, obj, pluralized=self._collection) | |
| return obj | |
| def index(self, request, **kwargs): | |
| 'Returns a list of the requested entity.' | |
| parent_id = kwargs.get(self._parent_id_name) | |
| policy.init() | |
| return self._items(request, True, parent_id) | |
| def show(self, request, id, **kwargs): | |
| 'Returns detailed information about the requested entity.' | |
| try: | |
| (field_list, added_fields) = self._do_field_list(api_common.list_args(request, 'fields')) | |
| parent_id = kwargs.get(self._parent_id_name) | |
| policy.init() | |
| return {self._resource: self._view(request.context, self._item(request, id, do_authz=True, field_list=field_list, parent_id=parent_id), fields_to_strip=added_fields)} | |
| except oslo_policy.PolicyNotAuthorized: | |
| msg = _('The resource could not be found.') | |
| raise webob.exc.HTTPNotFound(msg) | |
| def _emulate_bulk_create(self, obj_creator, request, body, parent_id=None): | |
| objs = [] | |
| try: | |
| for item in body[self._collection]: | |
| kwargs = {self._resource: item} | |
| if parent_id: | |
| kwargs[self._parent_id_name] = parent_id | |
| fields_to_strip = self._exclude_attributes_by_policy(request.context, item) | |
| objs.append(self._filter_attributes(obj_creator(request.context, **kwargs), fields_to_strip=fields_to_strip)) | |
| return objs | |
| except Exception: | |
| with excutils.save_and_reraise_exception(): | |
| for obj in objs: | |
| obj_deleter = getattr(self._plugin, self._plugin_handlers[self.DELETE]) | |
| try: | |
| kwargs = ({self._parent_id_name: parent_id} if parent_id else {}) | |
| obj_deleter(request.context, obj['id'], **kwargs) | |
| except Exception: | |
| LOG.exception('Unable to undo add for %(resource)s %(id)s', {'resource': self._resource, 'id': obj['id']}) | |
| def create(self, request, body=None, **kwargs): | |
| self._notifier.info(request.context, (self._resource + '.create.start'), body) | |
| return self._create(request, body, **kwargs) | |
| def _create(self, request, body, **kwargs): | |
| 'Creates a new instance of the requested entity.' | |
| parent_id = kwargs.get(self._parent_id_name) | |
| body = Controller.prepare_request_body(request.context, body, True, self._resource, self._attr_info, allow_bulk=self._allow_bulk) | |
| action = self._plugin_handlers[self.CREATE] | |
| if (self._collection in body): | |
| items = body[self._collection] | |
| else: | |
| items = [body] | |
| policy.init() | |
| request_deltas = collections.defaultdict(int) | |
| for item in items: | |
| self._validate_network_tenant_ownership(request, item[self._resource]) | |
| policy.enforce(request.context, action, item[self._resource], pluralized=self._collection) | |
| if ('tenant_id' not in item[self._resource]): | |
| continue | |
| tenant_id = item[self._resource]['tenant_id'] | |
| request_deltas[tenant_id] += 1 | |
| reservations = [] | |
| try: | |
| for (tenant, delta) in request_deltas.items(): | |
| reservation = quota.QUOTAS.make_reservation(request.context, tenant, {self._resource: delta}, self._plugin) | |
| reservations.append(reservation) | |
| except n_exc.QuotaResourceUnknown as e: | |
| LOG.debug(e) | |
| def notify(create_result): | |
| with db_api.context_manager.writer.using(request.context): | |
| for reservation in reservations: | |
| quota.QUOTAS.commit_reservation(request.context, reservation.reservation_id) | |
| resource_registry.set_resources_dirty(request.context) | |
| notifier_method = (self._resource + '.create.end') | |
| self._notifier.info(request.context, notifier_method, create_result) | |
| registry.notify(self._resource, events.BEFORE_RESPONSE, self, context=request.context, data=create_result, method_name=notifier_method, collection=self._collection, action=action, original={}) | |
| return create_result | |
| def do_create(body, bulk=False, emulated=False): | |
| kwargs = ({self._parent_id_name: parent_id} if parent_id else {}) | |
| if (bulk and (not emulated)): | |
| obj_creator = getattr(self._plugin, ('%s_bulk' % action)) | |
| else: | |
| obj_creator = getattr(self._plugin, action) | |
| try: | |
| if emulated: | |
| return self._emulate_bulk_create(obj_creator, request, body, parent_id) | |
| else: | |
| if (self._collection in body): | |
| kwargs.update({self._collection: body}) | |
| else: | |
| kwargs.update({self._resource: body}) | |
| return obj_creator(request.context, **kwargs) | |
| except Exception: | |
| with excutils.save_and_reraise_exception(): | |
| for reservation in reservations: | |
| quota.QUOTAS.cancel_reservation(request.context, reservation.reservation_id) | |
| if ((self._collection in body) and self._native_bulk): | |
| objs = do_create(body, bulk=True) | |
| fields_to_strip = self._exclude_attributes_by_policy(request.context, objs[0]) | |
| return notify({self._collection: [self._filter_attributes(obj, fields_to_strip=fields_to_strip) for obj in objs]}) | |
| elif (self._collection in body): | |
| objs = do_create(body, bulk=True, emulated=True) | |
| return notify({self._collection: objs}) | |
| else: | |
| obj = do_create(body) | |
| return notify({self._resource: self._view(request.context, obj)}) | |
| def delete(self, request, id, **kwargs): | |
| 'Deletes the specified entity.' | |
| if request.body: | |
| msg = _('Request body is not supported in DELETE.') | |
| raise webob.exc.HTTPBadRequest(msg) | |
| self._notifier.info(request.context, (self._resource + '.delete.start'), {(self._resource + '_id'): id}) | |
| return self._delete(request, id, **kwargs) | |
| def _delete(self, request, id, **kwargs): | |
| action = self._plugin_handlers[self.DELETE] | |
| policy.init() | |
| parent_id = kwargs.get(self._parent_id_name) | |
| obj = self._item(request, id, parent_id=parent_id) | |
| try: | |
| policy.enforce(request.context, action, obj, pluralized=self._collection) | |
| except oslo_policy.PolicyNotAuthorized: | |
| with excutils.save_and_reraise_exception() as ctxt: | |
| if (not policy.check(request.context, self._plugin_handlers[self.SHOW], obj, pluralized=self._collection)): | |
| ctxt.reraise = False | |
| msg = _('The resource could not be found.') | |
| raise webob.exc.HTTPNotFound(msg) | |
| obj_deleter = getattr(self._plugin, action) | |
| obj_deleter(request.context, id, **kwargs) | |
| resource_registry.set_resources_dirty(request.context) | |
| notifier_method = (self._resource + '.delete.end') | |
| result = {self._resource: self._view(request.context, obj)} | |
| notifier_payload = {(self._resource + '_id'): id} | |
| notifier_payload.update(result) | |
| self._notifier.info(request.context, notifier_method, notifier_payload) | |
| registry.notify(self._resource, events.BEFORE_RESPONSE, self, context=request.context, data=result, method_name=notifier_method, action=action, original={}) | |
| def update(self, request, id, body=None, **kwargs): | |
| "Updates the specified entity's attributes." | |
| try: | |
| payload = body.copy() | |
| except AttributeError: | |
| msg = (_('Invalid format: %s') % request.body) | |
| raise exceptions.BadRequest(resource='body', msg=msg) | |
| payload['id'] = id | |
| self._notifier.info(request.context, (self._resource + '.update.start'), payload) | |
| return self._update(request, id, body, **kwargs) | |
| def _update(self, request, id, body, **kwargs): | |
| body = Controller.prepare_request_body(request.context, body, False, self._resource, self._attr_info, allow_bulk=self._allow_bulk) | |
| action = self._plugin_handlers[self.UPDATE] | |
| field_list = [name for (name, value) in self._attr_info.items() if (value.get('required_by_policy') or value.get('primary_key') or ('default' not in value))] | |
| policy.init() | |
| parent_id = kwargs.get(self._parent_id_name) | |
| orig_obj = self._item(request, id, field_list=field_list, parent_id=parent_id) | |
| orig_object_copy = copy.copy(orig_obj) | |
| orig_obj.update(body[self._resource]) | |
| orig_obj[n_const.ATTRIBUTES_TO_UPDATE] = body[self._resource].keys() | |
| try: | |
| policy.enforce(request.context, action, orig_obj, pluralized=self._collection) | |
| except oslo_policy.PolicyNotAuthorized: | |
| with excutils.save_and_reraise_exception() as ctxt: | |
| if (not policy.check(request.context, self._plugin_handlers[self.SHOW], orig_obj, pluralized=self._collection)): | |
| ctxt.reraise = False | |
| msg = _('The resource could not be found.') | |
| raise webob.exc.HTTPNotFound(msg) | |
| obj_updater = getattr(self._plugin, action) | |
| kwargs = {self._resource: body} | |
| if parent_id: | |
| kwargs[self._parent_id_name] = parent_id | |
| obj = obj_updater(request.context, id, **kwargs) | |
| resource_registry.set_resources_dirty(request.context) | |
| result = {self._resource: self._view(request.context, obj)} | |
| notifier_method = (self._resource + '.update.end') | |
| self._notifier.info(request.context, notifier_method, result) | |
| registry.notify(self._resource, events.BEFORE_RESPONSE, self, context=request.context, data=result, method_name=notifier_method, action=action, original=orig_object_copy) | |
| return result | |
| def prepare_request_body(context, body, is_create, resource, attr_info, allow_bulk=False): | |
| 'Verifies required attributes are in request body.\n\n Also checking that an attribute is only specified if it is allowed\n for the given operation (create/update).\n\n Attribute with default values are considered to be optional.\n\n body argument must be the deserialized body.\n ' | |
| collection = (resource + 's') | |
| if (not body): | |
| raise webob.exc.HTTPBadRequest(_('Resource body required')) | |
| LOG.debug('Request body: %(body)s', {'body': body}) | |
| try: | |
| if (collection in body): | |
| if (not allow_bulk): | |
| raise webob.exc.HTTPBadRequest(_('Bulk operation not supported')) | |
| if (not body[collection]): | |
| raise webob.exc.HTTPBadRequest(_('Resources required')) | |
| bulk_body = [Controller.prepare_request_body(context, (item if (resource in item) else {resource: item}), is_create, resource, attr_info, allow_bulk) for item in body[collection]] | |
| return {collection: bulk_body} | |
| res_dict = body.get(resource) | |
| except (AttributeError, TypeError): | |
| msg = _('Body contains invalid data') | |
| raise webob.exc.HTTPBadRequest(msg) | |
| if (res_dict is None): | |
| msg = (_("Unable to find '%s' in request body") % resource) | |
| raise webob.exc.HTTPBadRequest(msg) | |
| attr_ops = attributes.AttributeInfo(attr_info) | |
| attr_ops.populate_project_id(context, res_dict, is_create) | |
| attributes.populate_project_info(attr_info) | |
| attr_ops.verify_attributes(res_dict) | |
| if is_create: | |
| attr_ops.fill_post_defaults(res_dict, exc_cls=webob.exc.HTTPBadRequest) | |
| else: | |
| for (attr, attr_vals) in attr_info.items(): | |
| if ((attr in res_dict) and (not attr_vals['allow_put'])): | |
| msg = (_('Cannot update read-only attribute %s') % attr) | |
| raise webob.exc.HTTPBadRequest(msg) | |
| attr_ops.convert_values(res_dict, exc_cls=webob.exc.HTTPBadRequest) | |
| return body | |
| def _validate_network_tenant_ownership(self, request, resource_item): | |
| if (request.context.is_admin or request.context.is_advsvc or (self._resource not in ('port', 'subnet'))): | |
| return | |
| network = self._plugin.get_network(request.context, resource_item['network_id']) | |
| if network.get('shared'): | |
| return | |
| network_owner = network['tenant_id'] | |
| if (network_owner != resource_item['tenant_id']): | |
| msg = _('The resource could not be found.') | |
| raise webob.exc.HTTPNotFound(msg) | |
| def create_resource(collection, resource, plugin, params, allow_bulk=False, member_actions=None, parent=None, allow_pagination=False, allow_sorting=False): | |
| controller = Controller(plugin, collection, resource, params, allow_bulk, member_actions=member_actions, parent=parent, allow_pagination=allow_pagination, allow_sorting=allow_sorting) | |
| return wsgi_resource.Resource(controller, FAULT_MAP) | |
Xet Storage Details
- Size:
- 24.4 kB
- Xet hash:
- 26eff8809f228f39b98bd8e2a57547d6bc80085a524d7856fa8b7af4360bfdc1
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.