Skip to content

Commit

Permalink
fix: Target config file endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 committed Oct 21, 2024
1 parent b04c2fe commit 92e539a
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import type { Target } from "@ctrlplane/db/schema";
import type { ColumnDef } from "@tanstack/react-table";
import { SiKubernetes, SiTerraform } from "@icons-pack/react-simple-icons";
import { IconLock, IconServer, IconTarget, IconX } from "@tabler/icons-react";
import { IconLock, IconX } from "@tabler/icons-react";
import {
flexRender,
getCoreRowModel,
Expand Down Expand Up @@ -35,6 +34,7 @@ import {
TableRow,
} from "@ctrlplane/ui/table";

import { TargetIcon } from "~/app/[workspaceSlug]/_components/TargetIcon";
import { api } from "~/trpc/react";

const columns: ColumnDef<Target>[] = [
Expand Down Expand Up @@ -78,24 +78,15 @@ const columns: ColumnDef<Target>[] = [
header: "Name",
accessorKey: "name",
cell: (info) => {
const includes = (key: string) => info.row.original.version.includes(key);
const isKube = includes("kubernetes");
const isVm = includes("vm") || includes("compute");
const isLocked = info.row.original.lockedAt != null;
const isTerraform = includes("terraform");

return (
<div className="flex items-center gap-2 px-2 py-1">
{isLocked ? (
<IconLock className="h-4 w-4 shrink-0 text-red-300" />
) : isKube ? (
<SiKubernetes className="h-4 w-4 shrink-0 text-blue-300" />
) : isVm ? (
<IconServer className="h-4 w-4 shrink-0 text-cyan-300" />
) : isTerraform ? (
<SiTerraform className="h-4 w-4 shrink-0 text-purple-300" />
) : (
<IconTarget className="h-4 w-4 shrink-0 text-neutral-300" />
{isLocked && <IconLock className="h-4 w-4 shrink-0 text-red-300" />}
{!isLocked && (
<TargetIcon
version={info.row.original.version}
kind={info.row.original.kind}
/>
)}
{info.getValue<string>()}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import type { TargetCondition } from "@ctrlplane/validators/targets";
import React, { useState } from "react";
import Link from "next/link";
import { useParams, useRouter, useSearchParams } from "next/navigation";
import { SiKubernetes, SiTerraform } from "@icons-pack/react-simple-icons";
import {
IconExternalLink,
IconLoader2,
IconPlant,
IconSelector,
IconServer,
IconTarget,
} from "@tabler/icons-react";
import * as LZString from "lz-string";
import { z } from "zod";
Expand Down Expand Up @@ -58,6 +55,7 @@ import {

import { api } from "~/trpc/react";
import { TargetConditionRender } from "./target-condition/TargetConditionRender";
import { TargetIcon } from "./TargetIcon";

const DeleteEnvironmentDialog: React.FC<{
environment: schema.Environment;
Expand Down Expand Up @@ -236,16 +234,6 @@ const TargetViewsCombobox: React.FC<{
);
};

const TargetIcon: React.FC<{ version: string }> = ({ version }) => {
if (version.includes("kubernetes"))
return <SiKubernetes className="h-6 w-6 shrink-0 text-blue-300" />;
if (version.includes("vm") || version.includes("compute"))
return <IconServer className="h-6 w-6 shrink-0 text-cyan-300" />;
if (version.includes("terraform"))
return <SiTerraform className="h-6 w-6 shrink-0 text-purple-300" />;
return <IconTarget className="h-6 w-6 shrink-0 text-neutral-300" />;
};

const filterForm = z.object({
targetFilter: targetCondition.optional(),
});
Expand Down Expand Up @@ -353,7 +341,7 @@ const EditFilterForm: React.FC<{
<div className="space-y-2">
{targets.data.items.map((target) => (
<div className="flex items-center gap-2" key={target.id}>
<TargetIcon version={target.version} />
<TargetIcon version={target.version} kind={target.kind} />
<div className="flex flex-col">
<span className="overflow-hidden text-nowrap text-sm">
{target.name}
Expand Down
18 changes: 18 additions & 0 deletions apps/webservice/src/app/[workspaceSlug]/_components/TargetIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { SiKubernetes, SiTerraform } from "@icons-pack/react-simple-icons";
import { IconServer, IconTarget, IconUsersGroup } from "@tabler/icons-react";

export const TargetIcon: React.FC<{ version: string; kind?: string }> = ({
version,
kind,
}) => {
if (kind?.toLowerCase().includes("shared"))
return <IconUsersGroup className="h-4 w-4 shrink-0 text-blue-300" />;
if (version.includes("kubernetes"))
return <SiKubernetes className="h-4 w-4 shrink-0 text-blue-300" />;
if (version.includes("vm") || version.includes("compute"))
return <IconServer className="h-4 w-4 shrink-0 text-cyan-300" />;
if (version.includes("terraform"))
return <SiTerraform className="h-4 w-4 shrink-0 text-purple-300" />;

return <IconTarget className="h-4 w-4 shrink-0 text-neutral-300" />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import type {
ReactFlowInstance,
} from "reactflow";
import { useCallback, useEffect, useState } from "react";
import { SiKubernetes, SiTerraform } from "@icons-pack/react-simple-icons";
import { IconTarget } from "@tabler/icons-react";
import ReactFlow, {
BaseEdge,
EdgeLabelRenderer,
Expand All @@ -27,6 +25,7 @@ import colors from "tailwindcss/colors";
import { cn } from "@ctrlplane/ui";

import { getLayoutedElementsDagre } from "~/app/[workspaceSlug]/_components/reactflow/layout";
import { TargetIcon } from "~/app/[workspaceSlug]/_components/TargetIcon";
import { api } from "~/trpc/react";

type TargetNodeProps = NodeProps<{
Expand All @@ -53,13 +52,7 @@ const TargetNode: React.FC<TargetNodeProps> = (node) => {
)}
>
<div className="flex h-12 w-12 items-center justify-center rounded-full">
{isKubernetes ? (
<SiKubernetes className="h-8 w-8 text-blue-500" />
) : isTerraform ? (
<SiTerraform className="h-8 w-8 text-purple-300" />
) : (
<IconTarget className="h-8 w-8 text-neutral-500" />
)}
<TargetIcon version={data.version} kind={data.kind} />
</div>
<div className="text-sm font-medium text-muted-foreground">
{data.kind}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
import { useState } from "react";
import Link from "next/link";
import { useParams } from "next/navigation";
import { SiKubernetes, SiTerraform } from "@icons-pack/react-simple-icons";
import {
IconChevronRight,
IconDotsVertical,
IconServer,
IconTarget,
} from "@tabler/icons-react";
import { IconChevronRight, IconDotsVertical } from "@tabler/icons-react";

import { cn } from "@ctrlplane/ui";
import { Badge } from "@ctrlplane/ui/badge";
Expand All @@ -24,20 +18,11 @@ import { Table, TableBody, TableCell, TableRow } from "@ctrlplane/ui/table";

import type { VariableData } from "./variable-data";
import { useTargetDrawer } from "~/app/[workspaceSlug]/_components/target-drawer/TargetDrawer";
import { TargetIcon } from "~/app/[workspaceSlug]/_components/TargetIcon";
import { useMatchSorterWithSearch } from "~/utils/useMatchSorter";
import { VariableDropdown } from "./VariableDropdown";
import { VariableValueDropdown } from "./VariableValueDropdown";

const TargetIcon: React.FC<{ version: string }> = ({ version }) => {
if (version.includes("kubernetes"))
return <SiKubernetes className="h-6 w-6 shrink-0 text-blue-300" />;
if (version.includes("vm") || version.includes("compute"))
return <IconServer className="h-6 w-6 shrink-0 text-cyan-300" />;
if (version.includes("terraform"))
return <SiTerraform className="h-6 w-6 shrink-0 text-purple-300" />;
return <IconTarget className="h-6 w-6 shrink-0 text-neutral-300" />;
};

export const VariableTable: React.FC<{
variables: VariableData[];
}> = ({ variables }) => {
Expand Down Expand Up @@ -249,7 +234,10 @@ export const VariableTable: React.FC<{
>
<div className="flex h-full items-center border-l border-neutral-800 pl-7">
<div className="flex h-full items-center gap-2 border-l border-neutral-800 pl-6">
<TargetIcon version={t.version} />
<TargetIcon
version={t.version}
kind={t.kind}
/>
<div className="flex flex-col">
<span className="overflow-hidden text-nowrap text-sm">
{t.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createTarget } from "@ctrlplane/db/schema";
import { upsertTargets } from "@ctrlplane/job-dispatch";
import { Permission } from "@ctrlplane/validators/auth";

import { getUser } from "../../../auth";
import { getUser } from "~/app/api/v1/auth";

const bodySchema = z.array(
createTarget
Expand All @@ -23,20 +23,22 @@ const canUpsertTarget = async (userId: string, workspaceId: string) =>
.perform(Permission.TargetCreate)
.on({ type: "workspace", id: workspaceId });

const parseBody = (rawBody: string): object => {
const parseJson = (rawBody: string): object => {
try {
return JSON.parse(rawBody);
} catch {
try {
const yamlResult = yaml.load(rawBody);
if (typeof yamlResult === "object" && yamlResult !== null) {
return yamlResult;
}
} catch {
// YAML parsing failed
}
} catch (e) {
throw new Error("Invalid input: not valid JSON", { cause: e });
}
};

const parseYaml = (rawBody: string) => {
try {
const targets: unknown[] = [];
yaml.loadAll(rawBody, (obj) => targets.push(obj));
return targets;
} catch (e) {
throw new Error("Invalid input: not valid YAML", { cause: e });
}
throw new Error("Invalid input: not valid JSON or YAML");
};

export const PATCH = async (
Expand All @@ -53,8 +55,14 @@ export const PATCH = async (
return NextResponse.json({ error: "Permission denied" }, { status: 403 });

const rawBody = await req.text();
const parsedBody = parseBody(rawBody);
const contentType = req.headers.get("content-type");
const parsedBody =
contentType === "application/x-yaml" || contentType === "application/yaml"
? parseYaml(rawBody)
: parseJson(rawBody);

const parsedTargets = bodySchema.parse(parsedBody);
if (parsedTargets.length === 0) return NextResponse.json({ count: 0 });

const targets = await upsertTargets(
db,
Expand Down
7 changes: 4 additions & 3 deletions packages/api/src/router/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,10 @@ const targetQuery = (db: Tx, checks: Array<SQL<unknown>>) =>
target: schema.target,
targetProvider: schema.targetProvider,
workspace: schema.workspace,
targetMetadata:
sql<_StringStringRecord>`jsonb_object_agg(target_metadata.key,
target_metadata.value)`.as("target_metadata"),
targetMetadata: sql<_StringStringRecord>`
jsonb_object_agg(target_metadata.key, target_metadata.value)
FILTER (WHERE target_metadata.key IS NOT NULL)
`.as("target_metadata"),
})
.from(schema.target)
.leftJoin(
Expand Down
23 changes: 12 additions & 11 deletions packages/job-dispatch/src/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,18 @@ export const upsertTargets = async (
),
);

await tx
.insert(targetMetadata)
.values(targetMetadataValues)
.onConflictDoUpdate({
target: [targetMetadata.targetId, targetMetadata.key],
set: buildConflictUpdateColumns(targetMetadata, ["value"]),
})
.catch((err) => {
log.error("Error inserting target metadata", { error: err });
throw err;
});
if (targetMetadataValues.length > 0)
await tx
.insert(targetMetadata)
.values(targetMetadataValues)
.onConflictDoUpdate({
target: [targetMetadata.targetId, targetMetadata.key],
set: buildConflictUpdateColumns(targetMetadata, ["value"]),
})
.catch((err) => {
log.error("Error inserting target metadata", { error: err });
throw err;
});

await tx
.delete(targetMetadata)
Expand Down
4 changes: 2 additions & 2 deletions packages/storage/src/Storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Readable } from "node:stream";
import { z } from "zod";

import type { StorageFile } from "./StorageFile";
// import type { StorageFile } from "./StorageFile.js";

export const FileMetadata = z.object({
name: z.string(),
Expand Down Expand Up @@ -43,7 +43,7 @@ export interface StorageDriver {
put(key: string, value: string): Promise<void>;
putStream(key: string, value: Readable): Promise<void>;

list(prefix: string): Promise<StorageFile[]>;
// list(prefix: string): Promise<StorageFile[]>;

getMetaData(key: string): Promise<ObjectMetadata>;
getSignedUrl(path: string, opts: SignedURLOptions): Promise<string>;
Expand Down
9 changes: 0 additions & 9 deletions packages/storage/src/StorageManager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
import type { Storage } from "@google-cloud/storage";

export class StorageManager {
constructor() {}

use(name: string): Storage {
return null;
}
}
9 changes: 5 additions & 4 deletions packages/storage/src/drivers/fs/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import type {
SignedURLOptions,
StorageDriver,
} from "../../Storage.js";
import type { StorageFile } from "../../StorageFile.js";

// import type { StorageFile } from "../../StorageFile.js";

type FSDriverOptions = {
location?: string;
Expand Down Expand Up @@ -75,9 +76,9 @@ export class FSDriver implements StorageDriver {
await fs.writeFile(location, value);
}

list(_: string): Promise<StorageFile[]> {
throw new Error("Method not implemented.");
}
// list(_: string): Promise<StorageFile[]> {
// throw new Error("Method not implemented.");
// }

getSignedUrl(key: string, opts: SignedURLOptions): Promise<string> {
if (this.options.generateSignedURL == null)
Expand Down
9 changes: 5 additions & 4 deletions packages/storage/src/drivers/gcs/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import type {
SignedURLOptions,
StorageDriver,
} from "../../Storage.js";
import type { StorageFile } from "../../StorageFile.js";

// import type { StorageFile } from "../../StorageFile.js";

type GCSDriverBaseOptions = {
bucket: string;
Expand Down Expand Up @@ -66,9 +67,9 @@ export class GCSStorageDriver implements StorageDriver {
});
}

list(_: string): Promise<StorageFile[]> {
throw new Error("Not implemented");
}
// list(_: string): Promise<StorageFile[]> {
// throw new Error("Not implemented");
// }

async getMetaData(key: string): Promise<ObjectMetadata> {
const [metadata] = await this.bucket.file(key).getMetadata();
Expand Down

0 comments on commit 92e539a

Please sign in to comment.