28 lines
1.5 KiB
TypeScript
28 lines
1.5 KiB
TypeScript
import { observedBoolean } from "./ospf-observed-values.js"
|
|
|
|
/** Native identity must exist before commands or configuration snapshots refer to it. */
|
|
export function readGreNativeRows(raw: unknown): Array<Record<string, string>> {
|
|
if (!Array.isArray(raw) || raw.some(row => !row || typeof row !== "object" || Array.isArray(row)
|
|
|| Object.values(row).some(value => typeof value !== "string")
|
|
|| [".id", "name"].some(key => typeof row[key] !== "string" || !row[key].trim())
|
|
|| row[".id"] !== row[".id"].trim())) {
|
|
throw new Error("Некорректный каталог GRE: отсутствует native ID/имя или повреждены поля")
|
|
}
|
|
if (new Set(raw.map(row => row[".id"])).size !== raw.length
|
|
|| new Set(raw.map(row => row.name.trim())).size !== raw.length) {
|
|
throw new Error("Повторяющиеся native ID или имена GRE")
|
|
}
|
|
return raw as Array<Record<string, string>>
|
|
}
|
|
|
|
/** Disabled is authoritative; an enabled interface also needs a known running flag. */
|
|
export function readGreObservedState(flags: { running?: string; disabled?: string }, name: string) {
|
|
const disabled = observedBoolean(flags.disabled)
|
|
const running = observedBoolean(flags.running)
|
|
if (disabled === null || (disabled === false && running === null)) {
|
|
throw new Error(`Не подтверждено состояние GRE ${name}`)
|
|
}
|
|
const status: "up" | "down" | "degraded" = disabled ? "down" : running ? "up" : "degraded"
|
|
return { disabled, status }
|
|
}
|