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

Commit 7dfa06f

Browse files
committed
A simple script to add users to a group based on a csv
2 parents 150f5ea + 9747b11 commit 7dfa06f

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ This is a constantly-evolving repository, where examples could change, appear, a
77
## Please contribute
88

99
All are welcome to submit examples. Please feel free to submit a PR for any examples you may want to share. Thank you!
10+
11+
## Very Nice!

python/upload_users_to_group.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
import looker_sdk
22
import csv
3-
4-
53
import exceptions
4+
65
####Initialize API/SDK for more info go here: https://pypi.org/project/looker-sdk/
7-
from looker_sdk import methods31, models
8-
sdk = looker_sdk.init31() # or init40() for v4.0 API
9-
me = sdk.me()
10-
#print(me)
6+
from looker_sdk import methods, models40
7+
sdk = looker_sdk.init40("../looker.ini")
118

129
#### GO TO ADMIN --> GROUPS AND FIND THE GROUP ID YOU WANT TO ADD THE PEOPLE TO. ADD IT BELOW
13-
group = 28
14-
data = []
15-
i=0
16-
with open('~/file.csv') as f:
17-
reader = csv.reader(f, delimiter=' ')
18-
for row in reader:
19-
data.append(str(row[i]))
10+
### Alternative would be to use the search groups endpoint
11+
12+
sdk.search_groups()
13+
def add_csv_of_users_to_group(group_id:int, file_path:str):
14+
data = []
15+
i=0
16+
with open(file_path) as f:
17+
reader = csv.reader(f, delimiter=' ')
18+
for row in reader:
19+
data.append(str(row[i]))
2020

21-
### loops through list and searches user
22-
### grabs user id and passes that through add user to group
23-
try:
24-
for email in data:
25-
for user in sdk.search_users(email=email):
26-
sdk.add_group_user(group_id=group, body=models.GroupIdForGroupUserInclusion(user_id= user.id))
27-
except KeyError:
28-
print('Key error \n')
29-
print(email)
30-
pass
31-
except TypeError:
32-
print('Key error \n')
33-
print(email)
34-
pass
35-
except IndexError:
36-
print('Index error \n')
37-
print(email)
38-
pass
21+
### loops through list and searches user
22+
### grabs user id and passes that through add user to group
23+
try:
24+
for email in data:
25+
for user in sdk.search_users(email=email):
26+
sdk.add_group_user(group_id=group_id, body=models40.GroupIdForGroupUserInclusion(user_id= user.id))
27+
except KeyError:
28+
print('Key error \n')
29+
print(email)
30+
pass
31+
except TypeError:
32+
print('Type error \n')
33+
print(email)
34+
pass
35+
except IndexError:
36+
print('Index error \n')
37+
print(email)
38+
pass

0 commit comments

Comments
 (0)