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
25 changes: 23 additions & 2 deletions packages/db/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { SQL } from "drizzle-orm";
import type { PgTable } from "drizzle-orm/pg-core";
import { getTableColumns, sql } from "drizzle-orm";
import { eq, getTableColumns, ilike, sql } from "drizzle-orm";

import { ColumnOperator } from "@ctrlplane/validators/conditions";

import type { db } from "./client";

Expand All @@ -19,9 +21,15 @@ export const takeFirstOrNull = <T extends any[]>(

export type Tx = Omit<typeof db, "$client">;

type ColumnKey<T extends PgTable> = keyof T["_"]["columns"];
type ColumnType<
T extends PgTable,
Q extends ColumnKey<T>,
> = T["_"]["columns"][Q];

export const buildConflictUpdateColumns = <
T extends PgTable,
Q extends keyof T["_"]["columns"],
Q extends ColumnKey<T>,
>(
table: T,
columns: Q[],
Expand All @@ -42,3 +50,16 @@ export function enumToPgEnum<T extends Record<string, any>>(
): [T[keyof T], ...T[keyof T][]] {
return Object.values(myEnum).map((value: any) => `${value}`) as any;
}

export const ColumnOperatorFn: Record<
ColumnOperator,
<T extends PgTable, Q extends ColumnKey<T>>(
column: ColumnType<T, Q>,
value: string,
) => SQL<unknown>
> = {
[ColumnOperator.Equals]: (column, value) => eq(column, value),
[ColumnOperator.StartsWith]: (column, value) => ilike(column, `${value}%`),
[ColumnOperator.EndsWith]: (column, value) => ilike(column, `%${value}`),
[ColumnOperator.Contains]: (column, value) => ilike(column, `%${value}%`),
};
37 changes: 26 additions & 11 deletions packages/db/src/schema/deployment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DeploymentCondition } from "@ctrlplane/validators/jobs";
import type { DeploymentCondition } from "@ctrlplane/validators/deployments";
import type { ResourceCondition } from "@ctrlplane/validators/resources";
import type { InferSelectModel, SQL } from "drizzle-orm";
import { relations, sql } from "drizzle-orm";
import { and, eq, not, or, relations, sql } from "drizzle-orm";
import {
integer,
jsonb,
Expand All @@ -13,12 +13,13 @@ import {
import { createInsertSchema } from "drizzle-zod";
import { z } from "zod";

import { ComparisonOperator } from "@ctrlplane/validators/conditions";
import {
isValidResourceCondition,
resourceCondition,
} from "@ctrlplane/validators/resources";

import type { Tx } from "../common.js";
import { ColumnOperatorFn } from "../common.js";
import { jobAgent } from "./job-agent.js";
import { system } from "./system.js";

Expand Down Expand Up @@ -117,11 +118,25 @@ export const deploymentDependency = pgTable(
(t) => ({ uniq: uniqueIndex().on(t.dependsOnId, t.deploymentId) }),
);

export function deploymentMatchSelector(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
tx: Tx,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
metadata?: DeploymentCondition | null,
): SQL<unknown> | undefined {
throw new Error("Not implemented");
}
const buildCondition = (cond: DeploymentCondition): SQL<unknown> => {
if (cond.type === "name")
return ColumnOperatorFn[cond.operator](deployment.name, cond.value);
if (cond.type === "slug")
return ColumnOperatorFn[cond.operator](deployment.slug, cond.value);
if (cond.type === "system") return eq(deployment.systemId, cond.value);
if (cond.type === "id") return eq(deployment.id, cond.value);

if (cond.conditions.length === 0) return sql`FALSE`;

const subCon = cond.conditions.map((c) => buildCondition(c));
const con =
cond.operator === ComparisonOperator.And ? and(...subCon)! : or(...subCon)!;
return cond.not ? not(con) : con;
};

export const deploymentMatchSelector = (
condition?: DeploymentCondition | null,
): SQL<unknown> | undefined =>
condition == null || Object.keys(condition).length === 0
? undefined
: buildCondition(condition);
6 changes: 2 additions & 4 deletions packages/db/src/schema/policy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type {
DeploymentCondition,
EnvironmentCondition,
} from "@ctrlplane/validators/jobs";
import type { DeploymentCondition } from "@ctrlplane/validators/deployments";
import type { EnvironmentCondition } from "@ctrlplane/validators/jobs";
import type { InferSelectModel } from "drizzle-orm";
import type { Options } from "rrule";
import { sql } from "drizzle-orm";
Expand Down
2 changes: 1 addition & 1 deletion packages/rule-engine/src/db/get-applicable-policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const matchPolicyTargetForResource = async (
.from(schema.deployment)
.where(
and(
schema.deploymentMatchSelector(db, deploymentSelector),
schema.deploymentMatchSelector(deploymentSelector),
eq(schema.deployment.id, deploymentId),
),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { z } from "zod";

import type { IdCondition } from "./id-condition.js";
import type { NameCondition } from "./name-condition.js";
import type { SlugCondition } from "./slug-condition.js";
import type { SystemCondition } from "./system-condition.js";
import { idCondition } from "./id-condition.js";
import { nameCondition } from "./name-condition.js";
import { slugCondition } from "./slug-condition.js";
import { systemCondition } from "./system-condition.js";

export const comparisonCondition: z.ZodType<ComparisonCondition> = z.lazy(() =>
z.object({
type: z.literal("comparison"),
operator: z.literal("and").or(z.literal("or")),
not: z.boolean().optional().default(false),
conditions: z.array(
z.union([nameCondition, slugCondition, systemCondition, idCondition]),
),
}),
);

export type ComparisonCondition = {
type: "comparison";
operator: "and" | "or";
not?: boolean;
conditions: Array<
NameCondition | SlugCondition | SystemCondition | IdCondition
>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { z } from "zod";

import type { ComparisonCondition } from "./comparison-condition.js";
import type { IdCondition } from "./id-condition.js";
import type { NameCondition } from "./name-condition.js";
import type { SlugCondition } from "./slug-condition.js";
import type { SystemCondition } from "./system-condition.js";
import { comparisonCondition } from "./comparison-condition.js";
import { idCondition } from "./id-condition.js";
import { nameCondition } from "./name-condition.js";
import { slugCondition } from "./slug-condition.js";
import { systemCondition } from "./system-condition.js";

export type DeploymentCondition =
| ComparisonCondition
| NameCondition
| SlugCondition
| SystemCondition
| IdCondition;

export const deploymentCondition: z.ZodType<DeploymentCondition> = z.lazy(() =>
z.union([
comparisonCondition,
nameCondition,
slugCondition,
systemCondition,
idCondition,
]),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from "zod";

export const idCondition = z.object({
type: z.literal("id"),
operator: z.literal("equals"),
value: z.string(),
});

export type IdCondition = z.infer<typeof idCondition>;
5 changes: 5 additions & 0 deletions packages/validators/src/deployments/conditions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./comparison-condition.js";
export * from "./name-condition.js";
export * from "./slug-condition.js";
export * from "./deployment-condition.js";
export * from "./system-condition.js";
11 changes: 11 additions & 0 deletions packages/validators/src/deployments/conditions/name-condition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { z } from "zod";

import { columnOperator } from "../../conditions/index.js";

export const nameCondition = z.object({
type: z.literal("name"),
operator: columnOperator,
value: z.string(),
});

export type NameCondition = z.infer<typeof nameCondition>;
11 changes: 11 additions & 0 deletions packages/validators/src/deployments/conditions/slug-condition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { z } from "zod";

import { columnOperator } from "../../conditions/index.js";

export const slugCondition = z.object({
type: z.literal("slug"),
operator: columnOperator,
value: z.string(),
});

export type SlugCondition = z.infer<typeof slugCondition>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from "zod";

export const systemCondition = z.object({
type: z.literal("system"),
operator: z.literal("equals"),
value: z.string(),
});

export type SystemCondition = z.infer<typeof systemCondition>;
2 changes: 2 additions & 0 deletions packages/validators/src/deployments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export enum StatsOrder {
}

export const statsOrder = z.nativeEnum(StatsOrder);

export * from "./conditions/index.js";
Loading