This file was earlier written to migrate using API but because lack of metadata, submit does not go through. Now the logic queries direct to database instead.
111 lines
3.6 KiB
Python
111 lines
3.6 KiB
Python
#!/usr/bin/python3
|
|
|
|
import json
|
|
|
|
import psycopg2
|
|
|
|
db_params = {
|
|
'dbname': '',
|
|
'user': '',
|
|
'password': '',
|
|
'host': '',
|
|
'port': ''
|
|
}
|
|
|
|
|
|
def transform_alert_threshold_and_elasticsearch_cpu():
|
|
print("Transforming alert threshold and elasticsearch cpu")
|
|
try:
|
|
conn = psycopg2.connect(**db_params)
|
|
cur = conn.cursor()
|
|
|
|
fetch_query = "SELECT id, data FROM temp_deployment"
|
|
cur.execute(fetch_query)
|
|
records = cur.fetchall()
|
|
|
|
for record in records:
|
|
_id = record[0]
|
|
json_data = record[1]
|
|
save = False
|
|
|
|
print("Checking for id: {}".format(_id))
|
|
|
|
if 'alerts' in json_data:
|
|
alerts = json_data['alerts']
|
|
for alert_type, alert_list in alerts.items():
|
|
for alert in alert_list:
|
|
if isinstance(alert, dict) and 'threshold' in alert and isinstance(
|
|
alert['threshold'], str) and alert['threshold'].isdigit():
|
|
print(
|
|
"Found threshold as string for id: {} at {}".format(_id,
|
|
alert_type))
|
|
save = True
|
|
alert['threshold'] = int(alert['threshold'])
|
|
|
|
if 'elasticSearch' in json_data:
|
|
es = json_data['elasticSearch']
|
|
if 'instance' in es:
|
|
instance = es['instance']
|
|
if 'cpu' in instance and isinstance(
|
|
instance['cpu'], str) and instance['cpu'].isdigit():
|
|
print("Found cpu as string for id: {}".format(_id))
|
|
save = True
|
|
instance['cpu'] = int(instance['cpu'])
|
|
|
|
if save:
|
|
updated_jsonb = json.dumps(json_data)
|
|
update_query = "UPDATE temp_deployment SET data = %s WHERE id = %s"
|
|
cur.execute(update_query, (updated_jsonb, _id))
|
|
|
|
conn.commit()
|
|
except Exception as e:
|
|
print("Error:", str(e))
|
|
finally:
|
|
if conn is not None:
|
|
conn.close()
|
|
|
|
|
|
def transform_disaster_recovery():
|
|
print("Transforming disasterRecovery")
|
|
try:
|
|
conn = psycopg2.connect(**db_params)
|
|
cur = conn.cursor()
|
|
|
|
fetch_query = "SELECT id, metadata FROM temp_manifest where metadata is not null"
|
|
cur.execute(fetch_query)
|
|
records = cur.fetchall()
|
|
|
|
for record in records:
|
|
_id = record[0]
|
|
json_data = record[1]
|
|
save = False
|
|
|
|
print("Checking for id: {}".format(_id))
|
|
|
|
if 'disasterRecovery' in json_data:
|
|
if json_data['disasterRecovery'] == "Yes":
|
|
print("Found disasterRecovery as Yes for id: {}".format(_id))
|
|
save = True
|
|
json_data['disasterRecovery'] = "True"
|
|
|
|
if json_data['disasterRecovery'] == "No":
|
|
print("Found disasterRecovery as No for id: {}".format(_id))
|
|
save = True
|
|
json_data['disasterRecovery'] = "False"
|
|
|
|
if save:
|
|
updated_jsonb = json.dumps(json_data)
|
|
update_query = "UPDATE temp_manifest SET metadata = %s WHERE id = %s"
|
|
cur.execute(update_query, (updated_jsonb, _id))
|
|
|
|
conn.commit()
|
|
except Exception as e:
|
|
print("Error:", str(e))
|
|
finally:
|
|
if conn is not None:
|
|
conn.close()
|
|
|
|
|
|
transform_alert_threshold_and_elasticsearch_cpu()
|
|
transform_disaster_recovery()
|