Started the migration tool scripts. Finished Karbon's script for mass delete and re-addition. Other small things.

This commit is contained in:
Norm Rasmussen
2024-08-28 17:06:43 -04:00
parent f9df92fba9
commit 691bf67219
10 changed files with 31851 additions and 2 deletions

View File

@ -0,0 +1,104 @@
import requests
import Apikeys
import csv
BASEURL = "https://api.northpass.com/v2/"
APIKEY = Apikeys.KARBON
HEADERS = {
"accept": "application/json",
"content-type": "application/json",
"X-Api-Key": APIKEY,
}
ADMIN_LIST = [
"thea.schlobohm@karbonhq.com",
"charles+karbon@northpass.com",
"elizabeth.blass@karbonhq.com",
"ian.clazie@karbonhq.com",
"kelly.gabriel@karbonhq.com",
"richard.newman@karbonhq.com",
"steven.luu@karbonhq.com",
]
def get_people_first():
count = 0
delete_list = []
while True:
count += 1
person_url = f"{BASEURL}people?page={count}"
# &filter[partnerships_type][not_eq]=Partnerships::Admin&include=partnerships&page={count}"
# person_url = f"{BASEURL}people?filter[email][not_cont]=karbonhq.com"
person_resp = requests.get(person_url, headers=HEADERS)
persjson = person_resp.json()
nextlink = persjson["links"]
for people in persjson["data"]:
# print(f"This is just everyone... {people['id']}")
if people["attributes"]["email"] in ADMIN_LIST:
pass
else:
learneruuid = people["id"]
print(people["attributes"]["email"])
delete_list.append(learneruuid)
if "next" not in nextlink:
break
del_load = {"payload": delete_list}
print(f"Deleting {len(delete_list)} people")
delete_everyone(delete_list)
def get_groups():
group_list = []
while True:
groups_url = f"{BASEURL}groups"
groups_resp = requests.get(groups_url, headers=HEADERS)
groupjson = groups_resp.json()
if len(groupjson['data']) == 0:
break
else:
for groups in groupjson["data"]:
guids = groups["id"]
print(f"Deleting Group {groups['attributes']['name']}")
group_list.append(guids)
delete_groups(guids)
print(f"I've just processed {len(group_list)} groups and deleted them.")
def delete_everyone(uuids):
if len(uuids) > 1500:
for chunk in range(0, len(uuids), 1500):
i = chunk
payload_1 = []
i_to_add = uuids[i : i + 1500]
for person in i_to_add:
payload_1.append(person)
print(f"This group of people has a count of {len(payload_1)}")
payload = {"payload": payload_1}
del_url = f"{BASEURL}bulk/people/delete"
response = requests.post(del_url, headers=HEADERS, json=payload)
print(response.status_code)
print(response.text)
def delete_groups(guids):
gdel_url = f"{BASEURL}groups/{guids}"
gresponse = requests.delete(gdel_url, headers=HEADERS)
print(f"Deleted group {guids} with status code {gresponse.status_code}")
codes = [500, 403, 404]
if gresponse.status_code in codes:
print(
f"Hm. Something went wrong. Status code: {response.status_code} and text:"
)
print(response.text)
else:
pass
if __name__ == "__main__":
print("Getting Group IDs and Deleting groups.")
get_groups()
# print("Groups have been deleted. Moving onto people.")
# get_people_first()

View File

@ -0,0 +1,59 @@
import requests
import json
import pprint
import csv
import pandas as pd
import Apikeys
import time
pp = pprint.PrettyPrinter(indent=4)
APIKEY = Apikeys.KARBON
HEADERS = {
"accept": "application/json",
"X-Api-Key": APIKEY,
}
BASEURL = "https://api.northpass.com/v2/"
IMPORTFILE = "/Users/normrasmussen/Downloads/karbon-invite-users.csv"
def bulk_invite_and_group():
"""
Bulk endpoint which invites new people and adds them to a group via this structure:
{
"email": "me@mac.com"
"groups": "GroupA"
}
This function looks for the group in the CSV as well.
"""
groups_list = []
df = pd.DataFrame()
data = pd.read_csv(IMPORTFILE)
df2 = data[[ "email", "customer_name", "customer_id" ]].copy()
customer_ids = df2["customer_id"].unique()
customer_ids = list(customer_ids)
for row in df2.itertuples():
groupname = f"{row[2]}--{row[3]}"
groups_list.append(groupname)
for customer in customer_ids:
payload = ""
tmp_group = data[data['customer_id'] == customer]
people = list(tmp_group["email"])
cust = str(tmp_group["customer_id"].unique())[2:-2]
url = f"{BASEURL}bulk/people"
payload_1 = []
for group in groups_list:
for person in people:
if cust in group:
miniload = {"email": person, "groups": group}
payload_1.append(miniload)
print(f"The {group} payload has {len(payload_1)}")
payload = {"data": {"attributes": {"people": payload_1}}}
print(payload)
response = requests.post(url, headers=HEADERS, json=payload)
print(f"Completed. Status code is {response.status_code}")
print(response.text)
if __name__ == "__main__":
bulk_invite_and_group()

