//native
async function connect(auth: RT.F5Bigip): Promise<{
base: string
headers: { [key: string]: string }
release: () => Promise<void>
}> {
const base = auth.base_url.replace(/\/+$/, "")
if (auth.auth_method === "basic") {
return {
base,
headers: {
Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
},
release: async () => {},
}
}
const login = await fetch(`${base}/mgmt/shared/authn/login`, {
method: "POST",
headers: { "Content-Type": "application/json", Accept: "application/json" },
body: JSON.stringify({
username: auth.username,
password: auth.password,
loginProviderName: auth.login_provider_name || "tmos",
}),
})
if (!login.ok) {
throw new Error(`${login.status} ${await login.text()}`)
}
const token: string = (await login.json()).token.token
return {
base,
headers: { "X-F5-Auth-Token": token },
release: async () => {
await fetch(
`${base}/mgmt/shared/authz/tokens/${encodeURIComponent(token)}`,
{
method: "DELETE",
headers: { "X-F5-Auth-Token": token },
}
)
},
}
}
/**
* List virtual servers
* List LTM virtual servers, one page at a time ($top / $skip). 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).
*/
export async function main(
auth: RT.F5Bigip,
partition: string | undefined,
top: number | undefined,
skip: number | undefined
) {
const f5 = await connect(auth)
try {
const url = new URL(`${f5.base}/mgmt/tm/ltm/virtual`)
const pageSize = top || 100
const offset = skip || 0
url.searchParams.append("$top", String(pageSize))
url.searchParams.append("$skip", String(offset))
if (partition != null && partition !== "") {
url.searchParams.append("$filter", `partition eq ${partition}`)
}
const response = await fetch(url, {
headers: { ...f5.headers, Accept: "application/json" },
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const page = await response.json()
const count = (page.items ?? []).length
// More pages: per totalItems when BIG-IP returns it, else when the page is full
// (f5-ansible pages $top/$skip until an empty page)
const more =
count > 0 &&
(page.totalItems != null
? offset + count < page.totalItems
: count === pageSize)
return { ...page, next_skip: more ? offset + count : null }
} finally {
await f5.release()
}
}
Submitted by hugo989 7 hours ago