Skip to content

Instantly share code, notes, and snippets.

@beniyama
Last active Sep 7, 2021
Embed
What would you like to do?
A sample script to disable looker users who haven't logged in for a while
import argparse
from datetime import datetime
from looker_sdk import client, models
DEFAULT_DAYS_TO_DISABLE = 180
def deactivate_users(days_to_disable):
role_cnt = {}
no_login_cnt = looker_user_cnt = active_user_cnt = errors = disabled_users = 0
sdk = client.setup("looker.ini")
my_id = sdk.me(fields="id")
all_users = sdk.all_users()
all_roles = sdk.all_roles()
for user in all_users:
user_id = user.id
if user.presumed_looker_employee:
looker_user_cnt += 1
elif not user.is_disabled:
active_user_cnt += 1
for role_id in user.role_ids:
if role_id in role_cnt:
role_cnt[role_id] += 1
else:
role_cnt[role_id] = 1
if user_id == my_id:
continue
login = user.credentials_email.logged_in_at
if login:
login_date = datetime.strptime(login[0:10], "%Y-%m-%d")
days_diff = (datetime.today() - login_date).days
if days_diff < days_to_disable:
continue
try:
res = input(f"{user.display_name} ({user.email})'s last login date is {login_date} ({days_diff} days before). Disable the user ?? y/N >>")
if res.lower() in ('y', 'ye', 'yes'):
sdk.update_user(user.id, body=models.WriteUser(is_disabled=True))
disabled_users += 1
print(f"...successfully disabled.")
else:
print(f"...skipped.")
except:
print(f"Deactivation process caused an error.")
errors += 1
else:
print(f"No login for {user.display_name} ({user.email})")
no_login_cnt += 1
print("-----------------------------")
print("---------- Summary ----------")
print("-----------------------------")
print(f" Total registered users: {len(all_users)} (incl. {looker_user_cnt} looker people)")
print(f" Active users: {active_user_cnt}")
print(f" No login users: {no_login_cnt}")
print("-----------------------------")
print(f" Users disabled in this operation: {disabled_users}")
print(f" Errors: {errors}")
print("-----------------------------")
for role in all_roles:
member_cnt = role_cnt[role.id] if role.id in role_cnt else 0
print(f" {role.name} : {member_cnt}")
print("-----------------------------")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--days", type=int, help=f"Number of inactive days before deactivation (default: {DEFAULT_DAYS_TO_DISABLE})")
args = parser.parse_args()
days_to_disable = args.days if args.days else DEFAULT_DAYS_TO_DISABLE
deactivate_users(days_to_disable)
# rename this to looker.ini
[Looker]
# API version is required
api_version=3.1
# Base URL for API. Do not include /api/* in the url
base_url=https://self-signed.looker.com:19999
# API 3 client id
client_id=YourClientID
# API 3 client secret
client_secret=YourClientSecret
# Set to false if testing locally against self-signed certs. Otherwise leave True
verify_ssl=True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment