Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added env migration script #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions migrateEnv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()