"Fossies" - the Fresh Open Source Software Archive

Member "zaqar-10.0.0/zaqar/transport/wsgi/v1_0/metadata.py" (13 May 2020, 3654 Bytes) of package /linux/misc/openstack/zaqar-10.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "metadata.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 9.0.0_vs_10.0.0.

    1 # Copyright (c) 2013 Rackspace, Inc.
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License");
    4 # you may not use this file except in compliance with the License.
    5 # You may obtain a copy of the License at
    6 #
    7 #    http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS,
   11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
   12 # implied.
   13 # See the License for the specific language governing permissions and
   14 # limitations under the License.
   15 
   16 import falcon
   17 from oslo_log import log as logging
   18 import six
   19 
   20 from zaqar.common import decorators
   21 from zaqar.i18n import _
   22 from zaqar.storage import errors as storage_errors
   23 from zaqar.transport import utils
   24 from zaqar.transport import validation
   25 from zaqar.transport.wsgi import errors as wsgi_errors
   26 from zaqar.transport.wsgi import utils as wsgi_utils
   27 
   28 
   29 LOG = logging.getLogger(__name__)
   30 
   31 
   32 class Resource(object):
   33     __slots__ = ('_wsgi_conf', '_validate', '_queue_ctrl')
   34 
   35     def __init__(self, _wsgi_conf, validate, queue_controller):
   36         self._wsgi_conf = _wsgi_conf
   37         self._validate = validate
   38         self._queue_ctrl = queue_controller
   39 
   40     @decorators.TransportLog("Queue metadata")
   41     def on_get(self, req, resp, project_id, queue_name):
   42         try:
   43             resp_dict = self._queue_ctrl.get_metadata(queue_name,
   44                                                       project=project_id)
   45 
   46         except storage_errors.DoesNotExist as ex:
   47             LOG.debug(ex)
   48             raise wsgi_errors.HTTPNotFound(six.text_type(ex))
   49 
   50         except Exception:
   51             description = _(u'Queue metadata could not be retrieved.')
   52             LOG.exception(description)
   53             raise wsgi_errors.HTTPServiceUnavailable(description)
   54 
   55         resp.content_location = req.path
   56         resp.body = utils.to_json(resp_dict)
   57         # status defaults to 200
   58 
   59     @decorators.TransportLog("Queue metadata")
   60     def on_put(self, req, resp, project_id, queue_name):
   61         try:
   62             # Place JSON size restriction before parsing
   63             self._validate.queue_metadata_length(req.content_length)
   64             # Deserialize queue metadata
   65             document = wsgi_utils.deserialize(req.stream, req.content_length)
   66             metadata = wsgi_utils.sanitize(document)
   67             # Restrict setting any reserved queue attributes
   68             for key in metadata:
   69                 if key.startswith('_'):
   70                     description = _(u'Reserved queue attributes in metadata '
   71                                     u'(which names start with "_") can not be '
   72                                     u'set in API v1.')
   73                     raise validation.ValidationFailed(description)
   74         except validation.ValidationFailed as ex:
   75             LOG.debug(ex)
   76             raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex))
   77 
   78         try:
   79             self._queue_ctrl.set_metadata(queue_name,
   80                                           metadata=metadata,
   81                                           project=project_id)
   82 
   83         except validation.ValidationFailed as ex:
   84             LOG.debug(ex)
   85             raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex))
   86 
   87         except storage_errors.QueueDoesNotExist as ex:
   88             raise wsgi_errors.HTTPNotFound(six.text_type(ex))
   89 
   90         except Exception:
   91             description = _(u'Metadata could not be updated.')
   92             LOG.exception(description)
   93             raise wsgi_errors.HTTPServiceUnavailable(description)
   94 
   95         resp.status = falcon.HTTP_204
   96         resp.location = req.path