-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpcd_add_m2n_data.py
47 lines (35 loc) · 1.35 KB
/
pcd_add_m2n_data.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
# pip install pandas # if needed
import pandas as pd
import authenticate_with_msal
# Parameters
PathToEnvironmentJSON = "example-env.json"
PathToCSV = 'data\M to N.csv'
EntityM = 'systemusers'
EntityN = 'teams'
MToNRelationship = 'teammembership_association'
# Column names in CSV must match EntityM and EntityN above
# Getting access token.
authentication = authenticate_with_msal.getAuthenticatedSession(PathToEnvironmentJSON)
session = authentication[0]
environmentURI = authentication[1]
# reading the CSV
df = pd.read_csv(PathToCSV)
successful_updates = 0
failures = 0
expected_updates = len(df)
for index, row in df.iterrows():
record_m = row[EntityM]
record_n = row[EntityN]
request_uri = f'{environmentURI}api/data/v9.2/{EntityM}({record_m})/{MToNRelationship}/$ref'
odata_id = f'{environmentURI}api/data/v9.2/{EntityN}({record_n})'
post_json = { "@odata.id": odata_id }
r = session.post(request_uri, json = post_json)
if r.status_code != 204:
failures += 1
raw = r.content.decode('utf-8')
print(f'Error linking {record_m} to {record_n}. Error {r.status_code}: \n{raw}\n')
else:
successful_updates += 1
if index % 10 == 0:
print(f"Processed: {index + 1}")
print(f'{successful_updates} UPDATES MADE OF {expected_updates} EXPECTED UPDATES.\n{failures} FAILURES.')