Buckets:
| import functools | |
| import microversion_parse | |
| from oslo_log import log as logging | |
| from oslo_serialization import jsonutils | |
| from oslo_utils import encodeutils | |
| from oslo_utils import strutils | |
| import six | |
| import webob | |
| from nova.api.openstack import api_version_request as api_version | |
| from nova.api.openstack import versioned_method | |
| from nova import exception | |
| from nova import i18n | |
| from nova.i18n import _ | |
| from nova import utils | |
| from nova import wsgi | |
| LOG = logging.getLogger(__name__) | |
| _SUPPORTED_CONTENT_TYPES = ('application/json', 'application/vnd.openstack.compute+json') | |
| _ROUTES_METHODS = ['create', 'delete', 'show', 'update'] | |
| _METHODS_WITH_BODY = ['POST', 'PUT'] | |
| DEFAULT_API_VERSION = '2.1' | |
| VER_METHOD_ATTR = 'versioned_methods' | |
| API_VERSION_REQUEST_HEADER = 'OpenStack-API-Version' | |
| LEGACY_API_VERSION_REQUEST_HEADER = 'X-OpenStack-Nova-API-Version' | |
| ENV_LEGACY_V2 = 'openstack.legacy_v2' | |
| def get_supported_content_types(): | |
| return _SUPPORTED_CONTENT_TYPES | |
| def item_get(item, item_key): | |
| if hasattr(item, '__getitem__'): | |
| return item[item_key] | |
| else: | |
| return getattr(item, item_key) | |
| class Request(wsgi.Request, ): | |
| 'Add some OpenStack API-specific logic to the base webob.Request.' | |
| def __init__(self, *args, **kwargs): | |
| super(Request, self).__init__(*args, **kwargs) | |
| self._extension_data = {'db_items': {}} | |
| if (not hasattr(self, 'api_version_request')): | |
| self.api_version_request = api_version.APIVersionRequest() | |
| def cache_db_items(self, key, items, item_key='id'): | |
| "Allow API methods to store objects from a DB query to be\n used by API extensions within the same API request.\n\n An instance of this class only lives for the lifetime of a\n single API request, so there's no need to implement full\n cache management.\n " | |
| db_items = self._extension_data['db_items'].setdefault(key, {}) | |
| for item in items: | |
| db_items[item_get(item, item_key)] = item | |
| def get_db_items(self, key): | |
| 'Allow an API extension to get previously stored objects within\n the same API request.\n\n Note that the object data will be slightly stale.\n ' | |
| return self._extension_data['db_items'][key] | |
| def get_db_item(self, key, item_key): | |
| 'Allow an API extension to get a previously stored object\n within the same API request.\n\n Note that the object data will be slightly stale.\n ' | |
| return self.get_db_items(key).get(item_key) | |
| def cache_db_instances(self, instances): | |
| self.cache_db_items('instances', instances, 'uuid') | |
| def cache_db_instance(self, instance): | |
| self.cache_db_items('instances', [instance], 'uuid') | |
| def get_db_instances(self): | |
| return self.get_db_items('instances') | |
| def get_db_instance(self, instance_uuid): | |
| return self.get_db_item('instances', instance_uuid) | |
| def cache_db_flavors(self, flavors): | |
| self.cache_db_items('flavors', flavors, 'flavorid') | |
| def cache_db_flavor(self, flavor): | |
| self.cache_db_items('flavors', [flavor], 'flavorid') | |
| def get_db_flavors(self): | |
| return self.get_db_items('flavors') | |
| def get_db_flavor(self, flavorid): | |
| return self.get_db_item('flavors', flavorid) | |
| def best_match_content_type(self): | |
| 'Determine the requested response content-type.' | |
| if ('nova.best_content_type' not in self.environ): | |
| content_type = None | |
| parts = self.path.rsplit('.', 1) | |
| if (len(parts) > 1): | |
| possible_type = ('application/' + parts[1]) | |
| if (possible_type in get_supported_content_types()): | |
| content_type = possible_type | |
| if (not content_type): | |
| content_type = self.accept.best_match(get_supported_content_types()) | |
| self.environ['nova.best_content_type'] = (content_type or 'application/json') | |
| return self.environ['nova.best_content_type'] | |
| def get_content_type(self): | |
| 'Determine content type of the request body.\n\n Does not do any body introspection, only checks header\n\n ' | |
| if ('Content-Type' not in self.headers): | |
| return None | |
| content_type = self.content_type | |
| if ((not content_type) or (content_type == 'text/plain')): | |
| return None | |
| if (content_type not in get_supported_content_types()): | |
| raise exception.InvalidContentType(content_type=content_type) | |
| return content_type | |
| def best_match_language(self): | |
| "Determine the best available language for the request.\n\n :returns: the best language match or None if the 'Accept-Language'\n header was not available in the request.\n " | |
| if (not self.accept_language): | |
| return None | |
| return self.accept_language.best_match(i18n.get_available_languages()) | |
| def set_api_version_request(self): | |
| 'Set API version request based on the request header information.' | |
| hdr_string = microversion_parse.get_version(self.headers, service_type='compute', legacy_headers=[LEGACY_API_VERSION_REQUEST_HEADER]) | |
| if (hdr_string is None): | |
| self.api_version_request = api_version.APIVersionRequest(api_version.DEFAULT_API_VERSION) | |
| elif (hdr_string == 'latest'): | |
| self.api_version_request = api_version.max_api_version() | |
| else: | |
| self.api_version_request = api_version.APIVersionRequest(hdr_string) | |
| if (not self.api_version_request.matches(api_version.min_api_version(), api_version.max_api_version())): | |
| raise exception.InvalidGlobalAPIVersion(req_ver=self.api_version_request.get_string(), min_ver=api_version.min_api_version().get_string(), max_ver=api_version.max_api_version().get_string()) | |
| def set_legacy_v2(self): | |
| self.environ[ENV_LEGACY_V2] = True | |
| def is_legacy_v2(self): | |
| return self.environ.get(ENV_LEGACY_V2, False) | |
| class ActionDispatcher(object, ): | |
| 'Maps method name to local methods through action name.' | |
| def dispatch(self, *args, **kwargs): | |
| 'Find and call local method.' | |
| action = kwargs.pop('action', 'default') | |
| action_method = getattr(self, str(action), self.default) | |
| return action_method(*args, **kwargs) | |
| def default(self, data): | |
| raise NotImplementedError() | |
| class JSONDeserializer(ActionDispatcher, ): | |
| def _from_json(self, datastring): | |
| try: | |
| return jsonutils.loads(datastring) | |
| except ValueError: | |
| msg = _('cannot understand JSON') | |
| raise exception.MalformedRequestBody(reason=msg) | |
| def deserialize(self, datastring, action='default'): | |
| return self.dispatch(datastring, action=action) | |
| def default(self, datastring): | |
| return {'body': self._from_json(datastring)} | |
| class JSONDictSerializer(ActionDispatcher, ): | |
| 'Default JSON request body serialization.' | |
| def serialize(self, data, action='default'): | |
| return self.dispatch(data, action=action) | |
| def default(self, data): | |
| return six.text_type(jsonutils.dumps(data)) | |
| def response(code): | |
| 'Attaches response code to a method.\n\n This decorator associates a response code with a method. Note\n that the function attributes are directly manipulated; the method\n is not wrapped.\n ' | |
| def decorator(func): | |
| func.wsgi_code = code | |
| return func | |
| return decorator | |
| class ResponseObject(object, ): | |
| 'Bundles a response object\n\n Object that app methods may return in order to allow its response\n to be modified by extensions in the code. Its use is optional (and\n should only be used if you really know what you are doing).\n ' | |
| def __init__(self, obj, code=None, headers=None): | |
| 'Builds a response object.' | |
| self.obj = obj | |
| self._default_code = 200 | |
| self._code = code | |
| self._headers = (headers or {}) | |
| self.serializer = JSONDictSerializer() | |
| def __getitem__(self, key): | |
| 'Retrieves a header with the given name.' | |
| return self._headers[key.lower()] | |
| def __setitem__(self, key, value): | |
| 'Sets a header with the given name to the given value.' | |
| self._headers[key.lower()] = value | |
| def __delitem__(self, key): | |
| 'Deletes the header with the given name.' | |
| del self._headers[key.lower()] | |
| def serialize(self, request, content_type): | |
| 'Serializes the wrapped object.\n\n Utility method for serializing the wrapped object. Returns a\n webob.Response object.\n ' | |
| serializer = self.serializer | |
| body = None | |
| if (self.obj is not None): | |
| body = serializer.serialize(self.obj) | |
| response = webob.Response(body=body) | |
| if response.headers.get('Content-Length'): | |
| response.headers['Content-Length'] = utils.utf8(response.headers['Content-Length']) | |
| response.status_int = self.code | |
| for (hdr, value) in self._headers.items(): | |
| response.headers[hdr] = utils.utf8(value) | |
| response.headers['Content-Type'] = utils.utf8(content_type) | |
| return response | |
| def code(self): | |
| 'Retrieve the response status.' | |
| return (self._code or self._default_code) | |
| def headers(self): | |
| 'Retrieve the headers.' | |
| return self._headers.copy() | |
| def action_peek(body): | |
| 'Determine action to invoke.\n\n This looks inside the json body and fetches out the action method\n name.\n ' | |
| try: | |
| decoded = jsonutils.loads(body) | |
| except ValueError: | |
| msg = _('cannot understand JSON') | |
| raise exception.MalformedRequestBody(reason=msg) | |
| if (len(decoded) != 1): | |
| msg = _('too many body keys') | |
| raise exception.MalformedRequestBody(reason=msg) | |
| return list(decoded.keys())[0] | |
| class ResourceExceptionHandler(object, ): | |
| 'Context manager to handle Resource exceptions.\n\n Used when processing exceptions generated by API implementation\n methods (or their extensions). Converts most exceptions to Fault\n exceptions, with the appropriate logging.\n ' | |
| def __enter__(self): | |
| return None | |
| def __exit__(self, ex_type, ex_value, ex_traceback): | |
| if (not ex_value): | |
| return True | |
| if isinstance(ex_value, exception.Forbidden): | |
| raise Fault(webob.exc.HTTPForbidden(explanation=ex_value.format_message())) | |
| elif isinstance(ex_value, exception.VersionNotFoundForAPIMethod): | |
| raise | |
| elif isinstance(ex_value, exception.Invalid): | |
| raise Fault(exception.ConvertedException(code=ex_value.code, explanation=ex_value.format_message())) | |
| elif isinstance(ex_value, TypeError): | |
| exc_info = (ex_type, ex_value, ex_traceback) | |
| LOG.error('Exception handling resource: %s', ex_value, exc_info=exc_info) | |
| raise Fault(webob.exc.HTTPBadRequest()) | |
| elif isinstance(ex_value, Fault): | |
| LOG.info('Fault thrown: %s', ex_value) | |
| raise ex_value | |
| elif isinstance(ex_value, webob.exc.HTTPException): | |
| LOG.info('HTTP exception thrown: %s', ex_value) | |
| raise Fault(ex_value) | |
| return False | |
| class Resource(wsgi.Application, ): | |
| "WSGI app that handles (de)serialization and controller dispatch.\n\n WSGI app that reads routing information supplied by RoutesMiddleware\n and calls the requested action method upon its controller. All\n controller action methods must accept a 'req' argument, which is the\n incoming wsgi.Request. If the operation is a PUT or POST, the controller\n method must also accept a 'body' argument (the deserialized request body).\n They may raise a webob.exc exception or return a dict, which will be\n serialized by requested content type.\n\n Exceptions derived from webob.exc.HTTPException will be automatically\n wrapped in Fault() to provide API friendly error responses.\n\n " | |
| support_api_request_version = False | |
| def __init__(self, controller, inherits=None): | |
| ':param controller: object that implement methods created by routes\n lib\n :param inherits: another resource object that this resource should\n inherit extensions from. Any action extensions that\n are applied to the parent resource will also apply\n to this resource.\n ' | |
| self.controller = controller | |
| self.default_serializers = dict(json=JSONDictSerializer) | |
| self.wsgi_actions = {} | |
| if controller: | |
| self.register_actions(controller) | |
| self.wsgi_extensions = {} | |
| self.wsgi_action_extensions = {} | |
| self.inherits = inherits | |
| def register_actions(self, controller): | |
| 'Registers controller actions with this resource.' | |
| actions = getattr(controller, 'wsgi_actions', {}) | |
| for (key, method_name) in actions.items(): | |
| self.wsgi_actions[key] = getattr(controller, method_name) | |
| def register_extensions(self, controller): | |
| 'Registers controller extensions with this resource.' | |
| extensions = getattr(controller, 'wsgi_extensions', []) | |
| for (method_name, action_name) in extensions: | |
| extension = getattr(controller, method_name) | |
| if action_name: | |
| if (action_name not in self.wsgi_action_extensions): | |
| self.wsgi_action_extensions[action_name] = [] | |
| self.wsgi_action_extensions[action_name].append(extension) | |
| else: | |
| if (method_name not in self.wsgi_extensions): | |
| self.wsgi_extensions[method_name] = [] | |
| self.wsgi_extensions[method_name].append(extension) | |
| def get_action_args(self, request_environment): | |
| 'Parse dictionary created by routes library.' | |
| if hasattr(self.controller, 'get_action_args'): | |
| return self.controller.get_action_args(request_environment) | |
| try: | |
| args = request_environment['wsgiorg.routing_args'][1].copy() | |
| except (KeyError, IndexError, AttributeError): | |
| return {} | |
| try: | |
| del args['controller'] | |
| except KeyError: | |
| pass | |
| try: | |
| del args['format'] | |
| except KeyError: | |
| pass | |
| return args | |
| def get_body(self, request): | |
| content_type = request.get_content_type() | |
| return (content_type, request.body) | |
| def deserialize(self, body): | |
| return JSONDeserializer().deserialize(body) | |
| def process_extensions(self, extensions, resp_obj, request, action_args): | |
| for ext in extensions: | |
| response = None | |
| try: | |
| with ResourceExceptionHandler(): | |
| response = ext(req=request, resp_obj=resp_obj, **action_args) | |
| except exception.VersionNotFoundForAPIMethod: | |
| continue | |
| except Fault as ex: | |
| response = ex | |
| if response: | |
| return response | |
| return None | |
| def _should_have_body(self, request): | |
| return (request.method in _METHODS_WITH_BODY) | |
| def __call__(self, request): | |
| 'WSGI method that controls (de)serialization and method dispatch.' | |
| if self.support_api_request_version: | |
| try: | |
| request.set_api_version_request() | |
| except exception.InvalidAPIVersionString as e: | |
| return Fault(webob.exc.HTTPBadRequest(explanation=e.format_message())) | |
| except exception.InvalidGlobalAPIVersion as e: | |
| return Fault(webob.exc.HTTPNotAcceptable(explanation=e.format_message())) | |
| action_args = self.get_action_args(request.environ) | |
| action = action_args.pop('action', None) | |
| try: | |
| (content_type, body) = self.get_body(request) | |
| accept = request.best_match_content_type() | |
| except exception.InvalidContentType: | |
| msg = _('Unsupported Content-Type') | |
| return Fault(webob.exc.HTTPUnsupportedMediaType(explanation=msg)) | |
| return self._process_stack(request, action, action_args, content_type, body, accept) | |
| def _process_stack(self, request, action, action_args, content_type, body, accept): | |
| 'Implement the processing stack.' | |
| try: | |
| (meth, extensions) = self.get_method(request, action, content_type, body) | |
| except (AttributeError, TypeError): | |
| return Fault(webob.exc.HTTPNotFound()) | |
| except KeyError as ex: | |
| msg = (_('There is no such action: %s') % ex.args[0]) | |
| return Fault(webob.exc.HTTPBadRequest(explanation=msg)) | |
| except exception.MalformedRequestBody: | |
| msg = _('Malformed request body') | |
| return Fault(webob.exc.HTTPBadRequest(explanation=msg)) | |
| if body: | |
| msg = (_("Action: '%(action)s', calling method: %(meth)s, body: %(body)s") % {'action': action, 'body': six.text_type(body, 'utf-8'), 'meth': str(meth)}) | |
| LOG.debug(strutils.mask_password(msg)) | |
| else: | |
| LOG.debug("Calling method '%(meth)s'", {'meth': str(meth)}) | |
| try: | |
| contents = self._get_request_content(body, request) | |
| except exception.MalformedRequestBody: | |
| msg = _('Malformed request body') | |
| return Fault(webob.exc.HTTPBadRequest(explanation=msg)) | |
| action_args.update(contents) | |
| project_id = action_args.pop('project_id', None) | |
| context = request.environ.get('nova.context') | |
| if (context and project_id and (project_id != context.project_id)): | |
| msg = (_("Malformed request URL: URL's project_id '%(project_id)s' doesn't match Context's project_id '%(context_project_id)s'") % {'project_id': project_id, 'context_project_id': context.project_id}) | |
| return Fault(webob.exc.HTTPBadRequest(explanation=msg)) | |
| response = None | |
| try: | |
| with ResourceExceptionHandler(): | |
| action_result = self.dispatch(meth, request, action_args) | |
| except Fault as ex: | |
| response = ex | |
| if (not response): | |
| resp_obj = None | |
| if ((type(action_result) is dict) or (action_result is None)): | |
| resp_obj = ResponseObject(action_result) | |
| elif isinstance(action_result, ResponseObject): | |
| resp_obj = action_result | |
| else: | |
| response = action_result | |
| if resp_obj: | |
| if hasattr(meth, 'wsgi_code'): | |
| resp_obj._default_code = meth.wsgi_code | |
| response = self.process_extensions(extensions, resp_obj, request, action_args) | |
| if (resp_obj and (not response)): | |
| response = resp_obj.serialize(request, accept) | |
| if hasattr(response, 'headers'): | |
| for (hdr, val) in list(response.headers.items()): | |
| if six.PY2: | |
| response.headers[hdr] = utils.utf8(val) | |
| else: | |
| response.headers[hdr] = encodeutils.safe_decode(utils.utf8(val)) | |
| if (not request.api_version_request.is_null()): | |
| response.headers[API_VERSION_REQUEST_HEADER] = ('compute ' + request.api_version_request.get_string()) | |
| response.headers[LEGACY_API_VERSION_REQUEST_HEADER] = request.api_version_request.get_string() | |
| response.headers.add('Vary', API_VERSION_REQUEST_HEADER) | |
| response.headers.add('Vary', LEGACY_API_VERSION_REQUEST_HEADER) | |
| return response | |
| def _get_request_content(self, body, request): | |
| contents = {} | |
| if self._should_have_body(request): | |
| if ((request.content_length == 0) or (request.content_length is None)): | |
| contents = {'body': None} | |
| else: | |
| contents = self.deserialize(body) | |
| return contents | |
| def get_method(self, request, action, content_type, body): | |
| (meth, extensions) = self._get_method(request, action, content_type, body) | |
| if self.inherits: | |
| (_meth, parent_ext) = self.inherits.get_method(request, action, content_type, body) | |
| extensions.extend(parent_ext) | |
| return (meth, extensions) | |
| def _get_method(self, request, action, content_type, body): | |
| 'Look up the action-specific method and its extensions.' | |
| try: | |
| if (not self.controller): | |
| meth = getattr(self, action) | |
| else: | |
| meth = getattr(self.controller, action) | |
| except AttributeError: | |
| if ((not self.wsgi_actions) or (action not in (_ROUTES_METHODS + ['action']))): | |
| raise | |
| else: | |
| return (meth, self.wsgi_extensions.get(action, [])) | |
| if (action == 'action'): | |
| action_name = action_peek(body) | |
| else: | |
| action_name = action | |
| return (self.wsgi_actions[action_name], self.wsgi_action_extensions.get(action_name, [])) | |
| def dispatch(self, method, request, action_args): | |
| 'Dispatch a call to the action-specific method.' | |
| try: | |
| return method(req=request, **action_args) | |
| except exception.VersionNotFoundForAPIMethod: | |
| return Fault(webob.exc.HTTPNotFound()) | |
| class ResourceV21(Resource, ): | |
| support_api_request_version = True | |
| def action(name): | |
| 'Mark a function as an action.\n\n The given name will be taken as the action key in the body.\n\n This is also overloaded to allow extensions to provide\n non-extending definitions of create and delete operations.\n ' | |
| def decorator(func): | |
| func.wsgi_action = name | |
| return func | |
| return decorator | |
| def extends(*args, **kwargs): | |
| "Indicate a function extends an operation.\n\n Can be used as either::\n\n @extends\n def index(...):\n pass\n\n or as::\n\n @extends(action='resize')\n def _action_resize(...):\n pass\n " | |
| def decorator(func): | |
| func.wsgi_extends = (func.__name__, kwargs.get('action')) | |
| return func | |
| if args: | |
| return decorator(*args) | |
| return decorator | |
| class ControllerMetaclass(type, ): | |
| 'Controller metaclass.\n\n This metaclass automates the task of assembling a dictionary\n mapping action keys to method names.\n ' | |
| def __new__(mcs, name, bases, cls_dict): | |
| 'Adds the wsgi_actions dictionary to the class.' | |
| actions = {} | |
| extensions = [] | |
| versioned_methods = None | |
| for base in bases: | |
| actions.update(getattr(base, 'wsgi_actions', {})) | |
| if (base.__name__ == 'Controller'): | |
| if (VER_METHOD_ATTR in base.__dict__): | |
| versioned_methods = getattr(base, VER_METHOD_ATTR) | |
| delattr(base, VER_METHOD_ATTR) | |
| for (key, value) in cls_dict.items(): | |
| if (not callable(value)): | |
| continue | |
| if getattr(value, 'wsgi_action', None): | |
| actions[value.wsgi_action] = key | |
| elif getattr(value, 'wsgi_extends', None): | |
| extensions.append(value.wsgi_extends) | |
| cls_dict['wsgi_actions'] = actions | |
| cls_dict['wsgi_extensions'] = extensions | |
| if versioned_methods: | |
| cls_dict[VER_METHOD_ATTR] = versioned_methods | |
| return super(ControllerMetaclass, mcs).__new__(mcs, name, bases, cls_dict) | |
| class Controller(object, ): | |
| 'Default controller.' | |
| _view_builder_class = None | |
| def __init__(self, view_builder=None): | |
| 'Initialize controller with a view builder instance.' | |
| if view_builder: | |
| self._view_builder = view_builder | |
| elif self._view_builder_class: | |
| self._view_builder = self._view_builder_class() | |
| else: | |
| self._view_builder = None | |
| def __getattribute__(self, key): | |
| def version_select(*args, **kwargs): | |
| 'Look for the method which matches the name supplied and version\n constraints and calls it with the supplied arguments.\n\n @return: Returns the result of the method called\n @raises: VersionNotFoundForAPIMethod if there is no method which\n matches the name and version constraints\n ' | |
| if (len(args) == 0): | |
| ver = kwargs['req'].api_version_request | |
| else: | |
| ver = args[0].api_version_request | |
| func_list = self.versioned_methods[key] | |
| for func in func_list: | |
| if ver.matches(func.start_version, func.end_version): | |
| functools.update_wrapper(version_select, func.func) | |
| return func.func(self, *args, **kwargs) | |
| raise exception.VersionNotFoundForAPIMethod(version=ver) | |
| try: | |
| version_meth_dict = object.__getattribute__(self, VER_METHOD_ATTR) | |
| except AttributeError: | |
| return object.__getattribute__(self, key) | |
| if (version_meth_dict and (key in object.__getattribute__(self, VER_METHOD_ATTR))): | |
| return version_select | |
| return object.__getattribute__(self, key) | |
| def api_version(cls, min_ver, max_ver=None): | |
| 'Decorator for versioning api methods.\n\n Add the decorator to any method which takes a request object\n as the first parameter and belongs to a class which inherits from\n wsgi.Controller.\n\n @min_ver: string representing minimum version\n @max_ver: optional string representing maximum version\n ' | |
| def decorator(f): | |
| obj_min_ver = api_version.APIVersionRequest(min_ver) | |
| if max_ver: | |
| obj_max_ver = api_version.APIVersionRequest(max_ver) | |
| else: | |
| obj_max_ver = api_version.APIVersionRequest() | |
| func_name = f.__name__ | |
| new_func = versioned_method.VersionedMethod(func_name, obj_min_ver, obj_max_ver, f) | |
| func_dict = getattr(cls, VER_METHOD_ATTR, {}) | |
| if (not func_dict): | |
| setattr(cls, VER_METHOD_ATTR, func_dict) | |
| func_list = func_dict.get(func_name, []) | |
| if (not func_list): | |
| func_dict[func_name] = func_list | |
| func_list.append(new_func) | |
| is_intersect = Controller.check_for_versions_intersection(func_list) | |
| if is_intersect: | |
| raise exception.ApiVersionsIntersect(name=new_func.name, min_ver=new_func.start_version, max_ver=new_func.end_version) | |
| func_list.sort(key=(lambda f: f.start_version), reverse=True) | |
| return f | |
| return decorator | |
| def is_valid_body(body, entity_name): | |
| if (not (body and (entity_name in body))): | |
| return False | |
| def is_dict(d): | |
| try: | |
| d.get(None) | |
| return True | |
| except AttributeError: | |
| return False | |
| return is_dict(body[entity_name]) | |
| def check_for_versions_intersection(func_list): | |
| 'Determines whether function list contains version intervals\n intersections or not. General algorithm:\n\n https://en.wikipedia.org/wiki/Intersection_algorithm\n\n :param func_list: list of VersionedMethod objects\n :return: boolean\n ' | |
| pairs = [] | |
| counter = 0 | |
| for f in func_list: | |
| pairs.append((f.start_version, 1, f)) | |
| pairs.append((f.end_version, (-1), f)) | |
| def compare(x): | |
| return x[0] | |
| pairs.sort(key=compare) | |
| for p in pairs: | |
| counter += p[1] | |
| if (counter > 1): | |
| return True | |
| return False | |
| class Fault(webob.exc.HTTPException, ): | |
| 'Wrap webob.exc.HTTPException to provide API friendly response.' | |
| _fault_names = {400: 'badRequest', 401: 'unauthorized', 403: 'forbidden', 404: 'itemNotFound', 405: 'badMethod', 409: 'conflictingRequest', 413: 'overLimit', 415: 'badMediaType', 429: 'overLimit', 501: 'notImplemented', 503: 'serviceUnavailable'} | |
| def __init__(self, exception): | |
| 'Create a Fault for the given webob.exc.exception.' | |
| self.wrapped_exc = exception | |
| for (key, value) in list(self.wrapped_exc.headers.items()): | |
| self.wrapped_exc.headers[key] = str(value) | |
| self.status_int = exception.status_int | |
| def __call__(self, req): | |
| 'Generate a WSGI response based on the exception passed to ctor.' | |
| user_locale = req.best_match_language() | |
| code = self.wrapped_exc.status_int | |
| fault_name = self._fault_names.get(code, 'computeFault') | |
| explanation = self.wrapped_exc.explanation | |
| LOG.debug('Returning %(code)s to user: %(explanation)s', {'code': code, 'explanation': explanation}) | |
| explanation = i18n.translate(explanation, user_locale) | |
| fault_data = {fault_name: {'code': code, 'message': explanation}} | |
| if ((code == 413) or (code == 429)): | |
| retry = self.wrapped_exc.headers.get('Retry-After', None) | |
| if retry: | |
| fault_data[fault_name]['retryAfter'] = retry | |
| if (not req.api_version_request.is_null()): | |
| self.wrapped_exc.headers[API_VERSION_REQUEST_HEADER] = ('compute ' + req.api_version_request.get_string()) | |
| self.wrapped_exc.headers[LEGACY_API_VERSION_REQUEST_HEADER] = req.api_version_request.get_string() | |
| self.wrapped_exc.headers.add('Vary', API_VERSION_REQUEST_HEADER) | |
| self.wrapped_exc.headers.add('Vary', LEGACY_API_VERSION_REQUEST_HEADER) | |
| self.wrapped_exc.content_type = 'application/json' | |
| self.wrapped_exc.charset = 'UTF-8' | |
| self.wrapped_exc.text = JSONDictSerializer().serialize(fault_data) | |
| return self.wrapped_exc | |
| def __str__(self): | |
| return self.wrapped_exc.__str__() | |
Xet Storage Details
- Size:
- 30.1 kB
- Xet hash:
- 832063fe81197ecfe4b1e8c554440d13ec1ed41ae042ca11832fa3377f812b48
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.