Edits history of script submission #22664 for ' Start SQL Warehouse (databricks)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_warehouse_id = string
    
    // Dropdown of the workspace's SQL warehouses so users pick by name instead of typing the id.
    export async function warehouse_id(auth: RT.Databricks) {
      const base = auth.workspace_url.replace(/\/$/, "")
      const response = await fetch(`${base}/api/2.0/sql/warehouses`, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { warehouses } = (await response.json()) as {
        warehouses?: { id: string; name?: string; state?: string }[]
      }
      return (warehouses ?? []).map((w) => ({
        value: w.id,
        label: `${w.name ?? w.id} (${w.state ?? "?"})`,
      }))
    }
    
    /**
     * Start SQL Warehouse
     * Start a stopped SQL warehouse. Returns {} on success; the warehouse transitions through STARTING to RUNNING asynchronously.
     */
    export async function main(
      auth: RT.Databricks,
      warehouse_id: DynSelect_warehouse_id
    ) {
      const base = auth.workspace_url.replace(/\/$/, "")
      const url = new URL(`${base}/api/2.0/sql/warehouses/${warehouse_id}/start`)
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const text = await response.text()
      return text ? JSON.parse(text) : { success: true }
    }
    

    Submitted by hugo989 5 days ago