From af7565b26bb63326b754b20cae390cebe4e146ce Mon Sep 17 00:00:00 2001 From: Eric Lyons <53839634+eric-lyons@users.noreply.github.com> Date: Thu, 22 Jul 2021 09:51:34 -0400 Subject: [PATCH 1/4] 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. --- examples/python/simple_schedule_plan.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 examples/python/simple_schedule_plan.py diff --git a/examples/python/simple_schedule_plan.py b/examples/python/simple_schedule_plan.py new file mode 100644 index 000000000..8e2188828 --- /dev/null +++ b/examples/python/simple_schedule_plan.py @@ -0,0 +1,15 @@ +from looker_sdk import methods, models40 +import looker_sdk +import exceptions +sdk = looker_sdk.init40("../looker.ini") + + +def create_simple_schedule(dashboard_id:int,user_id:int,schedule_title:str, format:str, email:str,type:str, message:str, crontab:str): + ### For more information on the Params accepted https://github.com/looker-open-source/sdk-codegen/blob/master/python/looker_sdk/sdk/api31/methods.py#L2144 + ### And for schedule destination go: https://github.com/looker-open-source/sdk-codegen/blob/master/python/looker_sdk/sdk/api31/models.py#L4601 + ### Supported formats vary by destination, but include: "txt", "csv", "inline_json", "json", "json_detail", "xlsx", "html", "wysiwyg_pdf", "assembled_pdf", "wysiwyg_png" + ### type: Type of the address ('email', 'webhook', 's3', or 'sftp') + schedule = sdk.create_scheduled_plan( + body=models40.WriteScheduledPlan(name=schedule_title, dashboard_id=dashboard_id, user_id=user_id, run_as_recipient= True, crontab=crontab, scheduled_plan_destination = [models40.ScheduledPlanDestination(format=format, apply_formatting=True, apply_vis=True, address=email, type=type, message=message)])) +create_simple_schedule(1234,453,"This is an automated test", "assembled_pdf", "test@looker.com", "email", "Hi Looker User!", "0 1 * * *") + From 4023d75a7d11c22a7846a355a5b23c8d217d15cb Mon Sep 17 00:00:00 2001 From: Eric Lyons <53839634+eric-lyons@users.noreply.github.com> Date: Thu, 22 Jul 2021 09:53:24 -0400 Subject: [PATCH 2/4] Update README.md Updated readme to point at simple_schedule_plan example --- examples/python/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/python/README.md b/examples/python/README.md index 6991d68ed..2266c054c 100644 --- a/examples/python/README.md +++ b/examples/python/README.md @@ -30,6 +30,7 @@ The full details of all Looker API endpoints are listed in Looker Docs: [Version - [Transfer all schedules of a user to another user](transfer_all_schedules.py) - [Pause/Resume or Copy Schedules](manage_schedules.py) +- [Create a Simple Schedule Plan](simple_schedule_plan.py) ## User : Manage Users From cc2ef6bb2df0d9b0aec0b7c74851f602f5422890 Mon Sep 17 00:00:00 2001 From: eric-lyons Date: Fri, 30 Jul 2021 21:13:22 +0000 Subject: [PATCH 3/4] add users to group from csv based on list of email --- .../python/add_users_to_group_from_csv.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 examples/python/add_users_to_group_from_csv.py diff --git a/examples/python/add_users_to_group_from_csv.py b/examples/python/add_users_to_group_from_csv.py new file mode 100644 index 000000000..dd83e590f --- /dev/null +++ b/examples/python/add_users_to_group_from_csv.py @@ -0,0 +1,50 @@ +import looker_sdk +import csv +import exceptions + +####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") \ No newline at end of file From 938fd1c2a4f7c415e650423ab469d0b08ab0ee04 Mon Sep 17 00:00:00 2001 From: Joel Dodge Date: Fri, 6 Aug 2021 15:32:01 +0000 Subject: [PATCH 4/4] Update examples/python/add_users_to_group_from_csv.py --- examples/python/add_users_to_group_from_csv.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/python/add_users_to_group_from_csv.py b/examples/python/add_users_to_group_from_csv.py index dd83e590f..58629a8a1 100644 --- a/examples/python/add_users_to_group_from_csv.py +++ b/examples/python/add_users_to_group_from_csv.py @@ -1,6 +1,5 @@ import looker_sdk import csv -import exceptions ####Initialize API/SDK for more info go here: https://pypi.org/project/looker-sdk/ from looker_sdk import methods, models40 @@ -47,4 +46,4 @@ def add_csv_of_users_to_group(group_name:str, file_path:str): 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") \ No newline at end of file +add_csv_of_users_to_group("GROUPNAME", "test.csv")