Ravi
Ravi

Reputation: 863

is there a way to test airflow connection from CLI or by a custom dag

We are using airflow 2.1 version and we have multiple connections(Linux/Non Linux based OS) defined in Airflow.

We created a dag by exporting all the values from connection metadata table and running a dummy bash command

session = airflow.settings.Session()

t1 = session.query(Connection)

But because of different OS we are facing some errors. Is there any generic way through CLI or any other way we can create a dag and then test all the connections and if it fails we want to get notified .

Upvotes: 1

Views: 1571

Answers (1)

Elad Kalif
Elad Kalif

Reputation: 16109

Airflow has the ability to test connection assuming the hook implemented test_connection function (see docs). You can test the connection from the UI in Admin -> Connections -> Choose the specific connection:

enter image description here

Or by using python code :

hook = YourHook(conn_id='your_conn_id')
status, msg = hook.test_connection()
assert status is True
assert msg == 'Connection successfully tested'

Upvotes: 2

Related Questions