forked from Sysdeploy/idlers-agent
Update server if already exists with same hostname
This commit is contained in:
parent
96e2328d5d
commit
d30d6418e1
89
main.py
89
main.py
|
@ -82,25 +82,31 @@ def create_post_data():
|
||||||
}
|
}
|
||||||
return post_data
|
return post_data
|
||||||
|
|
||||||
|
def get_existing_servers(host, api_key):
|
||||||
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
|
|
||||||
headers = {
|
headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': 'Bearer ' + api_key,
|
'Authorization': 'Bearer ' + api_key,
|
||||||
}
|
}
|
||||||
|
|
||||||
# Create the data for the post request
|
try:
|
||||||
post_data = create_post_data()
|
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)
|
payload = json.dumps(post_data)
|
||||||
|
|
||||||
# Send the POST request
|
# Send the POST request
|
||||||
|
@ -118,5 +124,62 @@ def send_post_request():
|
||||||
else:
|
else:
|
||||||
print(response.text)
|
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__':
|
if __name__ == '__main__':
|
||||||
send_post_request()
|
send_post_request()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user