"Fossies" - the Fresh Open Source Software Archive

Member "zaqar-9.0.0/zaqar/transport/wsgi/v1_1/queues.py" (16 Oct 2019, 5754 Bytes) of package /linux/misc/openstack/zaqar-9.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.

    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 ItemResource(object):
   33 
   34     __slots__ = ('_validate', '_queue_controller', '_message_controller')
   35 
   36     def __init__(self, validate, queue_controller, message_controller):
   37         self._validate = validate
   38         self._queue_controller = queue_controller
   39         self._message_controller = message_controller
   40 
   41     @decorators.TransportLog("Queue metadata")
   42     def on_get(self, req, resp, project_id, queue_name):
   43         try:
   44             resp_dict = self._queue_controller.get(queue_name,
   45                                                    project=project_id)
   46 
   47         except storage_errors.DoesNotExist as ex:
   48             LOG.debug(ex)
   49             raise wsgi_errors.HTTPNotFound(six.text_type(ex))
   50 
   51         except Exception as ex:
   52             LOG.exception(ex)
   53             description = _(u'Queue metadata could not be retrieved.')
   54             raise wsgi_errors.HTTPServiceUnavailable(description)
   55 
   56         resp.body = utils.to_json(resp_dict)
   57         # status defaults to 200
   58 
   59     @decorators.TransportLog("Queue item")
   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             metadata = None
   66             if req.content_length:
   67                 document = wsgi_utils.deserialize(req.stream,
   68                                                   req.content_length)
   69                 metadata = wsgi_utils.sanitize(document)
   70             # NOTE(Eva-i): reserved queue attributes is Zaqar's feature since
   71             # API v2. But we have to ensure the bad data will not come from
   72             # older APIs, so we validate metadata here.
   73             self._validate.queue_metadata_putting(metadata)
   74         except validation.ValidationFailed as ex:
   75             LOG.debug(ex)
   76             raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex))
   77 
   78         try:
   79             created = self._queue_controller.create(queue_name,
   80                                                     metadata=metadata,
   81                                                     project=project_id)
   82 
   83         except storage_errors.FlavorDoesNotExist as ex:
   84             LOG.exception(ex)
   85             raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex))
   86 
   87         except Exception as ex:
   88             LOG.exception(ex)
   89             description = _(u'Queue could not be created.')
   90             raise wsgi_errors.HTTPServiceUnavailable(description)
   91 
   92         resp.status = falcon.HTTP_201 if created else falcon.HTTP_204
   93         resp.location = req.path
   94 
   95     @decorators.TransportLog("Queue item")
   96     def on_delete(self, req, resp, project_id, queue_name):
   97         try:
   98             self._queue_controller.delete(queue_name, project=project_id)
   99 
  100         except Exception as ex:
  101             LOG.exception(ex)
  102             description = _(u'Queue could not be deleted.')
  103             raise wsgi_errors.HTTPServiceUnavailable(description)
  104 
  105         resp.status = falcon.HTTP_204
  106 
  107 
  108 class CollectionResource(object):
  109 
  110     __slots__ = ('_queue_controller', '_validate')
  111 
  112     def __init__(self, validate, queue_controller):
  113         self._queue_controller = queue_controller
  114         self._validate = validate
  115 
  116     @decorators.TransportLog("Queue collection")
  117     def on_get(self, req, resp, project_id):
  118         kwargs = {}
  119 
  120         # NOTE(kgriffs): This syntax ensures that
  121         # we don't clobber default values with None.
  122         req.get_param('marker', store=kwargs)
  123         req.get_param_as_int('limit', store=kwargs)
  124         req.get_param_as_bool('detailed', store=kwargs)
  125 
  126         try:
  127             self._validate.queue_listing(**kwargs)
  128             results = self._queue_controller.list(project=project_id, **kwargs)
  129 
  130             # Buffer list of queues
  131             queues = list(next(results))
  132 
  133         except validation.ValidationFailed as ex:
  134             LOG.debug(ex)
  135             raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex))
  136 
  137         except Exception as ex:
  138             LOG.exception(ex)
  139             description = _(u'Queues could not be listed.')
  140             raise wsgi_errors.HTTPServiceUnavailable(description)
  141 
  142         # Got some. Prepare the response.
  143         kwargs['marker'] = next(results) or kwargs.get('marker', '')
  144         for each_queue in queues:
  145             each_queue['href'] = req.path + '/' + each_queue['name']
  146 
  147         links = []
  148         if queues:
  149             links = [
  150                 {
  151                     'rel': 'next',
  152                     'href': req.path + falcon.to_query_str(kwargs)
  153                 }
  154             ]
  155 
  156         response_body = {
  157             'queues': queues,
  158             'links': links
  159         }
  160 
  161         resp.body = utils.to_json(response_body)
  162         # status defaults to 200