cd_e2e_monitoring_config/tools/makeDashboardsPublic/makeDashboardPublic.py

91 lines
2.3 KiB
Python

import os, requests, time, json, sys, logging, configparser
from datetime import datetime, timedelta
from http.cookies import SimpleCookie
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 update():
print(headers)
print(YOUR_DT_API_URL)
r = requests.get(''.join(YOUR_DT_API_URL) + '/api/config/v1/dashboards', headers=headers, cookies=dict(cookies), verify=False);
print(r.text)
dashboards = json.loads(r.text)
i = 0
while i<len(dashboards["dashboards"]):
print(dashboards["dashboards"][i]['id'])
data={
"id": dashboards["dashboards"][i]['id'],
"enabled": True,
"published": True,
"permissions": [
{
"type": "ALL",
"permission": "VIEW"
}
],
"publicAccess": {
"managementZoneIds": [],
"urls": {}
}
}
data = json.dumps(data,indent=4)
if dashboards["dashboards"][i]['owner'] == 'Ignacio.Goldman@partner.bmwgroup.com':
r = requests.put(''.join(YOUR_DT_API_URL) + '/api/config/v1/dashboards/'+dashboards["dashboards"][i]['id']+'/shareSettings', data, headers=headers, cookies=dict(cookies), verify=False);
print(r)
i+=1
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')
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)
update()
print("\n")