Skip to content

Commit

Permalink
feat: Another simple example add users to a Looker group based on a c…
Browse files Browse the repository at this point in the history
…sv. (#776)

* Create simple_schedule_plan.py

Many people have come on chat about creating schedules with the sdk and I just wanted to create a barebones example, so we can direct them to a public example.

* Update README.md

Updated readme to point at simple_schedule_plan example

* add users to group from csv based on list of email

* Update examples/python/add_users_to_group_from_csv.py

Co-authored-by: Joel Dodge <joeldodge@google.com>
  • Loading branch information
eric-lyons and joeldodge79 authored Aug 6, 2021
1 parent 4932baa commit 83cde03
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions examples/python/add_users_to_group_from_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import looker_sdk
import csv

####Initialize API/SDK for more info go here: https://pypi.org/project/looker-sdk/
from looker_sdk import methods, models40
sdk = looker_sdk.init40("../looker.ini")

#### GO TO ADMIN --> GROUPS AND FIND THE GROUP ID YOU WANT TO ADD THE PEOPLE TO. ADD IT BELOW
### Alternative would be to use the search groups endpoint
### Depending on the cleanliness of your source of emails, you may want to add more error handling
### 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.


def add_csv_of_users_to_group(group_name:str, file_path:str):
group = sdk.search_groups(name=group_name)
group = group[0]
if group:
data = []
i=0
with open(file_path) as f:
## MAKE SURE YOU DOUBLE CHECK THE DELIMITER IN YOUR CSV
reader = csv.reader(f, delimiter=' ')
for row in reader:
data.append(str(row[i]))

## loops through list and searches user
## grabs user id and passes that through add user to group
try:
for email in data:
for user in sdk.search_users(email=email):
#print(user.email)
if user.id:
sdk.add_group_user(group_id=group.id, body=models40.GroupIdForGroupUserInclusion(user_id= user.id))
else:
pass

except KeyError:
print('Key error \n')
pass
except TypeError:
print('Type error \n')
pass
except IndexError:
print('Index error \n')
pass
else:
print("Group does not exist")
## THE FILE NAME OF THE CSV WILL WORK IF IT IS IN THE SAME DIRECTORY
add_csv_of_users_to_group("GROUPNAME", "test.csv")

0 comments on commit 83cde03

Please sign in to comment.