forked from mikeghen/python_api_samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_users_to_group.py
More file actions
61 lines (42 loc) · 2.23 KB
/
add_users_to_group.py
File metadata and controls
61 lines (42 loc) · 2.23 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
import yaml ### install the pyyaml package
from lookerapi import LookerApi
import argparse
import csv
# You can execute this script from the command line like this:
# python3 add_users_to_group.py --filename=/path/to/file.csv
#
# Don't forget to set the host value on line 19
parser = argparse.ArgumentParser(description="Load user IDs from the CSV file specified by filename")
parser.add_argument("-f","--filename", help="Fully-qualified path of CSV file to parse",
type=str)
args = parser.parse_args()
### ------- OPEN THE CONFIG FILE and INSTANTIATE API -------
host = "" # the name of a Looker environment specified in a config.yml file in the same directory as this script
f = open('config.yml')
params = yaml.load(f)
f.close()
my_host = params['hosts'][host]['host']
my_secret = params['hosts'][host]['secret']
my_token = params['hosts'][host]['token']
looker = LookerApi(host=my_host,
token=my_token,
secret=my_secret)
def parse_csv_file_and_add_users_to_groups():
"""Parse a CSV file containing a User ID in the first column and a Group ID in the second column and update groups for users"""
f = open(args.filename, 'r', encoding='utf-8-sig') # the encoding may need to be updated depending on your file, see https://stackoverflow.com/a/17912811
csv_reader = csv.reader(f, delimiter=',')
line_number, count_of_updates = 0, 0
for line in csv_reader:
line_number = line_number + 1
try:
if int(line[0]) > 0 and int(line[1]) > 0:
# Add the Looker user who has the ID in the first column to the group that has the ID in the second column
looker.add_users_to_group(line[1], line[0])
# Remove any roles that have been assigned individually to this user because their roles will now be inherited from their new group
looker.set_user_role(line[0], [])
count_of_updates = count_of_updates + 1
except ValueError:
print("Ignoring line number " + str(line_number) + ' because this row does not contain two integer values')
print("Updated groups for " + str(count_of_updates) + " users")
if __name__ == "__main__":
parse_csv_file_and_add_users_to_groups()