-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmigrateEnv.py
133 lines (112 loc) · 4.81 KB
/
migrateEnv.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
import requests
import os
import copy
API_URL = 'https://api.getport.io/v1'
error = False
#The purpose of this script is to copy data between organization. It will copy both blueprints and entities.
#Fill in the secrets or set them as environment variables
OLD_CLIENT_ID = "" # or set to os.getenv("OLD_CLIENT_ID")
OLD_CLIENT_SECRET = "" # or set to os.getenv("OLD_CLIENT_SECRET")
NEW_CLIENT_ID = "" # or set to os.getenv("NEW_CLIENT_ID")
NEW_CLIENT_SECRET = "" # or set to os.getenv("NEW_CLIENT_SECRET")
old_credentials = { 'clientId': OLD_CLIENT_ID, 'clientSecret': OLD_CLIENT_SECRET }
old_credentials = requests.post(f'{API_URL}/auth/access_token', json=old_credentials)
old_access_token = old_credentials.json()["accessToken"]
old_headers = {
'Authorization': f'Bearer {old_access_token}'
}
new_credentials = { 'clientId': NEW_CLIENT_ID, 'clientSecret': NEW_CLIENT_SECRET }
new_credentials = requests.post(f'{API_URL}/auth/access_token', json=new_credentials)
new_access_token = new_credentials.json()["accessToken"]
new_headers = {
'Authorization': f'Bearer {new_access_token}'
}
def getBlueprints():
print("Getting blueprints")
res = requests.get(f'{API_URL}/blueprints', headers=old_headers)
resp = res.json()["blueprints"]
return resp
def getScorecards():
print("Getting scorecards")
res = requests.get(f'{API_URL}/scorecards', headers=old_headers)
resp = res.json()["scorecards"]
return resp
def getActions():
print("Getting actions")
res = requests.get(f'{API_URL}/actions', headers=old_headers)
resp = res.json()["actions"]
return resp
def getTeams():
print("Getting teams")
res = requests.get(f'{API_URL}/teams', headers=old_headers)
resp = res.json()["teams"]
return resp
def postBlueprints(blueprints):
print("Posting blueprints")
blueprintsWithoutRelation = copy.deepcopy(blueprints)
for bp in blueprintsWithoutRelation:
print(f"posting blueprint {bp['identifier']}")
bp.get("relations").clear()
bp.get("mirrorProperties").clear()
res = requests.post(f'{API_URL}/blueprints', headers=new_headers, json=bp)
if res.status_code != 200:
print("error posting blueprint:" + res.json())
error = True
for blueprint in blueprints:
print(f"patching blueprint {blueprint['identifier']} with relations")
res = requests.patch(f'{API_URL}/blueprints/{blueprint["identifier"]}', headers=new_headers, json=blueprint)
if res.status_code != 200:
print("error patching blueprint:" + res.json())
error = True
def postEntities(blueprints):
for blueprint in blueprints:
print(f"getting entities for blueprint {blueprint['identifier']}")
res = requests.get(f'{API_URL}/blueprints/{blueprint["identifier"]}/entities', headers=old_headers)
resp = res.json()["entities"]
print(f"posting entities for blueprint {blueprint['identifier']}")
for entity in resp:
if entity["icon"] is None:
entity.pop("icon", None)
res = requests.post(f'{API_URL}/blueprints/{blueprint["identifier"]}/entities?upsert=true&validation_only=false&create_missing_related_entities=true&merge=false', headers=new_headers, json=entity)
if res.status_code != 200:
print("error posting entity:" + res.json())
error = True
def postScorecards(scorecards):
print("Posting scorecards")
for scorecard in scorecards:
print(f"posting scorecard {scorecard['identifier']}")
res = requests.post(f'{API_URL}/blueprints/{scorecard["blueprint"]}/scorecards', headers=new_headers, json=scorecard)
if res.status_code != 200:
print("error posting scorecard:" + res.json())
error = True
def postActions(actions):
print("Posting actions")
for action in actions:
res = requests.post(f'{API_URL}/blueprints/{action["blueprint"]}/actions', headers=new_headers, json=action)
if res.status_code != 200:
print(f"error posting action {action["identifier"]} :" + res.json())
error = True
def postTeams(teams):
print("Posting teams")
for team in teams:
res = requests.post(f'{API_URL}/teams', headers=new_headers, json=team)
if res.status_code != 200:
print(f"error posting team {team['identifier']} :" + res.json())
error = True
def main():
blueprints = getBlueprints()
postBlueprints(blueprints)
postEntities(blueprints)
scorecards = getScorecards()
postScorecards(scorecards)
actions = getActions()
postActions(actions)
teams = getTeams()
postTeams(teams)
if error:
print("Errors occured during migration, please check logs")
else:
print("No errors were caught during migration")
if __name__ == "__main__":
main()