Files
deployment-portal-be/scripts/amc_manifest_migration.py

65 lines
2.4 KiB
Python

#!/usr/bin/python3
import requests
url_to_fetch = "" # FILL ME
url_to_post = "" # FILL ME
get_cookie = "" # FILL ME
get_x_xsrf_token = "" # FILL ME
post_cookie = "" # FILL ME
post_x_xsrf_token = "" # FILL ME
get_manifest = requests.Session()
get_manifest.headers.update({'cookie': get_cookie, 'x-xsrf-token': get_x_xsrf_token})
post_manifest = requests.Session()
post_manifest.headers.update({'cookie': post_cookie, 'x-xsrf-token': post_x_xsrf_token})
ids = [] # FILL ME
def remove_version_id_keys(data):
if isinstance(data, dict):
return {key: remove_version_id_keys(value) for key, value in data.items() if
key not in ['version', 'id']}
elif isinstance(data, list):
return [remove_version_id_keys(item) for item in data]
else:
return data
def replace_key(data, key_to_replace, value_to_replace, replace_with):
if isinstance(data, dict):
return {
key: replace_key(value, key_to_replace, value_to_replace, replace_with) if isinstance(
value, (dict, list))
else (
value.replace(value_to_replace, replace_with) if isinstance(value,
str) and key == key_to_replace
else replace_with if value == value_to_replace and key == key_to_replace and isinstance(
value, type(value_to_replace))
else value
)
for key, value in data.items()
}
elif isinstance(data, list):
return [replace_key(item, key_to_replace, value_to_replace, replace_with) for item in data]
else:
return data
replacements = [
('product', 'lending', 'amc'),
('infraVertical', 'lending', 'amc'),
('cluster', 'nonprod.np.navi-tech.in', 'aps1.np.navi-amc.in'),
('endpoint', 'np.navi-tech.in', 'np.navi-amc.in'),
('isDeployed', True, False)
]
for id in ids:
r = get_manifest.get(url_to_fetch + id)
get_manifest_response = r.json()
manifest_data_to_post = remove_version_id_keys(get_manifest_response)
for key, old_value, new_value in replacements:
manifest_data_to_post = replace_key(manifest_data_to_post, key, old_value, new_value)
post_manifest_response = post_manifest.post(f"{url_to_post}", json=manifest_data_to_post)
print(f'manifestId:{id} status code {post_manifest_response.status_code}')