예제 #1
0
    def message_list(self, req):
        """Gets a list of messages on a queue

        :param req: Request instance ready to be sent.
        :type req: `api.common.Request`
        :return: resp: Response instance
        :type: resp: `api.common.Response`
        """
        project_id = req._headers.get('X-Project-ID')
        queue_name = req._body.get('queue_name')

        LOG.debug(u'Message list - queue: %(queue)s, '
                  u'project: %(project)s', {
                      'queue': queue_name,
                      'project': project_id
                  })

        try:
            kwargs = api_utils.get_headers(req)

            self._validate.client_id_uuid_safe(req._headers.get('Client-ID'))
            client_uuid = api_utils.get_client_uuid(req)

            self._validate.message_listing(**kwargs)
            results = self._message_controller.list(queue_name,
                                                    project=project_id,
                                                    client_uuid=client_uuid,
                                                    **kwargs)

            # Buffer messages
            cursor = next(results)
            messages = list(cursor)
        except (ValueError, api_errors.BadRequest,
                validation.ValidationFailed) as ex:
            LOG.debug(ex)
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers)
        except storage_errors.DoesNotExist as ex:
            LOG.debug(ex)
            headers = {'status': 404}
            return api_utils.error_response(req, ex, headers)

        if messages:
            # Found some messages, so prepare the response
            kwargs['marker'] = next(results)
            messages = [
                api_utils.format_message(message) for message in messages
            ]

        headers = {'status': 200}
        body = {'messages': messages}

        return response.Response(req, body, headers)
예제 #2
0
    def message_list(self, req):
        """Gets a list of messages on a queue

        :param req: Request instance ready to be sent.
        :type req: `api.common.Request`
        :return: resp: Response instance
        :type: resp: `api.common.Response`
        """
        project_id = req._headers.get('X-Project-ID')
        queue_name = req._body.get('queue_name')

        LOG.debug(u'Message list - queue: %(queue)s, '
                  u'project: %(project)s',
                  {'queue': queue_name, 'project': project_id})

        try:
            kwargs = api_utils.get_headers(req)

            self._validate.client_id_uuid_safe(req._headers.get('Client-ID'))
            client_uuid = api_utils.get_client_uuid(req)

            self._validate.message_listing(**kwargs)
            results = self._message_controller.list(
                queue_name,
                project=project_id,
                client_uuid=client_uuid,
                **kwargs)

            # Buffer messages
            cursor = next(results)
            messages = list(cursor)
        except (ValueError, api_errors.BadRequest,
                validation.ValidationFailed) as ex:
            LOG.debug(ex)
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers)
        except storage_errors.DoesNotExist as ex:
            LOG.debug(ex)
            headers = {'status': 404}
            return api_utils.error_response(req, ex, headers)

        if messages:
            # Found some messages, so prepare the response
            kwargs['marker'] = next(results)
            messages = [api_utils.format_message(message)
                        for message in messages]

        headers = {'status': 200}
        body = {'messages': messages}

        return response.Response(req, body, headers)
예제 #3
0
파일: endpoints.py 프로젝트: wenchma/zaqar
    def message_post(self, req):
        """Post a set of messages to a queue

        :param req: Request instance ready to be sent.
        :type req: `api.common.Request`
        :return: resp: Response instance
        :type: resp: `api.common.Response`
        """
        project_id = req._headers.get('X-Project-ID')
        queue_name = req._body.get('queue_name')

        LOG.debug(u'Messages post - queue:  %(queue)s, '
                  u'project: %(project)s',
                  {'queue': queue_name, 'project': project_id})

        messages = req._body.get('messages')

        if messages is None:
            ex = _(u'Invalid request.')
            error = _(u'No messages were found in the request body.')
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers, error)

        try:
            # Place JSON size restriction before parsing
            self._validate.message_length(len(str(messages)))
        except validation.ValidationFailed as ex:
            LOG.debug(ex)
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers)

        _message_post_spec = (
            ('ttl', int, self._defaults.message_ttl),
            ('body', '*', None),
        )

        try:
            messages = api_utils.sanitize(messages,
                                          _message_post_spec,
                                          doctype=list)
        except api_errors.BadRequest as ex:
            LOG.debug(ex)
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers)

        try:
            client_uuid = api_utils.get_client_uuid(req)

            self._validate.message_posting(messages)

            if not self._queue_controller.exists(queue_name, project_id):
                self._validate.queue_identification(queue_name, project_id)
                self._queue_controller.create(queue_name, project=project_id)

            message_ids = self._message_controller.post(
                queue_name,
                messages=messages,
                project=project_id,
                client_uuid=client_uuid)
        except (api_errors.BadRequest, validation.ValidationFailed) as ex:
            LOG.debug(ex)
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers)
        except storage_errors.DoesNotExist as ex:
            LOG.debug(ex)
            headers = {'status': 404}
            return api_utils.error_response(req, ex, headers)
        except storage_errors.MessageConflict as ex:
            LOG.exception(ex)
            error = _(u'No messages could be enqueued.')
            headers = {'status': 500}
            return api_utils.error_response(req, ex, headers, error)

        # Prepare the response
        headers = {'status': 201}
        body = {'message_ids': message_ids}

        return response.Response(req, body, headers)
