def __func(playbook): data = request.get_json() if 'name' in data and data['name']: new_playbook_name = data['name'] else: new_playbook_name = playbook.name + "_Copy" playbook_json = playbook_schema.dump(playbook) playbook_json['name'] = new_playbook_name playbook_json.pop('id') if 'workflows' in playbook_json: for workflow in playbook_json['workflows']: workflow.pop('is_valid', None) regenerate_workflow_ids(workflow) try: new_playbook = playbook_schema.load(playbook_json) current_app.running_context.execution_db.session.add(new_playbook) current_app.running_context.execution_db.session.commit() except IntegrityError: current_app.running_context.execution_db.session.rollback() current_app.logger.error('Could not copy Playbook {}. Unique constraint failed'.format(playbook_id)) return unique_constraint_problem('playbook', 'copy', playbook_id) except ValueError: current_app.running_context.execution_db.session.rollback() current_app.logger.error('Could not copy Playbook {}. Invalid input'.format(playbook_id)) return improper_json_problem('playbook', 'copy', playbook_id) current_app.logger.info('Copied playbook {0} to {1}'.format(playbook_id, new_playbook_name)) return playbook_schema.dump(new_playbook), OBJECT_CREATED
def __func(workflow): data = request.get_json() if 'name' in data and data['name']: new_workflow_name = data['name'] else: new_workflow_name = workflow.name + "_Copy" workflow_json = workflow_schema.dump(workflow) workflow_json = workflow_json.data workflow_json.pop('id') workflow_json['name'] = new_workflow_name regenerate_workflow_ids(workflow_json) if executiondb.execution_db.session.query( exists().where(Playbook.id == playbook_id)).scalar(): playbook = executiondb.execution_db.session.query( Playbook).filter_by(id=playbook_id).first() else: executiondb.execution_db.session.rollback() current_app.logger.error( 'Could not copy workflow {}. Playbook does not exist'.format( playbook_id)) return Problem.from_crud_resource( OBJECT_DNE_ERROR, 'workflow', 'copy', 'Could not copy workflow {}. Playbook with id {} does not exist.' .format(workflow_id, playbook_id)) try: new_workflow = workflow_schema.load(workflow_json) new_workflow = new_workflow.data executiondb.execution_db.session.add(new_workflow) playbook.add_workflow(new_workflow) executiondb.execution_db.session.commit() except IntegrityError: executiondb.execution_db.session.rollback() current_app.logger.error( 'Could not copy workflow {}. Unique constraint failed'.format( new_workflow_name)) return unique_constraint_problem('workflow', 'copy', new_workflow_name) current_app.logger.info('Workflow {0} copied to {1}'.format( workflow_id, new_workflow.id)) return workflow_schema.dump(new_workflow).data, OBJECT_CREATED
def __func(workflow): data = request.get_json() if 'name' in data and data['name']: new_workflow_name = data['name'] else: new_workflow_name = workflow.name + "_Copy" workflow_json = workflow_schema.dump(workflow) workflow_json.pop('id') workflow_json.pop('is_valid', None) workflow_json['name'] = new_workflow_name regenerate_workflow_ids(workflow_json) if current_app.running_context.execution_db.session.query(exists().where(Playbook.id == playbook_id)).scalar(): playbook = current_app.running_context.execution_db.session.query(Playbook).filter_by( id=playbook_id).first() else: current_app.running_context.execution_db.session.rollback() current_app.logger.error('Could not copy workflow {}. Playbook does not exist'.format(playbook_id)) return Problem.from_crud_resource( OBJECT_DNE_ERROR, 'workflow', 'copy', 'Could not copy workflow {}. Playbook with id {} does not exist.'.format(workflow_id, playbook_id)) try: new_workflow = workflow_schema.load(workflow_json) current_app.running_context.execution_db.session.add(new_workflow) playbook.add_workflow(new_workflow) current_app.running_context.execution_db.session.commit() except IntegrityError: current_app.running_context.execution_db.session.rollback() current_app.logger.error('Could not copy workflow {}. Unique constraint failed'.format(new_workflow_name)) return unique_constraint_problem('workflow', 'copy', new_workflow_name) current_app.logger.info('Workflow {0} copied to {1}'.format(workflow_id, new_workflow.id)) return workflow_schema.dump(new_workflow), OBJECT_CREATED
def __func(playbook): data = request.get_json() if 'name' in data and data['name']: new_playbook_name = data['name'] else: new_playbook_name = playbook.name + "_Copy" playbook_json = playbook_schema.dump(playbook) playbook_json['name'] = new_playbook_name playbook_json.pop('id') if 'workflows' in playbook_json: for workflow in playbook_json['workflows']: workflow.pop('is_valid', None) regenerate_workflow_ids(workflow) try: new_playbook = playbook_schema.load(playbook_json) current_app.running_context.execution_db.session.add(new_playbook) current_app.running_context.execution_db.session.commit() except IntegrityError: current_app.running_context.execution_db.session.rollback() current_app.logger.error( 'Could not copy Playbook {}. Unique constraint failed'.format( playbook_id)) return unique_constraint_problem('playbook', 'copy', playbook_id) except ValueError: current_app.running_context.execution_db.session.rollback() current_app.logger.error( 'Could not copy Playbook {}. Invalid input'.format( playbook_id)) return improper_json_problem('playbook', 'copy', playbook_id) current_app.logger.info('Copied playbook {0} to {1}'.format( playbook_id, new_playbook_name)) return playbook_schema.dump(new_playbook), OBJECT_CREATED