custom scripts/tools

custom-service-JONYR
qxz15oi 2021-05-06 16:03:38 +02:00
parent cc7351f509
commit 36dd856a48
19 changed files with 1081 additions and 0 deletions

View File

@ -0,0 +1,17 @@
### makeDashboardsPublic
- After creating a dashboard with Monaco, it may not be visible.
- The visibility of the dashboard is not defined within the JSON. There's a separate endpoint to configure the visibility of it.
- This script iterates through all dashboard in all tenants (the ones created by Monaco, so just the ones with ignacio.goldman@partner.bmw.com as the owner)
making them public.
- It's recommended to execute this script after an execution of Monaco
#### Get Started
1. Configure the tenants under the file *config.ini [TENANTS]* using the format described:
`tenant1 = managed-env token`
`tenant2 = saas-env token`
- You can list as many tenants as you want.
4. Execute the script:
`python makeDashboardsPublic.py`

View File

@ -0,0 +1,7 @@
[TENANTS]
#tenant1 = https://dynatracemgd-cn.bmwgroup.net/e/b921f1b9-c00e-4031-b9d1-f5a0d530757b CN_PreProd_TOKEN
#tenant2 = https://dynatracemgd-cn.bmwgroup.net/e/ab88c03b-b7fc-45f0-9115-9e9ecc0ced35 CN_Prod_TOKEN
#tenant3 = https://xxu26128.live.dynatrace.com xxu26128_TOKEN
#tenant4 = https://wgv50241.live.dynatrace.com wgv50241_TOKEN
#tenant5 = https://qqk70169.live.dynatrace.com qqk70169_TOKEN
tenant6 = https://onb44935.live.dynatrace.com onb44935_TOKEN

View File

@ -0,0 +1,90 @@
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")

View File

@ -0,0 +1,54 @@
### Provider Breakdown Configuration
- Dynatrace automatically recognizes more than 1,000 3rd-party content providers, including Google, Amazon, Twitter, and LinkedIn.
Typical 3rd-party content includes Facebook and Twitter widgets, gravatars, and similar resources.
It's worth monitoring this type of content to know exactly how it affects your applications and to understand if such content is the root cause of any page slow-downs.
- This extesions allows you to set up rules that define how your applications' downloaded content resources (images, CSS, 3rd party widgets, and more) are displayed
and categorized for analysis.
- This extension allows you to keep the configuration consistent accross all tenants
#### Download configuration
1. Configure *config.ini* file:
```
downloadConfig = 1
updateConfig = 0
```
2. Run the script:
```
python providerBreakdown.py
```
3. The configuration will appear under */data/<tenant>/contentResources/contentResources.json*
#### Create configuration
*Use case: A team wants to create a new provider breakdown rule*
1. Configure *config.ini* file:
```
downloadConfig = 0
updateConfig = 1
```
2. Edit the file */data/<tenant>/contentResources/contentResources.json* adding the desired rule. For example:
```
{
"resourceName": "OneLogin (Prod)",
"resourceType": "FIRST_PARTY_RESOURCES",
"brandIconUrl": "https://empty",
"domainNamePatterns": [
"customer.bmwgroup.com"
]
}
```
3. Run the script:
```
python providerBreakdown.py
```
#### Delete configuration
1. Same process as before:
- access the JSON
- delete the target rule
- run the update

View File

@ -0,0 +1,14 @@
[ENDPOINT]
endpoint = /api/config/v1/contentResources
[ACTION]
downloadConfig = 0
updateConfig = 1
[TENANTS]
#tenant1 = https://dynatracemgd-cn.bmwgroup.net/e/b921f1b9-c00e-4031-b9d1-f5a0d530757b CN_PreProd_TOKEN
#tenant2 = https://dynatracemgd-cn.bmwgroup.net/e/ab88c03b-b7fc-45f0-9115-9e9ecc0ced35 CN_Prod_TOKEN
tenant3 = https://xxu26128.live.dynatrace.com xxu26128_TOKEN
tenant4 = https://wgv50241.live.dynatrace.com wgv50241_TOKEN
tenant5 = https://qqk70169.live.dynatrace.com qqk70169_TOKEN
tenant6 = https://onb44935.live.dynatrace.com onb44935_TOKEN

View File

@ -0,0 +1,14 @@
{
"resourceProviders": [
{
"resourceName": "Countly NA",
"resourceType": "FIRST_PARTY_RESOURCES",
"brandIconUrl": null,
"domainNamePatterns": [
"api.countly-preprod.bmwusa.com"
]
}
],
"resourceUrlCleanupRules": null,
"resourceTypes": null
}

View File

