mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-10 20:02:26 +00:00
♻️ applyBlueprint core logic now saves the blueprint in the DB
This commit is contained in:
@@ -1,22 +1,35 @@
|
|||||||
import { db, newts, Target } from "@server/db";
|
import { db, newts, blueprints, Blueprint } from "@server/db";
|
||||||
import { Config, ConfigSchema } from "./types";
|
import { Config, ConfigSchema } from "./types";
|
||||||
import { ProxyResourcesResults, updateProxyResources } from "./proxyResources";
|
import { ProxyResourcesResults, updateProxyResources } from "./proxyResources";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { resources, targets, sites } from "@server/db";
|
import { sites } from "@server/db";
|
||||||
import { eq, and, asc, or, ne, count, isNotNull } from "drizzle-orm";
|
import { eq, and, isNotNull } from "drizzle-orm";
|
||||||
import { addTargets as addProxyTargets } from "@server/routers/newt/targets";
|
import { addTargets as addProxyTargets } from "@server/routers/newt/targets";
|
||||||
import { addTargets as addClientTargets } from "@server/routers/client/targets";
|
import { addTargets as addClientTargets } from "@server/routers/client/targets";
|
||||||
import {
|
import {
|
||||||
ClientResourcesResults,
|
ClientResourcesResults,
|
||||||
updateClientResources
|
updateClientResources
|
||||||
} from "./clientResources";
|
} from "./clientResources";
|
||||||
|
import { BlueprintSource } from "@server/routers/blueprints/types";
|
||||||
|
import { stringify as stringifyYaml } from "yaml";
|
||||||
|
import { faker } from "@faker-js/faker";
|
||||||
|
|
||||||
export async function applyBlueprint(
|
type ApplyBlueprintArgs = {
|
||||||
orgId: string,
|
orgId: string;
|
||||||
configData: unknown,
|
configData: unknown;
|
||||||
siteId?: number
|
name?: string;
|
||||||
): Promise<void> {
|
siteId?: number;
|
||||||
|
source?: BlueprintSource;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function applyBlueprint({
|
||||||
|
orgId,
|
||||||
|
configData,
|
||||||
|
siteId,
|
||||||
|
name,
|
||||||
|
source = "API"
|
||||||
|
}: ApplyBlueprintArgs): Promise<Blueprint> {
|
||||||
// Validate the input data
|
// Validate the input data
|
||||||
const validationResult = ConfigSchema.safeParse(configData);
|
const validationResult = ConfigSchema.safeParse(configData);
|
||||||
if (!validationResult.success) {
|
if (!validationResult.success) {
|
||||||
@@ -24,6 +37,9 @@ export async function applyBlueprint(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const config: Config = validationResult.data;
|
const config: Config = validationResult.data;
|
||||||
|
let blueprintSucceeded: boolean = false;
|
||||||
|
let blueprintMessage: string;
|
||||||
|
let error: any | null = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let proxyResourcesResults: ProxyResourcesResults = [];
|
let proxyResourcesResults: ProxyResourcesResults = [];
|
||||||
@@ -120,10 +136,41 @@ export async function applyBlueprint(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
logger.error(`Failed to update database from config: ${error}`);
|
blueprintSucceeded = true;
|
||||||
throw error;
|
blueprintMessage = "success";
|
||||||
|
} catch (err) {
|
||||||
|
blueprintSucceeded = false;
|
||||||
|
blueprintMessage = `Failed to update blueprint from config: ${err}`;
|
||||||
|
logger.error(blueprintMessage);
|
||||||
|
error = err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let blueprint: Blueprint | null = null;
|
||||||
|
await db.transaction(async (trx) => {
|
||||||
|
const newBlueprint = await trx
|
||||||
|
.insert(blueprints)
|
||||||
|
.values({
|
||||||
|
orgId,
|
||||||
|
name:
|
||||||
|
name ??
|
||||||
|
`${faker.word.adjective()} ${faker.word.adjective()} ${faker.word.noun()}`,
|
||||||
|
contents: stringifyYaml(configData),
|
||||||
|
createdAt: Math.floor(Date.now() / 1000),
|
||||||
|
succeeded: blueprintSucceeded,
|
||||||
|
message: blueprintMessage,
|
||||||
|
source: source
|
||||||
|
})
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
blueprint = newBlueprint[0];
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!blueprint || !blueprintSucceeded) {
|
||||||
|
throw error ?? "Unknown Server Error";
|
||||||
|
}
|
||||||
|
|
||||||
|
return blueprint;
|
||||||
}
|
}
|
||||||
|
|
||||||
// await updateDatabaseFromConfig("org_i21aifypnlyxur2", {
|
// await updateDatabaseFromConfig("org_i21aifypnlyxur2", {
|
||||||
|
|||||||
Reference in New Issue
Block a user