예제 #4
0
    def message_post(self, req):
        """Post a set of messages to a queue

        :param req: Request instance ready to be sent.
        :type req: `api.common.Request`
        :return: resp: Response instance
        :type: resp: `api.common.Response`
        """
        project_id = req._headers.get('X-Project-ID')
        queue_name = req._body.get('queue_name')

        LOG.debug(u'Messages post - queue:  %(queue)s, '
                  u'project: %(project)s',
                  {'queue': queue_name, 'project': project_id})

        messages = req._body.get('messages')

        if messages is None:
            ex = _(u'Invalid request.')
            error = _(u'No messages were found in the request body.')
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers, error)

        try:
            # NOTE(flwang): Replace 'exists' with 'get_metadata' won't impact
            # the performance since both of them will call
            # collection.find_one()
            queue_meta = None
            try:
                queue_meta = self._queue_controller.get_metadata(queue_name,
                                                                 project_id)
            except storage_errors.DoesNotExist as ex:
                self._validate.queue_identification(queue_name, project_id)
                self._queue_controller.create(queue_name, project=project_id)
                # NOTE(flwang): Queue is created in lazy mode, so no metadata
                # set.
                queue_meta = {}

            queue_max_msg_size = queue_meta.get('_max_messages_post_size',
                                                None)
            queue_default_ttl = queue_meta.get('_default_message_ttl', None)

            # TODO(flwang): To avoid any unexpected regression issue, we just
            # leave the _message_post_spec attribute of class as it's. It
            # should be removed in Newton release.
            if queue_default_ttl:
                _message_post_spec = (('ttl', int, queue_default_ttl),
                                      ('body', '*', None),)
            else:
                _message_post_spec = (('ttl', int, self._defaults.message_ttl),
                                      ('body', '*', None),)
            # Place JSON size restriction before parsing
            self._validate.message_length(len(str(messages)),
                                          max_msg_post_size=queue_max_msg_size)
        except validation.ValidationFailed as ex:
            LOG.debug(ex)
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers)

        try:
            messages = api_utils.sanitize(messages,
                                          _message_post_spec,
                                          doctype=list)
        except api_errors.BadRequest as ex:
            LOG.debug(ex)
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers)

        try:
            client_uuid = api_utils.get_client_uuid(req)

            self._validate.message_posting(messages)

            message_ids = self._message_controller.post(
                queue_name,
                messages=messages,
                project=project_id,
                client_uuid=client_uuid)
        except (api_errors.BadRequest, validation.ValidationFailed) as ex:
            LOG.debug(ex)
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers)
        except storage_errors.DoesNotExist as ex:
            LOG.debug(ex)
            headers = {'status': 404}
            return api_utils.error_response(req, ex, headers)
        except storage_errors.MessageConflict as ex:
            LOG.exception(ex)
            error = _(u'No messages could be enqueued.')
            headers = {'status': 500}
            return api_utils.error_response(req, ex, headers, error)

        # Prepare the response
        headers = {'status': 201}
        body = {'message_ids': message_ids}

        return response.Response(req, body, headers)
