0

List groups

by
Published today

List configuration group names, one page at a time, with next_offset for the following page.

Script aruba_central_onprem Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 hours ago
1
//native
2

3
/**
4
 * List groups
5
 * List configuration group names, one page at a time (max 100 per page). Returns { total, data } plus next_offset (null on the last page).
6
 */
7
export async function main(
8
  auth: RT.ArubaCentralOnprem,
9
  limit: number | undefined,
10
  offset: number | undefined
11
) {
12
  const token = await getAccessToken(auth, "read")
13
  const base = auth.base_url.replace(/\/+$/, "")
14
  const pageSize = limit || 20
15
  const start = offset || 0
16

17
  const url = new URL(`${base}/configuration/v2/groups`)
18
  url.searchParams.append("limit", String(pageSize))
19
  url.searchParams.append("offset", String(start))
20

21
  const response = await fetch(url, {
22
    headers: {
23
      Authorization: `Bearer ${token}`,
24
      Accept: "application/json",
25
    },
26
  })
27
  if (!response.ok) {
28
    throw new Error(`${response.status} ${await response.text()}`)
29
  }
30

31
  const page = (await response.json()) as { total: number; data: string[] }
32
  const next = start + page.data.length
33
  return {
34
    ...page,
35
    next_offset: page.data.length > 0 && next < page.total ? next : null,
36
  }
37
}
38

39
// OAuth grant mechanism documented for Central On-Premises (API Gateway > OAuth grant):
40
// login -> authorization code -> access token. Tokens last 2 hours, so a fresh one is minted per run.
41
async function getAccessToken(
42
  auth: RT.ArubaCentralOnprem,
43
  scope: "read" | "all"
44
): Promise<string> {
45
  const base = auth.base_url.replace(/\/+$/, "")
46

47
  const loginUrl = new URL(`${base}/oauth2/authorize/central/api/login`)
48
  loginUrl.searchParams.append("client_id", auth.client_id)
49
  const login = await fetch(loginUrl, {
50
    method: "POST",
51
    headers: { "Content-Type": "application/json", Accept: "application/json" },
52
    body: JSON.stringify({ username: auth.username, password: auth.password }),
53
  })
54
  if (!login.ok) {
55
    throw new Error(`login: ${login.status} ${await login.text()}`)
56
  }
57
  const cookies = login.headers.get("set-cookie") ?? ""
58
  const csrf = cookies.match(/csrftoken=([^;,\s]+)/)?.[1]
59
  const session = cookies.match(/session=([^;,\s]+)/)?.[1]
60
  if (!csrf || !session) {
61
    throw new Error(
62
      "login: response did not set the csrftoken and session cookies"
63
    )
64
  }
65

66
  const authorizeUrl = new URL(`${base}/oauth2/authorize/central/api`)
67
  authorizeUrl.searchParams.append("client_id", auth.client_id)
68
  authorizeUrl.searchParams.append("response_type", "code")
69
  authorizeUrl.searchParams.append("scope", scope)
70
  const authorize = await fetch(authorizeUrl, {
71
    method: "POST",
72
    headers: {
73
      "Content-Type": "application/json",
74
      Accept: "application/json",
75
      Cookie: `session=${session}`,
76
      "X-CSRF-Token": csrf,
77
    },
78
    body: JSON.stringify({ customer_id: auth.customer_id }),
79
  })
80
  if (!authorize.ok) {
81
    throw new Error(`authorize: ${authorize.status} ${await authorize.text()}`)
82
  }
83
  const { auth_code } = (await authorize.json()) as { auth_code: string }
84

85
  const tokenUrl = new URL(`${base}/oauth2/token`)
86
  tokenUrl.searchParams.append("client_id", auth.client_id)
87
  tokenUrl.searchParams.append("client_secret", auth.client_secret)
88
  tokenUrl.searchParams.append("grant_type", "authorization_code")
89
  tokenUrl.searchParams.append("code", auth_code)
90
  const token = await fetch(tokenUrl, {
91
    method: "POST",
92
    headers: { Accept: "application/json" },
93
  })
94
  if (!token.ok) {
95
    throw new Error(`token: ${token.status} ${await token.text()}`)
96
  }
97
  const { access_token } = (await token.json()) as { access_token: string }
98
  return access_token
99
}
100