🚚 rename integration API applyBlueprint to apply JSON blueprint and the UI applyBlueprint to apply YAML blueprint

This commit is contained in:
Fred KISSIE
2025-10-29 03:08:48 +01:00
parent 4c567cf2d7
commit 10ce732b8d
6 changed files with 33 additions and 64 deletions

View File

@@ -1,30 +1,12 @@
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 { eq } from "drizzle-orm";
import {
apiKeyOrg,
apiKeys,
domains,
Org,
orgDomains,
orgs,
roleActions,
roles,
userOrgs,
users,
actions
} 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 logger from "@server/logger"; import logger from "@server/logger";
import config from "@server/lib/config";
import { fromError } from "zod-validation-error"; import { fromError } from "zod-validation-error";
import { defaultRoleAllowedActions } from "../role";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
import { isValidCIDR } from "@server/lib/validators"; import { applyBlueprint } from "@server/lib/blueprints/applyBlueprint";
import { applyBlueprint as applyBlueprintFunc } from "@server/lib/blueprints/applyBlueprint";
const applyBlueprintSchema = z const applyBlueprintSchema = z
.object({ .object({
@@ -41,8 +23,8 @@ const applyBlueprintParamsSchema = z
registry.registerPath({ registry.registerPath({
method: "put", method: "put",
path: "/org/{orgId}/blueprint", path: "/org/{orgId}/blueprint",
description: "Apply a base64 encoded blueprint to an organization", description: "Apply a base64 encoded JSON blueprint to an organization",
tags: [OpenAPITags.Org], tags: [OpenAPITags.Org, OpenAPITags.Blueprint],
request: { request: {
params: applyBlueprintParamsSchema, params: applyBlueprintParamsSchema,
body: { body: {
@@ -56,7 +38,7 @@ registry.registerPath({
responses: {} responses: {}
}); });
export async function applyBlueprint( export async function applyJSONBlueprint(
req: Request, req: Request,
res: Response, res: Response,
next: NextFunction next: NextFunction
@@ -100,7 +82,11 @@ export async function applyBlueprint(
const blueprintParsed = JSON.parse(decoded); const blueprintParsed = JSON.parse(decoded);
// Update the blueprint in the database // Update the blueprint in the database
await applyBlueprintFunc(orgId, blueprintParsed); await applyBlueprint({
orgId,
configData: blueprintParsed,
source: "API"
});
} catch (error) { } catch (error) {
logger.error(`Failed to update database from config: ${error}`); logger.error(`Failed to update database from config: ${error}`);
return next( return next(

View File

@@ -7,15 +7,14 @@ import createHttpError from "http-errors";
import HttpCode from "@server/types/HttpCode"; import HttpCode from "@server/types/HttpCode";
import { fromZodError } from "zod-validation-error"; import { fromZodError } from "zod-validation-error";
import response from "@server/lib/response"; import response from "@server/lib/response";
import { type Blueprint, blueprints, db, loginPage } from "@server/db"; import { type Blueprint } from "@server/db";
import { parse as parseYaml } from "yaml"; import { parse as parseYaml } from "yaml";
import { ConfigSchema } from "@server/lib/blueprints/types"; import { ConfigSchema } from "@server/lib/blueprints/types";
import { BlueprintSource } from "./types";
const applyBlueprintSchema = z const applyBlueprintSchema = z
.object({ .object({
name: z.string().min(1).max(255), name: z.string().min(1).max(255),
contents: z blueprint: z
.string() .string()
.min(1) .min(1)
.superRefine((val, ctx) => { .superRefine((val, ctx) => {
@@ -40,10 +39,9 @@ const applyBlueprintParamsSchema = z
export type CreateBlueprintResponse = Blueprint; export type CreateBlueprintResponse = Blueprint;
registry.registerPath({ registry.registerPath({
method: "post", method: "put",
path: "/org/{orgId}/blueprint", path: "/org/{orgId}/blueprint",
description: description: "Create and apply a YAML blueprint to an organization",
"Create and Apply a base64 encoded blueprint to an organization",
tags: [OpenAPITags.Org, OpenAPITags.Blueprint], tags: [OpenAPITags.Org, OpenAPITags.Blueprint],
request: { request: {
params: applyBlueprintParamsSchema, params: applyBlueprintParamsSchema,
@@ -58,7 +56,7 @@ registry.registerPath({
responses: {} responses: {}
}); });
export async function createAndApplyBlueprint( export async function applyYAMLBlueprint(
req: Request, req: Request,
res: Response, res: Response,
next: NextFunction next: NextFunction
@@ -86,7 +84,7 @@ export async function createAndApplyBlueprint(
); );
} }
const { contents, name } = parsedBody.data; const { blueprint: contents, name } = parsedBody.data;
logger.debug(`Received blueprint:`, contents); logger.debug(`Received blueprint:`, contents);
@@ -102,42 +100,26 @@ export async function createAndApplyBlueprint(
); );
} }
let blueprintSucceeded: boolean; let blueprint: Blueprint | null = null;
let blueprintMessage: string;
try { try {
await applyBlueprint(orgId, parsedConfig); blueprint = await applyBlueprint({
blueprintSucceeded = true; orgId,
blueprintMessage = "success"; name,
source: "UI",
configData: parsedConfig
});
} catch (error) { } catch (error) {
blueprintSucceeded = false; // We do nothing, the error is thrown for the other APIs & websockets for backwards compatibility
blueprintMessage = `Failed to update blueprint from config: ${error}`; // for this API, the error is already saved in the blueprint and we don't need to handle it
logger.error(blueprintMessage); logger.error(error);
} }
let blueprint: Blueprint | null = null;
await db.transaction(async (trx) => {
const newBlueprint = await trx
.insert(blueprints)
.values({
orgId,
name,
contents,
createdAt: Math.floor(Date.now() / 1000),
succeeded: blueprintSucceeded,
message: blueprintMessage,
source: "UI" as BlueprintSource
})
.returning();
blueprint = newBlueprint[0];
});
if (!blueprint) { if (!blueprint) {
return next( return next(
createHttpError( createHttpError(
HttpCode.INTERNAL_SERVER_ERROR, HttpCode.INTERNAL_SERVER_ERROR,
"Failed to create resource" "Failed to save blueprint in the database"
) )
); );
} }
@@ -146,9 +128,9 @@ export async function createAndApplyBlueprint(
data: blueprint, data: blueprint,
success: true, success: true,
error: false, error: false,
message: blueprintSucceeded message: blueprint.succeeded
? "Blueprint applied with success" ? "Blueprint applied with success"
: `Blueprint applied with errors: ${blueprintMessage}`, : `Blueprint applied with errors: ${blueprint.message}`,
status: HttpCode.CREATED status: HttpCode.CREATED
}); });
} catch (error) { } catch (error) {

View File

@@ -1,3 +1,4 @@
export * from "./listBlueprints"; export * from "./listBlueprints";
export * from "./createAndApplyBlueprint"; export * from "./applyYAMLBlueprint";
export * from "./applyJSONBlueprint";
export * from "./getBlueprint"; export * from "./getBlueprint";

View File

@@ -823,7 +823,7 @@ authenticated.put(
"/org/:orgId/blueprint", "/org/:orgId/blueprint",
verifyOrgAccess, verifyOrgAccess,
verifyUserHasAction(ActionsEnum.applyBlueprint), verifyUserHasAction(ActionsEnum.applyBlueprint),
blueprints.createAndApplyBlueprint blueprints.applyYAMLBlueprint
); );
authenticated.get( authenticated.get(

View File

@@ -1,5 +1,6 @@
import * as site from "./site"; import * as site from "./site";
import * as org from "./org"; import * as org from "./org";
import * as blueprints from "./blueprints";
import * as resource from "./resource"; import * as resource from "./resource";
import * as domain from "./domain"; import * as domain from "./domain";
import * as target from "./target"; import * as target from "./target";
@@ -663,5 +664,5 @@ authenticated.put(
"/org/:orgId/blueprint", "/org/:orgId/blueprint",
verifyApiKeyOrgAccess, verifyApiKeyOrgAccess,
verifyApiKeyHasAction(ActionsEnum.applyBlueprint), verifyApiKeyHasAction(ActionsEnum.applyBlueprint),
org.applyBlueprint blueprints.applyJSONBlueprint
); );

View File

@@ -7,4 +7,3 @@ export * from "./checkId";
export * from "./getOrgOverview"; export * from "./getOrgOverview";
export * from "./listOrgs"; export * from "./listOrgs";
export * from "./pickOrgDefaults"; export * from "./pickOrgDefaults";
export * from "./applyBlueprint";