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

main
ts 442 lines 14.3 KB
Raw
1 import * as bigtable from "@distilled.cloud/gcp/bigtableadmin_v2";
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 * as Schedule from "effect/Schedule";
10 import { GcpEnvironment } from "../Environment.ts";
11 import {
12 createInternalLabels,
13 diffLabels,
14 hasAlchemyLabels,
15 stripInternalLabels,
16 toLabels,
17 } from "../Labels.ts";
18 import type { Providers } from "../Providers.ts";
19 import {
20 clusterLocation,
21 DEFAULT_CLUSTER_ID,
22 DEFAULT_EDITION,
23 DEFAULT_INSTANCE_TYPE,
24 DEFAULT_SERVE_NODES,
25 DEFAULT_STORAGE,
26 DEFAULT_ZONE,
27 getInstanceByName,
28 instanceName,
29 lastSegment,
30 listAlchemyInstances,
31 MAX_INSTANCE_ID_LENGTH,
32 MIN_INSTANCE_ID_LENGTH,
33 parseResourceName,
34 toPhysicalId,
35 waitForOperation,
36 } from "./operations.ts";
37
38 export type ClusterAutoscalingLimits = {
39 /** Minimum nodes the autoscaler can scale down to. */
40 minServeNodes?: number;
41 /** Maximum nodes the autoscaler can scale up to. */
42 maxServeNodes?: number;
43 };
44
45 export type ClusterAutoscalingTargets = {
46 /** Target CPU utilization percent (`10`–`80`). */
47 cpuUtilizationPercent?: number;
48 /** Target storage utilization in GiB per node. */
49 storageUtilizationGibPerNode?: number;
50 };
51
52 export type InstanceClusterSpec = {
53 /**
54 * Zone (`us-central1-b`) or full location
55 * (`projects/{project}/locations/{zone}`). Immutable after create.
56 * @default "us-central1-b"
57 */
58 location?: string;
59 /**
60 * Manual node count. Mutually exclusive with autoscaling.
61 * @default 1
62 */
63 serveNodes?: number;
64 /**
65 * Storage type. Immutable after create.
66 * @default "HDD"
67 */
68 defaultStorageType?: bigtable.ClusterDefaultStorageTypeEnum | (string & {});
69 /**
70 * Customer-managed KMS key
71 * (`projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}`).
72 * Immutable.
73 */
74 kmsKeyName?: string;
75 /**
76 * Node scaling factor. Immutable.
77 */
78 nodeScalingFactor?: bigtable.ClusterNodeScalingFactorEnum | (string & {});
79 /** Autoscaling limits. Mutually exclusive with `serveNodes`. */
80 autoscalingLimits?: ClusterAutoscalingLimits;
81 /** Autoscaling targets. Used with `autoscalingLimits`. */
82 autoscalingTargets?: ClusterAutoscalingTargets;
83 };
84
85 export type InstanceProps = {
86 /**
87 * Instance id (the `{instance}` segment of
88 * `projects/{project}/instances/{instance}`). If omitted, a unique name
89 * is generated from the stack, stage, and logical id. Must be 6-33
90 * characters, start with a letter, and contain only lowercase letters,
91 * numbers, and hyphens. Immutable — changing it replaces the instance.
92 */
93 instanceId?: string;
94 /**
95 * User-facing display name. Defaults to the instance id.
96 */
97 displayName?: string;
98 /**
99 * Instance type. `DEVELOPMENT` is deprecated; new instances should use
100 * `PRODUCTION`.
101 * @default "PRODUCTION"
102 */
103 type?: bigtable.InstanceTypeEnum | (string & {});
104 /**
105 * Instance edition.
106 * @default "ENTERPRISE"
107 */
108 edition?: bigtable.InstanceEditionEnum | (string & {});
109 /**
110 * User labels. Alchemy ownership labels are merged in automatically.
111 */
112 labels?: Record<string, string>;
113 /**
114 * Clusters created with the instance. At least one is required by the
115 * API. Subsequent cluster changes belong on {@link Cluster}. Keys are
116 * cluster ids.
117 * @default { cluster: { location: "us-central1-b", serveNodes: 1, defaultStorageType: "HDD" } }
118 */
119 clusters?: Record<string, InstanceClusterSpec>;
120 };
121
122 export type Instance = Resource<
123 "GCP.Bigtable.Instance",
124 InstanceProps,
125 {
126 /** Full resource name `projects/{project}/instances/{instance}`. */
127 name: string;
128 /** Instance id (last path segment). */
129 instanceId: string;
130 /** Project id. */
131 project: string;
132 /** User-facing display name. */
133 displayName: string | undefined;
134 /** Instance type (`PRODUCTION`, `DEVELOPMENT`). */
135 type: string;
136 /** Instance edition (`ENTERPRISE`, `ENTERPRISE_PLUS`). */
137 edition: string | undefined;
138 /** User labels (Alchemy ownership labels stripped). */
139 labels: Record<string, string>;
140 /** Server-reported state (`READY`, `CREATING`, …). */
141 state: string | undefined;
142 /** RFC3339 creation timestamp. */
143 createTime: string | undefined;
144 },
145 never,
146 Providers
147 >;
148
149 /**
150 * A Cloud Bigtable instance — a container for clusters, tables, and app
151 * profiles.
152 *
153 * Create requires at least one cluster. The default is a single 1-node
154 * HDD cluster in `us-central1-b`. Changing `instanceId` replaces the
155 * instance. `displayName`, `type`, `edition`, and `labels` update in
156 * place. Cluster create-time fields (`location`, storage type, CMEK) are
157 * immutable; manage clusters after create with GCP.Bigtable.Cluster.
158 *
159 * Provisioning typically takes one to two minutes.
160 *
161 * ### Creating an Instance
162 * **Example:** Generated name, 1-node HDD cluster
163 * ```typescript
164 * const instance = yield* GCP.Bigtable.Instance("Data", {});
165 * ```
166 *
167 * **Example:** Explicit id, labels, and cluster
168 * ```typescript
169 * const instance = yield* GCP.Bigtable.Instance("Data", {
170 * instanceId: "app-bt",
171 * displayName: "app bigtable",
172 * type: "PRODUCTION",
173 * labels: { env: "prod" },
174 * clusters: {
175 * cluster: {
176 * location: "us-central1-b",
177 * serveNodes: 1,
178 * defaultStorageType: "HDD",
179 * },
180 * },
181 * });
182 * ```
183 *
184 * ### Observing Instances
185 * **Example:** Read the bound instance
186 * ```typescript
187 * const getInstance = yield* GCP.Bigtable.GetInstance(instance);
188 * const live = yield* getInstance();
189 * ```
190 *
191 * @resource
192 * @category Bigtable
193 */
194 export const Instance = Resource<Instance>("GCP.Bigtable.Instance");
195
196 export class InstanceNotResolved extends Data.TaggedError("GCP.Bigtable.InstanceNotResolved")<{
197 resourceName: string;
198 }> {}
199
200 export class InstanceNotReady extends Data.TaggedError("GCP.Bigtable.InstanceNotReady")<{
201 resourceName: string;
202 state: string;
203 }> {}
204
205 export class InstanceStillExists extends Data.TaggedError("GCP.Bigtable.InstanceStillExists")<{
206 resourceName: string;
207 }> {}
208
209 const normalizeType = (type: string | undefined) => {
210 const value = (type ?? DEFAULT_INSTANCE_TYPE).toUpperCase();
211 return value === "TYPE_UNSPECIFIED" ? DEFAULT_INSTANCE_TYPE : value;
212 };
213
214 const normalizeEdition = (edition: string | undefined) => {
215 const value = (edition ?? DEFAULT_EDITION).toUpperCase();
216 return value === "EDITION_UNSPECIFIED" ? DEFAULT_EDITION : value;
217 };
218
219 const userLabels = (
220 labels: Record<string, string | undefined> | null | undefined,
221 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
222
223 const toId = (id: string, instanceId: string | undefined, existing?: string) =>
224 toPhysicalId(id, instanceId, existing, MAX_INSTANCE_ID_LENGTH, MIN_INSTANCE_ID_LENGTH);
225
226 const defaultClusters = (): Record<string, InstanceClusterSpec> => ({
227 [DEFAULT_CLUSTER_ID]: {
228 location: DEFAULT_ZONE,
229 serveNodes: DEFAULT_SERVE_NODES,
230 defaultStorageType: DEFAULT_STORAGE,
231 },
232 });
233
234 const toClusterBody = (project: string, spec: InstanceClusterSpec): bigtable.Cluster => {
235 const location = clusterLocation(project, spec.location ?? DEFAULT_ZONE);
236 const storage = (spec.defaultStorageType ?? DEFAULT_STORAGE).toUpperCase();
237 const body: bigtable.Cluster = {
238 location,
239 defaultStorageType: storage,
240 nodeScalingFactor: spec.nodeScalingFactor,
241 encryptionConfig: spec.kmsKeyName ? { kmsKeyName: spec.kmsKeyName } : undefined,
242 };
243 if (spec.autoscalingLimits !== undefined) {
244 body.clusterConfig = {
245 clusterAutoscalingConfig: {
246 autoscalingLimits: spec.autoscalingLimits,
247 autoscalingTargets: spec.autoscalingTargets,
248 },
249 };
250 } else {
251 body.serveNodes = spec.serveNodes ?? DEFAULT_SERVE_NODES;
252 }
253 return body;
254 };
255
256 const toAttrs = (instance: bigtable.Instance, project: string) => {
257 const name = instance.name ?? "";
258 const parsed = parseResourceName(name);
259 return {
260 name,
261 instanceId: parsed.instanceId || lastSegment(name),
262 project: parsed.project || project,
263 displayName: instance.displayName,
264 type: normalizeType(instance.type),
265 edition: instance.edition,
266 labels: userLabels(instance.labels),
267 state: instance.state,
268 createTime: instance.createTime,
269 };
270 };
271
272 const waitUntilExists = (name: string) =>
273 getInstanceByName(name).pipe(
274 Effect.flatMap((instance) =>
275 instance
276 ? Effect.succeed(instance)
277 : Effect.fail(new InstanceNotResolved({ resourceName: name })),
278 ),
279 Effect.retry({
280 while: (error) => error._tag === "GCP.Bigtable.InstanceNotResolved",
281 times: 8,
282 schedule: Schedule.spaced("1 second"),
283 }),
284 );
285
286 const waitUntilReady = (name: string) =>
287 getInstanceByName(name).pipe(
288 Effect.filterOrFail(
289 (instance): instance is bigtable.Instance => instance !== undefined,
290 () => new InstanceNotResolved({ resourceName: name }),
291 ),
292 Effect.filterOrFail(
293 (instance) => (instance.state ?? "STATE_NOT_KNOWN") === "READY",
294 (instance) =>
295 new InstanceNotReady({
296 resourceName: name,
297 state: instance.state ?? "STATE_NOT_KNOWN",
298 }),
299 ),
300 Effect.retry({
301 while: (error) =>
302 error._tag === "GCP.Bigtable.InstanceNotReady" ||
303 error._tag === "GCP.Bigtable.InstanceNotResolved",
304 times: 10,
305 schedule: Schedule.spaced("8 seconds"),
306 }),
307 );
308
309 const waitUntilGone = (name: string) =>
310 getInstanceByName(name).pipe(
311 Effect.flatMap((instance) =>
312 instance === undefined
313 ? Effect.void
314 : Effect.fail(new InstanceStillExists({ resourceName: name })),
315 ),
316 Effect.retry({
317 while: (error) => error._tag === "GCP.Bigtable.InstanceStillExists",
318 times: 10,
319 schedule: Schedule.spaced("8 seconds"),
320 }),
321 );
322
323 export const InstanceProvider = () =>
324 Provider.succeed(Instance, {
325 stables: ["name", "instanceId", "project", "createTime"],
326
327 diff: Effect.fn(function* ({ news, olds, output }) {
328 if (!isResolved(news)) return undefined;
329 const previousId = olds?.instanceId ?? output?.instanceId;
330 const nextId = news.instanceId ?? previousId;
331 if (previousId !== undefined && nextId !== undefined && previousId !== nextId) {
332 return { action: "replace" as const };
333 }
334 return undefined;
335 }),
336
337 read: Effect.fn(function* ({ id, olds, output }) {
338 const env = yield* GcpEnvironment.current;
339 const instanceId = yield* toId(id, olds?.instanceId, output?.instanceId);
340 const name = output?.name ?? instanceName(env.project, instanceId);
341 const existing = yield* getInstanceByName(name);
342 if (existing === undefined) return undefined;
343 const attrs = toAttrs(existing, env.project);
344 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
345 }),
346
347 list: () =>
348 Effect.gen(function* () {
349 const env = yield* GcpEnvironment.current;
350 const instances = yield* listAlchemyInstances(env.project);
351 return instances.map((instance) => toAttrs(instance, env.project));
352 }),
353
354 reconcile: Effect.fn(function* ({ id, news, output }) {
355 const env = yield* GcpEnvironment.current;
356 const instanceId = yield* toId(id, news.instanceId, output?.instanceId);
357 const name = instanceName(env.project, instanceId);
358 const type = normalizeType(news.type ?? output?.type);
359 const edition = normalizeEdition(news.edition ?? output?.edition);
360 // Bigtable display names must be 4-30 characters.
361 const displayName = news.displayName ?? instanceId.slice(0, 30);
362 const desiredLabels = {
363 ...toLabels(news.labels),
364 ...(yield* createInternalLabels(id)),
365 };
366 const clusters = news.clusters ?? defaultClusters();
367
368 let current = yield* getInstanceByName(name);
369
370 if (current === undefined) {
371 const clusterMap: bigtable.ClusterMap = {};
372 for (const [clusterId, spec] of Object.entries(clusters)) {
373 clusterMap[clusterId] = toClusterBody(env.project, spec);
374 }
375 const created = yield* bigtable
376 .createProjectsInstances({
377 parent: `projects/${env.project}`,
378 body: {
379 instanceId,
380 instance: {
381 displayName,
382 type,
383 edition,
384 labels: desiredLabels,
385 },
386 clusters: clusterMap,
387 },
388 })
389 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
390 if (created !== undefined) {
391 yield* waitForOperation(created, { alreadyExistsOk: true });
392 }
393 current = yield* waitUntilExists(name);
394 }
395
396 if (current === undefined) {
397 return yield* new InstanceNotResolved({ resourceName: name });
398 }
399
400 if ((current.state ?? "STATE_NOT_KNOWN") !== "READY") {
401 current = yield* waitUntilReady(name);
402 }
403
404 const observedLabels = tagRecord(current.labels);
405 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
406 const labelsChanged = upsert.length > 0 || removed.length > 0;
407 const displayChanged = (current.displayName ?? "") !== displayName;
408 const typeChanged = normalizeType(current.type) !== type;
409 const editionChanged =
410 normalizeEdition(current.edition) !== edition && news.edition !== undefined;
411
412 if (labelsChanged || displayChanged || typeChanged || editionChanged) {
413 const mask = [
414 labelsChanged ? "labels" : undefined,
415 displayChanged ? "display_name" : undefined,
416 typeChanged ? "type" : undefined,
417 editionChanged ? "edition" : undefined,
418 ].filter((field): field is string => field !== undefined);
419 const patched = yield* bigtable.partialUpdateInstanceProjectsInstances({
420 name,
421 updateMask: mask.join(","),
422 body: {
423 displayName,
424 type,
425 edition,
426 labels: desiredLabels,
427 },
428 });
429 yield* waitForOperation(patched);
430 current = yield* waitUntilExists(name);
431 }
432
433 return toAttrs(current, env.project);
434 }),
435
436 delete: Effect.fn(function* ({ output }) {
437 yield* bigtable
438 .deleteProjectsInstances({ name: output.name })
439 .pipe(Effect.catchTag("NotFound", () => Effect.void));
440 yield* waitUntilGone(output.name);
441 }),
442 });