Ejemplo n.º 1
0
    def on_get(self, req, resp, project_id, queue_name, message_id):
        try:
            messages = self.message_controller.get(
                queue_name,
                message_id,
                project=project_id)

            message = next(messages)

        except StopIteration:
            # Good project_id and queue, but no messages
            raise falcon.HTTPNotFound()
        except storage_exceptions.DoesNotExist:
            # This can happen if the queue or project_id is invalid
            raise falcon.HTTPNotFound()
        except Exception as ex:
            LOG.exception(ex)
            description = _('Message could not be retrieved.')
            raise wsgi_exceptions.HTTPServiceUnavailable(description)

        # Prepare response
        message['href'] = req.path
        del message['id']

        resp.content_location = req.relative_uri
        resp.body = helpers.to_json(message)
        resp.status = falcon.HTTP_200
Ejemplo n.º 2
0
    def on_get(self, req, resp, project_id, queue_name, claim_id):
        try:
            meta, msgs = self.claim_controller.get(
                queue_name,
                claim_id=claim_id,
                project=project_id)

            # Buffer claimed messages
            # TODO(kgriffs): Optimize along with serialization (see below)
            meta['messages'] = list(msgs)

        except storage_exceptions.DoesNotExist:
            raise falcon.HTTPNotFound
        except Exception as ex:
            LOG.exception(ex)
            description = _('Claim could not be queried.')
            raise wsgi_exceptions.HTTPServiceUnavailable(description)

        # Serialize claimed messages
        # TODO(kgriffs): Optimize
        for msg in meta['messages']:
            msg['href'] = _msg_uri_from_claim(
                req.path.rsplit('/', 2)[0], msg['id'], meta['id'])
            del msg['id']

        meta['href'] = req.path
        del meta['id']

        resp.content_location = req.relative_uri
        resp.body = helpers.to_json(meta)
        resp.status = falcon.HTTP_200
Ejemplo n.º 3
0
    def on_post(self, req, resp, project_id, queue_name):
        uuid = req.get_header('Client-ID', required=True)

        # Pull out just the fields we care about
        messages = wsgi_helpers.filter_stream(
            req.stream,
            req.content_length,
            MESSAGE_POST_SPEC,
            doctype=wsgi_helpers.JSONArray)

        # Verify that at least one message was provided.
        try:
            first_message = next(messages)
        except StopIteration:
            description = _('No messages were provided.')
            raise wsgi_exceptions.HTTPBadRequestBody(description)

        # Hack to make message_controller oblivious to the
        # fact that we just popped the first message.
        messages = itertools.chain((first_message,), messages)

        # Enqueue the messages
        partial = False

        try:
            message_ids = self.message_controller.post(
                queue_name,
                messages=messages,
                project=project_id,
                client_uuid=uuid)

        except storage_exceptions.DoesNotExist:
            raise falcon.HTTPNotFound()
        except storage_exceptions.MessageConflict as ex:
            LOG.exception(ex)
            partial = True
            message_ids = ex.succeeded_ids

            if not message_ids:
                # TODO(kgriffs): Include error code that is different
                # from the code used in the generic case, below.
                description = _('No messages could be enqueued.')
                raise wsgi_exceptions.HTTPServiceUnavailable(description)

        except Exception as ex:
            LOG.exception(ex)
            description = _('Messages could not be enqueued.')
            raise wsgi_exceptions.HTTPServiceUnavailable(description)

        # Prepare the response
        resp.status = falcon.HTTP_201

        ids_value = ','.join(message_ids)
        resp.location = req.path + '?ids=' + ids_value

        hrefs = [req.path + '/' + id for id in message_ids]
        body = {'resources': hrefs, 'partial': partial}
        resp.body = helpers.to_json(body)
Ejemplo n.º 4
0
    def on_get(self, req, resp, project_id, queue_name):
        message_ids = req.get_param_as_list('ids')
        if message_ids is None:
            doc = self._get_metadata(project_id, queue_name)
        else:
            base_path = req.path + '/messages'
            doc = self._get_messages_by_id(base_path, project_id, queue_name,
                                           message_ids)

        resp.content_location = req.relative_uri
        resp.body = helpers.to_json(doc)
