diff --git a/README.md b/README.md index 22eaa4dc..c062b18e 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,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! 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() 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 * * *") + + 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!")])) 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")