@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 386 lines 13.2 KB
Raw
1 import * as kafka from "@distilled.cloud/gcp/managedkafka_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import { tagRecord } from "alchemy/Tags";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import { GcpEnvironment } from "../Environment.ts";
10 import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
11 import type { Providers } from "../Providers.ts";
12 import {
13 asCountString,
14 DEFAULT_MEMORY_BYTES,
15 DEFAULT_VCPU_COUNT,
16 defaultSubnet,
17 expandParent,
18 fieldMask,
19 fingerprint,
20 getConnectCluster,
21 listAlchemyConnectClusters,
22 locationParent,
23 normalizeLocation,
24 parseName,
25 replaceOnIdentity,
26 retryCreateQuota,
27 ResourceNotResolved,
28 rfc1035,
29 stringMapOf,
30 MAX_CLUSTER_ID_LENGTH,
31 toPhysicalId,
32 userLabels,
33 waitForOperation,
34 waitUntilExists,
35 waitUntilGone,
36 waitUntilReady,
37 } from "./internal.ts";
38
39 export type ConnectNetworkConfig = {
40 /**
41 * Primary VPC subnet for Connect workers. Must be RFC1918 and at least
42 * `/22`. Defaults to the project's `default` subnet.
43 */
44 primarySubnet?: string;
45 /** Additional subnets. Deprecated — Connect can reach any endpoint. */
46 additionalSubnets?: string[];
47 /** Extra DNS domain names visible to workers. */
48 dnsDomainNames?: string[];
49 };
50
51 export type ConnectAccessConfig = {
52 /** VPC networks granted access. At least one is required. */
53 networkConfigs?: ConnectNetworkConfig[];
54 };
55
56 export type ConnectGcpConfig = {
57 /** Access configuration. */
58 accessConfig?: ConnectAccessConfig;
59 /** Secret Manager versions loaded into workers. Maximum 32. */
60 secretPaths?: string[];
61 };
62
63 export type ConnectCapacityConfig = {
64 /**
65 * vCPUs to provision. Minimum 3.
66 * @default 3
67 */
68 vcpuCount?: number | string;
69 /**
70 * Memory in bytes. Minimum 3221225472 (3 GiB).
71 * @default 3221225472
72 */
73 memoryBytes?: number | string;
74 };
75
76 export type ConnectClusterProps = {
77 /**
78 * Connect cluster id. Lowercase letters, digits, and hyphens; max 53
79 * characters (longer values are truncated). If omitted, a unique RFC1035
80 * name of at most 53 characters is generated. Immutable — changing it
81 * replaces the cluster.
82 */
83 connectClusterId?: string;
84 /**
85 * Region (`us-central1`, …). Immutable.
86 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
87 */
88 location?: string;
89 /**
90 * Kafka cluster this Connect cluster attaches to
91 * (`projects/{project}/locations/{location}/clusters/{cluster}`).
92 * Immutable — changing it replaces the Connect cluster.
93 */
94 kafkaCluster: string;
95 /**
96 * Primary subnet for workers. Defaults to the project's `default`
97 * subnet in `location`. Immutable.
98 */
99 primarySubnet?: string;
100 /** Additional subnets. */
101 additionalSubnets?: string[];
102 /** Extra DNS domain names. */
103 dnsDomainNames?: string[];
104 /** Full GCP config. Flattened fields win when both are set. */
105 gcpConfig?: ConnectGcpConfig;
106 /** Capacity. Defaults to 3 vCPU / 3 GiB. */
107 capacityConfig?: ConnectCapacityConfig;
108 /** Worker config overrides. Currently unused by the API. */
109 config?: Record<string, string>;
110 /** Secret Manager versions loaded into workers. */
111 secretPaths?: string[];
112 /**
113 * User labels. Alchemy ownership labels are merged in automatically.
114 */
115 labels?: Record<string, string>;
116 };
117
118 export type ConnectCluster = Resource<
119 "GCP.ManagedKafka.ConnectCluster",
120 ConnectClusterProps,
121 {
122 /** Full resource name `.../connectClusters/{connectCluster}`. */
123 name: string;
124 /** Connect cluster id. */
125 connectClusterId: string;
126 /** Project id. */
127 project: string;
128 /** Location id. */
129 location: string;
130 /** Attached Kafka cluster. */
131 kafkaCluster: string;
132 /** User labels (Alchemy ownership labels stripped). */
133 labels: Record<string, string>;
134 /** Server-reported state. */
135 state: string | undefined;
136 /** vCPU count. */
137 vcpuCount: string | undefined;
138 /** Memory in bytes. */
139 memoryBytes: string | undefined;
140 /** Primary subnet. */
141 primarySubnet: string | undefined;
142 /** Worker config. */
143 config: Record<string, string>;
144 /** RFC3339 creation timestamp. */
145 createTime: string | undefined;
146 /** RFC3339 last-update timestamp. */
147 updateTime: string | undefined;
148 },
149 never,
150 Providers
151 >;
152
153 /**
154 * A Managed Service for Apache Kafka Connect cluster.
155 *
156 * Changing `connectClusterId`, `location`, `kafkaCluster`, or the primary
157 * subnet replaces the cluster. Capacity, labels, and worker config update
158 * in place. Provisioning typically takes more than a minute.
159 *
160 * ### Creating a Connect Cluster
161 * **Example:** Attach to a Kafka cluster
162 * ```typescript
163 * const connect = yield* GCP.ManagedKafka.ConnectCluster("Connect", {
164 * kafkaCluster: cluster.name,
165 * });
166 * ```
167 *
168 * **Example:** Explicit id and labels
169 * ```typescript
170 * const connect = yield* GCP.ManagedKafka.ConnectCluster("Connect", {
171 * connectClusterId: "app-connect",
172 * kafkaCluster: cluster.name,
173 * labels: { env: "prod" },
174 * });
175 * ```
176 *
177 * @resource
178 * @category ManagedKafka
179 */
180 export const ConnectCluster = Resource<ConnectCluster>("GCP.ManagedKafka.ConnectCluster");
181
182 export class ConnectClusterNotResolved extends Data.TaggedError(
183 "GCP.ManagedKafka.ConnectClusterNotResolved",
184 )<{
185 resourceName: string;
186 }> {}
187
188 const resourceName = (project: string, location: string, connectClusterId: string) =>
189 `projects/${project}/locations/${location}/connectClusters/${connectClusterId}`;
190
191 const kafkaClusterOf = (value: string, project: string, location: string) =>
192 expandParent(value, project, location, "clusters");
193
194 const primarySubnetOf = (news: ConnectClusterProps, project: string, location: string) =>
195 news.primarySubnet ??
196 news.gcpConfig?.accessConfig?.networkConfigs?.[0]?.primarySubnet ??
197 defaultSubnet(project, location);
198
199 const desiredGcpConfig = (
200 news: ConnectClusterProps,
201 project: string,
202 location: string,
203 ): kafka.ConnectGcpConfig => ({
204 accessConfig: {
205 networkConfigs: [
206 {
207 primarySubnet: primarySubnetOf(news, project, location),
208 additionalSubnets:
209 news.additionalSubnets ??
210 news.gcpConfig?.accessConfig?.networkConfigs?.[0]?.additionalSubnets,
211 dnsDomainNames:
212 news.dnsDomainNames ?? news.gcpConfig?.accessConfig?.networkConfigs?.[0]?.dnsDomainNames,
213 },
214 ],
215 },
216 secretPaths: news.secretPaths ?? news.gcpConfig?.secretPaths,
217 });
218
219 const toAttrs = (cluster: kafka.ConnectCluster, project: string) => {
220 const name = cluster.name ?? "";
221 const parsed = parseName(name, "connectClusters");
222 return {
223 name,
224 connectClusterId: parsed.id,
225 project: parsed.project || project,
226 location: parsed.location,
227 kafkaCluster: cluster.kafkaCluster ?? "",
228 labels: userLabels(cluster.labels),
229 state: cluster.state,
230 vcpuCount: cluster.capacityConfig?.vcpuCount,
231 memoryBytes: cluster.capacityConfig?.memoryBytes,
232 primarySubnet: cluster.gcpConfig?.accessConfig?.networkConfigs?.[0]?.primarySubnet,
233 config: stringMapOf(cluster.config),
234 createTime: cluster.createTime,
235 updateTime: cluster.updateTime,
236 };
237 };
238
239 export const ConnectClusterProvider = () =>
240 Provider.succeed(ConnectCluster, {
241 stables: ["name", "connectClusterId", "project", "location", "kafkaCluster", "createTime"],
242
243 diff: Effect.fn(function* ({ news, olds, output }) {
244 if (!isResolved(news)) return undefined;
245 const env = yield* GcpEnvironment.current;
246 const location = normalizeLocation(news.location ?? output?.location, env.region);
247 const previousKafka = olds?.kafkaCluster ?? output?.kafkaCluster ?? "";
248 const nextKafka = kafkaClusterOf(news.kafkaCluster, env.project, location);
249 const previousSubnet = olds?.primarySubnet ?? output?.primarySubnet ?? "";
250 const nextSubnet = primarySubnetOf(news, env.project, location);
251 return replaceOnIdentity({
252 previousId: olds?.connectClusterId ?? output?.connectClusterId,
253 nextId: news.connectClusterId
254 ? rfc1035(news.connectClusterId, "connect", MAX_CLUSTER_ID_LENGTH)
255 : (olds?.connectClusterId ?? output?.connectClusterId),
256 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
257 nextLocation: location,
258 extra: previousKafka !== nextKafka || previousSubnet !== nextSubnet,
259 });
260 }),
261
262 read: Effect.fn(function* ({ id, olds, output }) {
263 const env = yield* GcpEnvironment.current;
264 const connectClusterId = yield* toPhysicalId(
265 id,
266 olds?.connectClusterId,
267 output?.connectClusterId,
268 "connect",
269 MAX_CLUSTER_ID_LENGTH,
270 );
271 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
272 const name = output?.name ?? resourceName(env.project, location, connectClusterId);
273 const existing = yield* getConnectCluster(name);
274 if (existing === undefined) return undefined;
275 const attrs = toAttrs(existing, env.project);
276 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
277 }),
278
279 list: () =>
280 Effect.gen(function* () {
281 const env = yield* GcpEnvironment.current;
282 const clusters = yield* listAlchemyConnectClusters(env.project, env.region);
283 return clusters.map((cluster) => toAttrs(cluster, env.project));
284 }),
285
286 reconcile: Effect.fn(function* ({ id, news, output }) {
287 const env = yield* GcpEnvironment.current;
288 const connectClusterId = yield* toPhysicalId(
289 id,
290 news.connectClusterId,
291 output?.connectClusterId,
292 "connect",
293 MAX_CLUSTER_ID_LENGTH,
294 );
295 const location = normalizeLocation(news.location ?? output?.location, env.region);
296 const name = output?.name ?? resourceName(env.project, location, connectClusterId);
297 const kafkaCluster = kafkaClusterOf(news.kafkaCluster, env.project, location);
298 const desiredLabels = {
299 ...toLabels(news.labels),
300 ...(yield* createInternalLabels(id)),
301 };
302 const capacity = {
303 vcpuCount: asCountString(news.capacityConfig?.vcpuCount, DEFAULT_VCPU_COUNT),
304 memoryBytes: asCountString(news.capacityConfig?.memoryBytes, DEFAULT_MEMORY_BYTES),
305 };
306 const gcpConfig = desiredGcpConfig(news, env.project, location);
307
308 let current = yield* getConnectCluster(name);
309
310 if (current === undefined) {
311 const created = yield* kafka
312 .createProjectsLocationsConnectClusters({
313 parent: locationParent(env.project, location),
314 connectClusterId,
315 body: {
316 kafkaCluster,
317 gcpConfig,
318 capacityConfig: capacity,
319 config: news.config,
320 labels: desiredLabels,
321 },
322 })
323 .pipe(
324 retryCreateQuota,
325 Effect.catchTag("Conflict", () => Effect.undefined),
326 );
327 if (created !== undefined) {
328 yield* waitForOperation(created);
329 }
330 current = yield* waitUntilExists(getConnectCluster(name), name);
331 }
332
333 if (current === undefined) {
334 return yield* new ConnectClusterNotResolved({ resourceName: name });
335 }
336
337 current = yield* waitUntilReady(getConnectCluster(name), name, (cluster) => cluster.state);
338
339 const observedLabels = tagRecord(current.labels);
340 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
341 const labelsChanged = upsert.length > 0 || removed.length > 0;
342 const capacityChanged =
343 (current.capacityConfig?.vcpuCount ?? "") !== (capacity.vcpuCount ?? "") ||
344 (current.capacityConfig?.memoryBytes ?? "") !== (capacity.memoryBytes ?? "");
345 const configChanged =
346 fingerprint(stringMapOf(current.config)) !== fingerprint(stringMapOf(news.config));
347 const secretsChanged =
348 fingerprint(current.gcpConfig?.secretPaths ?? []) !==
349 fingerprint(gcpConfig.secretPaths ?? []);
350
351 if (labelsChanged || capacityChanged || configChanged || secretsChanged) {
352 const op = yield* kafka.patchProjectsLocationsConnectClusters({
353 name,
354 updateMask: fieldMask([
355 labelsChanged && "labels",
356 capacityChanged && "capacity_config",
357 configChanged && "config",
358 secretsChanged && "gcp_config.secret_paths",
359 ]),
360 body: {
361 labels: desiredLabels,
362 capacityConfig: capacity,
363 config: news.config,
364 gcpConfig,
365 },
366 });
367 yield* waitForOperation(op);
368 current = yield* waitUntilReady(getConnectCluster(name), name, (cluster) => cluster.state);
369 }
370
371 if (current === undefined) {
372 return yield* new ResourceNotResolved({ resourceName: name });
373 }
374 return toAttrs(current, env.project);
375 }),
376
377 delete: Effect.fn(function* ({ output }) {
378 const op = yield* kafka
379 .deleteProjectsLocationsConnectClusters({ name: output.name })
380 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
381 if (op !== undefined) {
382 yield* waitForOperation(op, { notFoundOk: true });
383 }
384 yield* waitUntilGone(getConnectCluster(output.name), output.name);
385 }),
386 });