|
32 | 32 | import sys |
33 | 33 |
|
34 | 34 | from databricks.sdk.oauth import OAuthClient, OidcEndpoints, get_workspace_endpoints |
| 35 | +from databricks.sdk.service.compute import ListClustersFilterBy, State |
35 | 36 |
|
36 | 37 | APP_NAME = "flask-demo" |
37 | 38 | all_clusters_template = """<ul> |
38 | | -{% for cluster in w.clusters.list() -%} |
| 39 | +{% for cluster in clusters -%} |
39 | 40 | <li><a |
40 | 41 | target="_blank" |
41 | | - href="{{ w.config.host }}/#setting/clusters/{{ cluster.cluster_id }}/configuration"> |
| 42 | + href="{{ workspace_host }}/#setting/clusters/{{ cluster.cluster_id }}/configuration"> |
42 | 43 | {{ cluster.cluster_name }}</a> is {{ cluster.state }}</li> |
43 | 44 | {% endfor %} |
44 | 45 | </ul>""" |
45 | 46 |
|
46 | 47 |
|
47 | | -def create_flask_app(host: str, oidc_endpoints: OidcEndpoints, client_id: str, client_secret: str, redirect_url: str): |
| 48 | +def create_flask_app(workspace_host: str, client_id: str, client_secret: str): |
48 | 49 | """The create_flask_app function creates a Flask app that is enabled with OAuth. |
49 | 50 |
|
50 | 51 | It initializes the app and web session secret keys with a randomly generated token. It defines two routes for |
@@ -72,29 +73,34 @@ def callback(): |
72 | 73 | def index(): |
73 | 74 | """The index page checks if the user has already authenticated and retrieves the user's credentials using |
74 | 75 | the Databricks SDK WorkspaceClient. It then renders the template with the clusters' list.""" |
| 76 | + oidc_endpoints = get_workspace_endpoints(workspace_host) |
| 77 | + port = request.environ.get("SERVER_PORT") |
| 78 | + redirect_url=f"http://localhost:{port}/callback" |
75 | 79 | if "creds" not in session: |
76 | 80 | oauth_client = OAuthClient(oidc_endpoints=oidc_endpoints, |
77 | 81 | client_id=client_id, |
78 | 82 | client_secret=client_secret, |
79 | 83 | redirect_url=redirect_url) |
80 | 84 | consent = oauth_client.initiate_consent() |
81 | 85 | session["consent"] = consent.as_dict() |
82 | | - return redirect(oidc_endpoints.authorization_endpoint) |
| 86 | + return redirect(consent.authorization_url) |
83 | 87 |
|
84 | 88 | from databricks.sdk import WorkspaceClient |
85 | 89 | from databricks.sdk.oauth import SessionCredentials |
86 | 90 |
|
87 | 91 | credentials_strategy = SessionCredentials.from_dict(session["creds"], |
88 | | - oidc_endpoints=oidc_endpoints, |
| 92 | + token_endpoint=oidc_endpoints.token_endpoint, |
89 | 93 | client_id=client_id, |
90 | 94 | client_secret=client_secret, |
91 | 95 | redirect_url=redirect_url) |
92 | | - workspace_client = WorkspaceClient(host=host, |
| 96 | + workspace_client = WorkspaceClient(host=workspace_host, |
93 | 97 | product=APP_NAME, |
94 | 98 | credentials_strategy=credentials_strategy, |
95 | 99 | ) |
96 | | - |
97 | | - return render_template_string(all_clusters_template, w=workspace_client) |
| 100 | + clusters = workspace_client.clusters.list( |
| 101 | + filter_by=ListClustersFilterBy(cluster_states=[State.RUNNING, State.PENDING]) |
| 102 | + ) |
| 103 | + return render_template_string(all_clusters_template, workspace_host=workspace_host, clusters=clusters) |
98 | 104 |
|
99 | 105 | return app |
100 | 106 |
|
@@ -137,12 +143,10 @@ def parse_arguments() -> argparse.Namespace: |
137 | 143 | logging.getLogger("databricks.sdk").setLevel(logging.DEBUG) |
138 | 144 |
|
139 | 145 | args = parse_arguments() |
140 | | - oidc_endpoints = get_workspace_endpoints(args.host) |
141 | 146 | client_id, client_secret = args.client_id, args.client_secret |
142 | 147 | if not client_id: |
143 | 148 | client_id, client_secret = register_custom_app(args) |
144 | | - redirect_url=f"http://localhost:{args.port}/callback" |
145 | | - app = create_flask_app(args.host, oidc_endpoints, client_id, client_secret, redirect_url) |
| 149 | + app = create_flask_app(args.host, client_id, client_secret) |
146 | 150 |
|
147 | 151 | app.run( |
148 | 152 | host="localhost", |
|
0 commit comments