[Script] Disable inactive users

Hi all, I am sharing a script using Looker Python SDK to disable users based on a number of days users have not logged into Looker. Please note that the script is not supported by Looker team, so if you have questions, please post here. 

Link to run in Google Colab

import looker_sdk
from looker_sdk import models40
from looker_sdk import methods40
import os
from datetime import datetime

# Environment to log in
os.environ['LOOKERSDK_BASE_URL'] = 'foo'
os.environ['LOOKERSDK_CLIENT_ID'] = 'foo'
os.environ['LOOKERSDK_CLIENT_SECRET'] = 'foo'

#Initialize the SDK
sdk = looker_sdk.init40() # or init40() for v4.0 API
print(sdk.me()["first_name"] + " has logged in")

# Main function

def deactivate(auth_method, days):
'''Deactivate unengaged users who have not logged into a Looker instance in the past amount of days.

Args:
auth_method (str): Possible input are 'email', 'embed', 'google' (Google OAuth), 'ldap', 'looker_openid', saml', 'oidc', 'totp'
days(int): Number of days since last attempted UI login
'''

'''The all_users() method returns all users in a Looker instance. To use search patterns, use search_users(), or search_users_names()'''

users = sdk.all_users()

'''In Looker UI, a non-embed user usually has one primary authentication method (usually email or SSO). In calling API/SDK call, all authentication methods will be returned, with non-active authentication returned as an empty dictionary'''
auth = "credentials_" + auth_method

'''A user with a valid authentication method but never logged in has the value for "logged_in_at" as an empty string.'''
users_with_login_date = list(filter(lambda user:user[auth] is not None and user[auth]["logged_in_at"] != "", users))

if len(users_with_login_date) == 0:
return ("No users are using the provided authentication method")

else:
user_deactivated_count = 0
user_skip_count = 0
for user in users_with_login_date:
last_login_date_str = user[auth]["logged_in_at"][0:10]
last_login_date = datetime.strptime(last_login_date_str,'%Y-%m-%d')
days_since_last_login = abs(datetime.now() - last_login_date).days
if days_since_last_login > days:
sdk.update_user(user_id = user["id"],body=models40.WriteUser(is_disabled = False))
user_deactivated_count += 1
print("Disabled user id {user_id}, {email}, whose last login date is {date}".format(user_id = user["id"], email=user[auth]["email"],date=last_login_date_str))
else:
user_skip_count += 1
print("Skipped user ID {user_id}, {email}, whose last attempted login is {date}".format(user_id = user["id"],email=user[auth]["email"], date=last_login_date_str))

print("Deactivated {user_deactivated_count}users".format(user_deactivated_count=user_deactivated_count))
4 6 2,959