1 | |
2 |
|
3 | |
4 | * List devices |
5 | * List the devices in the CloudVision inventory (resource API inventory.v1) with their device ID (serial number), hostname, model, EOS version, system MAC address and telemetry streaming status. Filter on streaming status if needed. |
6 | */ |
7 | export async function main( |
8 | auth: RT.AristaCloudvision, |
9 | streaming_status: "any" | "active" | "inactive" = "any" |
10 | ) { |
11 | const response = await fetch( |
12 | new URL("/api/resources/inventory/v1/Device/all", auth.base_url), |
13 | { |
14 | headers: { |
15 | Authorization: `Bearer ${auth.token}`, |
16 | Accept: "application/json", |
17 | }, |
18 | } |
19 | ) |
20 | const text = await response.text() |
21 | if (response.status === 401 || response.status === 403) { |
22 | throw new Error( |
23 | `CloudVision refused the token (HTTP ${response.status}): check that the service-account token is valid, not expired, and that its role allows this call. ${text}` |
24 | ) |
25 | } |
26 | if (!response.ok) { |
27 | throw new Error( |
28 | `CloudVision Device/all failed: HTTP ${response.status} ${text.slice(0, 500)}` |
29 | ) |
30 | } |
31 | |
32 | const devices = [] |
33 | for (const line of text.split("\n")) { |
34 | if (!line.trim()) continue |
35 | if (!line.trim().startsWith("{")) { |
36 | throw new Error( |
37 | `CloudVision returned a non-JSON response for Device/all: check that base_url is the CloudVision address. ${text.slice(0, 500)}` |
38 | ) |
39 | } |
40 | const item = JSON.parse(line) |
41 | if (item.error) { |
42 | throw new Error( |
43 | `CloudVision Device/all failed: ${JSON.stringify(item.error)}` |
44 | ) |
45 | } |
46 | if (item.result?.value) devices.push(item.result.value) |
47 | } |
48 | const wanted = { |
49 | active: "STREAMING_STATUS_ACTIVE", |
50 | inactive: "STREAMING_STATUS_INACTIVE", |
51 | }[streaming_status as "active" | "inactive"] |
52 | return wanted ? devices.filter((d) => d.streamingStatus === wanted) : devices |
53 | } |
54 |
|