cd_e2e_monitoring_config/tools/providerBreakdown/providerBreakdown.py

167 lines
4.7 KiB
Python

import os, requests, time, json, sys, logging, configparser
from datetime import datetime, timedelta
from http.cookies import SimpleCookie
from pathlib import Path
def checkAPIlimit():
global number_of_calls
print("API calls: ", str(number_of_calls+1))
if number_of_calls == 59:
print("Sleeping")
time.sleep(60)
number_of_calls=0
def saveValues():
YOUR_DT_API_URL = tenant_info.split(" ")[0].rstrip('\n')
YOUR_DT_API_TOKEN = tenant_info.split(" ")[1].rstrip('\n')
return YOUR_DT_API_URL, YOUR_DT_API_TOKEN
def getList():
print('Iterate through list? y/n')
iterate = input()
if iterate == 'y':
print('Enter the key of the object:')
jsonKey = input()
else:
jsonKey = None
return iterate, jsonKey
def download():
#Create /data folder
endpointLast = endpoint
if "/" in endpoint:
endpointLast = endpoint.split("/")[-1]
directory="data/"+env_id+"/"+endpointLast
Path(directory).mkdir(parents=True, exist_ok=True)
print(''.join(YOUR_DT_API_URL) + endpoint)
r = requests.get(''.join(YOUR_DT_API_URL) + endpoint, headers=headers, cookies=dict(cookies), verify=False);
data_json = json.loads(r.text)
iterate = False
for key in data_json.keys():
if type(data_json[key]) is list:
if "id" in data_json[key][0]:
iterate = True
iterative_key = key
if iterate == True:
i=0
while i<len(data_json[iterative_key]):
id_rule = data_json[iterative_key][i]['id']
rule = requests.get(''.join(YOUR_DT_API_URL) + endpoint +"/"+id_rule, headers=headers, cookies=dict(cookies), verify=False);
f = open(directory+"/"+id_rule+".json", "w")
json_data = json.loads(rule.text)
json_formatted_str = json.dumps(json_data, indent=2)
f.write(json_formatted_str)
print(rule.text)
i+=1
else:
f = open(directory+"/"+endpointLast+".json", "w")
json_data = json.loads(r.text)
json_formatted_str = json.dumps(json_data, indent=2)
f.write(json_formatted_str)
def update():
endpointLast = endpoint
if "/" in endpoint:
endpointLast = endpoint.split("/")[-1]
directory = "data/"+env_id+"/"+endpointLast
r = requests.get(''.join(YOUR_DT_API_URL) + endpoint, headers=headers, cookies=dict(cookies), verify=False);
print(r.text)
for filename in os.listdir(directory):
with open(os.path.join(directory, filename), 'r+') as myfile:
data=myfile.read()
json_data = json.loads(data)
#Single json / no iterative
if filename == endpointLast+'.json':
r = requests.put(''.join(YOUR_DT_API_URL) + endpoint, data, headers=headers, cookies=dict(cookies), verify=False)
print(r)
else:
#Iterative through id
if "id" in json_data:
# PUT - update
r = requests.put(''.join(YOUR_DT_API_URL) + endpoint+"/"+filename.split(".")[0], data, headers=headers, cookies=dict(cookies), verify=False)
print(r)
else:
# POST - create
# Creates the rule
r = requests.post(''.join(YOUR_DT_API_URL) + endpoint, data, headers=headers, cookies=dict(cookies), verify=False)
print(r)
# Add the id to the json
json_data["id"] = json.loads(r.text)["id"]
myfile.seek(0)
json_formatted_str = json.dumps(json_data, indent=2)
myfile.write(json_formatted_str)
myfile.truncate()
# Rename the file with the id
os.rename(os.getcwd()+"/"+directory+"/"+filename,os.getcwd()+"/"+directory+"/"+json.loads(r.text)["id"]+".json")
if __name__ == '__main__':
number_of_calls = 0
YOUR_DT_API_URL = ''
YOUR_DT_API_TOKEN = ''
cookies = {"null":"null"}
user = "null"
config = configparser.ConfigParser()
config.read('config.ini')
updateConfig = config['ACTION']['updateConfig']
downloadConfig = config['ACTION']['downloadConfig']
endpoint = config['ENDPOINT']['endpoint']
endpoint = endpoint.rstrip("\n")
for (key, val) in config.items('TENANTS'):
tenant_info = val
extracted_tenant_info = saveValues()
YOUR_DT_API_URL = extracted_tenant_info[0]
TOKEN = extracted_tenant_info[1]
YOUR_DT_API_TOKEN = os.getenv(TOKEN)
#Build the header and cookie
headers={}
headers["Authorization"] = "Api-Token "+YOUR_DT_API_TOKEN
headers["Content-Type"] = "application/json"
#Generate env_id
if "/e/" in YOUR_DT_API_URL:
env_id = YOUR_DT_API_URL.split("/e/")[1]
else:
env_id = YOUR_DT_API_URL.split("//")[1]
#Create /data folder
if not os.path.exists('data/'):
os.mkdir('data/')
#Create tenant folder
path = "data/"+env_id
if not os.path.exists(path):
os.mkdir(path)
print("\n")
if downloadConfig=='1':
print("Downloading rules of "+ env_id)
download()
if updateConfig=='1':
print("Updating rules of "+ env_id)
update()
print("\n")