Ejemplo n.º 5
0
    def on_get(self, req, resp, project_id, queue_name):
        try:
            resp_dict = self.queue_ctrl.stats(queue_name,
                                              project=project_id)

            resp.content_location = req.path
            resp.body = helpers.to_json(resp_dict)
            resp.status = falcon.HTTP_200

        except Exception as ex:
            LOG.exception(ex)
            title = _('Service temporarily unavailable')
            msg = _('Please try again in a few seconds.')
            raise falcon.HTTPServiceUnavailable(title, msg, 30)
Ejemplo n.º 6
0
    def on_get(self, req, resp, tenant_id, queue_name, message_id):
        try:
            msg = self.msg_ctrl.get(queue_name,
                                    message_id=message_id,
                                    tenant=tenant_id)

            msg['href'] = req.path
            del msg['id']

            resp.content_location = req.relative_uri
            resp.body = helpers.to_json(msg)
            resp.status = falcon.HTTP_200

        except exceptions.DoesNotExist:
            raise falcon.HTTPNotFound
Ejemplo n.º 7
0
    def on_get(self, req, resp, project_id, queue_name):
        resp.content_location = req.relative_uri

        ids = req.get_param_as_list('ids')
        if ids is None:
            response = self._get(req, project_id, queue_name)

            if response is None:
                resp.status = falcon.HTTP_204
                return
        else:
            base_path = req.path + '/messages'
            response = self._get_by_id(base_path, project_id, queue_name, ids)

        resp.body = helpers.to_json(response)
Ejemplo n.º 8
0
    def on_get(self, req, resp, tenant_id, queue_name):
        try:
            doc = self.queue_ctrl.get(queue_name,
                                      tenant=tenant_id)

            resp.content_location = req.relative_uri
            resp.body = helpers.to_json(doc)

        except exceptions.DoesNotExist:
            raise falcon.HTTPNotFound

        except Exception as ex:
            LOG.exception(ex)
            title = _('Service temporarily unavailable')
            msg = _('Please try again in a few seconds.')
            raise falcon.HTTPServiceUnavailable(title, msg, 30)
Ejemplo n.º 9
0
    def on_get(self, req, resp, project_id, queue_name):
        try:
            resp_dict = self.queue_ctrl.stats(queue_name,
                                              project=project_id)

            resp.content_location = req.path
            resp.body = helpers.to_json(resp_dict)
            resp.status = falcon.HTTP_200

        except storage_exceptions.DoesNotExist:
            raise falcon.HTTPNotFound()

        except Exception as ex:
            LOG.exception(ex)
            description = _('Queue stats could not be read.')
            raise wsgi_exceptions.HTTPServiceUnavailable(description)
Ejemplo n.º 10
0
    def on_get(self, req, resp, tenant_id):
        resp_dict = {}
        try:
            queues = self.queue_ctrl.list(tenant_id)

            resp_dict['queues'] = list(queues)
            for queue in resp_dict['queues']:
                queue['href'] = req.path + '/' + queue['name']

        except Exception as ex:
            LOG.exception(ex)
            title = _('Service temporarily unavailable')
            msg = _('Please try again in a few seconds.')
            raise falcon.HTTPServiceUnavailable(title, msg, 30)

        resp.content_location = req.relative_uri
        resp.body = helpers.to_json(resp_dict)
        resp.status = falcon.HTTP_200
Ejemplo n.º 11
0
    def on_post(self, req, resp, project_id, queue_name):
        # Check for an explicit limit on the # of messages to claim
        limit = req.get_param_as_int('limit')
        claim_options = {} if limit is None else {'limit': limit}

        # Read claim metadata (e.g., TTL) and raise appropriate
        # HTTP errors as needed.
        metadata, = wsgi_helpers.filter_stream(req.stream, req.content_length,
                                               CLAIM_METADATA_SPEC)

        # Claim some messages
        try:
            cid, msgs = self.claim_controller.create(
                queue_name,
                metadata=metadata,
                project=project_id,
                **claim_options)

            # Buffer claimed messages
            # TODO(kgriffs): optimize, along with serialization (below)
            resp_msgs = list(msgs)

        except storage_exceptions.DoesNotExist:
            raise falcon.HTTPNotFound()
        except Exception as ex:
            LOG.exception(ex)
            description = _('Claim could not be created.')
            raise wsgi_exceptions.HTTPServiceUnavailable(description)

        # Serialize claimed messages, if any. This logic assumes
        # the storage driver returned well-formed messages.
        if len(resp_msgs) != 0:
            for msg in resp_msgs:
                msg['href'] = _msg_uri_from_claim(
                    req.path.rpartition('/')[0], msg['id'], cid)

                del msg['id']

            resp.location = req.path + '/' + cid
            resp.body = helpers.to_json(resp_msgs)
            resp.status = falcon.HTTP_200
        else:
            resp.status = falcon.HTTP_204