예제 #5
0
    def message_post(self, req):
        """Post a set of messages to a queue

        :param req: Request instance ready to be sent.
        :type req: `api.common.Request`
        :return: resp: Response instance
        :type: resp: `api.common.Response`
        """
        project_id = req._headers.get('X-Project-ID')
        queue_name = req._body.get('queue_name')

        LOG.debug(u'Messages post - queue:  %(queue)s, '
                  u'project: %(project)s',
                  {'queue': queue_name, 'project': project_id})

        messages = req._body.get('messages')

        if messages is None:
            ex = _(u'Invalid request.')
            error = _(u'No messages were found in the request body.')
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers, error)

        try:
            # NOTE(flwang): Replace 'exists' with 'get_metadata' won't impact
            # the performance since both of them will call
            # collection.find_one()
            queue_meta = None
            try:
                queue_meta = self._queue_controller.get_metadata(queue_name,
                                                                 project_id)
            except storage_errors.DoesNotExist as ex:
                self._validate.queue_identification(queue_name, project_id)
                self._queue_controller.create(queue_name, project=project_id)
                # NOTE(flwang): Queue is created in lazy mode, so no metadata
                # set.
                queue_meta = {}

            queue_max_msg_size = queue_meta.get('_max_messages_post_size',
                                                None)
            queue_default_ttl = queue_meta.get('_default_message_ttl')

            if queue_default_ttl:
                _message_post_spec = (('ttl', int, queue_default_ttl),
                                      ('body', '*', None),)
            else:
                _message_post_spec = (('ttl', int, self._defaults.message_ttl),
                                      ('body', '*', None),)
            # Place JSON size restriction before parsing
            self._validate.message_length(len(str(messages)),
                                          max_msg_post_size=queue_max_msg_size)
        except validation.ValidationFailed as ex:
            LOG.debug(ex)
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers)

        try:
            messages = api_utils.sanitize(messages,
                                          _message_post_spec,
                                          doctype=list)
        except api_errors.BadRequest as ex:
            LOG.debug(ex)
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers)

        try:
            self._validate.client_id_uuid_safe(req._headers.get('Client-ID'))
            client_uuid = api_utils.get_client_uuid(req)

            self._validate.message_posting(messages)

            message_ids = self._message_controller.post(
                queue_name,
                messages=messages,
                project=project_id,
                client_uuid=client_uuid)
        except (ValueError, api_errors.BadRequest,
                validation.ValidationFailed) as ex:
            LOG.debug(ex)
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers)
        except storage_errors.DoesNotExist as ex:
            LOG.debug(ex)
            headers = {'status': 404}
            return api_utils.error_response(req, ex, headers)
        except storage_errors.MessageConflict as ex:
            LOG.exception(ex)
            error = _(u'No messages could be enqueued.')
            headers = {'status': 500}
            return api_utils.error_response(req, ex, headers, error)

        # Prepare the response
        headers = {'status': 201}
        body = {'message_ids': message_ids}

        return response.Response(req, body, headers)
예제 #6
0
    def message_post(self, req):
        """Post a set of messages to a queue

        :param req: Request instance ready to be sent.
        :type req: `api.common.Request`
        :return: resp: Response instance
        :type: resp: `api.common.Response`
        """
        project_id = req._headers.get('X-Project-ID')
        queue_name = req._body.get('queue_name')

        LOG.debug(
            u'Messages post - queue:  %(queue)s, '
            u'project: %(project)s', {
                'queue': queue_name,
                'project': project_id
            })

        messages = req._body.get('messages')

        if messages is None:
            ex = _(u'Invalid request.')
            error = _(u'No messages were found in the request body.')
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers, error)

        try:
            # Place JSON size restriction before parsing
            self._validate.message_length(len(str(messages)))
        except validation.ValidationFailed as ex:
            LOG.debug(ex)
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers)

        _message_post_spec = (
            ('ttl', int, self._defaults.message_ttl),
            ('body', '*', None),
        )

        try:
            messages = api_utils.sanitize(messages,
                                          _message_post_spec,
                                          doctype=list)
        except api_errors.BadRequest as ex:
            LOG.debug(ex)
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers)

        try:
            client_uuid = api_utils.get_client_uuid(req)

            self._validate.message_posting(messages)

            if not self._queue_controller.exists(queue_name, project_id):
                self._validate.queue_identification(queue_name, project_id)
                self._queue_controller.create(queue_name, project=project_id)

            message_ids = self._message_controller.post(
                queue_name,
                messages=messages,
                project=project_id,
                client_uuid=client_uuid)
        except (api_errors.BadRequest, validation.ValidationFailed) as ex:
            LOG.debug(ex)
            headers = {'status': 400}
            return api_utils.error_response(req, ex, headers)
        except storage_errors.DoesNotExist as ex:
            LOG.debug(ex)
            headers = {'status': 404}
            return api_utils.error_response(req, ex, headers)
        except storage_errors.MessageConflict as ex:
            LOG.exception(ex)
            error = _(u'No messages could be enqueued.')
            headers = {'status': 500}
            return api_utils.error_response(req, ex, headers, error)

        # Prepare the response
        headers = {'status': 201}
        body = {'message_ids': message_ids}

        return response.Response(req, body, headers)