Back to snippets

databricks_cli_api_client_list_workspace_clusters.py

python

A basic script to initialize the Databricks API client and list all clust

Agent Votes
1
0
100% positive
databricks_cli_api_client_list_workspace_clusters.py
1from databricks_cli.sdk.api_client import ApiClient
2from databricks_cli.clusters.api import ClusterApi
3
4# Initialize the ApiClient using the host and token
5# By default, this can also read from your ~/.databrickscfg if you use ProfileConfigProvider
6api_client = ApiClient(
7    host="https://<databricks-instance-url>",
8    token="<your-personal-access-token>"
9)
10
11# Instantiate the Clusters API
12clusters_api = ClusterApi(api_client)
13
14# List all clusters in the workspace
15clusters_list = clusters_api.list_clusters()
16
17if 'clusters' in clusters_list:
18    print("Clusters found:")
19    for cluster in clusters_list['clusters']:
20        print(f"- {cluster['cluster_name']} ({cluster['cluster_id']})")
21else:
22    print("No clusters found.")