Update server if already exists with same hostname

This commit is contained in:
Swapnil 2024-06-03 13:01:50 +05:30
parent 96e2328d5d
commit d30d6418e1
Signed by untrusted user: swapnil
GPG Key ID: 58029C48BB100574

89
main.py
View File

@ -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()