Handle deleting targets

This commit is contained in:
Owen
2026-04-26 17:55:26 -07:00
parent 4ff811c5bd
commit 8ca72a39da
3 changed files with 92 additions and 96 deletions

View File

@@ -28,36 +28,18 @@ export async function addTargets(
{ incrementConfigVersion: true, compress: canCompress(version, "newt") } { incrementConfigVersion: true, compress: canCompress(version, "newt") }
); );
// Create a map for quick lookup const healthCheckTargets = healthCheckData.map((hc) => {
const healthCheckMap = new Map<number, TargetHealthCheck>();
healthCheckData.forEach((hc) => {
if (hc.targetId !== null) {
healthCheckMap.set(hc.targetId, hc);
}
});
const healthCheckTargets = targets.map((target) => {
const hc = healthCheckMap.get(target.targetId);
// If no health check data found, skip this target
if (!hc) {
logger.warn(
`No health check configuration found for target ${target.targetId}`
);
return null;
}
// Ensure all necessary fields are present // Ensure all necessary fields are present
const isTCP = hc.hcMode?.toLowerCase() === "tcp"; const isTCP = hc.hcMode?.toLowerCase() === "tcp";
if (!hc.hcHostname || !hc.hcPort || !hc.hcInterval) { if (!hc.hcHostname || !hc.hcPort || !hc.hcInterval) {
logger.debug( logger.debug(
`Skipping target ${target.targetId} due to missing health check fields` `Skipping hc ${hc.targetHealthCheckId} due to missing health check fields`
); );
return null; return null;
} }
if (!isTCP && (!hc.hcPath || !hc.hcMethod)) { if (!isTCP && (!hc.hcPath || !hc.hcMethod)) {
logger.debug( logger.debug(
`Skipping target ${target.targetId} due to missing HTTP health check fields` `Skipping hc ${hc.targetHealthCheckId} due to missing HTTP health check fields`
); );
return null; return null;
} }
@@ -105,7 +87,7 @@ export async function addTargets(
// Filter out any null values from health check targets // Filter out any null values from health check targets
const validHealthCheckTargets = healthCheckTargets.filter( const validHealthCheckTargets = healthCheckTargets.filter(
(target) => target !== null (hc) => hc !== null
); );
await sendToClient( await sendToClient(
@@ -213,6 +195,7 @@ export async function removeStandaloneHealthCheck(
export async function removeTargets( export async function removeTargets(
newtId: string, newtId: string,
targets: Target[], targets: Target[],
healthCheckData: TargetHealthCheck[],
protocol: string, protocol: string,
version?: string | null version?: string | null
) { ) {
@@ -234,8 +217,8 @@ export async function removeTargets(
{ incrementConfigVersion: true } { incrementConfigVersion: true }
); );
const healthCheckTargets = targets.map((target) => { const healthCheckTargets = healthCheckData.map((hc) => {
return target.targetId; return hc.targetHealthCheckId;
}); });
await sendToClient( await sendToClient(

View File

@@ -1,8 +1,8 @@
import { Request, Response, NextFunction } from "express"; import { Request, Response, NextFunction } from "express";
import { z } from "zod"; import { z } from "zod";
import { db } from "@server/db"; import { db, targetHealthCheck } from "@server/db";
import { newts, resources, sites, targets } from "@server/db"; import { newts, resources, sites, targets } from "@server/db";
import { eq } from "drizzle-orm"; import { eq, inArray } from "drizzle-orm";
import response from "@server/lib/response"; import response from "@server/lib/response";
import HttpCode from "@server/types/HttpCode"; import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors"; import createHttpError from "http-errors";
@@ -52,6 +52,16 @@ export async function deleteResource(
.from(targets) .from(targets)
.where(eq(targets.resourceId, resourceId)); .where(eq(targets.resourceId, resourceId));
const targetHealthChecksToBeRemoved = await db
.select()
.from(targetHealthCheck)
.where(
inArray(
targetHealthCheck.targetId,
targetsToBeRemoved.map((t) => t.targetId)
)
);
const [deletedResource] = await db const [deletedResource] = await db
.delete(resources) .delete(resources)
.where(eq(resources.resourceId, resourceId)) .where(eq(resources.resourceId, resourceId))
@@ -66,44 +76,40 @@ export async function deleteResource(
); );
} }
// const [site] = await db const [site] = await db
// .select() .select()
// .from(sites) .from(sites)
// .where(eq(sites.siteId, deletedResource.siteId!)) .where(eq(sites.siteId, targets.siteId))
// .limit(1); .limit(1);
//
// if (!site) { if (!site) {
// return next( return next(
// createHttpError( createHttpError(
// HttpCode.NOT_FOUND, HttpCode.NOT_FOUND,
// `Site with ID ${deletedResource.siteId} not found` `Site with ID ${targets.siteId} not found`
// ) )
// ); );
// } }
//
// if (site.pubKey) { if (site.pubKey) {
// if (site.type == "wireguard") { if (site.type == "newt") {
// await addPeer(site.exitNodeId!, { // get the newt on the site by querying the newt table for siteId
// publicKey: site.pubKey, const [newt] = await db
// allowedIps: await getAllowedIps(site.siteId) .select()
// }); .from(newts)
// } else if (site.type == "newt") { .where(eq(newts.siteId, site.siteId))
// // get the newt on the site by querying the newt table for siteId .limit(1);
// const [newt] = await db
// .select() await removeTargets(
// .from(newts) newt.newtId,
// .where(eq(newts.siteId, site.siteId)) targetsToBeRemoved,
// .limit(1); targetHealthChecksToBeRemoved,
// deletedResource.protocol,
// removeTargets( newt.version
// newt.newtId, );
// targetsToBeRemoved, }
// deletedResource.protocol, }
// deletedResource.proxyPort
// );
// }
// }
//
return response(res, { return response(res, {
data: null, data: null,
success: true, success: true,

View File

@@ -12,6 +12,7 @@ import { fromError } from "zod-validation-error";
import { removeTargets } from "../newt/targets"; import { removeTargets } from "../newt/targets";
import { getAllowedIps } from "./helpers"; import { getAllowedIps } from "./helpers";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
import { targetHealthCheck } from "@server/db/pg";
const deleteTargetSchema = z.strictObject({ const deleteTargetSchema = z.strictObject({
targetId: z.string().transform(Number).pipe(z.int().positive()) targetId: z.string().transform(Number).pipe(z.int().positive())
@@ -46,6 +47,11 @@ export async function deleteTarget(
const { targetId } = parsedParams.data; const { targetId } = parsedParams.data;
const [deletedHealthCheck] = await db
.delete(targetHealthCheck)
.where(eq(targetHealthCheck.targetId, targetId))
.returning();
const [deletedTarget] = await db const [deletedTarget] = await db
.delete(targets) .delete(targets)
.where(eq(targets.targetId, targetId)) .where(eq(targets.targetId, targetId))
@@ -74,38 +80,39 @@ export async function deleteTarget(
); );
} }
// const [site] = await db const [site] = await db
// .select() .select()
// .from(sites) .from(sites)
// .where(eq(sites.siteId, resource.siteId!)) .where(eq(sites.siteId, targets.siteId))
// .limit(1); .limit(1);
//
// if (!site) { if (!site) {
// return next( return next(
// createHttpError( createHttpError(
// HttpCode.NOT_FOUND, HttpCode.NOT_FOUND,
// `Site with ID ${resource.siteId} not found` `Site with ID ${targets.siteId} not found`
// ) )
// ); );
// } }
//
// if (site.pubKey) { if (site.pubKey) {
// if (site.type == "wireguard") { if (site.type == "newt") {
// await addPeer(site.exitNodeId!, { // get the newt on the site by querying the newt table for siteId
// publicKey: site.pubKey, const [newt] = await db
// allowedIps: await getAllowedIps(site.siteId) .select()
// }); .from(newts)
// } else if (site.type == "newt") { .where(eq(newts.siteId, site.siteId))
// // get the newt on the site by querying the newt table for siteId .limit(1);
// const [newt] = await db
// .select() await removeTargets(
// .from(newts) newt.newtId,
// .where(eq(newts.siteId, site.siteId)) [deletedTarget],
// .limit(1); [deletedHealthCheck],
// resource.protocol,
// removeTargets(newt.newtId, [deletedTarget], resource.protocol, resource.proxyPort); newt.version
// } );
// } }
}
return response(res, { return response(res, {
data: null, data: null,