0

Terminate Cluster

by
Published 4 days ago

Terminate (stop) a running cluster. The cluster definition is retained and can be restarted later; this is not a permanent delete.

Script databricks Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 days ago
1
//native
2

3
export type DynSelect_cluster_id = string
4

5
// Dropdown of the workspace's clusters so users pick by name instead of typing the id.
6
export async function cluster_id(auth: RT.Databricks) {
7
  const base = auth.workspace_url.replace(/\/$/, "")
8
  const response = await fetch(`${base}/api/2.1/clusters/list`, {
9
    headers: {
10
      Authorization: `Bearer ${auth.token}`,
11
      Accept: "application/json",
12
    },
13
  })
14
  if (!response.ok) {
15
    throw new Error(`${response.status} ${await response.text()}`)
16
  }
17
  const { clusters } = (await response.json()) as {
18
    clusters?: { cluster_id: string; cluster_name?: string; state?: string }[]
19
  }
20
  return (clusters ?? []).map((c) => ({
21
    value: c.cluster_id,
22
    label: `${c.cluster_name ?? c.cluster_id} (${c.state ?? "?"})`,
23
  }))
24
}
25

26
/**
27
 * Terminate Cluster
28
 * Terminate (stop) a running cluster. The cluster definition is retained and can be restarted later; this is not a permanent delete.
29
 */
30
export async function main(
31
  auth: RT.Databricks,
32
  cluster_id: DynSelect_cluster_id
33
) {
34
  const base = auth.workspace_url.replace(/\/$/, "")
35
  const url = new URL(`${base}/api/2.1/clusters/delete`)
36

37
  const response = await fetch(url, {
38
    method: "POST",
39
    headers: {
40
      Authorization: `Bearer ${auth.token}`,
41
      "Content-Type": "application/json",
42
      Accept: "application/json",
43
    },
44
    body: JSON.stringify({ cluster_id }),
45
  })
46

47
  if (!response.ok) {
48
    throw new Error(`${response.status} ${await response.text()}`)
49
  }
50

51
  return await response.json()
52
}
53