Ejemplo n.º 12
0
    def on_get(self, req, resp, tenant_id, queue_name):
        uuid = req.get_header('Client-ID', required=True)

        #TODO(zyuan): where do we define the limits?
        kwargs = {
            'marker': req.get_param('marker'),
            'limit': req.get_param_as_int('limit'),
            'echo': req.get_param_as_bool('echo'),
        }
        kwargs = dict([(k, v) for k, v in kwargs.items()
                       if v is not None])

        resp_dict = {}

        try:
            msgs = self.msg_ctrl.list(queue_name,
                                      tenant=tenant_id,
                                      client_uuid=uuid,
                                      **kwargs)
            resp_dict['messages'] = list(msgs)

        except exceptions.DoesNotExist:
            raise falcon.HTTPNotFound

        if len(resp_dict['messages']) != 0:
            kwargs['marker'] = resp_dict['messages'][-1]['marker']
            for msg in resp_dict['messages']:
                msg['href'] = req.path + '/' + msg['id']
                del msg['id']
                del msg['marker']

            resp_dict['links'] = [
                {
                    'rel': 'next',
                    'href': req.path + falcon.to_query_str(kwargs)
                }
            ]

            resp.content_location = req.relative_uri
            resp.body = helpers.to_json(resp_dict)
            resp.status = falcon.HTTP_200
        else:
            resp.status = falcon.HTTP_204
Ejemplo n.º 13
0
    def on_get(self, req, resp, project_id, queue_name):
        LOG.debug(_("Queue metadata GET - queue: %(queue)s, "
                    "project: %(project)s") %
                  {"queue": queue_name, "project": project_id})

        try:
            resp_dict = self.queue_ctrl.get_metadata(queue_name,
                                                     project=project_id)

        except storage_exceptions.DoesNotExist:
            raise falcon.HTTPNotFound()

        except Exception as ex:
            LOG.exception(ex)
            description = _('Queue metadata could not be retrieved.')
            raise wsgi_exceptions.HTTPServiceUnavailable(description)

        resp.content_location = req.path
        resp.body = helpers.to_json(resp_dict)
        resp.status = falcon.HTTP_200
Ejemplo n.º 14
0
    def on_get(self, req, resp, project_id):
        # TODO(kgriffs): Optimize
        kwargs = helpers.purge({
            'marker': req.get_param('marker'),
            'limit': req.get_param_as_int('limit'),
            'detailed': req.get_param_as_bool('detailed'),
        })

        try:
            results = self.queue_controller.list(project=project_id, **kwargs)
        except Exception as ex:
            LOG.exception(ex)
            description = _('Queues could not be listed.')
            raise wsgi_exceptions.HTTPServiceUnavailable(description)

        # Buffer list of queues
        queues = list(results.next())

        # Check for an empty list
        if len(queues) == 0:
            resp.status = falcon.HTTP_204
            return

        # Got some. Prepare the response.
        kwargs['marker'] = results.next()
        for each_queue in queues:
            each_queue['href'] = req.path + '/' + each_queue['name']

        response_body = {
            'queues': queues,
            'links': [
                {
                    'rel': 'next',
                    'href': req.path + falcon.to_query_str(kwargs)
                }
            ]
        }

        resp.content_location = req.relative_uri
        resp.body = helpers.to_json(response_body)
        resp.status = falcon.HTTP_200
