-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete_entities.py
76 lines (74 loc) · 2.17 KB
/
delete_entities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import json
import requests
import threading
API_URL = "https://api.getport.io/v1"
MAX_THREADS = 5
sema = threading.Semaphore(value=MAX_THREADS)
CLIENT_ID = "CLIENT_ID"
CLIENT_SECRET = "CLIENT_SECRET"
API_URL = "https://api.getport.io/v1"
credentials = {"clientId": CLIENT_ID, "clientSecret": CLIENT_SECRET}
token_response = requests.post(f"{API_URL}/auth/access_token", json=credentials)
access_token = token_response.json()["accessToken"]
print(access_token)
headers = {"Authorization": f"Bearer {access_token}"}
def delete_port_entity(blueprint, entity):
sema.acquire()
res = requests.delete(
f"{API_URL}/blueprints/{blueprint}/entities/{entity}?delete_dependents=true",
headers=headers,
)
sema.release()
if res.status_code == 200:
print(f"Entity {entity} deleted")
else:
print(f"Encountered error deleting {entity}: {res.json()}")
def delete_all(blueprints):
report_threads = []
for blue in blueprints:
search = {
"combinator": "and",
"rules": [{"property": "$blueprint", "value": blue, "operator": "="}],
}
entities = requests.post(f"{API_URL}/entities/search", headers=headers, json=search)
print(f"Starting to delete all {blue} entities")
res = entities.json()
for ent in res["entities"]:
report_thread = threading.Thread(target=delete_port_entity, args=(blue, ent["identifier"]))
report_thread.start()
report_threads.append(report_thread)
for thread in report_threads:
thread.join()
all_blueprints = requests.get(f"{API_URL}/blueprints", headers=headers)
if all_blueprints.status_code != 200:
print("Failed to get blueprints, exiting")
exit(1)
blueprints = []
for blueprint in all_blueprints.json()["blueprints"]:
blueprints.append(blueprint["identifier"])
# Enter here the identifiers of the blueprints you want to delete all entities from
# blueprints = ['pod', 'workload', 'workflow-run', 'workflow', 'pullRequest', 'node', 'namespace', 'cluster', 'issue', 'microservice']
delete_all(blueprints)
print("Done deleting")