Is there a way to create/modify connections through Airflow API

前端 未结 3 1881
醉酒成梦
醉酒成梦 2020-12-05 04:58

Going through Admin -> Connections, we have the ability to create/modify a connection\'s params, but I\'m wondering if I can do the same through API so I can

3条回答
  •  醉梦人生
    2020-12-05 05:54

    First check if connection exists, after create new Connection using from airflow.models import Connection :

    def create_conn(conn_id, conn_type, host, login, password, port):
        conn = Connection(
            conn_id=conn_id,
            conn_type=conn_type,
            host=host,
            login=login,
            password=password,
            port=port
        )
        session = settings.Session()
        conn_name = session\
        .query(Connection)\
        .filter(Connection.conn_id == conn.conn_id)\
        .first()
    
        if str(conn_name) == str(conn_id):
            return logging.info(f"Connection {conn_id} already exists")
    
        session.add(conn)
        session.commit()
        logging.info(Connection.log_info(conn))
        logging.info(f'Connection {conn_id} is created')
    

提交回复
热议问题