Ejemplo n.º 15
0
    def on_get(self, req, resp, tenant_id, queue_name, claim_id):
        try:
            meta, msgs = self.claim_ctrl.get(
                queue_name,
                claim_id=claim_id,
                tenant=tenant_id)

            meta['messages'] = list(msgs)
            for msg in meta['messages']:
                msg['href'] = _msg_uri_from_claim(
                    req.path.rsplit('/', 2)[0], msg['id'], meta['id'])
                del msg['id']

            meta['href'] = req.path
            del meta['id']

            resp.content_location = req.relative_uri
            resp.body = helpers.to_json(meta)
            resp.status = falcon.HTTP_200

        except exceptions.DoesNotExist:
            raise falcon.HTTPNotFound
Ejemplo n.º 16
0
    def on_post(self, req, resp, tenant_id, queue_name):
        if req.content_length is None or req.content_length == 0:
            raise falcon.HTTPBadRequest(_('Bad request'),
                                        _('Missing claim metadata.'))

        #TODO(zyuan): where do we define the limits?
        kwargs = {
            'limit': req.get_param_as_int('limit'),
        }
        kwargs = dict([(k, v) for k, v in kwargs.items() if v is not None])

        try:
            metadata = _filtered(helpers.read_json(req.stream))
            cid, msgs = self.claim_ctrl.create(
                queue_name,
                metadata=metadata,
                tenant=tenant_id,
                **kwargs)
            resp_msgs = list(msgs)

            if len(resp_msgs) != 0:
                for msg in resp_msgs:
                    msg['href'] = _msg_uri_from_claim(
                        req.path.rpartition('/')[0], msg['id'], cid)
                    del msg['id']

                resp.location = req.path + '/' + cid
                resp.body = helpers.to_json(resp_msgs)
                resp.status = falcon.HTTP_200
            else:
                resp.status = falcon.HTTP_204

        except helpers.MalformedJSON:
            raise falcon.HTTPBadRequest(_('Bad request'),
                                        _('Malformed claim metadata.'))

        except exceptions.DoesNotExist:
            raise falcon.HTTPNotFound
Ejemplo n.º 17
0
    def on_get(self, req, resp, tenant_id, queue_name):
        resp_dict = self.queue_ctrl.stats(queue_name, tenant=tenant_id)

        resp.content_location = req.path
        resp.body = helpers.to_json(resp_dict)
        resp.status = falcon.HTTP_200
Ejemplo n.º 18
0
    def on_get(self, req, resp, project_id, queue_name):
        uuid = req.get_header('Client-ID', required=True)

        # TODO(kgriffs): Optimize
        kwargs = helpers.purge({
            'marker': req.get_param('marker'),
            'limit': req.get_param_as_int('limit'),
            'echo': req.get_param_as_bool('echo'),
        })

        try:
            results = self.message_controller.list(
                queue_name,
                project=project_id,
                client_uuid=uuid,
                **kwargs)

            # Buffer messages
            cursor = results.next()
            messages = list(cursor)

        except storage_exceptions.DoesNotExist:
            raise falcon.HTTPNotFound()

        except storage_exceptions.MalformedMarker:
            title = _('Invalid query string parameter')
            description = _('The value for the query string '
                            'parameter "marker" could not be '
                            'parsed. We recommend using the '
                            '"next" URI from a previous '
                            'request directly, rather than '
                            'constructing the URI manually. ')

            raise falcon.HTTPBadRequest(title, description)

        except Exception as ex:
            LOG.exception(ex)
            description = _('Messages could not be listed.')
            raise wsgi_exceptions.HTTPServiceUnavailable(description)

        # Check for no content
        if len(messages) == 0:
            resp.status = falcon.HTTP_204
            return

        # Found some messages, so prepare the response
        kwargs['marker'] = results.next()
        for each_message in messages:
            each_message['href'] = req.path + '/' + each_message['id']
            del each_message['id']

        response_body = {
            'messages': messages,
            'links': [
                {
                    'rel': 'next',
                    'href': req.path + falcon.to_query_str(kwargs)
                }
            ]
        }

        resp.content_location = req.relative_uri
        resp.body = helpers.to_json(response_body)
        resp.status = falcon.HTTP_200