0

Start Cluster

by
Published 4 days ago

Start a terminated cluster. Returns {} on success; the cluster transitions through PENDING to RUNNING asynchronously.

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
 * Start Cluster
28
 * Start a terminated cluster. Returns {} on success; the cluster transitions through PENDING to RUNNING asynchronously.
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/start`)
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