1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330 | try:
from airflow import DAG
from datetime import timedelta
from datetime import datetime
from airflow import settings
from airflow.models import Connection, Variable
from airflow.operators.python_operator import PythonOperator
from airflow.hooks.base import BaseHook
except Exception as e:
print("Error {} ".format(e))
# Postgres RDS Connection Configuration
conns = [
{
"conn_id": "postgres_rds",
"conn_type": "postgres",
"host": "drowdatewarehouse.crlwwhgepgi7.ap-east-1.rds.amazonaws.com",
"schema": "drowDateWareHouse",
"login": "dRowAdmin",
"password": "drowsuper",
"port": 5432
}
]
# Airflow Variables Configuration
variables = [
# Example:
# {
# "key": "example_var",
# "value": "example_value",
# "description": "Example variable description"
# }
]
def clear_error_connections():
"""Clear connections that have encryption errors"""
session = settings.Session()
try:
print("Checking for erroneous connections...")
error_conns = session.query(Connection).filter(Connection.is_encrypted == True).all()
print(f"Found {len(error_conns)} connections with encryption flag")
for conn in error_conns:
session.delete(conn)
print(f"Deleted erroneous connection: {conn.conn_id}")
session.commit()
print("Error connections cleared.")
except Exception as e:
print(f"Error clearing connections: {e}")
session.rollback()
finally:
session.close()
def validate_connections():
"""Test all connections and identify invalid ones"""
session = settings.Session()
invalid_connections = []
valid_connections = []
try:
print("=" * 80)
print("VALIDATING ALL AIRFLOW CONNECTIONS")
print("=" * 80)
all_connections = session.query(Connection).all()
print(f"\nTotal connections found: {len(all_connections)}\n")
for conn in all_connections:
conn_id = conn.conn_id
conn_type = conn.conn_type
print(f"Testing connection: '{conn_id}' (type: {conn_type})")
try:
# Try to get the connection using BaseHook
hook_conn = BaseHook.get_connection(conn_id)
# Attempt to test the connection based on type
if conn_type == 'postgres':
# Try to import and test PostgreSQL connection
try:
import psycopg2
conn_uri = hook_conn.get_uri()
test_conn = psycopg2.connect(
host=hook_conn.host,
port=hook_conn.port,
user=hook_conn.login,
password=hook_conn.password,
database=hook_conn.schema,
connect_timeout=10
)
test_conn.close()
print(f" ✓ Connection '{conn_id}' is VALID")
valid_connections.append({
'conn_id': conn_id,
'conn_type': conn_type,
'host': hook_conn.host,
'port': hook_conn.port,
'schema': hook_conn.schema
})
except ImportError:
print(f" ⚠ Cannot test '{conn_id}': psycopg2 not installed")
valid_connections.append({
'conn_id': conn_id,
'conn_type': conn_type,
'host': hook_conn.host,
'status': 'untested - missing driver'
})
except Exception as e:
print(f" ✗ Connection '{conn_id}' is INVALID: {str(e)}")
invalid_connections.append({
'conn_id': conn_id,
'conn_type': conn_type,
'host': hook_conn.host,
'port': hook_conn.port,
'schema': hook_conn.schema,
'error': str(e)
})
elif conn_type == 'mysql':
try:
import pymysql
test_conn = pymysql.connect(
host=hook_conn.host,
port=hook_conn.port or 3306,
user=hook_conn.login,
password=hook_conn.password,
database=hook_conn.schema,
connect_timeout=10
)
test_conn.close()
print(f" ✓ Connection '{conn_id}' is VALID")
valid_connections.append({
'conn_id': conn_id,
'conn_type': conn_type,
'host': hook_conn.host,
'port': hook_conn.port,
'schema': hook_conn.schema
})
except ImportError:
print(f" ⚠ Cannot test '{conn_id}': pymysql not installed")
valid_connections.append({
'conn_id': conn_id,
'conn_type': conn_type,
'status': 'untested - missing driver'
})
except Exception as e:
print(f" ✗ Connection '{conn_id}' is INVALID: {str(e)}")
invalid_connections.append({
'conn_id': conn_id,
'conn_type': conn_type,
'host': hook_conn.host,
'error': str(e)
})
else:
# For other connection types, just check if they can be retrieved
print(f" ⚠ Connection '{conn_id}' type '{conn_type}' - basic validation only")
valid_connections.append({
'conn_id': conn_id,
'conn_type': conn_type,
'status': 'exists - detailed test not implemented'
})
except Exception as e:
print(f" ✗ Connection '{conn_id}' ERROR: {str(e)}")
invalid_connections.append({
'conn_id': conn_id,
'conn_type': conn_type,
'error': str(e)
})
# Print summary
print("\n" + "=" * 80)
print("VALIDATION SUMMARY")
print("=" * 80)
print(f"\n✓ Valid/Testable Connections: {len(valid_connections)}")
for conn in valid_connections:
status = conn.get('status', 'VALID')
print(f" - {conn['conn_id']} ({conn['conn_type']}) - {status}")
print(f"\n✗ Invalid/Problematic Connections: {len(invalid_connections)}")
if invalid_connections:
print("\n⚠️ ATTENTION: The following connections have issues:\n")
for conn in invalid_connections:
print(f" - Connection ID: {conn['conn_id']}")
print(f" Type: {conn['conn_type']}")
if 'host' in conn:
print(f" Host: {conn.get('host', 'N/A')}")
print(f" Error: {conn['error']}")
print()
else:
print(" (None found)")
print("=" * 80)
# Store invalid connections for future reference
if invalid_connections:
Variable.set(
"invalid_connections_list",
str(invalid_connections),
description="List of connections that failed validation"
)
print(f"\n📝 Invalid connections list saved to Airflow Variable 'invalid_connections_list'")
except Exception as e:
print(f"Error in validate_connections: {e}")
finally:
session.close()
def create_or_update_connections():
"""Create new connections or update existing ones based on configuration"""
session = settings.Session()
try:
for conn in conns:
try:
existing_conn = session.query(Connection).filter(
Connection.conn_id == conn["conn_id"]
).first()
if not existing_conn:
# Create new connection
new_conn = Connection(
conn_id=conn["conn_id"],
conn_type=conn["conn_type"],
host=conn.get("host"),
schema=conn.get("schema"),
login=conn.get("login"),
password=conn.get("password"),
port=conn.get("port"),
extra=conn.get("extra")
)
session.add(new_conn)
print(f"✓ Connection '{conn['conn_id']}' created.")
else:
# Update existing connection
existing_conn.conn_type = conn["conn_type"]
existing_conn.host = conn.get("host")
existing_conn.schema = conn.get("schema")
existing_conn.login = conn.get("login")
existing_conn.password = conn.get("password")
existing_conn.port = conn.get("port")
if "extra" in conn:
existing_conn.extra = conn.get("extra")
print(f"✓ Connection '{conn['conn_id']}' updated.")
except Exception as e:
print(f"✗ Error managing connection '{conn.get('conn_id', 'unknown')}': {e}")
session.rollback()
continue
session.commit()
print(f"Successfully processed {len(conns)} connection(s).")
except Exception as e:
print(f"Error in create_or_update_connections: {e}")
session.rollback()
finally:
session.close()
def create_or_update_variables():
"""Create new variables or update existing ones based on configuration"""
try:
for var in variables:
try:
key = var["key"]
value = var["value"]
description = var.get("description", "")
# Check if variable exists
existing_var = Variable.get(key, default_var=None)
if existing_var is None:
# Create new variable
Variable.set(key, value, description=description)
print(f"✓ Variable '{key}' created.")
else:
# Update existing variable
Variable.set(key, value, description=description)
print(f"✓ Variable '{key}' updated.")
except Exception as e:
print(f"✗ Error managing variable '{var.get('key', 'unknown')}': {e}")
continue
print(f"Successfully processed {len(variables)} variable(s).")
except Exception as e:
print(f"Error in create_or_update_variables: {e}")
# Execute the function once everyday
with DAG(
dag_id='manage_connections_and_variables',
schedule_interval='@daily',
default_args={
"owner": "airflow",
"retries": 1,
"retry_delay": timedelta(minutes=5),
"start_date": datetime(2023, 1, 17)
},
catchup=False,
description='Manage Airflow Connections and Variables from configuration'
) as dag:
clear_error_connections_task = PythonOperator(
task_id='clear_error_connections',
python_callable=clear_error_connections,
op_kwargs={},
)
validate_connections_task = PythonOperator(
task_id='validate_connections',
python_callable=validate_connections,
op_kwargs={},
)
create_or_update_connections_task = PythonOperator(
task_id='create_or_update_connections',
python_callable=create_or_update_connections,
op_kwargs={},
)
create_or_update_variables_task = PythonOperator(
task_id='create_or_update_variables',
python_callable=create_or_update_variables,
op_kwargs={},
)
# Define task dependencies
# First clear errors, then create/update connections and variables in parallel, then validate
clear_error_connections_task >> [create_or_update_connections_task, create_or_update_variables_task] >> validate_connections_task
|