0

List pools

Published 20 days ago

List LTM pools, paged with top (default 100) and skip, optionally restricted to one partition and with members inlined. Returns items, nextLink, totalItems and next_skip.

Script f5_bigip Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 21 days ago
1
//native
2

3
async function connect(auth: RT.F5Bigip): Promise<{
4
  base: string
5
  headers: { [key: string]: string }
6
  release: () => Promise<void>
7
}> {
8
  const base = auth.base_url.replace(/\/+$/, "")
9
  if (auth.auth_method === "basic") {
10
    return {
11
      base,
12
      headers: {
13
        Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
14
      },
15
      release: async () => {},
16
    }
17
  }
18
  const login = await fetch(`${base}/mgmt/shared/authn/login`, {
19
    method: "POST",
20
    headers: { "Content-Type": "application/json", Accept: "application/json" },
21
    body: JSON.stringify({
22
      username: auth.username,
23
      password: auth.password,
24
      loginProviderName: auth.login_provider_name || "tmos",
25
    }),
26
  })
27
  if (!login.ok) {
28
    throw new Error(`${login.status} ${await login.text()}`)
29
  }
30
  const token: string = (await login.json()).token.token
31
  return {
32
    base,
33
    headers: { "X-F5-Auth-Token": token },
34
    release: async () => {
35
      await fetch(
36
        `${base}/mgmt/shared/authz/tokens/${encodeURIComponent(token)}`,
37
        {
38
          method: "DELETE",
39
          headers: { "X-F5-Auth-Token": token },
40
        }
41
      )
42
    },
43
  }
44
}
45

46
/**
47
 * List pools
48
 * List LTM pools, one page at a time ($top / $skip), optionally with their members inlined (expandSubcollections). The response keeps BIG-IP's items, nextLink and totalItems, plus next_skip to pass as skip for the next page (null on the last page).
49
 */
50
export async function main(
51
  auth: RT.F5Bigip,
52
  partition: string | undefined,
53
  include_members: boolean | undefined,
54
  top: number | undefined,
55
  skip: number | undefined
56
) {
57
  const f5 = await connect(auth)
58
  try {
59
    const url = new URL(`${f5.base}/mgmt/tm/ltm/pool`)
60
    const pageSize = top || 100
61
    const offset = skip || 0
62
    if (include_members) {
63
      url.searchParams.append("expandSubcollections", "true")
64
    }
65
    url.searchParams.append("$top", String(pageSize))
66
    url.searchParams.append("$skip", String(offset))
67
    if (partition != null && partition !== "") {
68
      url.searchParams.append("$filter", `partition eq ${partition}`)
69
    }
70

71
    const response = await fetch(url, {
72
      headers: { ...f5.headers, Accept: "application/json" },
73
    })
74

75
    if (!response.ok) {
76
      throw new Error(`${response.status} ${await response.text()}`)
77
    }
78

79
    const page = await response.json()
80
    const count = (page.items ?? []).length
81
    // More pages: per totalItems when BIG-IP returns it, else when the page is full
82
    // (f5-ansible pages $top/$skip until an empty page)
83
    const more =
84
      count > 0 &&
85
      (page.totalItems != null
86
        ? offset + count < page.totalItems
87
        : count === pageSize)
88
    return { ...page, next_skip: more ? offset + count : null }
89
  } finally {
90
    await f5.release()
91
  }
92
}
93