Disabling user schedules

 

 

from looker_sdk import models,sdk
sdk = looker_sdk.init40(config_file='../looker.ini', section='LOOKER')

'''
To use, please follow the instructions below.

1) Create a looker.ini file with 
    [LOOKER]
    # Base URL for API. Do not include /api/* in the url. If hosted on GCP, remove the :19999 leaving just https://your.cloud.looker.com
    base_url=https://your.cloud.looker.com
    # API  client id
    client_id=#######################
    # API  client secret
    client_secret=########################
    # Set to false if testing locally against self-signed certs. Otherwise leave True
    verify_ssl=True
'''

def disable_user_schedules(user_id, enabled): 
    """ Pause all schedules of a particular user id
    Args:    
    user_id (int) 
    enabled (bool): "False" to pause schedule
    
    Notes: Schedules with "enabled = False" will disappear from Admin > Schedules in Looker UI , that is the reason it is important to save the schedules data for potential future use in enabling the schedules.
    """
    updated_schedules= []
  
    fields = ["user_id", "id", "enabled", "look_id", "dashboard_id", "lookml_dashboard_id"]
    # Find all schedules of a particular user (this function only retrieves schedules that are currently enabled.) 
    schedule_plans = sdk.all_scheduled_plans(user_id=user_id,fields=",".join(fields))
    if not schedule_plans:
        print(f"No schedules found for user {user_id}")
    else:
        for schedule in schedule_plans:
            try:
                updated_schedule = sdk.update_scheduled_plan(schedule.id, body=models.WriteScheduledPlan(enabled=enabled)) # pause/resume a schedule
                updated_schedule_dict = {field: updated_schedule.get(field) for field in fields if field in updated_schedule}
                content_types = {"look_id": "look", "dashboard_id": "dashboard", "lookml_dashboard_id": "lookml_dashboard"}
                for key, value in content_types.items():
                    if key in updated_schedule_dict:
                        updated_schedule_dict['content_id'] = updated_schedule_dict.pop(key)
                        updated_schedule_dict['content_type'] = value
                        break
                updated_schedule_dict['updated_timestamp'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                updated_schedules.append(updated_schedule_dict)
            except Exception as e:
                print(f"Error occurred while updating schedule {schedule.id}: {e}")
                continue
        print(f"Successfully set enabled: {enabled} for all schedules of user {user_id}")
    return updated_schedules

 

 

Save the result of the function disable_user_schedules to a file or database for potential future use in enabling the schedules.

 

0 0 38