@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 222 lines 7.63 KB
Raw
1 import * as apigee from "@distilled.cloud/gcp/apigee_v1";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import { GcpEnvironment } from "../Environment.ts";
8 import type { Providers } from "../Providers.ts";
9 import {
10 deployedConfig,
11 environmentIdOf,
12 environmentNameOf,
13 lastSegment,
14 listProjectEnvironments,
15 missingToUndefined,
16 namesFromConfig,
17 organizationIdOf,
18 parseOrgEnv,
19 toResourceId,
20 } from "./common.ts";
21
22 const MAX_NAME_LENGTH = 255;
23
24 export type EnvironmentsKeystoreProps = {
25 /**
26 * Apigee organization id or `organizations/{org}`. Defaults to the
27 * current GCP project id. Immutable — changing it replaces the keystore.
28 */
29 organization?: string;
30 /**
31 * Environment id or `organizations/{org}/environments/{env}`. Immutable —
32 * changing it replaces the keystore.
33 */
34 environment: string;
35 /**
36 * Keystore id (last path segment). If omitted, a unique name is generated
37 * from the stack, stage, and logical id. Must match
38 * `[\w[:space:].-]{1,255}`. Immutable — changing it replaces the keystore.
39 */
40 keystoreId?: string;
41 };
42
43 export type EnvironmentsKeystore = Resource<
44 "GCP.Apigee.EnvironmentsKeystore",
45 EnvironmentsKeystoreProps,
46 {
47 /** Full resource name `organizations/{org}/environments/{env}/keystores/{keystore}`. */
48 name: string;
49 /** Keystore id (last path segment). */
50 keystoreId: string;
51 /** Apigee organization id. */
52 organizationId: string;
53 /** Environment id. */
54 environmentId: string;
55 /** Alias ids stored in this keystore. */
56 aliases: string[];
57 },
58 never,
59 Providers
60 >;
61
62 /**
63 * An Apigee environment keystore or truststore for TLS certificates.
64 *
65 * Keystores have no labels or description, so `list` enumerates every
66 * keystore in Apigee environments mapped to this GCP project for
67 * `pnpm nuke:gcp`. Name is identity — changing `keystoreId`,
68 * `organization`, or `environment` replaces the keystore. Aliases are
69 * managed by `EnvironmentsKeystoresAlias`.
70 *
71 * ### Creating a Keystore
72 * **Example:** Generated name
73 * ```typescript
74 * const keystore = yield* GCP.Apigee.EnvironmentsKeystore("Tls", {
75 * environment: "eval",
76 * });
77 * ```
78 *
79 * **Example:** Named keystore
80 * ```typescript
81 * const keystore = yield* GCP.Apigee.EnvironmentsKeystore("Tls", {
82 * environment: "eval",
83 * keystoreId: "app-tls",
84 * });
85 * ```
86 *
87 * @resource
88 * @category Apigee
89 */
90 export const EnvironmentsKeystore = Resource<EnvironmentsKeystore>(
91 "GCP.Apigee.EnvironmentsKeystore",
92 );
93
94 export class EnvironmentsKeystoreNotResolved extends Data.TaggedError(
95 "GCP.Apigee.EnvironmentsKeystoreNotResolved",
96 )<{
97 resourceName: string;
98 }> {}
99
100 const resourceName = (organizationId: string, environmentId: string, keystoreId: string) =>
101 `${environmentNameOf(organizationId, environmentId)}/keystores/${keystoreId}`;
102
103 const toAttrs = (
104 keystore: apigee.GoogleCloudApigeeV1Keystore,
105 organizationId: string,
106 environmentId: string,
107 ) => {
108 const raw = keystore.name ?? "";
109 const parsed = parseOrgEnv(raw);
110 const keystoreId = lastSegment(raw);
111 return {
112 name: raw.includes("/") ? raw : resourceName(organizationId, environmentId, keystoreId || raw),
113 keystoreId: keystoreId || raw,
114 organizationId: parsed.organizationId || organizationId,
115 environmentId: parsed.environmentId || environmentId,
116 aliases: keystore.aliases ?? [],
117 };
118 };
119
120 const getByName = (name: string) =>
121 missingToUndefined(apigee.getOrganizationsEnvironmentsKeystores({ name }));
122
123 const listIds = (parent: string) =>
124 deployedConfig(parent).pipe(Effect.map((config) => namesFromConfig(config?.keystores)));
125
126 export const EnvironmentsKeystoreProvider = () =>
127 Provider.succeed(EnvironmentsKeystore, {
128 stables: ["name", "keystoreId", "organizationId", "environmentId"],
129
130 diff: Effect.fn(function* ({ news, olds, output }) {
131 if (!isResolved(news)) return undefined;
132 const previousId = olds?.keystoreId ?? output?.keystoreId;
133 const previousOrg = olds?.organization ?? output?.organizationId;
134 const previousEnv = olds?.environment ?? output?.environmentId;
135 const idChanged =
136 previousId !== undefined && news.keystoreId !== undefined && news.keystoreId !== previousId;
137 const orgChanged =
138 previousOrg !== undefined &&
139 news.organization !== undefined &&
140 organizationIdOf(news.organization, "") !== organizationIdOf(previousOrg, "");
141 const envChanged =
142 previousEnv !== undefined &&
143 environmentIdOf(news.environment) !== environmentIdOf(previousEnv);
144 if (idChanged || orgChanged || envChanged) {
145 return { action: "replace" as const, deleteFirst: false };
146 }
147 return undefined;
148 }),
149
150 read: Effect.fn(function* ({ id, olds, output }) {
151 const { project } = yield* GcpEnvironment.current;
152 const organizationId = organizationIdOf(
153 olds?.organization ?? output?.organizationId,
154 project,
155 );
156 const environmentId = environmentIdOf(olds?.environment ?? output?.environmentId ?? "");
157 const keystoreId = yield* toResourceId(id, olds?.keystoreId, output?.keystoreId, {
158 maxLength: MAX_NAME_LENGTH,
159 });
160 const name = output?.name ?? resourceName(organizationId, environmentId, keystoreId);
161 const existing = yield* getByName(name);
162 if (existing === undefined) return undefined;
163 return toAttrs(existing, organizationId, environmentId);
164 }),
165
166 list: () =>
167 Effect.gen(function* () {
168 const environments = yield* listProjectEnvironments();
169 const found: EnvironmentsKeystore["Attributes"][] = [];
170 for (const item of environments) {
171 const ids = yield* listIds(item.parent);
172 for (const raw of ids) {
173 const name = raw.includes("/")
174 ? raw
175 : resourceName(item.organizationId, item.environmentId, raw);
176 const keystore = yield* getByName(name);
177 if (keystore === undefined) {
178 found.push(toAttrs({ name }, item.organizationId, item.environmentId));
179 continue;
180 }
181 found.push(toAttrs(keystore, item.organizationId, item.environmentId));
182 }
183 }
184 return found;
185 }),
186
187 reconcile: Effect.fn(function* ({ id, news, output }) {
188 const { project } = yield* GcpEnvironment.current;
189 const organizationId = organizationIdOf(news.organization, project);
190 const environmentId = environmentIdOf(news.environment);
191 const keystoreId = yield* toResourceId(id, news.keystoreId, output?.keystoreId, {
192 maxLength: MAX_NAME_LENGTH,
193 });
194 const parent = environmentNameOf(organizationId, environmentId);
195 const name = resourceName(organizationId, environmentId, keystoreId);
196
197 let current = yield* getByName(output?.name ?? name);
198
199 if (current === undefined) {
200 const created = yield* apigee
201 .createOrganizationsEnvironmentsKeystores({
202 parent,
203 name: keystoreId,
204 body: { name: keystoreId },
205 })
206 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
207 current = created ?? undefined;
208 }
209
210 if (current === undefined) {
211 return yield* new EnvironmentsKeystoreNotResolved({ resourceName: name });
212 }
213
214 return toAttrs(current, organizationId, environmentId);
215 }),
216
217 delete: Effect.fn(function* ({ output }) {
218 yield* apigee
219 .deleteOrganizationsEnvironmentsKeystores({ name: output.name })
220 .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.void));
221 }),
222 });