0

List nodes for a cluster

by
Published Oct 17, 2025

Sort order: Region name, node name Can be used by the following roles assigned at the organization, folder or cluster scope: - ORG_ADMIN - CLUSTER_ADMIN - CLUSTER_OPERATOR_WRITER - CLUSTER_DEVELOPER

Script cockroachdb Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Cockroachdb = {
3
  token: string;
4
};
5
/**
6
 * List nodes for a cluster
7
 * Sort order: Region name, node name
8

9
Can be used by the following roles assigned at the organization, folder or cluster scope:
10
- ORG_ADMIN
11
- CLUSTER_ADMIN
12
- CLUSTER_OPERATOR_WRITER
13
- CLUSTER_DEVELOPER
14

15
 */
16
export async function main(
17
  auth: Cockroachdb,
18
  cluster_id: string,
19
  region_name: string | undefined,
20
  pagination_page: string | undefined,
21
  pagination_limit: string | undefined,
22
  pagination_as_of_time: string | undefined,
23
  pagination_sort_order: "ASC" | "DESC" | undefined,
24
) {
25
  const url = new URL(
26
    `https://cockroachlabs.cloud/api/v1/clusters/${cluster_id}/nodes`,
27
  );
28
  for (const [k, v] of [
29
    ["region_name", region_name],
30
    ["pagination.page", pagination_page],
31
    ["pagination.limit", pagination_limit],
32
    ["pagination.as_of_time", pagination_as_of_time],
33
    ["pagination.sort_order", pagination_sort_order],
34
  ]) {
35
    if (v !== undefined && v !== "" && k !== undefined) {
36
      url.searchParams.append(k, v);
37
    }
38
  }
39
  const response = await fetch(url, {
40
    method: "GET",
41
    headers: {
42
      Authorization: "Bearer " + auth.token,
43
    },
44
    body: undefined,
45
  });
46
  if (!response.ok) {
47
    const text = await response.text();
48
    throw new Error(`${response.status} ${text}`);
49
  }
50
  return await response.json();
51
}
52