mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-04 17:56:38 +00:00
✨ create and apply blueprint
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { OpenAPITags, registry } from "@server/openApi";
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
import z from "zod";
|
import z from "zod";
|
||||||
import { applyBlueprint as applyBlueprintFunc } from "@server/lib/blueprints/applyBlueprint";
|
import { applyBlueprint } from "@server/lib/blueprints/applyBlueprint";
|
||||||
import { NextFunction, Request, Response } from "express";
|
import { NextFunction, Request, Response } from "express";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import createHttpError from "http-errors";
|
import createHttpError from "http-errors";
|
||||||
@@ -9,6 +9,8 @@ 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, blueprints, db, loginPage } from "@server/db";
|
||||||
import { parse as parseYaml } from "yaml";
|
import { parse as parseYaml } from "yaml";
|
||||||
|
import { ConfigSchema } from "@server/lib/blueprints/types";
|
||||||
|
import { BlueprintSource } from "./types";
|
||||||
|
|
||||||
const applyBlueprintSchema = z
|
const applyBlueprintSchema = z
|
||||||
.object({
|
.object({
|
||||||
@@ -16,9 +18,9 @@ const applyBlueprintSchema = z
|
|||||||
contents: z
|
contents: z
|
||||||
.string()
|
.string()
|
||||||
.min(1)
|
.min(1)
|
||||||
.superRefine((contents, ctx) => {
|
.superRefine((val, ctx) => {
|
||||||
try {
|
try {
|
||||||
parseYaml(contents);
|
parseYaml(val);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ctx.addIssue({
|
ctx.addIssue({
|
||||||
code: z.ZodIssueCode.custom,
|
code: z.ZodIssueCode.custom,
|
||||||
@@ -86,42 +88,67 @@ export async function createAndApplyBlueprint(
|
|||||||
|
|
||||||
const { contents, name } = parsedBody.data;
|
const { contents, name } = parsedBody.data;
|
||||||
|
|
||||||
logger.debug(`Received blueprint: ${contents}`);
|
logger.debug(`Received blueprint:`, contents);
|
||||||
|
|
||||||
// try {
|
const parsedConfig = parseYaml(contents);
|
||||||
// // then parse the json
|
// apply the validation in advance so that error concerning the format are ruled out first
|
||||||
// const blueprintParsed = parseYaml(contents);
|
const validationResult = ConfigSchema.safeParse(parsedConfig);
|
||||||
|
if (!validationResult.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromZodError(validationResult.error)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// // Update the blueprint in the database
|
let blueprintSucceeded: boolean;
|
||||||
// await applyBlueprintFunc(orgId, blueprintParsed);
|
let blueprintMessage: string;
|
||||||
|
|
||||||
// await db.transaction(async (trx) => {
|
try {
|
||||||
// const newBlueprint = await trx
|
await applyBlueprint(orgId, parsedConfig);
|
||||||
// .insert(blueprints)
|
blueprintSucceeded = true;
|
||||||
// .values({
|
blueprintMessage = "success";
|
||||||
// orgId,
|
} catch (error) {
|
||||||
// name,
|
blueprintSucceeded = false;
|
||||||
// contents
|
blueprintMessage = `Failed to update blueprint from config: ${error}`;
|
||||||
// // createdAt
|
logger.error(blueprintMessage);
|
||||||
// })
|
}
|
||||||
// .returning();
|
|
||||||
// });
|
|
||||||
// } catch (error) {
|
|
||||||
// logger.error(`Failed to update database from config: ${error}`);
|
|
||||||
|
|
||||||
// return next(
|
let blueprint: Blueprint | null = null;
|
||||||
// createHttpError(
|
await db.transaction(async (trx) => {
|
||||||
// HttpCode.BAD_REQUEST,
|
const newBlueprint = await trx
|
||||||
// `Failed to update database from config: ${error}`
|
.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"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return response(res, {
|
return response(res, {
|
||||||
data: null,
|
data: blueprint,
|
||||||
success: true,
|
success: true,
|
||||||
error: false,
|
error: false,
|
||||||
message: "Blueprint applied successfully",
|
message: blueprintSucceeded
|
||||||
|
? "Blueprint applied with success"
|
||||||
|
: `Blueprint applied with errors: ${blueprintMessage}`,
|
||||||
status: HttpCode.CREATED
|
status: HttpCode.CREATED
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -820,7 +820,7 @@ authenticated.get(
|
|||||||
);
|
);
|
||||||
|
|
||||||
authenticated.put(
|
authenticated.put(
|
||||||
"/org/:orgId/blueprints",
|
"/org/:orgId/blueprint",
|
||||||
verifyOrgAccess,
|
verifyOrgAccess,
|
||||||
verifyUserHasAction(ActionsEnum.applyBlueprint),
|
verifyUserHasAction(ActionsEnum.applyBlueprint),
|
||||||
blueprints.createAndApplyBlueprint
|
blueprints.createAndApplyBlueprint
|
||||||
|
|||||||
Reference in New Issue
Block a user