//native
/**
* List devices
* 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.
*/
export async function main(
auth: RT.AristaCloudvision,
streaming_status: "any" | "active" | "inactive" = "any"
) {
const response = await fetch(
new URL("/api/resources/inventory/v1/Device/all", auth.base_url),
{
headers: {
Authorization: `Bearer ${auth.token}`,
Accept: "application/json",
},
}
)
const text = await response.text()
if (response.status === 401 || response.status === 403) {
throw new Error(
`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}`
)
}
if (!response.ok) {
throw new Error(
`CloudVision Device/all failed: HTTP ${response.status} ${text.slice(0, 500)}`
)
}
// GetAll streams one JSON object per line: {"result": {"value": {...}}}.
const devices = []
for (const line of text.split("\n")) {
if (!line.trim()) continue
if (!line.trim().startsWith("{")) {
throw new Error(
`CloudVision returned a non-JSON response for Device/all: check that base_url is the CloudVision address. ${text.slice(0, 500)}`
)
}
const item = JSON.parse(line)
if (item.error) {
throw new Error(
`CloudVision Device/all failed: ${JSON.stringify(item.error)}`
)
}
if (item.result?.value) devices.push(item.result.value)
}
const wanted = {
active: "STREAMING_STATUS_ACTIVE",
inactive: "STREAMING_STATUS_INACTIVE",
}[streaming_status as "active" | "inactive"]
return wanted ? devices.filter((d) => d.streamingStatus === wanted) : devices
}
Submitted by hugo989 5 hours ago