Files
MikrotikManager/backend/src/services/container-removal.ts
T

27 lines
1.5 KiB
TypeScript

import { z } from "zod"
import { containerRuntimeStatus } from "./container-runtime-status.js"
const rowsSchema = z.array(z.object({ ".id": z.string().min(1) }).passthrough()).superRefine((rows, ctx) => {
if (new Set(rows.map(row => row[".id"])).size !== rows.length) ctx.addIssue({ code: "custom", message: "Дубли RouterOS ID" })
})
export class ContainerRemovalConflict extends Error {}
type Client = {
get(path: string): Promise<unknown>
delete(path: string): Promise<unknown>
}
/** Never stop implicitly: removal requires a fresh, unambiguous stopped state. */
export async function removeStoppedContainer(client: Client, rosId: string): Promise<void> {
const rows = rowsSchema.parse(await client.get("/container"))
const target = rows.find(row => row[".id"] === rosId)
if (!target) throw new ContainerRemovalConflict("Контейнер уже отсутствует. Обновите каталог.")
if (containerRuntimeStatus(target) !== "stopped") {
throw new ContainerRemovalConflict("Удаление доступно только после подтверждённой остановки контейнера. Остановите его и обновите каталог.")
}
await client.delete(`/container/${encodeURIComponent(rosId)}`)
const after = rowsSchema.parse(await client.get("/container"))
if (after.some(row => row[".id"] === rosId)) throw new Error("Удаление не подтверждено повторным чтением RouterOS. Обновите каталог перед новым действием.")
}