from decouple import config import yaml import requests import json import pandas as pd def get_request(url, headers): try: response = requests.get(url, headers=headers) 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): try: response = requests.post(url, body, headers=headers,verify=False) 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): try: response = requests.put(url, body, headers=headers,verify=False) 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 GatherReportingInfo(DTAPIToken, DTENV,friendlyName): reportingFolder = config('REPORTING_FOLDER') env = DTENV DTAPIToken = DTAPIToken DTAPIURL= env + "/api/config/v1/dashboards" headers = { 'Content-Type': 'application/json', 'Authorization': 'Api-Token ' + DTAPIToken } #r = requests.get(DTAPIURL,headers=headers) r = get_request(DTAPIURL,headers) df = pd.DataFrame() mgmt = pd.DataFrame() dashboards = [] for dashboard in r.json()['dashboards']: if dashboard['owner'] == 'Ignacio.Goldman@partner.bmwgroup.com': DTAPIURL= env + "/api/config/v1/dashboards/" + dashboard['id']+ '/shareSettings' #response = requests.get(DTAPIURL, headers=headers) data={ "id": dashboard['id'], "enabled": True, "preset": True, "permissions": [ { "type": "ALL", "permission": "VIEW" } ], "publicAccess": { "managementZoneIds": [], "urls": {} } } data = json.dumps(data,indent=4) r = put_request(DTAPIURL,headers, data) with open('./environment.yaml') as file: doc = yaml.safe_load(file) for item, doc in doc.items(): token = dict(doc[2]) url = dict(doc[1]) 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') GatherReportingInfo(DTTOKEN,DTURL,item) else: print("token not found, skipping " + item)