Skip to content
This repository was archived by the owner on Sep 25, 2020. It is now read-only.

Commit 56c4c47

Browse files
committed
Upload CSV and add users to a Group -- added group name rather than ID.
1 parent 45b1129 commit 56c4c47

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

python/upload_users_to_group.py

+33-27
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,38 @@
1111
### Depending on the cleanliness of your source of emails, you may want to add more error handling
1212
### EG check for structure, add users without Looker accounts to an output file, or even pass them into another endpoint where you create an account.
1313

14-
def add_csv_of_users_to_group(group_id:int, file_path:str):
15-
data = []
16-
i=0
17-
with open(file_path) as f:
18-
reader = csv.reader(f, delimiter=' ')
19-
for row in reader:
20-
data.append(str(row[i]))
2114

22-
## loops through list and searches user
23-
## grabs user id and passes that through add user to group
24-
try:
25-
for email in data:
26-
for user in sdk.search_users(email=email):
27-
#print(user.email)
28-
if user.id:
29-
sdk.add_group_user(group_id=group_id, body=models40.GroupIdForGroupUserInclusion(user_id= user.id))
30-
else:
31-
pass
15+
def add_csv_of_users_to_group(group_name:str, file_path:str):
16+
group = sdk.search_groups(name=group_name)
17+
group = group[0]
18+
if group:
19+
data = []
20+
i=0
21+
with open(file_path) as f:
22+
reader = csv.reader(f, delimiter=' ')
23+
for row in reader:
24+
data.append(str(row[i]))
3225

33-
except KeyError:
34-
print('Key error \n')
35-
pass
36-
except TypeError:
37-
print('Type error \n')
38-
pass
39-
except IndexError:
40-
print('Index error \n')
41-
pass
42-
add_csv_of_users_to_group(121, "test.csv")
26+
## loops through list and searches user
27+
## grabs user id and passes that through add user to group
28+
try:
29+
for email in data:
30+
for user in sdk.search_users(email=email):
31+
#print(user.email)
32+
if user.id:
33+
sdk.add_group_user(group_id=group.id, body=models40.GroupIdForGroupUserInclusion(user_id= user.id))
34+
else:
35+
pass
36+
37+
except KeyError:
38+
print('Key error \n')
39+
pass
40+
except TypeError:
41+
print('Type error \n')
42+
pass
43+
except IndexError:
44+
print('Index error \n')
45+
pass
46+
else:
47+
print("Group does not exist")
48+
add_csv_of_users_to_group("all but 1", "test.csv")

0 commit comments

Comments
 (0)