@ -0,0 +1,22 @@
{
"resourceProviders": [
{
"resourceName": "Countly ROW",
"resourceType": "FIRST_PARTY_RESOURCES",
"brandIconUrl": null,
"domainNamePatterns": [
"api.countly-preprod.bmwgroup.com"
]
},
{
"resourceName": "hereapi.com - Vector Tile API",
"resourceType": "THIRD_PARTY_RESOURCES",
"brandIconUrl": "https://www.here.com/themes/custom/here_com_theme/favicon.ico",
"domainNamePatterns": [
"vector.hereapi.com"
]
}
],
"resourceUrlCleanupRules": null,
"resourceTypes": null
}

View File

@ -0,0 +1,14 @@
{
"resourceProviders": [
{
"resourceName": "Countly NA",
"resourceType": "FIRST_PARTY_RESOURCES",
"brandIconUrl": null,
"domainNamePatterns": [
"api.countly.bmwusa.com"
]
}
],
"resourceUrlCleanupRules": null,
"resourceTypes": null
}

View File

@ -0,0 +1,54 @@
{
"resourceProviders": [
{
"resourceName": "OneLogin (Prod)",
"resourceType": "FIRST_PARTY_RESOURCES",
"brandIconUrl": "https://empty",
"domainNamePatterns": [
"customer.bmwgroup.com"
]
},
{
"resourceName": "OneLogin (Int)",
"resourceType": "FIRST_PARTY_RESOURCES",
"brandIconUrl": "https://empty",
"domainNamePatterns": [
"customer-i.bmwgroup.com"
]
},
{
"resourceName": "CoCo API",
"resourceType": "FIRST_PARTY_RESOURCES",
"brandIconUrl": "https://empty",
"domainNamePatterns": [
"cocoapi.bmwgroup.com"
]
},
{
"resourceName": "Countly ROW",
"resourceType": "FIRST_PARTY_RESOURCES",
"brandIconUrl": "https://empty",
"domainNamePatterns": [
"api.countly.bmwgroup.com"
]
},
{
"resourceName": "bmwgroup.com",
"resourceType": "FIRST_PARTY_RESOURCES",
"brandIconUrl": "https://empty",
"domainNamePatterns": [
"bmwgroup.com"
]
},
{
"resourceName": "hereapi.com - Vector Tile API",
"resourceType": "THIRD_PARTY_RESOURCES",
"brandIconUrl": "https://www.here.com/themes/custom/here_com_theme/favicon.ico",
"domainNamePatterns": [
"vector.hereapi.com"
]
}
],
"resourceUrlCleanupRules": null,
"resourceTypes": null
}

View File

