mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-10 20:02:26 +00:00
🚚 rename integration API applyBlueprint to apply JSON blueprint and the UI applyBlueprint to apply YAML blueprint
This commit is contained in:
142
server/routers/blueprints/applyYAMLBlueprint.ts
Normal file
142
server/routers/blueprints/applyYAMLBlueprint.ts
Normal file
@@ -0,0 +1,142 @@
|
||||
import { OpenAPITags, registry } from "@server/openApi";
|
||||
import z from "zod";
|
||||
import { applyBlueprint } from "@server/lib/blueprints/applyBlueprint";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import logger from "@server/logger";
|
||||
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 } from "@server/db";
|
||||
import { parse as parseYaml } from "yaml";
|
||||
import { ConfigSchema } from "@server/lib/blueprints/types";
|
||||
|
||||
const applyBlueprintSchema = z
|
||||
.object({
|
||||
name: z.string().min(1).max(255),
|
||||
blueprint: z
|
||||
.string()
|
||||
.min(1)
|
||||
.superRefine((val, ctx) => {
|
||||
try {
|
||||
parseYaml(val);
|
||||
} catch (error) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: `Invalid YAML: ${error instanceof Error ? error.message : "Unknown error"}`
|
||||
});
|
||||
}
|
||||
})
|
||||
})
|
||||
.strict();
|
||||
|
||||
const applyBlueprintParamsSchema = z
|
||||
.object({
|
||||
orgId: z.string()
|
||||
})
|
||||
.strict();
|
||||
|
||||
export type CreateBlueprintResponse = Blueprint;
|
||||
|
||||
registry.registerPath({
|
||||
method: "put",
|
||||
path: "/org/{orgId}/blueprint",
|
||||
description: "Create and apply a YAML blueprint to an organization",
|
||||
tags: [OpenAPITags.Org, OpenAPITags.Blueprint],
|
||||
request: {
|
||||
params: applyBlueprintParamsSchema,
|
||||
body: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: applyBlueprintSchema
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
responses: {}
|
||||
});
|
||||
|
||||
export async function applyYAMLBlueprint(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<any> {
|
||||
try {
|
||||
const parsedParams = applyBlueprintParamsSchema.safeParse(req.params);
|
||||
if (!parsedParams.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
fromZodError(parsedParams.error)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { orgId } = parsedParams.data;
|
||||
|
||||
const parsedBody = applyBlueprintSchema.safeParse(req.body);
|
||||
if (!parsedBody.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
fromZodError(parsedBody.error)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { blueprint: contents, name } = parsedBody.data;
|
||||
|
||||
logger.debug(`Received blueprint:`, contents);
|
||||
|
||||
const parsedConfig = parseYaml(contents);
|
||||
// apply the validation in advance so that error concerning the format are ruled out first
|
||||
const validationResult = ConfigSchema.safeParse(parsedConfig);
|
||||
if (!validationResult.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
fromZodError(validationResult.error)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
let blueprint: Blueprint | null = null;
|
||||
|
||||
try {
|
||||
blueprint = await applyBlueprint({
|
||||
orgId,
|
||||
name,
|
||||
source: "UI",
|
||||
configData: parsedConfig
|
||||
});
|
||||
} catch (error) {
|
||||
// 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);
|
||||
}
|
||||
|
||||
if (!blueprint) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.INTERNAL_SERVER_ERROR,
|
||||
"Failed to save blueprint in the database"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return response(res, {
|
||||
data: blueprint,
|
||||
success: true,
|
||||
error: false,
|
||||
message: blueprint.succeeded
|
||||
? "Blueprint applied with success"
|
||||
: `Blueprint applied with errors: ${blueprint.message}`,
|
||||
status: HttpCode.CREATED
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
return next(
|
||||
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user