From a0b82901459c8e01f56b4fd9b12e3c079f5ec8e8 Mon Sep 17 00:00:00 2001 From: eric-lyons Date: Tue, 23 Jun 2020 22:29:32 -0400 Subject: [PATCH 01/10] New Script that allows User to upload users to a group from a CSV if they already have a looker account --- python/upload_users_to_group.py | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 python/upload_users_to_group.py diff --git a/python/upload_users_to_group.py b/python/upload_users_to_group.py new file mode 100644 index 00000000..2348425e --- /dev/null +++ b/python/upload_users_to_group.py @@ -0,0 +1,38 @@ +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 methods31, models +sdk = looker_sdk.init31() # or init40() for v4.0 API +me = sdk.me() +#print(me) + +#### GO TO ADMIN --> GROUPS AND FIND THE GROUP ID YOU WANT TO ADD THE PEOPLE TO. ADD IT BELOW +group = 28 +data = [] +i=0 +with open('~/file.csv') as f: + 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): + sdk.add_group_user(group_id=group, body=models.GroupIdForGroupUserInclusion(user_id= user.id)) +except KeyError: + print('Key error \n') + print(email) + pass +except TypeError: + print('Key error \n') + print(email) + pass +except IndexError: + print('Index error \n') + print(email) + pass From 486a4f6f5b2d719f0399d37a81b03da715a23872 Mon Sep 17 00:00:00 2001 From: eric-lyons Date: Tue, 23 Jun 2020 22:53:53 -0400 Subject: [PATCH 02/10] example of creating a schedule plan --- python/simple_schedule_plan.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 python/simple_schedule_plan.py diff --git a/python/simple_schedule_plan.py b/python/simple_schedule_plan.py new file mode 100644 index 00000000..a09227c0 --- /dev/null +++ b/python/simple_schedule_plan.py @@ -0,0 +1,20 @@ +import looker_sdk + +# from typing import cast, MutableSequence, Sequence + +####Initialize API/SDK for more info go here: https://pypi.org/project/looker-sdk/ +from looker_sdk import methods31, models +sdk = looker_sdk.init31() # or init40() for v4.0 API +me = sdk.me() + + +### DEFINE VALUES HERE #### +# DASHBOARDID = VALUE +# USERID = VALUE +# SCHEDULETITLE = VALUE +# EMAIL = VALUE + +#Simple Create schedule plan example +## 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 +schedule = sdk.create_scheduled_plan(body=models.WriteScheduledPlan(name=SCHEDULETITLE, dashboard_id=DASHBOARDID, user_id=USERID, run_as_recipient= True, crontab="0 1 * * *", scheduled_plan_destination = [models.ScheduledPlanDestination(format="csv_zip", apply_formatting=True, apply_vis=True, address=EMAIL, type="email", message="Aloha!")])) From 9747b11cc5b26d551a7f7b26ec90b375efc3bd32 Mon Sep 17 00:00:00 2001 From: Eric Lyons <53839634+eric-lyons@users.noreply.github.com> Date: Thu, 18 Mar 2021 11:43:12 -0400 Subject: [PATCH 03/10] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0384bbed..c961cebb 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,5 @@ This is a constantly-evolving repository, where examples could change, appear, a ## Please contribute All are welcome to submit examples. Please feel free to submit a PR for any examples you may want to share. Thank you! + +## Very Nice! From 150f5ea8358ff08e3f5fbef19063fd8c57b34ec4 Mon Sep 17 00:00:00 2001 From: eric-lyons Date: Thu, 22 Jul 2021 13:47:46 +0000 Subject: [PATCH 04/10] Example simple schedule function --- python/simple_schedule_example.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 python/simple_schedule_example.py diff --git a/python/simple_schedule_example.py b/python/simple_schedule_example.py new file mode 100644 index 00000000..67e6a658 --- /dev/null +++ b/python/simple_schedule_example.py @@ -0,0 +1,16 @@ +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 45b1129fc5dd5d609f1705a1c380f3f46116310c Mon Sep 17 00:00:00 2001 From: eric-lyons Date: Fri, 30 Jul 2021 19:27:01 +0000 Subject: [PATCH 05/10] Upload CSV and add users to a Group --- python/upload_users_to_group.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/python/upload_users_to_group.py b/python/upload_users_to_group.py index 1a975e72..627e1e7b 100644 --- a/python/upload_users_to_group.py +++ b/python/upload_users_to_group.py @@ -8,8 +8,9 @@ #### 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. -sdk.search_groups() def add_csv_of_users_to_group(group_id:int, file_path:str): data = [] i=0 @@ -18,21 +19,24 @@ def add_csv_of_users_to_group(group_id:int, file_path:str): 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 + ## 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): - sdk.add_group_user(group_id=group_id, body=models40.GroupIdForGroupUserInclusion(user_id= user.id)) + #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') - print(email) pass except TypeError: print('Type error \n') - print(email) pass except IndexError: print('Index error \n') - print(email) - pass \ No newline at end of file + pass +add_csv_of_users_to_group(121, "test.csv") \ No newline at end of file From 56c4c478e200ed6932e83bcfd0fc697273bb67ad Mon Sep 17 00:00:00 2001 From: eric-lyons Date: Fri, 30 Jul 2021 19:36:23 +0000 Subject: [PATCH 06/10] Upload CSV and add users to a Group -- added group name rather than ID. --- python/upload_users_to_group.py | 60 ++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/python/upload_users_to_group.py b/python/upload_users_to_group.py index 627e1e7b..ca51cf3d 100644 --- a/python/upload_users_to_group.py +++ b/python/upload_users_to_group.py @@ -11,32 +11,38 @@ ### 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_id:int, file_path:str): - data = [] - i=0 - with open(file_path) as f: - 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 +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: + reader = csv.reader(f, delimiter=' ') + for row in reader: + data.append(str(row[i])) - except KeyError: - print('Key error \n') - pass - except TypeError: - print('Type error \n') - pass - except IndexError: - print('Index error \n') - pass -add_csv_of_users_to_group(121, "test.csv") \ No newline at end of file + ## 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") +add_csv_of_users_to_group("all but 1", "test.csv") \ No newline at end of file From 1d4deaba3b8651209d3fbfa1597d414ff6073064 Mon Sep 17 00:00:00 2001 From: Eric Lyons <53839634+eric-lyons@users.noreply.github.com> Date: Fri, 30 Jul 2021 15:42:21 -0400 Subject: [PATCH 07/10] Update upload_users_to_group.py Added some more comments --- python/upload_users_to_group.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/upload_users_to_group.py b/python/upload_users_to_group.py index ca51cf3d..50879407 100644 --- a/python/upload_users_to_group.py +++ b/python/upload_users_to_group.py @@ -6,10 +6,8 @@ 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. +### 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): @@ -19,6 +17,7 @@ def add_csv_of_users_to_group(group_name:str, file_path:str): data = [] i=0 with open(file_path) as f: + ### make sure you set the right delimiter here for your own CSV reader = csv.reader(f, delimiter=' ') for row in reader: data.append(str(row[i])) @@ -45,4 +44,5 @@ def add_csv_of_users_to_group(group_name:str, file_path:str): pass else: print("Group does not exist") -add_csv_of_users_to_group("all but 1", "test.csv") \ No newline at end of file +## File name will work rather than path if in same directory as script. +add_csv_of_users_to_group("GROUPNAME", "test.csv") From 56f8d0c9dfc5698373fc1382d1ca43a5317aa78e Mon Sep 17 00:00:00 2001 From: Eric Lyons <53839634+eric-lyons@users.noreply.github.com> Date: Fri, 30 Jul 2021 15:44:59 -0400 Subject: [PATCH 08/10] Delete upload_users_to_group.py --- python/upload_users_to_group.py | 48 --------------------------------- 1 file changed, 48 deletions(-) delete mode 100644 python/upload_users_to_group.py diff --git a/python/upload_users_to_group.py b/python/upload_users_to_group.py deleted file mode 100644 index 50879407..00000000 --- a/python/upload_users_to_group.py +++ /dev/null @@ -1,48 +0,0 @@ -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") - -### 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 set the right delimiter here for your own 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") -## File name will work rather than path if in same directory as script. -add_csv_of_users_to_group("GROUPNAME", "test.csv") From fc294218ac6413d29f3cd63056284829cf46216e Mon Sep 17 00:00:00 2001 From: Eric Lyons <53839634+eric-lyons@users.noreply.github.com> Date: Fri, 30 Jul 2021 15:50:59 -0400 Subject: [PATCH 09/10] Create upload_users_via_csv_to_group New script that uploads a list of users via email from a csv to a specific looker group. --- python/upload_users_via_csv_to_group | 50 ++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 python/upload_users_via_csv_to_group diff --git a/python/upload_users_via_csv_to_group b/python/upload_users_via_csv_to_group new file mode 100644 index 00000000..ea2cb21b --- /dev/null +++ b/python/upload_users_via_csv_to_group @@ -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") From 9eb170db03c3eb814b6e19c2067f228c016565c8 Mon Sep 17 00:00:00 2001 From: Eric Lyons <53839634+eric-lyons@users.noreply.github.com> Date: Wed, 8 Jan 2025 15:00:15 -0500 Subject: [PATCH 10/10] Create set_user_attribute_value.py Add example to set user attributes --- python/set_user_attribute_value.py | 84 ++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 python/set_user_attribute_value.py diff --git a/python/set_user_attribute_value.py b/python/set_user_attribute_value.py new file mode 100644 index 00000000..ba7783ca --- /dev/null +++ b/python/set_user_attribute_value.py @@ -0,0 +1,84 @@ +"""Sets a user attribute value for a group or user in Looker. + +Usage: + python set_user_attribute_value.py + +Examples: + python set_user_attribute_value.py "user" "274" "26" "A great value" + python set_user_attribute_value.py "group" "5" "12" "Another value" +""" + +import argparse +import looker_sdk +from looker_sdk import models40 as models, error + +def set_user_attribute_value(sdk, user_id, user_attribute_id, value): + """Sets the user attribute value for a specific user. + + Args: + sdk: The Looker SDK instance. + user_id: The ID of the user. + user_attribute_id: The ID of the user attribute. + value: The value to set. + """ + try: + sdk.set_user_attribute_user_value( + user_id=user_id, + user_attribute_id=user_attribute_id, + body=models.WriteUserAttributeWithValue(value=value) + ) + print(f"Successfully set user attribute {user_attribute_id} for user {user_id} to '{value}'") + except error.SDKError as e: + print(f"Error setting user attribute for user {user_id}: {e}") + except Exception as e: + print(f"An unexpected error occurred: {e}") + +def set_group_attribute_value(sdk, group_id, user_attribute_id, value): + """Sets the user attribute value for a specific group. + + Args: + sdk: The Looker SDK instance. + group_id: The ID of the group. + user_attribute_id: The ID of the user attribute. + value: The value to set. + """ + try: + body = [ + models.UserAttributeGroupValue( + group_id=group_id, + user_attribute_id=user_attribute_id, + value=value + ) + ] + sdk.set_user_attribute_group_values( + user_attribute_id=user_attribute_id, + body=body + ) + print(f"Successfully set user attribute {user_attribute_id} for group {group_id} to '{value}'") + except error.SDKError as e: + print(f"Error setting user attribute for group {group_id}: {e}") + except Exception as e: + print(f"An unexpected error occurred: {e}") + +def main(): + parser = argparse.ArgumentParser(description="Set user attribute value for a user or group in Looker.") + parser.add_argument("type", choices=["user", "group"], help="Whether to set the attribute for a 'user' or 'group'") + parser.add_argument("id", type=str, help="The ID of the user or group") + parser.add_argument("attribute_id", type=str, help="The ID of the user attribute") + parser.add_argument("value", help="The value to set for the user attribute") + + args = parser.parse_args() + + try: + sdk = looker_sdk.init40("looker.ini") + except error.SDKError as e: + print(f"Error initializing Looker SDK: {e}") + return + + if args.type == "user": + set_user_attribute_value(sdk, args.id, args.attribute_id, args.value) + else: # args.type == "group" + set_group_attribute_value(sdk, args.id, args.attribute_id, args.value) + +if __name__ == "__main__": + main()