Updating destinations from all Schedules (Bulk) using the Python SDK

Sometimes an admin would like to remove a destination (or a list of them) from the schedules of an instance, however, sometimes the number of schedules is so great that doing that change manually is just too much.

There are no endpoints/calls on the API to remove destinations like that, so we need to use the Update Scheduled Plan endpoint for this, essentially we need to do this:

  1. We need to fetch all the schedules of the instance
  2. For every schedule found, we need to compare the destination addresses to the addresses that we want to remove
  3. The destinations that match with the unwanted addresses need to be removed from the destination list, so we can feed the new destination list back to the update call

There could be also the case that the destination list is left empty after this process, so it depends on the admin, what to do with the schedules that end up with such a list.

The following script is a draft of what could be used (as a base) for a process like that, using the Looker Python SDK

 

import looker_sdk

sdk = looker_sdk.init31('looker.ini')

def remove_destinations_from_schedules(addresses_to_remove):

# Get all schedules on the instance
schedules = sdk.all_scheduled_plans(all_users=True)

# Variable for code readibility (line length for Community Post)
spd = 'scheduled_plan_destination'

# Iterates through all the schedules
for schedule in schedules:

# A schedule must always have at least one destination, we can get the
# scheduled_plan_id from the 1st destination on the list of
# destinations this id is needed to update/delete a schedule with the
# SDK
scheduled_plan_id = schedule[spd][0]['scheduled_plan_id']

# We could get the length of the destination list to identify if a
# change was made
destination_count = len(schedule[spd])

# We remove the destinations on the list of destinations that have an
# address matching the addresses we want to remove.
schedule[spd] = [destination for destination in schedule[spd]
if destination['address'] not in addresses_to_remove]

# We need to make sure that the destination list is not empty after
# removing the destinations/addresses that we did not want anymore, in
# this case we could delete the Schedule
if not schedule[spd]:
print(f'Deleting Schedule ({scheduled_plan_id}), no destinations\n')
res = sdk.delete_scheduled_plan(scheduled_plan_id=scheduled_plan_id)
print(f'Response: {res}\n\n')
# If the destination list had no changes on the number of elements,
# then nothing needs to be done on that schedule
elif destination_count == len(schedule[spd]):
print(f'Schedule ({scheduled_plan_id}), no matches -> no update\n\n')
# If the list of destinations is not empty, we update the Schedule with
# the new list of destinations
else:
print(f'Updating Schedule ({scheduled_plan_id}), match(es) found!\n')
res = sdk.update_scheduled_plan(scheduled_plan_id=scheduled_plan_id,
body=schedule)
print(f'Response: {res}\n\n')

# MAIN
if __name__ == "__main__":
remove_destinations_from_schedules(['foo@looker.com', 'bar@google.com'])
1 0 216