import requests
import json
import time
import datetime as dt
import jwt
import os
heading = """
██ ██ ██████ ████████ ██ ██ ██████ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██████ ██ ████ ██ ███████
██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ██ ██████ ███████
"""
def read_file(path):
try:
with open(path, 'r') as f:
return json.loads(f.read())
except FileNotFoundError:
print(path, "Not found")
exit(-1)
except:
print('Error: something went wrong')
exit(-1)
# main
print(heading, end='\n\n\n')
filePath = input('Enter the fileName of json file that have API keys (store json file in current folder)\n')
data = read_file(path=os.getcwd() + "\\" + filePath)
seconds_to_expiry = 3600
exp = time.time() + seconds_to_expiry
expiry = dt.datetime.fromtimestamp(exp).strftime('%Y-%m-%d %H:%M')
domain = data['domain']
customerId = data['customerId']
key = data['key']
secret = data['secret']
domainSuffix = data['domainSuffix']
token = jwt.encode({'iss':key, 'exp':exp}, secret, algorithm='HS256')
auth = f'Bearer {token}'
creds = {'domain':domain,'customerId':customerId}
creds['Expiration'] = expiry
creds['Authorization'] = auth
hostName = input("Enter the Host Name of asset to be deleted \n")
filters = "{%22hostName%22%3A{%22equals%22%3A%22" + hostName +"%22}}"
print("\nAuth Token is ready")
print("\nCalling API...")
# Format the URL
assetInfoURL = f'https://{domain}{domainSuffix}/public/api/customers/{customerId}/assets?filters={filters}'
response = requests.get(assetInfoURL, headers=creds)
if(response.status_code == 200):
data = response.json()
assetId = data['items'][0]['id']
print("\nSuccess!!!")
print(f"\nAssetId Received: {assetId}")
print("\nDeleting Asset...")
# Format the URL
assetDeletionURL = f'https://{domain}.uptycs.io/public/api/customers/{customerId}/assets/{assetId}'
response = requests.delete(assetDeletionURL, headers=creds)
if(response.status_code == 200):
print("\nAsset Deletion Completed!!!")
else:
print("\nError while deleting asset")
print(response.json())
else:
print("\nError!!!")
print(response.json())