Edits history of script submission #22666 for ' Terminate Cluster (databricks)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_cluster_id = string
    
    // Dropdown of the workspace's clusters so users pick by name instead of typing the id.
    export async function cluster_id(auth: RT.Databricks) {
      const base = auth.workspace_url.replace(/\/$/, "")
      const response = await fetch(`${base}/api/2.1/clusters/list`, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { clusters } = (await response.json()) as {
        clusters?: { cluster_id: string; cluster_name?: string; state?: string }[]
      }
      return (clusters ?? []).map((c) => ({
        value: c.cluster_id,
        label: `${c.cluster_name ?? c.cluster_id} (${c.state ?? "?"})`,
      }))
    }
    
    /**
     * Terminate Cluster
     * Terminate (stop) a running cluster. The cluster definition is retained and can be restarted later; this is not a permanent delete.
     */
    export async function main(
      auth: RT.Databricks,
      cluster_id: DynSelect_cluster_id
    ) {
      const base = auth.workspace_url.replace(/\/$/, "")
      const url = new URL(`${base}/api/2.1/clusters/delete`)
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({ cluster_id }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 days ago