import type { MikrotikClient } from "./mikrotik.js" import { nativeBgpRows, type NativeBgpRow } from "./bgp-export-model.js" import { readOspfEffectiveDisabled } from "./ospf-effective-disabled.js" import { McpError } from "../modules/mcp/security.js" /** Prove a configured peer, including RouterOS releases that omit the D flag. */ export async function readStaticBgpConnection(client: Pick, id: string) { const connections = nativeBgpRows(await client.get("/routing/bgp/connection")), target = connections.find(row => row[".id"] === id) if (!target) throw new McpError("NOT_FOUND", "BGP-подключение не найдено", 404) const [connection] = await readOspfEffectiveDisabled([target], nativeId => client.post("/routing/bgp/connection/get", { ".id": nativeId, "value-name": "disabled" })) if (!["false", "no", false, "true", "yes", true].includes(connection.disabled as string | boolean)) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён disabled BGP-подключения", 409) connection.disabled = ["true", "yes", true].includes(connection.disabled as string | boolean) ? "true" : "false" if (connection.dynamic === undefined) { const value = await client.post("/routing/bgp/connection/get", { ".id": id, "value-name": "dynamic" }) if (Array.isArray(value) && value.length === 0) { const selected = nativeBgpRows(await client.post("/routing/bgp/connection/print", { ".proplist": ".id,dynamic", ".query": [".id=" + id, "-dynamic", "#&"] })) if (selected.length !== 1 || selected[0][".id"] !== id || selected[0].dynamic !== undefined && !["false", "no", false].includes(selected[0].dynamic as string | boolean)) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён static/dynamic BGP-подключения по native query", 409) connection.dynamic = "false" } else { if (!value || typeof value !== "object" || !("ret" in value) || typeof value.ret !== "string" || !["true", "false", "yes", "no"].includes(value.ret)) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён static/dynamic BGP-подключения", 409) connection.dynamic = value.ret } } if (["true", "yes", true].includes(connection.dynamic as string | boolean)) throw new McpError("STATE_CONFLICT", "Dynamic BGP connection не изменяется", 409) if (!["false", "no", false].includes(connection.dynamic as string | boolean)) throw new McpError("STATE_UNAVAILABLE", "Не подтверждён static/dynamic BGP-подключения", 409) connection.dynamic = "false" return { connection, connections } }