Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { z } from "zod";

import { and, eq, takeFirstOrNull } from "@ctrlplane/db";
import * as SCHEMA from "@ctrlplane/db/schema";

import { authn } from "../../auth";
import { parseBody } from "../../body-parser";
import { request } from "../../middleware";

const bodySchema = z.object({
workspaceId: z.string().uuid(),
deploymentId: z.string().uuid(),
resourceIdentifier: z.string(),
});

export const POST = request()
.use(authn)
.use(parseBody(bodySchema))
.handle<{ body: z.infer<typeof bodySchema> }>(async (ctx) => {
try {
const { body, db } = ctx;

const resource = await db
.select()
.from(SCHEMA.resource)
.where(
and(
eq(SCHEMA.resource.identifier, body.resourceIdentifier),
eq(SCHEMA.resource.workspaceId, body.workspaceId),
),
)
.then(takeFirstOrNull);
if (!resource)
return Response.json({ error: "Resource not found" }, { status: 404 });

const deployment = await db
.select()
.from(SCHEMA.deployment)
.innerJoin(
SCHEMA.system,
eq(SCHEMA.deployment.systemId, SCHEMA.system.id),
)
.where(
and(
eq(SCHEMA.deployment.id, body.deploymentId),
eq(SCHEMA.system.workspaceId, body.workspaceId),
),
)
.then(takeFirstOrNull);
if (!deployment)
return Response.json(
{ error: "Deployment not found" },
{ status: 404 },
);

await db
.insert(SCHEMA.deploymentResourceRelationship)
.values(body)
.returning();

return Response.json(body);
} catch (error) {
if (error instanceof Error && error.message.includes("duplicate key"))
return Response.json(
{ error: "Resource already associated with a deployment" },
{ status: 400 },
);
return Response.json(
{ error: "Failed to create relationship" },
{ status: 500 },
);
}
});
10 changes: 0 additions & 10 deletions apps/webservice/src/app/api/v1/relationship/openapi.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { z } from "zod";

import { and, eq, takeFirstOrNull } from "@ctrlplane/db";
import * as SCHEMA from "@ctrlplane/db/schema";

import { authn } from "../../auth";
import { parseBody } from "../../body-parser";
import { request } from "../../middleware";

const bodySchema = z.object({
workspaceId: z.string().uuid(),
fromIdentifier: z.string(),
toIdentifier: z.string(),
type: z.enum(["associated_with", "depends_on"]),
});

export const POST = request()
.use(authn)
.use(parseBody(bodySchema))
.handle<{ body: z.infer<typeof bodySchema> }>(async (ctx) => {
try {
const { body, db } = ctx;

const fromResource = await db
.select()
.from(SCHEMA.resource)
.where(
and(
eq(SCHEMA.resource.identifier, body.fromIdentifier),
eq(SCHEMA.resource.workspaceId, body.workspaceId),
),
)
.then(takeFirstOrNull);
if (!fromResource)
return Response.json(
{ error: `${body.fromIdentifier} not found` },
{ status: 404 },
);

const toResource = await db
.select()
.from(SCHEMA.resource)
.where(
and(
eq(SCHEMA.resource.identifier, body.toIdentifier),
eq(SCHEMA.resource.workspaceId, body.workspaceId),
),
)
.then(takeFirstOrNull);
if (!toResource)
return Response.json(
{ error: `${body.toIdentifier} not found` },
{ status: 404 },
);

await db.insert(SCHEMA.resourceRelationship).values({
sourceId: fromResource.id,
targetId: toResource.id,
type: body.type,
});

return Response.json(
{ message: "Relationship created" },
{ status: 200 },
);
} catch (error) {
if (error instanceof Error && error.message.includes("duplicate key"))
return Response.json(
{ error: "Relationship already exists" },
{ status: 400 },
);
return Response.json(
{ error: "Failed to create relationship" },
{ status: 500 },
);
}
});
46 changes: 0 additions & 46 deletions apps/webservice/src/app/api/v1/relationship/route.ts

This file was deleted.

20 changes: 20 additions & 0 deletions packages/db/drizzle/0039_hard_joystick.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CREATE TABLE IF NOT EXISTS "deployment_resource_relationship" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"workspace_id" uuid NOT NULL,
"deployment_id" uuid NOT NULL,
"resource_identifier" text NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "deployment_resource_relationship" ADD CONSTRAINT "deployment_resource_relationship_deployment_id_deployment_id_fk" FOREIGN KEY ("deployment_id") REFERENCES "public"."deployment"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "deployment_resource_relationship" ADD CONSTRAINT "deployment_resource_relationship_resource_identifier_workspace_id_resource_identifier_workspace_id_fk" FOREIGN KEY ("resource_identifier","workspace_id") REFERENCES "public"."resource"("identifier","workspace_id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "deployment_resource_relationship_workspace_id_resource_identifier_index" ON "deployment_resource_relationship" USING btree ("workspace_id","resource_identifier");
Loading
Loading