File diff suppressed because it is too large Load Diff

72
Scripts/Karbon/typescript Normal file
View File

@ -0,0 +1,72 @@
Script started on Wed Aug 28 12:49:13 2024


7~/Documents/Work/Scripts/Karbon ......................................................................................................................................................................... at 12:49:13
> [?2004h[?25l8

M~/Documents/Work/Scripts/Karbon on main ...................................................................................................................................................... py Scripts at 12:49:13
> [?25h[?2004h[?25l
M
M~/Documents/Work/Scripts/Karbon on main !1 ?4 ................................................................................................................................................ py Scripts at 12:49:13
> [?25hssssh root@192.168.200.19sc                     p -r * 'root@192.168.200.18:/main/Media/Music/Sacred Harp Singers of Cork/'scr                                                                          riipptscriptt t --eh  hep lp[?25l[?2004l
M> script --help[?25h
script: illegal option -- -
usage: script [-aeFkpqr] [-t time] [file [command ...]]
script -p [-deq] [-T fmt] [file]
%

M~/Documents/Work/Scripts/Karbon on main !1 ?4 ................................................................................................................................................ py Scripts at 12:49:21
> [?2004h[?25l
M
M~/Documents/Work/Scripts/Karbon on main !1 ?4 ................................................................................................................................................ py Scripts at 12:49:21
> [?25hmmmv translate_css.py ./Luminatema                            n curlmanmann n s   ipsc  ript[?25l[?2004l
M> man script[?25h
[?1049h[?1h=
SCRIPT(1) General Commands Manual SCRIPT(1)
NAME
script make typescript of terminal session
SYNOPSIS
script [-aeFkqr] [-t time] [file [command ...]]
script -p [-deq] [-T fmt] [file]
DESCRIPTION
The script utility makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file
can be printed out later with lpr(1).
If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is saved in the file typescript.
:
 ESCESCOOBB
 If the argument command is given, script will run the specified command with an optional argument vector instead of an interactive shell.
:
 ESCESCOOBB

:
 ESCESCOOBB
 The following options are available:
:
 ESCESCOOBB

:
 ESCESCOOBB
 -a Append the output to file or typescript, retaining the prior contents.
:
 ESCESCOOBB

:
 ESCESCOOBB
 -d When playing back a session with the -p flag, do not sleep between records when playing back a timestamped session.
:
 ESCESCOOBB


View File

@ -2,7 +2,7 @@ import Apikeys
import requests
import pandas as pd
IMPORTFILE = '~/Downloads/karbon-existing-groups.csv'
IMPORTFILE = '~/Downloads/karbon-invite-users.csv'
APIKEY = Apikeys.KARBON
HEADERS = {"accept": "application/json", "X-Api-Key": APIKEY }
BASEURL = "https://api.northpass.com/v2/"

View File

@ -0,0 +1 @@
SANDBOX = "SlpQlju219WnWogn94dQUT6Yt"

View File

@ -0,0 +1,51 @@
import requests
import Apikeys
import pprint
PP = pprint.PrettyPrinter(indent=4)
APIKEY = Apikeys.SANDBOX
HEADERS = {"content-type": "application/json", "X-Api-Key": APIKEY}
BASEURL = "https://api.northpass.com/v2/migration"
def create_project():
"""
Function to create a project with just a name. While ID is in the example payload, it isn't needed.
Will generate a project ID for you in the return that you can use to transfer everything.
Note that it will not give you an error if a project already exists with that name!
"""
payload = { "data": {
"type": "migration_projects",
"attributes": {
"name": "My Second Migration Project"
},
} }
create_req = requests.post(f"{BASEURL}/projects", headers=HEADERS, json=payload)
print(payload)
print(create_req.status_code)
print(create_req.text)
def get_all_projects():
"""
Returns all projects. Leverage PrettyPrint or uncomment the name to just return names and ids.
"""
get_proj = requests.get(f"{BASEURL}/projects", headers=HEADERS)
print(get_proj.status_code)
# PP.pprint(get_proj.json())
for items in get_proj.json()['data']:
print(f"{ items['attributes']['name'] } -- { items['id'] }")
def get_specific_project():
"""
Returns results from a specific project.
"""
proj_id = "13aa7aed-3fb5-4488-9185-3befd0c1ae86"
get_spec_proj = requests.get(f"{BASEURL}/projects/{proj_id}", headers=HEADERS)
print(get_spec_proj.status_code)
PP.pprint(get_spec_proj.json())
if __name__ == "__main__":
# create_project()
# get_all_projects()
get_specific_project()