UniFi integration: network devices, Wi-Fi and networks, as part of the dependency map
A UniFi controller (API key on Network 9+, or a read-only local login on any version) is read every five minutes. Its switches, access points and gateways are kept with state, IP, uptime, firmware and uplink; its networks and Wi-Fi names beside them (passphrases are never read). Nothing is written to the controller. Network > Network devices lists them. Devices are a new node type in the dependency map: uplinks become dependencies by themselves, others can be written by hand. A device offline past a limit is an offline alert, folds what depends on it, can be silenced with maintenance and have runbooks. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
73a06ebe63
commit
396e074108
48 files changed
+6893
-102
No files matched your search
@@ -2,7 +2,7 @@ import { Router, type Request, type Response } from "express";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { integrations, integrationCredentials, integrationTypes, servers } from "../db/schema.js";
|
||||
import { integrations, integrationCredentials, integrationTypes, servers, type IntegrationType } from "../db/schema.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { encryptSecret } from "../crypto.js";
|
||||
@@ -25,6 +25,8 @@ import { createPbsAdapter } from "../integrations/pbs/adapter.js";
|
||||
import { createOsTicketAdapter } from "../integrations/osticket/adapter.js";
|
||||
import { attachMatchedServers, summarizeMonitors, toMatchableServers } from "../services/uptimeKumaMatch.js";
|
||||
import { getSourceStatus } from "../services/integrationHealthMonitor.js";
|
||||
import { removeIntegrationData, syncUnifiIntegration } from "../services/unifiSync.js";
|
||||
import { invalidateAlertsCache } from "../services/alerts.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
import { ID_PATTERNS, isValidId } from "../utils/pathSegment.js";
|
||||
|
||||
@@ -60,6 +62,13 @@ integrationsRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
|
||||
const configValueSchema = z.union([z.string(), z.boolean()]);
|
||||
|
||||
/** What to tell the person when a form was submitted without something it needs. */
|
||||
function missingMessage(type: IntegrationType, missing: string[]): string {
|
||||
if (type === "unifi") return "UniFi needs an API key, or a username together with its password.";
|
||||
const labels = (INTEGRATION_FIELDS[type] ?? []).filter((f) => missing.includes(f.key)).map((f) => f.label);
|
||||
return `Fill in: ${labels.length > 0 ? labels.join(", ") : missing.join(", ")}.`;
|
||||
}
|
||||
|
||||
const createIntegrationSchema = z.object({
|
||||
type: z.enum(integrationTypes),
|
||||
name: z.string().min(1).max(200),
|
||||
@@ -79,7 +88,7 @@ integrationsRouter.post("/", requireRole("admin"), asyncHandler(async (req, res)
|
||||
|
||||
const missing = validateIntegrationConfig(type, config);
|
||||
if (missing.length > 0) {
|
||||
return res.status(400).json({ error: "missing_fields", fields: missing });
|
||||
return res.status(400).json({ error: "missing_fields", fields: missing, message: missingMessage(type, missing) });
|
||||
}
|
||||
|
||||
const { secretFields, nonSecretFields } = splitIntegrationConfig(type, config);
|
||||
@@ -114,6 +123,9 @@ integrationsRouter.post("/", requireRole("admin"), asyncHandler(async (req, res)
|
||||
detail: { type, name },
|
||||
});
|
||||
|
||||
// A new UniFi controller is read at once, so its equipment is there when the person opens the page (not waited for: a slow controller must not slow this).
|
||||
if (type === "unifi") void syncUnifiIntegration(created.id).then(() => invalidateAlertsCache());
|
||||
|
||||
res.status(201).json({
|
||||
integration: {
|
||||
id: created.id,
|
||||
@@ -213,6 +225,8 @@ integrationsRouter.patch("/:id", requireRole("admin"), asyncHandler(async (req,
|
||||
},
|
||||
});
|
||||
|
||||
if (updated.type === "unifi" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncUnifiIntegration(updated.id).then(() => invalidateAlertsCache());
|
||||
|
||||
res.json({
|
||||
integration: {
|
||||
id: updated.id,
|
||||
@@ -288,6 +302,7 @@ integrationsRouter.delete("/:id", requireRole("admin"), asyncHandler(async (req,
|
||||
.where(eq(servers.proxmoxIntegrationId, id))
|
||||
.returning({ id: servers.id });
|
||||
|
||||
if (existing.type === "unifi") await removeIntegrationData(id);
|
||||
await db.delete(integrations).where(eq(integrations.id, id));
|
||||
if (existing.credentialId) {
|
||||
await db.delete(integrationCredentials).where(eq(integrationCredentials.id, existing.credentialId));
|
||||
|
||||
Reference in new issue
Block a user