@ -0,0 +1,165 @@
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
if iterate == True:
i=0
while i<len(data_json['values']):
id_rule = data_json['values'][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")

View File

@ -0,0 +1,78 @@
### Service Detection Rules Configuration
**Important: just for Dynatrace Experts. Test the rule first in a PreProd environment to validate it's behavior**
- Enables you to manage the configuration of service detection rules.
- Split/Merge services based on specific rules
#### Download configuration
1. Configure *config.ini* file:
```
downloadConfig = 1
updateConfig = 0
```
2. Run the script:
```
python serviceDetectionApi.py
```
3. The configuration will appear under */data/<tenant>/FULL_WEB_REQUEST/<rule_id>.json*
#### Create configuration
*Use case: ServiceA has a KeyRequestA used in the SLO dashboard. After a new deployment, the team changed the web server name of their application (ServiceA)*
*causing that Dynatrace detects the service as a new service (ServiceB). This new service doesn't have any keyRequest defined, so the SLO dashboard results incomplete*
*To fix this situation (or others such as changes in ports), you can create service detection rules to merge services with differences in their metadata*
*You can also use these rules to further split a service*
1. Configure *config.ini* file:
```
deleteConfig = 0
downloadConfig = 0
updateConfig = 1
```
2. Copy a rule from */data/<tenant>/FULL_WEB_REQUEST/* and paste it in the same folder. (**Do not change the file name, it will be automatically changed once the rule is created**)
3. Modify the rule as intended.
4. Remove the *id* from the rule and save it
5. Run the script:
```
python serviceDetectionApi.py
```
6. Check if the file has automatically changed the name and if it has automatically added an "id" key (may be at the bottom)
#### Move configuration across tenants
1. Configure *config.ini* file:
```
deleteConfig = 0
downloadConfig = 0
updateConfig = 1
```
2. Copy the *<rule_id>.json* file from tenantA
3. Paste the rule into the tenant where you want to create the rule
4. Delete the *id* from the rule definition, so the script recognize that it's a new rule
5. Run the script:
```
python serviceDetectionApi.py
```
#### Delete configuration
1. Configure *config.ini* file:
```
deleteConfig = 1
downloadConfig = 0
updateConfig = 0
```
2. Remove the file of the rule you want to delete from the folder
3. Run the script:
```
python serviceDetectionApi.py
```
4. The script will automatically compare the rules in the tenant vs the ones defined in the configuration folder. It will detect that there's one that has been deleted
from the folder and **it will ask you if you want to proceed deleting it from the tenant**.
- Press *y* or *yes* in case you want to proceed with the deletion
- Press any other key to cancel the operation

View File

@ -0,0 +1,20 @@
[ENDPOINT]
endpoint = /api/config/v1/service/detectionRules/FULL_WEB_REQUEST
[ACTION]
deleteConfig = 0
downloadConfig = 1
updateConfig = 0
[MC]
MC = No
Cookie = <cookie>
User-Agent = <user-agent>
[TENANTS]
#tenant1 = https://dynatracemgd-cn.bmwgroup.net/e/b921f1b9-c00e-4031-b9d1-f5a0d530757b CN_PreProd_TOKEN
#tenant2 = https://dynatracemgd-cn.bmwgroup.net/e/ab88c03b-b7fc-45f0-9115-9e9ecc0ced35 CN_Prod_TOKEN
tenant3 = https://xxu26128.live.dynatrace.com xxu26128_TOKEN
tenant4 = https://wgv50241.live.dynatrace.com wgv50241_TOKEN
tenant5 = https://qqk70169.live.dynatrace.com qqk70169_TOKEN
tenant6 = https://onb44935.live.dynatrace.com onb44935_TOKEN

View File

@ -0,0 +1,53 @@
{
"type": "FULL_WEB_REQUEST",
"metadata": {
"configurationVersions": [
0
],
"clusterVersion": "1.216.107.20210505-135918"
},
"managementZones": [
"CD_vehicle-service"
],
"id": "04562e4f-667b-4a9d-8e93-fa10e919e3f5",
"name": "VehicleService - TEST",
"description": "Service merging",
"enabled": true,
"conditions": [
{
"attributeType": "PG_TAG",
"compareOperations": [
{
"type": "TAG",
"compareKeyOnly": false,
"tags": [
{
"context": "CONTEXTLESS",
"key": "Environment",
"value": "TEST"
}
]
}
]
},
{
"attributeType": "APPLICATION_ID",
"compareOperations": [
{
"type": "EQUALS",
"negate": false,
"ignoreCase": false,
"values": [
"VehicleService"
]
}
]
}
],
"applicationId": {
"transformations": [],
"valueOverride": "VehicleService"
},
"contextRoot": null,
"serverName": null
}

View File

@ -0,0 +1,53 @@
{
"type": "FULL_WEB_REQUEST",
"metadata": {
"configurationVersions": [
0
],
"clusterVersion": "1.216.107.20210505-135918"
},
"managementZones": [
"CD_vehicle-service"
],
"id": "6f903959-0881-4671-bb26-c07d9936a303",
"name": "VehicleService - TEST",
"description": "Service merging",
"enabled": true,
"conditions": [
{
"attributeType": "PG_TAG",
"compareOperations": [
{
"type": "TAG",
"compareKeyOnly": false,
"tags": [
{
"context": "CONTEXTLESS",
"key": "Environment",
"value": "TEST"
}
]
}
]
},
{
"attributeType": "APPLICATION_ID",
"compareOperations": [
{
"type": "EQUALS",
"negate": false,
"ignoreCase": false,
"values": [
"VehicleService"
]
}
]
}
],
"applicationId": {
"transformations": [],
"valueOverride": "VehicleService"
},
"contextRoot": null,
"serverName": null
}

View File

@ -0,0 +1,56 @@
{
"type": "FULL_WEB_REQUEST",
"metadata": {
"configurationVersions": [
0
],
"clusterVersion": "1.216.107.20210505-135918"
},
"managementZones": [
"CD_vehicle-service"
],
"id": "9d9df32f-04be-4e81-b818-60c91c3a1cd1",
"name": "VehicleService - PROD",
"description": "Service merging",
"enabled": true,
"conditions": [
{
"attributeType": "PG_TAG",
"compareOperations": [
{
"type": "TAG",
"compareKeyOnly": false,
"tags": [
{
"context": "CONTEXTLESS",
"key": "Environment",
"value": "PROD"
}
]
}
]
},
{
"attributeType": "APPLICATION_ID",
"compareOperations": [
{
"type": "EQUALS",
"negate": false,
"ignoreCase": false,
"values": [
"VehicleService"
]
}
]
}
],
"applicationId": {
"transformations": [],
"valueOverride": "VehicleService"
},
"contextRoot": null,
"serverName": {
"transformations": [],
"valueOverride": "VehicleService"
}
}

View File

@ -0,0 +1,56 @@
{
"type": "FULL_WEB_REQUEST",
"metadata": {
"configurationVersions": [
0
],
"clusterVersion": "1.216.107.20210505-135918"
},
"managementZones": [
"CD_trip-service"
],
"id": "cff1f368-9a66-4a69-9b49-15db54745fb3",
"name": "TripService - PROD",
"description": "Service merging",
"enabled": true,
"conditions": [
{
"attributeType": "PG_TAG",
"compareOperations": [
{
"type": "TAG",
"compareKeyOnly": false,
"tags": [
{
"context": "CONTEXTLESS",
"key": "Environment",
"value": "PROD"
}
]
}
]
},
{
"attributeType": "APPLICATION_ID",
"compareOperations": [
{
"type": "EQUALS",
"negate": false,
"ignoreCase": false,
"values": [
"TripService"
]
}
]
}
],
"applicationId": {
"transformations": [],
"valueOverride": "TripService"
},
"contextRoot": null,
"serverName": {
"transformations": [],
"valueOverride": "TripService"
}
}

View File

@ -0,0 +1,56 @@
{
"type": "FULL_WEB_REQUEST",
"metadata": {
"configurationVersions": [
0
],
"clusterVersion": "1.216.107.20210505-135918"
},
"managementZones": [
"CD_trip-service"
],
"id": "3365a46d-7238-4193-a8e1-812f00eb336d",
"name": "TripService - PROD",
"description": "Service merging",
"enabled": true,
"conditions": [
{
"attributeType": "PG_TAG",
"compareOperations": [
{
"type": "TAG",
"compareKeyOnly": false,
"tags": [
{
"context": "CONTEXTLESS",
"key": "Environment",
"value": "PROD"
}
]
}
]
},
{
"attributeType": "APPLICATION_ID",
"compareOperations": [
{
"type": "EQUALS",
"negate": false,
"ignoreCase": false,
"values": [
"TripService"
]
}
]
}
],
"applicationId": {
"transformations": [],
"valueOverride": "TripService"
},
"contextRoot": null,
"serverName": {
"transformations": [],
"valueOverride": "TripService"
}
}

View File

@ -0,0 +1,56 @@
{
"type": "FULL_WEB_REQUEST",
"metadata": {
"configurationVersions": [
0
],
"clusterVersion": "1.216.107.20210505-135918"
},
"managementZones": [
"CD_vehicle-service"
],
"id": "9d9df32f-04be-4e81-b818-60c91c3a1cd1",
"name": "VehicleService - PROD",
"description": "Service merging",
"enabled": true,
"conditions": [
{
"attributeType": "PG_TAG",
"compareOperations": [
{
"type": "TAG",
"compareKeyOnly": false,
"tags": [
{
"context": "CONTEXTLESS",
"key": "Environment",
"value": "PROD"
}
]
}
]
},
{
"attributeType": "APPLICATION_ID",
"compareOperations": [
{
"type": "EQUALS",
"negate": false,
"ignoreCase": false,
"values": [
"VehicleService"
]
}
]
}
],
"applicationId": {
"transformations": [],
"valueOverride": "VehicleService"
},
"contextRoot": null,
"serverName": {
"transformations": [],
"valueOverride": "VehicleService"
}
}

View File

@ -0,0 +1,202 @@
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 delete():
endpointLast = endpoint
if "/" in endpoint:
endpointLast = endpoint.split("/")[-1]
directory="data/"+env_id+"/"+endpointLast
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)
i=0
while i<len(data_json['values']):
delete_file = True
id_rule = data_json['values'][i]['id']
#Match id with file name
for filename in os.listdir(directory):
if filename.split(".")[0] == id_rule:
delete_file = False
if delete_file==True:
#Confirm deletion
question = input('Would you like to delete '+ data_json['values'][i]['id']+"?\n")
if question.lower() == 'yes' or question.lower() == 'y':
print("Deleting "+data_json['values'][i]['id'])
r = requests.delete(''.join(YOUR_DT_API_URL) + endpoint+"/"+data_json['values'][i]['id'], headers=headers, cookies=dict(cookies), verify=False);
print(r)
else:
print ("Deletion skipped")
i+=1
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
if iterate == True:
i=0
while i<len(data_json['values']):
id_rule = data_json['values'][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')
deleteConfig = config['ACTION']['deleteConfig']
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 deleteConfig=='1':
print("Deleting rules of "+ env_id)
delete()
if downloadConfig=='1':
print("Downloading rules of "+ env_id)
download()
if updateConfig=='1':
print("Updating rules of "+ env_id)
update()
print("\n")