110 lines
3.7 KiB
Python
110 lines
3.7 KiB
Python
from decouple import config
|
|
import yaml
|
|
import requests
|
|
import json
|
|
import pandas as pd
|
|
import sys
|
|
|
|
proxies=dict(http='socks5://localhost:3020', https='socks5://localhost:3020')
|
|
|
|
def get_request(url, headers, proxies):
|
|
try:
|
|
response = requests.get(url, headers=headers, proxies=proxies)
|
|
response.raise_for_status()
|
|
except requests.exceptions.HTTPError as errh:
|
|
return "An Http Error occurred:" + repr(errh)
|
|
except requests.exceptions.ConnectionError as errc:
|
|
return "An Error Connecting to the API occurred:" + repr(errc)
|
|
except requests.exceptions.Timeout as errt:
|
|
return "A Timeout Error occurred:" + repr(errt)
|
|
except requests.exceptions.RequestException as err:
|
|
return "An Unknown Error occurred" + repr(err)
|
|
|
|
return response
|
|
|
|
def post_request(url, headers,body, proxies):
|
|
try:
|
|
response = requests.post(url, body, headers=headers,verify=False, proxies=proxies)
|
|
response.raise_for_status()
|
|
except requests.exceptions.HTTPError as errh:
|
|
return "An Http Error occurred:" + repr(errh)
|
|
except requests.exceptions.ConnectionError as errc:
|
|
return "An Error Connecting to the API occurred:" + repr(errc)
|
|
except requests.exceptions.Timeout as errt:
|
|
return "A Timeout Error occurred:" + repr(errt)
|
|
except requests.exceptions.RequestException as err:
|
|
return "An Unknown Error occurred" + repr(err)
|
|
|
|
return response
|
|
|
|
def put_request(url, headers,body, proxies):
|
|
try:
|
|
response = requests.put(url, body, headers=headers,verify=False, proxies=proxies)
|
|
response.raise_for_status()
|
|
except requests.exceptions.HTTPError as errh:
|
|
return "An Http Error occurred:" + repr(errh)
|
|
except requests.exceptions.ConnectionError as errc:
|
|
return "An Error Connecting to the API occurred:" + repr(errc)
|
|
except requests.exceptions.Timeout as errt:
|
|
return "A Timeout Error occurred:" + repr(errt)
|
|
except requests.exceptions.RequestException as err:
|
|
return "An Unknown Error occurred" + repr(err)
|
|
|
|
return response
|
|
|
|
def CreateToken(DTAPIToken, DTENV,nwzone, dtEnvInfo, tokenname):
|
|
|
|
env = DTENV
|
|
DTAPIToken = DTAPIToken
|
|
|
|
|
|
DTAPIURL = env + "/api/v2/apiTokens"
|
|
|
|
headers = {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Api-Token ' + DTAPIToken
|
|
}
|
|
|
|
|
|
data = '{"name":"' + tokenname + '","scopes":["InstallerDownload","ReadConfig","WriteConfig","DataExport"]}'
|
|
#data = '{"name":"' + tokenname + '","scopes":["ReadConfig","DataExport","entities.read"]}'
|
|
|
|
r = post_request(DTAPIURL,headers,data, proxies)
|
|
print(r)
|
|
row = {'Environment':dtEnvInfo,'DT_API_URL':env +"/api", 'DT_[API|PAAS]_TOKEN':r.json()['token'],'DT_NETWORK_ZONE':nwzone}
|
|
|
|
return row
|
|
#################
|
|
##main
|
|
#################
|
|
|
|
if len(sys.argv) != 2:
|
|
print('createToken.py <filename>')
|
|
sys.exit()
|
|
|
|
with open('./environment.yaml') as file:
|
|
doc = yaml.safe_load(file)
|
|
|
|
df = pd.DataFrame()
|
|
#tokenname="runtime_AKS"
|
|
tokenname=sys.argv[1]
|
|
|
|
for item, doc in doc.items():
|
|
token = dict(doc[2])
|
|
url = dict(doc[1])
|
|
nwzone = dict(doc[3])
|
|
dtEnvInfo = dict(doc[4])
|
|
print("Crawling through: " + item)
|
|
print("Check if token exists in environment...")
|
|
if(config(token.get('env-token-name')) != ""):
|
|
print("Gather data, hold on a minute")
|
|
DTTOKEN = config(token.get('env-token-name'))
|
|
DTURL = url.get('env-url')
|
|
|
|
row = CreateToken(DTTOKEN,DTURL,nwzone.get('network-zone'),dtEnvInfo.get('env-info'),tokenname)
|
|
df = df.append(row,ignore_index=True)
|
|
|
|
else:
|
|
print("token not found, skipping " + item)
|
|
print(df)
|
|
df.to_excel(tokenname+".xlsx") |