🚚 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

@@ -0,0 +1,113 @@
import { Request, Response, NextFunction } from "express";
import { z } from "zod";
import response from "@server/lib/response";
import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors";
import logger from "@server/logger";
import { fromError } from "zod-validation-error";
import { OpenAPITags, registry } from "@server/openApi";
import { applyBlueprint } from "@server/lib/blueprints/applyBlueprint";
const applyBlueprintSchema = z
.object({
blueprint: z.string()
})
.strict();
const applyBlueprintParamsSchema = z
.object({
orgId: z.string()
})
.strict();
registry.registerPath({
method: "put",
path: "/org/{orgId}/blueprint",
description: "Apply a base64 encoded JSON blueprint to an organization",
tags: [OpenAPITags.Org, OpenAPITags.Blueprint],
request: {
params: applyBlueprintParamsSchema,
body: {
content: {
"application/json": {
schema: applyBlueprintSchema
}
}
}
},
responses: {}
});
export async function applyJSONBlueprint(
req: Request,
res: Response,
next: NextFunction
): Promise<any> {
try {
const parsedParams = applyBlueprintParamsSchema.safeParse(req.params);
if (!parsedParams.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromError(parsedParams.error).toString()
)
);
}
const { orgId } = parsedParams.data;
const parsedBody = applyBlueprintSchema.safeParse(req.body);
if (!parsedBody.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromError(parsedBody.error).toString()
)
);
}
const { blueprint } = parsedBody.data;
if (!blueprint) {
logger.warn("No blueprint provided");
return;
}
logger.debug(`Received blueprint: ${blueprint}`);
try {
// first base64 decode the blueprint
const decoded = Buffer.from(blueprint, "base64").toString("utf-8");
// then parse the json
const blueprintParsed = JSON.parse(decoded);
// Update the blueprint in the database
await applyBlueprint({
orgId,
configData: blueprintParsed,
source: "API"
});
} catch (error) {
logger.error(`Failed to update database from config: ${error}`);
return next(
createHttpError(
HttpCode.BAD_REQUEST,
`Failed to update database from config: ${error}`
)
);
}
return response(res, {
data: null,
success: true,
error: false,
message: "Blueprint applied successfully",
status: HttpCode.CREATED
});
} catch (error) {
logger.error(error);
return next(
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
);
}
}

View File

@@ -7,15 +7,14 @@ import createHttpError from "http-errors";
import HttpCode from "@server/types/HttpCode";
import { fromZodError } from "zod-validation-error";
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 { ConfigSchema } from "@server/lib/blueprints/types";
import { BlueprintSource } from "./types";
const applyBlueprintSchema = z
.object({
name: z.string().min(1).max(255),
contents: z
blueprint: z
.string()
.min(1)
.superRefine((val, ctx) => {
@@ -40,10 +39,9 @@ const applyBlueprintParamsSchema = z
export type CreateBlueprintResponse = Blueprint;
registry.registerPath({
method: "post",
method: "put",
path: "/org/{orgId}/blueprint",
description:
"Create and Apply a base64 encoded blueprint to an organization",
description: "Create and apply a YAML blueprint to an organization",
tags: [OpenAPITags.Org, OpenAPITags.Blueprint],
request: {
params: applyBlueprintParamsSchema,
@@ -58,7 +56,7 @@ registry.registerPath({
responses: {}
});
export async function createAndApplyBlueprint(
export async function applyYAMLBlueprint(
req: Request,
res: Response,
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);
@@ -102,42 +100,26 @@ export async function createAndApplyBlueprint(
);
}
let blueprintSucceeded: boolean;
let blueprintMessage: string;
let blueprint: Blueprint | null = null;
try {
await applyBlueprint(orgId, parsedConfig);
blueprintSucceeded = true;
blueprintMessage = "success";
blueprint = await applyBlueprint({
orgId,
name,
source: "UI",
configData: parsedConfig
});
} catch (error) {
blueprintSucceeded = false;
blueprintMessage = `Failed to update blueprint from config: ${error}`;
logger.error(blueprintMessage);
// We do nothing, the error is thrown for the other APIs & websockets for backwards compatibility
// for this API, the error is already saved in the blueprint and we don't need to handle it
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) {
return next(
createHttpError(
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,
success: true,
error: false,
message: blueprintSucceeded
message: blueprint.succeeded
? "Blueprint applied with success"
: `Blueprint applied with errors: ${blueprintMessage}`,
: `Blueprint applied with errors: ${blueprint.message}`,
status: HttpCode.CREATED
});
} catch (error) {

View File

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