0

Start SQL Warehouse

by
Published 4 days ago

Start a stopped SQL warehouse. Returns {} on success; the warehouse transitions through STARTING 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_warehouse_id = string
4

5
// Dropdown of the workspace's SQL warehouses so users pick by name instead of typing the id.
6
export async function warehouse_id(auth: RT.Databricks) {
7
  const base = auth.workspace_url.replace(/\/$/, "")
8
  const response = await fetch(`${base}/api/2.0/sql/warehouses`, {
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 { warehouses } = (await response.json()) as {
18
    warehouses?: { id: string; name?: string; state?: string }[]
19
  }
20
  return (warehouses ?? []).map((w) => ({
21
    value: w.id,
22
    label: `${w.name ?? w.id} (${w.state ?? "?"})`,
23
  }))
24
}
25

26
/**
27
 * Start SQL Warehouse
28
 * Start a stopped SQL warehouse. Returns {} on success; the warehouse transitions through STARTING to RUNNING asynchronously.
29
 */
30
export async function main(
31
  auth: RT.Databricks,
32
  warehouse_id: DynSelect_warehouse_id
33
) {
34
  const base = auth.workspace_url.replace(/\/$/, "")
35
  const url = new URL(`${base}/api/2.0/sql/warehouses/${warehouse_id}/start`)
36

37
  const response = await fetch(url, {
38
    method: "POST",
39
    headers: {
40
      Authorization: `Bearer ${auth.token}`,
41
      Accept: "application/json",
42
    },
43
  })
44

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

49
  const text = await response.text()
50
  return text ? JSON.parse(text) : { success: true }
51
}
52