def delete(self, var_name): """Delete a single variable in Airflow""" with airflow_sql_alchemy_session() as session: query = session.query(Variable).filter_by(key=var_name) if len(query.all()) == 0: abort(NOT_FOUND_RESPONSE_CODE, message=NOT_FOUND_MESSAGE) session.query(Variable).filter_by(key=var_name).delete() session.commit() return Response(status=DELETE_RESPONSE_SUCCESS_CODE)
def get(self, dag_run_id): """Retrieve a DAG Run's Status from Airflow""" log.warning("dag_run_id {}".format(dag_run_id)) with airflow_sql_alchemy_session() as session: dr = session.query(DagRun).filter(DagRun.run_id == dag_run_id).first() if dr is None: abort(NOT_FOUND_RESPONSE_CODE, message=DAG_RUN_NOT_FOUND_MESSAGE) response_data = _process_dag_run_to_response_object(dr) return Response( json.dumps(response_data), status=GET_RESPONSE_SUCCESS_CODE, mimetype=JSON_MIME_TYPE )
def delete(self, dag_id): """Delete a DAG from Airflow (This will delete the file containing the DAG)""" dag = check_for_dag_id(dag_id) if dag is None: abort(NOT_FOUND_RESPONSE_CODE, message=NOT_FOUND_MESSAGE) with airflow_sql_alchemy_session() as session: try: os.unlink(dag.fileloc) except FileNotFoundError: pass session.query(DagModel).filter( DagModel.dag_id == dag.dag_id).delete() session.commit() return Response(status=DELETE_RESPONSE_SUCCESS_CODE)
def post(self, var_name): """Create/Update a single variable in Airflow""" with airflow_sql_alchemy_session() as session: deserialize_json = api.payload[deserialize_json_param.name] raw_var_value = api.payload[VALUE_KEY] var = set_airflow_variable(var_name, raw_var_value, deserialize_json, session) session.commit() response = { NAME_KEY: var_name, VALUE_KEY: var, DESERIALIZE_JSON_KEY: deserialize_json } return Response( response=json.dumps(response), status=POST_RESPONSE_SUCCESS_CODE, mimetype=JSON_MIME_TYPE )
def post(self): """Create/Update multiple variables in Airflow""" if len(api.payload) == 0: abort(BAD_REQUEST_RESPONSE_CODE, messsage="No Variables were supplied to Create/Update") variables_created = [] with airflow_sql_alchemy_session() as session: for var in api.payload: var_name = var[NAME_KEY] deserialize_json = var[deserialize_json_param.name] var_value = set_airflow_variable(var_name, var[VALUE_KEY], deserialize_json, session) variables_created.append({ NAME_KEY: var_name, VALUE_KEY: var_value, DESERIALIZE_JSON_KEY: deserialize_json }) session.commit() return Response( response=json.dumps(variables_created), status=POST_RESPONSE_SUCCESS_CODE, mimetype=JSON_MIME_TYPE )
def get(self, dag_id): """Get all DAG Run's for a DAG in Airflow""" args = self.get_parser.parse_args() log.warning("dag_id {}".format(dag_id)) log.warning("before {}".format(args.get(execution_date_before.name))) log.warning("after {}".format(args.get(execution_date_after.name))) if check_for_dag_id(dag_id) is None: abort(NOT_FOUND_RESPONSE_CODE, message=NOT_FOUND_MESSAGE) with airflow_sql_alchemy_session() as session: query = session.query(DagRun).filter(DagRun.dag_id == dag_id) if args.get(execution_date_before.name): query = query.filter(DagRun.execution_date < args.get( execution_date_before.name)) if args.get(execution_date_after.name): query = query.filter( DagRun.execution_date > args.get(execution_date_after.name) ) drs = query.all() response_data = [ _process_dag_run_to_response_object(dag_run) for dag_run in drs ] return Response(json.dumps(response_data), status=GET_RESPONSE_SUCCESS_CODE, mimetype=JSON_MIME_TYPE)
def get(self): """Get all variables in Airflow""" with airflow_sql_alchemy_session() as session: airflow_variables = session.query(Variable).all() var_list = [] d = json.JSONDecoder() for var in airflow_variables: val = None deserialize_json = True try: val = d.decode(var.val) except Exception: val = var.val deserialize_json = False var_list.append({ NAME_KEY: var.key, VALUE_KEY: val, DESERIALIZE_JSON_KEY: deserialize_json }) return Response( response=json.dumps(var_list), status=GET_RESPONSE_SUCCESS_CODE, mimetype=JSON_MIME_TYPE )
def set_is_paused(is_paused, dag_id): with airflow_sql_alchemy_session() as session: dm = session.query(DagModel).filter(DagModel.dag_id == dag_id).first() dm.is_paused = is_paused session.commit()