Python falcon.HTTP_409 Examples

The following are 10 code examples of falcon.HTTP_409(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module falcon , or try the search function .
Example #1
Source File: rollback.py    From armada with Apache License 2.0 6 votes vote down vote up
def on_post(self, req, resp, release):
        try:
            with self.get_tiller(req, resp) as tiller:
                msg = self.handle(req, release, tiller)
                resp.body = json.dumps({
                    'message': msg,
                })
                resp.content_type = 'application/json'
                resp.status = falcon.HTTP_200
        except LockException as e:
            self.return_error(resp, falcon.HTTP_409, message=str(e))
        except Exception as e:
            self.logger.exception('Caught unexpected exception')
            err_message = 'Failed to rollback release: {}'.format(e)
            self.error(req.context, err_message)
            self.return_error(resp, falcon.HTTP_500, message=err_message) 
Example #2
Source File: test.py    From armada with Apache License 2.0 6 votes vote down vote up
def on_get(self, req, resp, release):
        try:

            with self.get_tiller(req, resp) as tiller:
                success = self.handle(req, release, tiller)

            if success:
                msg = {
                    'result': 'PASSED: {}'.format(release),
                    'message': 'MESSAGE: Test Pass'
                }
            else:
                msg = {
                    'result': 'FAILED: {}'.format(release),
                    'message': 'MESSAGE: Test Fail'
                }

            resp.body = json.dumps(msg)
            resp.status = falcon.HTTP_200
            resp.content_type = 'application/json'
        except LockException as e:
            self.return_error(resp, falcon.HTTP_409, message=str(e)) 
Example #3
Source File: configdocs_helper.py    From shipyard with Apache License 2.0 6 votes vote down vote up
def add_collection(self, collection_id, document_string):
        """Triggers a call to Deckhand to add a collection(bucket)
        Documents are assumed to be a string input, not a
        collection.
        Returns the id of the buffer version.
        """
        try:
            self.deckhand.put_bucket(collection_id, document_string)
        except DocumentExistsElsewhereError as deee:
            LOG.info('Deckhand has rejected this input because an included '
                     'document exists in another bucket already')
            raise ApiError(
                title='Documents may not exist in more than one collection',
                description=deee.response_message,
                status=falcon.HTTP_409)
        except DeckhandRejectedInputError as drie:
            LOG.info('Deckhand has rejected this input because: %s',
                     drie.response_message)
            raise ApiError(
                title="Document(s) invalid syntax or otherwise unsuitable",
                description=drie.response_message)
        # reset the revision dict so it regenerates.
        self.revision_dict = None
        return self.get_revision_id(BUFFER) 
Example #4
Source File: validate_intermediate_commit.py    From shipyard with Apache License 2.0 6 votes vote down vote up
def validate(self):
        if self.action.get('allow_intermediate_commits'):
            LOG.debug("Intermediate commit check skipped due to user input")
        else:
            intermediate_commits = (
                self.configdocs_helper.check_intermediate_commit())
            if intermediate_commits:
                raise ApiError(
                    title='Intermediate commit detected',
                    description=(
                        'The current committed revision of documents has '
                        'other prior commits that have not been used as '
                        'part of a site action, e.g. update_site. If you '
                        'are aware and these other commits are intended, '
                        'please rerun this action with the option '
                        '`allow-intermediate-commits=True`'),
                    status=falcon.HTTP_409,
                    retry=False
                ) 
Example #5
Source File: test.py    From armada with Apache License 2.0 5 votes vote down vote up
def on_post(self, req, resp):
        # TODO(fmontei): Validation Content-Type is application/x-yaml.
        try:
            with self.get_tiller(req, resp) as tiller:
                return self.handle(req, resp, tiller)
        except LockException as e:
            self.return_error(resp, falcon.HTTP_409, message=str(e)) 
Example #6
Source File: api_lock.py    From shipyard with Apache License 2.0 5 votes vote down vote up
def api_lock(api_lock_type):
    """
    Decorator to handle allowing a resource method to institute a lock
    based on the specified lock type.
    These locks are intended for use around methods such as on_post
    and on_get, etc...
    """
    def lock_decorator(func):
        @wraps(func)
        def func_wrapper(self, req, resp, *args, **kwargs):
            lock = ApiLock(api_lock_type,
                           req.context.external_marker,
                           req.context.user)
            try:
                lock.acquire()
                return func(self, req, resp, *args, **kwargs)
            except ApiLockAcquireError:
                raise ApiError(
                    title='Blocked by another process',
                    description=(
                        'Another process is currently blocking this request '
                        'with a lock for {}. Lock expires in not more '
                        'than {} seconds'.format(
                            lock.lock_type_name,
                            lock.expires
                        )
                    ),
                    status=falcon.HTTP_409,
                    retry=False,
                )
            finally:
                lock.release()
        return func_wrapper
    return lock_decorator 
Example #7
Source File: actions_control_api.py    From shipyard with Apache License 2.0 5 votes vote down vote up
def pause_dag(self, dag_id, execution_date):
        """
        Sets the pause flag on this dag/execution
        """
        try:
            AIRFLOW_DB.pause_dag_run(
                dag_id=dag_id, execution_date=execution_date)
        except AirflowStateError as state_error:
            raise ApiError(
                title='Unable to pause action',
                description=state_error.message,
                status=falcon.HTTP_409) 
Example #8
Source File: actions_control_api.py    From shipyard with Apache License 2.0 5 votes vote down vote up
def unpause_dag(self, dag_id, execution_date):
        """
        Clears the pause flag on this dag/execution
        """
        try:
            AIRFLOW_DB.unpause_dag_run(
                dag_id=dag_id, execution_date=execution_date)
        except AirflowStateError as state_error:
            raise ApiError(
                title='Unable to unpause action',
                description=state_error.message,
                status=falcon.HTTP_409) 
Example #9
Source File: configdocs_api.py    From shipyard with Apache License 2.0 5 votes vote down vote up
def commit_configdocs(self, helper, force, dryrun):
        """
        Attempts to commit the configdocs
        """
        if helper.is_buffer_empty():
            raise ApiError(
                title=CommitConfigDocsResource.unable_to_commmit,
                description='There are no documents in the buffer to commit',
                status=falcon.HTTP_409,
                retry=True)
        validations = helper.get_validations_for_revision(
            helper.get_revision_id(configdocs_helper.BUFFER)
        )
        if dryrun:
            validations['code'] = falcon.HTTP_200
            if 'message' in validations:
                validations['message'] = (
                    validations['message'] + ' DRYRUN')
            else:
                validations['message'] = 'DRYRUN'
        else:
            if force or validations.get('status') == 'Success':
                helper.tag_buffer(configdocs_helper.COMMITTED)
            if force and validations.get('status') == 'Failure':
                # override the status in the response
                validations['code'] = falcon.HTTP_200
                if 'message' in validations:
                    validations['message'] = (
                        validations['message'] + ' FORCED SUCCESS')
                else:
                    validations['message'] = 'FORCED SUCCESS'
        return validations 
Example #10
Source File: armada.py    From armada with Apache License 2.0 4 votes vote down vote up
def on_post(self, req, resp):
        # Load data from request and get options
        if req.content_type == 'application/x-yaml':
            data = list(self.req_yaml(req))
            if type(data[0]) is list:
                documents = list(data[0])
            else:
                documents = data
        elif req.content_type == 'application/json':
            self.logger.debug("Applying manifest based on reference.")
            req_body = self.req_json(req)
            doc_ref = req_body.get('hrefs', None)

            if not doc_ref:
                self.logger.info("Request did not contain 'hrefs'.")
                resp.status = falcon.HTTP_400
                return

            data = ReferenceResolver.resolve_reference(doc_ref)
            documents = list()
            for d in data:
                documents.extend(list(yaml.safe_load_all(d.decode())))

            if req_body.get('overrides', None):
                overrides = Override(
                    documents, overrides=req_body.get('overrides'))
                documents = overrides.update_manifests()
        else:
            self.error(
                req.context, "Unknown content-type %s" % req.content_type)
            # TODO(fmontei): Use falcon.<Relevant API Exception Class> instead.
            return self.return_error(
                resp,
                falcon.HTTP_415,
                message="Request must be in application/x-yaml"
                "or application/json")
        try:
            with self.get_tiller(req, resp) as tiller:
                msg = self.handle(req, documents, tiller)
                resp.body = json.dumps({
                    'message': msg,
                })
                resp.content_type = 'application/json'
                resp.status = falcon.HTTP_200

        except exceptions.ManifestException as e:
            self.return_error(resp, falcon.HTTP_400, message=str(e))
        except LockException as e:
            self.return_error(resp, falcon.HTTP_409, message=str(e))
        except Exception as e:
            self.logger.exception('Caught unexpected exception')
            err_message = 'Failed to apply manifest: {}'.format(e)
            self.error(req.context, err_message)
            self.return_error(resp, falcon.HTTP_500, message=err_message)