Пример #1
0
    def put(self, inquiry_id, response_data, requester_user):
        """Provide response data to an Inquiry

            In general, provided the response data validates against the provided
            schema, and the user has the appropriate permissions to respond,
            this will set the Inquiry execution to a successful status, and resume
            the parent workflow.

            Handles requests:
                PUT /inquiries/<inquiry id>
        """
        LOG.debug("Inquiry %s received response payload: %s" %
                  (inquiry_id, response_data.response))

        # Set requester to system user if not provided.
        if not requester_user:
            requester_user = cfg.CONF.system_user.user

        # Retrieve the inquiry by id.
        try:
            inquiry = self._get_one_by_id(
                id=inquiry_id,
                requester_user=requester_user,
                permission_type=rbac_types.PermissionType.INQUIRY_RESPOND)
        except db_exceptions.StackStormDBObjectNotFoundError as e:
            LOG.exception('Unable to identify inquiry with id "%s".' %
                          inquiry_id)
            api_router.abort(http_client.NOT_FOUND, six.text_type(e))
        except rbac_exceptions.ResourceAccessDeniedError as e:
            LOG.exception('User is denied access to inquiry "%s".' %
                          inquiry_id)
            api_router.abort(http_client.FORBIDDEN, six.text_type(e))
        except Exception as e:
            LOG.exception('Unable to get record for inquiry "%s".' %
                          inquiry_id)
            api_router.abort(http_client.INTERNAL_SERVER_ERROR,
                             six.text_type(e))

        # Check if inquiry can still be respond to.
        try:
            inquiry_service.check_inquiry(inquiry)
        except Exception as e:
            LOG.exception('Fail checking validity of inquiry "%s".' %
                          inquiry_id)
            api_router.abort(http_client.BAD_REQUEST, six.text_type(e))

        # Check if user has permission to respond to this inquiry.
        try:
            inquiry_service.check_permission(inquiry, requester_user)
        except Exception as e:
            LOG.exception('Fail checking permission for inquiry "%s".' %
                          inquiry_id)
            api_router.abort(http_client.FORBIDDEN, six.text_type(e))

        # Validate the body of the response against the schema parameter for this inquiry.
        try:
            inquiry_service.validate_response(inquiry, response_data.response)
        except Exception as e:
            LOG.exception('Fail checking response for inquiry "%s".' %
                          inquiry_id)
            api_router.abort(http_client.BAD_REQUEST, six.text_type(e))

        # Respond to inquiry and update if there is a partial response.
        try:
            inquiry_service.respond(inquiry,
                                    response_data.response,
                                    requester=requester_user)
        except Exception as e:
            LOG.exception('Fail to update response for inquiry "%s".' %
                          inquiry_id)
            api_router.abort(http_client.INTERNAL_SERVER_ERROR,
                             six.text_type(e))

        return {'id': inquiry_id, 'response': response_data.response}
Пример #2
0
    def put(self, inquiry_id, response_data, requester_user):
        """Provide response data to an Inquiry

            In general, provided the response data validates against the provided
            schema, and the user has the appropriate permissions to respond,
            this will set the Inquiry execution to a successful status, and resume
            the parent workflow.

            Handles requests:
                PUT /inquiries/<inquiry id>
        """
        LOG.debug("Inquiry %s received response payload: %s" % (inquiry_id, response_data.response))

        # Set requester to system user if not provided.
        if not requester_user:
            requester_user = cfg.CONF.system_user.user

        # Retrieve the inquiry by id.
        try:
            inquiry = self._get_one_by_id(
                id=inquiry_id,
                requester_user=requester_user,
                permission_type=rbac_types.PermissionType.INQUIRY_RESPOND
            )
        except db_exceptions.StackStormDBObjectNotFoundError as e:
            LOG.exception('Unable to identify inquiry with id "%s".' % inquiry_id)
            api_router.abort(http_client.NOT_FOUND, six.text_type(e))
        except rbac_exceptions.ResourceAccessDeniedError as e:
            LOG.exception('User is denied access to inquiry "%s".' % inquiry_id)
            api_router.abort(http_client.FORBIDDEN, six.text_type(e))
        except Exception as e:
            LOG.exception('Unable to get record for inquiry "%s".' % inquiry_id)
            api_router.abort(http_client.INTERNAL_SERVER_ERROR, six.text_type(e))

        # Check if inquiry can still be respond to.
        try:
            inquiry_service.check_inquiry(inquiry)
        except Exception as e:
            LOG.exception('Fail checking validity of inquiry "%s".' % inquiry_id)
            api_router.abort(http_client.BAD_REQUEST, six.text_type(e))

        # Check if user has permission to respond to this inquiry.
        try:
            inquiry_service.check_permission(inquiry, requester_user)
        except Exception as e:
            LOG.exception('Fail checking permission for inquiry "%s".' % inquiry_id)
            api_router.abort(http_client.FORBIDDEN, six.text_type(e))

        # Validate the body of the response against the schema parameter for this inquiry.
        try:
            inquiry_service.validate_response(inquiry, response_data.response)
        except Exception as e:
            LOG.exception('Fail checking response for inquiry "%s".' % inquiry_id)
            api_router.abort(http_client.BAD_REQUEST, six.text_type(e))

        # Respond to inquiry and update if there is a partial response.
        try:
            inquiry_service.respond(inquiry, response_data.response, requester=requester_user)
        except Exception as e:
            LOG.exception('Fail to update response for inquiry "%s".' % inquiry_id)
            api_router.abort(http_client.INTERNAL_SERVER_ERROR, six.text_type(e))

        return {
            'id': inquiry_id,
            'response': response_data.response
        }