"Fossies" - the Fresh Open Source Software Archive

Member "zaqar-10.0.0/zaqar/transport/wsgi/v1_0/queues.py" (13 May 2020, 4240 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. See also the latest Fossies "Diffs" side-by-side code changes report for "queues.py": 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.transport import utils
   23 from zaqar.transport import validation
   24 from zaqar.transport.wsgi import errors as wsgi_errors
   25 
   26 
   27 LOG = logging.getLogger(__name__)
   28 
   29 
   30 class ItemResource(object):
   31 
   32     __slots__ = ('_queue_controller', '_message_controller')
   33 
   34     def __init__(self, queue_controller, message_controller):
   35         self._queue_controller = queue_controller
   36         self._message_controller = message_controller
   37 
   38     @decorators.TransportLog("Queue item")
   39     def on_put(self, req, resp, project_id, queue_name):
   40         try:
   41             created = self._queue_controller.create(
   42                 queue_name, project=project_id)
   43 
   44         except Exception:
   45             description = _(u'Queue could not be created.')
   46             LOG.exception(description)
   47             raise wsgi_errors.HTTPServiceUnavailable(description)
   48 
   49         resp.status = falcon.HTTP_201 if created else falcon.HTTP_204
   50         resp.location = req.path
   51 
   52     @decorators.TransportLog("Queue item")
   53     def on_head(self, req, resp, project_id, queue_name):
   54         if self._queue_controller.exists(queue_name, project=project_id):
   55             resp.status = falcon.HTTP_204
   56         else:
   57             resp.status = falcon.HTTP_404
   58 
   59         resp.content_location = req.path
   60 
   61     on_get = on_head
   62 
   63     @decorators.TransportLog("Queue item")
   64     def on_delete(self, req, resp, project_id, queue_name):
   65         try:
   66             self._queue_controller.delete(queue_name, project=project_id)
   67 
   68         except Exception:
   69             description = _(u'Queue could not be deleted.')
   70             LOG.exception(description)
   71             raise wsgi_errors.HTTPServiceUnavailable(description)
   72 
   73         resp.status = falcon.HTTP_204
   74 
   75 
   76 class CollectionResource(object):
   77 
   78     __slots__ = ('_queue_controller', '_validate')
   79 
   80     def __init__(self, validate, queue_controller):
   81         self._queue_controller = queue_controller
   82         self._validate = validate
   83 
   84     def on_get(self, req, resp, project_id):
   85         LOG.debug(u'Queue collection GET')
   86 
   87         kwargs = {}
   88 
   89         # NOTE(kgriffs): This syntax ensures that
   90         # we don't clobber default values with None.
   91         req.get_param('marker', store=kwargs)
   92         req.get_param_as_int('limit', store=kwargs)
   93         req.get_param_as_bool('detailed', store=kwargs)
   94 
   95         try:
   96             self._validate.queue_listing(**kwargs)
   97             results = self._queue_controller.list(project=project_id, **kwargs)
   98 
   99             # Buffer list of queues
  100             queues = list(next(results))
  101 
  102         except validation.ValidationFailed as ex:
  103             LOG.debug(ex)
  104             raise wsgi_errors.HTTPBadRequestAPI(six.text_type(ex))
  105 
  106         except Exception:
  107             description = _(u'Queues could not be listed.')
  108             LOG.exception(description)
  109             raise wsgi_errors.HTTPServiceUnavailable(description)
  110 
  111         # Check for an empty list
  112         if len(queues) == 0:
  113             resp.status = falcon.HTTP_204
  114             return
  115 
  116         # Got some. Prepare the response.
  117         kwargs['marker'] = next(results)
  118         for each_queue in queues:
  119             each_queue['href'] = req.path + '/' + each_queue['name']
  120 
  121         response_body = {
  122             'queues': queues,
  123             'links': [
  124                 {
  125                     'rel': 'next',
  126                     'href': req.path + falcon.to_query_str(kwargs)
  127                 }
  128             ]
  129         }
  130 
  131         resp.content_location = req.relative_uri
  132         resp.body = utils.to_json(response_body)
  133         # status defaults to 200