def trigger_dag(dag_id): """ Trigger a new dag run for a Dag with an execution date of now unless specified in the data. """ data = request.get_json(force=True) run_id = None if 'run_id' in data: run_id = data['run_id'] conf = None if 'conf' in data: conf = data['conf'] if not isinstance(conf, dict): error_message = 'Dag Run conf must be a dictionary object, other types are not supported' log.error(error_message) response = jsonify({'error': error_message}) response.status_code = 400 return response execution_date = None if 'execution_date' in data and data['execution_date'] is not None: execution_date = data['execution_date'] # Convert string datetime into actual datetime try: execution_date = timezone.parse(execution_date) except ValueError: log.error( "Given execution date could not be identified as a date.") error_message = ( f'Given execution date, {execution_date}, could not be identified as a date. ' f'Example date format: 2015-11-16T14:34:15+00:00') response = jsonify({'error': error_message}) response.status_code = 400 return response replace_microseconds = execution_date is None if 'replace_microseconds' in data: replace_microseconds = to_boolean(data['replace_microseconds']) try: dr = trigger.trigger_dag(dag_id, run_id, conf, execution_date, replace_microseconds) except AirflowException as err: log.error(err) response = jsonify(error=f"{err}") response.status_code = err.status_code return response if getattr(g, 'user', None): log.info("User %s created %s", g.user, dr) response = jsonify(message=f"Created {dr}", execution_date=dr.execution_date.isoformat(), run_id=dr.run_id) return response
def trigger_dag(dag_id): """ Trigger a new dag run for a Dag with an execution date of now unless specified in the data. """ set_config(dag_id) data = request.get_json(force=True) run_id = None if 'run_id' in data: run_id = data['run_id'] conf = None if 'conf' in data: conf = data['conf'] execution_date = None if 'execution_date' in data and data['execution_date'] is not None: execution_date = data['execution_date'] # Convert string datetime into actual datetime try: execution_date = timezone.parse(execution_date) except ValueError: error_message = ( 'Given execution date, {}, could not be identified ' 'as a date. Example date format: 2015-11-16T14:34:15+00:00' .format(execution_date)) log.info(error_message) response = jsonify({'error': error_message}) response.status_code = 400 return response replace_microseconds = (execution_date is None) if 'replace_microseconds' in data: replace_microseconds = to_boolean(data['replace_microseconds']) try: dr = trigger.trigger_dag(dag_id, run_id, conf, execution_date, replace_microseconds) except AirflowException as err: log.error(err) response = jsonify(error="{}".format(err)) response.status_code = err.status_code return response if getattr(g, 'user', None): log.info("User %s created %s", g.user, dr) response = jsonify( message="Created {}".format(dr), execution_date=dr.execution_date.isoformat(), run_id=dr.run_id ) return response
def __init__(self, conn_id: str = default_conn_name) -> None: super().__init__() connection = self.get_connection(conn_id) self.connection = connection connection_prefix = 'http' # connection.extra contains info about using https (true) or http (false) if to_boolean(connection.extra): connection_prefix = 'https' url = f'{connection_prefix}://{connection.host}:{connection.port}' self.log.info('Trying to connect to %s', url) self.jenkins_server = jenkins.Jenkins(url, connection.login, connection.password)
def _try_to_boolean(value: Any): if isinstance(value, (str, type(None))): return to_boolean(value) return value
def _get_conn_params(self) -> Dict[str, Optional[str]]: """ One method to fetch connection params as a dict used in get_uri() and get_connection() """ conn = self.get_connection( self.snowflake_conn_id) # type: ignore[attr-defined] account = conn.extra_dejson.get('extra__snowflake__account', '') or conn.extra_dejson.get( 'account', '') warehouse = conn.extra_dejson.get('extra__snowflake__warehouse', '') or conn.extra_dejson.get( 'warehouse', '') database = conn.extra_dejson.get('extra__snowflake__database', '') or conn.extra_dejson.get( 'database', '') region = conn.extra_dejson.get('extra__snowflake__region', '') or conn.extra_dejson.get( 'region', '') role = conn.extra_dejson.get('extra__snowflake__role', '') or conn.extra_dejson.get('role', '') schema = conn.schema or '' authenticator = conn.extra_dejson.get('authenticator', 'snowflake') session_parameters = conn.extra_dejson.get('session_parameters') insecure_mode = to_boolean( conn.extra_dejson.get('extra__snowflake__insecure_mode', conn.extra_dejson.get('insecure_mode', None))) conn_config = { "user": conn.login, "password": conn.password or '', "schema": self.schema or schema, "database": self.database or database, "account": self.account or account, "warehouse": self.warehouse or warehouse, "region": self.region or region, "role": self.role or role, "authenticator": self.authenticator or authenticator, "session_parameters": self.session_parameters or session_parameters, # application is used to track origin of the requests "application": os.environ.get("AIRFLOW_SNOWFLAKE_PARTNER", "AIRFLOW"), } if insecure_mode: conn_config['insecure_mode'] = insecure_mode # If private_key_file is specified in the extra json, load the contents of the file as a private # key and specify that in the connection configuration. The connection password then becomes the # passphrase for the private key. If your private key file is not encrypted (not recommended), then # leave the password empty. private_key_file = conn.extra_dejson.get('private_key_file') if private_key_file: with open(private_key_file, "rb") as key: passphrase = None if conn.password: passphrase = conn.password.strip().encode() p_key = serialization.load_pem_private_key( key.read(), password=passphrase, backend=default_backend()) pkb = p_key.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption(), ) conn_config['private_key'] = pkb conn_config.pop('password', None) return conn_config