mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-25 06:16:40 +00:00
✨add enpoint for getting all resource names
This commit is contained in:
@@ -307,6 +307,13 @@ authenticated.get(
|
|||||||
resource.listResources
|
resource.listResources
|
||||||
);
|
);
|
||||||
|
|
||||||
|
authenticated.get(
|
||||||
|
"/org/:orgId/resource-names",
|
||||||
|
verifyOrgAccess,
|
||||||
|
verifyUserHasAction(ActionsEnum.listResources),
|
||||||
|
resource.listAllResourceNames
|
||||||
|
);
|
||||||
|
|
||||||
authenticated.get(
|
authenticated.get(
|
||||||
"/org/:orgId/user-resources",
|
"/org/:orgId/user-resources",
|
||||||
verifyOrgAccess,
|
verifyOrgAccess,
|
||||||
|
|||||||
@@ -25,3 +25,4 @@ export * from "./getUserResources";
|
|||||||
export * from "./setResourceHeaderAuth";
|
export * from "./setResourceHeaderAuth";
|
||||||
export * from "./addEmailToResourceWhitelist";
|
export * from "./addEmailToResourceWhitelist";
|
||||||
export * from "./removeEmailFromResourceWhitelist";
|
export * from "./removeEmailFromResourceWhitelist";
|
||||||
|
export * from "./listAllResourceNames";
|
||||||
|
|||||||
90
server/routers/resource/listAllResourceNames.ts
Normal file
90
server/routers/resource/listAllResourceNames.ts
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { db, resourceHeaderAuth } from "@server/db";
|
||||||
|
import {
|
||||||
|
resources,
|
||||||
|
userResources,
|
||||||
|
roleResources,
|
||||||
|
resourcePassword,
|
||||||
|
resourcePincode,
|
||||||
|
targets,
|
||||||
|
targetHealthCheck
|
||||||
|
} from "@server/db";
|
||||||
|
import response from "@server/lib/response";
|
||||||
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
import createHttpError from "http-errors";
|
||||||
|
import { sql, eq, or, inArray, and, count } from "drizzle-orm";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
import { fromZodError } from "zod-validation-error";
|
||||||
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
|
import type {
|
||||||
|
ResourceWithTargets,
|
||||||
|
ListResourcesResponse
|
||||||
|
} from "./listResources";
|
||||||
|
|
||||||
|
const listResourcesParamsSchema = z.strictObject({
|
||||||
|
orgId: z.string()
|
||||||
|
});
|
||||||
|
|
||||||
|
function queryResourceNames(orgId: string) {
|
||||||
|
return db
|
||||||
|
.select({
|
||||||
|
resourceId: resources.resourceId,
|
||||||
|
name: resources.name
|
||||||
|
})
|
||||||
|
.from(resources)
|
||||||
|
|
||||||
|
.where(eq(resources.orgId, orgId));
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ListResourceNamesResponse = Awaited<
|
||||||
|
ReturnType<typeof queryResourceNames>
|
||||||
|
>;
|
||||||
|
|
||||||
|
registry.registerPath({
|
||||||
|
method: "get",
|
||||||
|
path: "/org/{orgId}/resources-names",
|
||||||
|
description: "List all resource names for an organization.",
|
||||||
|
tags: [OpenAPITags.Org, OpenAPITags.Resource],
|
||||||
|
request: {
|
||||||
|
params: z.object({
|
||||||
|
orgId: z.string()
|
||||||
|
})
|
||||||
|
},
|
||||||
|
responses: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function listAllResourceNames(
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<any> {
|
||||||
|
try {
|
||||||
|
const parsedParams = listResourcesParamsSchema.safeParse(req.params);
|
||||||
|
if (!parsedParams.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromZodError(parsedParams.error)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const orgId = parsedParams.data.orgId;
|
||||||
|
|
||||||
|
const data = await queryResourceNames(orgId);
|
||||||
|
|
||||||
|
return response<ListResourceNamesResponse>(res, {
|
||||||
|
data,
|
||||||
|
success: true,
|
||||||
|
error: false,
|
||||||
|
message: "Resource Names retrieved successfully",
|
||||||
|
status: HttpCode.OK
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error);
|
||||||
|
return next(
|
||||||
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,21 +8,19 @@ import {
|
|||||||
resourcePassword,
|
resourcePassword,
|
||||||
resourcePincode,
|
resourcePincode,
|
||||||
targets,
|
targets,
|
||||||
targetHealthCheck,
|
targetHealthCheck
|
||||||
} from "@server/db";
|
} from "@server/db";
|
||||||
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";
|
||||||
import { sql, eq, or, inArray, and, count } from "drizzle-orm";
|
import { sql, eq, or, inArray, and, count } from "drizzle-orm";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import stoi from "@server/lib/stoi";
|
|
||||||
import { fromZodError } from "zod-validation-error";
|
import { fromZodError } from "zod-validation-error";
|
||||||
import { OpenAPITags, registry } from "@server/openApi";
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
import { warn } from "console";
|
|
||||||
|
|
||||||
const listResourcesParamsSchema = z.strictObject({
|
const listResourcesParamsSchema = z.strictObject({
|
||||||
orgId: z.string()
|
orgId: z.string()
|
||||||
});
|
});
|
||||||
|
|
||||||
const listResourcesSchema = z.object({
|
const listResourcesSchema = z.object({
|
||||||
limit: z
|
limit: z
|
||||||
@@ -67,7 +65,7 @@ type JoinedRow = {
|
|||||||
hcEnabled: boolean | null;
|
hcEnabled: boolean | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
// grouped by resource with targets[])
|
// grouped by resource with targets[])
|
||||||
export type ResourceWithTargets = {
|
export type ResourceWithTargets = {
|
||||||
resourceId: number;
|
resourceId: number;
|
||||||
name: string;
|
name: string;
|
||||||
@@ -89,7 +87,7 @@ export type ResourceWithTargets = {
|
|||||||
ip: string;
|
ip: string;
|
||||||
port: number;
|
port: number;
|
||||||
enabled: boolean;
|
enabled: boolean;
|
||||||
healthStatus?: 'healthy' | 'unhealthy' | 'unknown';
|
healthStatus?: "healthy" | "unhealthy" | "unknown";
|
||||||
}>;
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -118,7 +116,7 @@ function queryResources(accessibleResourceIds: number[], orgId: string) {
|
|||||||
targetEnabled: targets.enabled,
|
targetEnabled: targets.enabled,
|
||||||
|
|
||||||
hcHealth: targetHealthCheck.hcHealth,
|
hcHealth: targetHealthCheck.hcHealth,
|
||||||
hcEnabled: targetHealthCheck.hcEnabled,
|
hcEnabled: targetHealthCheck.hcEnabled
|
||||||
})
|
})
|
||||||
.from(resources)
|
.from(resources)
|
||||||
.leftJoin(
|
.leftJoin(
|
||||||
@@ -273,16 +271,25 @@ export async function listResources(
|
|||||||
enabled: row.enabled,
|
enabled: row.enabled,
|
||||||
domainId: row.domainId,
|
domainId: row.domainId,
|
||||||
headerAuthId: row.headerAuthId,
|
headerAuthId: row.headerAuthId,
|
||||||
targets: [],
|
targets: []
|
||||||
};
|
};
|
||||||
map.set(row.resourceId, entry);
|
map.set(row.resourceId, entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (row.targetId != null && row.targetIp && row.targetPort != null && row.targetEnabled != null) {
|
if (
|
||||||
let healthStatus: 'healthy' | 'unhealthy' | 'unknown' = 'unknown';
|
row.targetId != null &&
|
||||||
|
row.targetIp &&
|
||||||
|
row.targetPort != null &&
|
||||||
|
row.targetEnabled != null
|
||||||
|
) {
|
||||||
|
let healthStatus: "healthy" | "unhealthy" | "unknown" =
|
||||||
|
"unknown";
|
||||||
|
|
||||||
if (row.hcEnabled && row.hcHealth) {
|
if (row.hcEnabled && row.hcHealth) {
|
||||||
healthStatus = row.hcHealth as 'healthy' | 'unhealthy' | 'unknown';
|
healthStatus = row.hcHealth as
|
||||||
|
| "healthy"
|
||||||
|
| "unhealthy"
|
||||||
|
| "unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
entry.targets.push({
|
entry.targets.push({
|
||||||
@@ -290,7 +297,7 @@ export async function listResources(
|
|||||||
ip: row.targetIp,
|
ip: row.targetIp,
|
||||||
port: row.targetPort,
|
port: row.targetPort,
|
||||||
enabled: row.targetEnabled,
|
enabled: row.targetEnabled,
|
||||||
healthStatus: healthStatus,
|
healthStatus: healthStatus
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user