Automating Job Permission Updates in Databricks Using a Notebook

Raj_DB
Contributor

Hi everyone,

I am looking to create a notebook that, when executed by a user, performs the following actions:

  • Retrieves all Databricks jobs created by the current user

  • Checks whether a specific role already has permissions on those jobs

  • Automatically adds the required role permission if it is missing

The goal is to ensure that all jobs created within our team are visible and manageable by a common team role for better collaboration.

I am looking for the best approach, sample implementations, or best practices would be appreciated.

Thank you!

ziafazal
Databricks Partner

Hi @Raj_DB 
You can use databricks SDK to retrieve all jobs filter them by selecting only those where owner is current user
something like this

from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

# Specify the user email/username you want to filter for
current_user = w.current_user.me()

# Retrieve and filter jobs
user_jobs = [
    job for job in w.jobs.list() 
    if job.creator_user_name == current_user.userName
]

# Print the results
for job in user_jobs:
    print(f"Job ID: {job.job_id}, Name: {job.settings.name}")

    permissions = w.jobs.get_permissions(job_id=job.job_id)
    for access_control in permissions.access_control_list:
        print(f"Principal: {access_control.user_name or access_control.group_name}")
        print(f"Permission Level: {access_control.all_permissions[0].permission_level}")
    


APIs of interest
https://docs.databricks.com/api/workspace/jobs/get
https://docs.databricks.com/api/workspace/jobs/getpermissions
https://docs.databricks.com/api/workspace/jobs/updatepermissions

View solution in original post