1 | |
2 |
|
3 | |
4 | * List network devices |
5 | * List one page of network access devices (NADs). Filters use ERS syntax field.OPERATOR.value on ipaddress, name, description, location or type (e.g. name.STARTSW.sw-). size is up to 100 (default 20); pages start at 1. Pass next_page from the result to get the following page. |
6 | */ |
7 | export async function main( |
8 | auth: RT.CiscoIse, |
9 | page: number | undefined, |
10 | size: number | undefined, |
11 | filters: string[] | undefined, |
12 | filter_type: "and" | "or" | undefined, |
13 | sort_asc: "name" | "description" | undefined, |
14 | sort_desc: "name" | "description" | undefined |
15 | ) { |
16 | const base = new URL(auth.base_url) |
17 | if (auth.ers_port) base.port = String(auth.ers_port) |
18 | const url = new URL("/ers/config/networkdevice", base) |
19 | if (page != null) url.searchParams.append("page", String(page)) |
20 | if (size != null) url.searchParams.append("size", String(size)) |
21 | for (const f of filters ?? []) { |
22 | if (f) url.searchParams.append("filter", f) |
23 | } |
24 | if (filter_type) url.searchParams.append("filterType", filter_type) |
25 | if (sort_asc) url.searchParams.append("sortasc", sort_asc) |
26 | if (sort_desc) url.searchParams.append("sortdsc", sort_desc) |
27 |
|
28 | const response = await fetch(url, { |
29 | headers: { |
30 | Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
31 | Accept: "application/json", |
32 | }, |
33 | }) |
34 |
|
35 | if (!response.ok) { |
36 | throw new Error(`${response.status} ${await response.text()}`) |
37 | } |
38 |
|
39 | const { SearchResult } = await response.json() |
40 | const pageOf = (link: { href?: string } | undefined) => { |
41 | const p = link?.href ? new URL(link.href).searchParams.get("page") : null |
42 | return p ? Number(p) : null |
43 | } |
44 | return { |
45 | total: SearchResult.total, |
46 | resources: SearchResult.resources ?? [], |
47 | next_page: pageOf(SearchResult.nextPage), |
48 | previous_page: pageOf(SearchResult.previousPage), |
49 | } |
50 | } |
51 |
|