From d30d6418e154d93f3068e430935dfd984587350b Mon Sep 17 00:00:00 2001 From: Swapnil Date: Mon, 3 Jun 2024 13:01:50 +0530 Subject: [PATCH] Update server if already exists with same hostname --- main.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index a61e3c4..841c68e 100644 --- a/main.py +++ b/main.py @@ -82,25 +82,31 @@ def create_post_data(): } return post_data - -def send_post_request(): - # Get the API key and HOST from environment variables - api_key = os.getenv('API_KEY') - host = os.getenv('HOST') - if not api_key: - raise Exception('API_KEY not found in environment variables') - if not host: - raise Exception('HOST not found in environment variables') - - # Create the headers for the request +def get_existing_servers(host, api_key): headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + api_key, } - # Create the data for the post request - post_data = create_post_data() + try: + response = requests.get(host, headers=headers) + response.raise_for_status() + return response.json() + except: + return [] +def existing_server_id(existing_servers, post_data): + for server in existing_servers: + if server['hostname'] == post_data['hostname']: + return server['id'] + return None + +def create_server(host, api_key, post_data): + # Create the headers for the request + headers = { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + api_key, + } payload = json.dumps(post_data) # Send the POST request @@ -118,5 +124,62 @@ def send_post_request(): else: print(response.text) +def update_server(host, api_key, post_data, server_id): + headers = { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + api_key, + } + + # remove following keys from post_data + # ssh_port, ip1, currency, price, payment_term, next_due_date + post_data.pop('ssh_port', None) + post_data.pop('ip1', None) + post_data.pop('currency', None) + post_data.pop('price', None) + post_data.pop('payment_term', None) + post_data.pop('next_due_date', None) + + payload = json.dumps(post_data) + + try: + response = requests.put(host + '/' + str(server_id), headers=headers, data=payload) + response.raise_for_status() + except requests.exceptions.HTTPError as errh: + print ("Http Error:",errh) + except requests.exceptions.ConnectionError as errc: + print ("Error Connecting:",errc) + except requests.exceptions.Timeout as errt: + print ("Timeout Error:",errt) + except requests.exceptions.RequestException as err: + print ("Something went wrong:",err) + else: + print(response.text) + +def send_post_request(): + # Get the API key and HOST from environment variables + api_key = os.getenv('API_KEY') + host = os.getenv('HOST') + if not api_key: + raise Exception('API_KEY not found in environment variables') + if not host: + raise Exception('HOST not found in environment variables') + + # Create the data for the post request + post_data = create_post_data() + + # Get the existing servers + existing_servers = get_existing_servers(host, api_key) + + # Check if the server already exists + server_id = existing_server_id(existing_servers, post_data) + + # If the server exists, update it + if server_id: + print('Server already exists with id: {}, Updating...'.format(server_id)) + update_server(host, api_key, post_data, server_id) + else: + print('Server does not exist, Creating...') + create_server(host, api_key, post_data) + if __name__ == '__main__': send_post_request()