@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 1,010 lines 35.2 KB
Raw
1 import {
2 diffMigrations,
3 migrationAttributesOf,
4 MigrationError,
5 migrationsAttrs,
6 migrationsInputOf,
7 stampedOf,
8 type MigrationAttributes,
9 type MigrationsInput,
10 } from "@alchemy.run/sql/Migrations";
11 import * as alloydb from "@distilled.cloud/gcp/alloydb_v1";
12 import { Unowned } from "alchemy/AdoptPolicy";
13 import { isResolved } from "alchemy/Diff";
14 import { createPhysicalName } from "alchemy/PhysicalName";
15 import * as Provider from "alchemy/Provider";
16 import { Resource } from "alchemy/Resource";
17 import { tagRecord } from "alchemy/Tags";
18 import * as Data from "effect/Data";
19 import * as Effect from "effect/Effect";
20 import type * as Redacted from "effect/Redacted";
21 import * as Schedule from "effect/Schedule";
22 import * as Stream from "effect/Stream";
23 import { GcpEnvironment } from "../Environment.ts";
24 import {
25 createInternalLabels,
26 diffLabels,
27 hasAlchemyLabels,
28 stripInternalLabels,
29 toLabels,
30 } from "../Labels.ts";
31 import type { Providers } from "../Providers.ts";
32 import { runInstanceMigrations } from "./Migrations.ts";
33 import { waitForDeleteOperation, waitForOperation } from "./operations.ts";
34
35 const DEFAULT_INSTANCE_TYPE = "PRIMARY";
36 const DEFAULT_CPU_COUNT = 2;
37 const DEFAULT_READ_POOL_NODES = 1;
38 const MAX_NAME_LENGTH = 63;
39
40 export type MachineConfig = {
41 /**
42 * vCPU count. Must match `machineType` when both are set. Minimum 2
43 * for PRIMARY instances.
44 * @default 2
45 */
46 cpuCount?: number;
47 /**
48 * Machine type (e.g. `n2-highmem-4`, `n2-highmem-8`).
49 */
50 machineType?: string;
51 };
52
53 export type ReadPoolConfig = {
54 /**
55 * Node count in the read pool. Required when `instanceType` is
56 * `READ_POOL`.
57 * @default 1
58 */
59 nodeCount?: number;
60 };
61
62 export type QueryInsightsConfig = alloydb.QueryInsightsInstanceConfig;
63 export type ObservabilityConfig = alloydb.ObservabilityInstanceConfig;
64 export type ClientConnectionConfig = alloydb.ClientConnectionConfig;
65 export type InstanceNetworkConfig = alloydb.InstanceNetworkConfig;
66 export type PscInstanceConfig = alloydb.PscInstanceConfig;
67 export type ConnectionPoolConfig = alloydb.ConnectionPoolConfig;
68
69 export type InstanceProps = {
70 /**
71 * Parent cluster id or full resource name
72 * (`projects/{project}/locations/{location}/clusters/{cluster}`).
73 * Immutable — changing it replaces the instance.
74 */
75 cluster: string;
76 /**
77 * Region of the parent cluster (`us-central1`, …). Ignored when
78 * `cluster` is a full resource name. Immutable — changing it replaces
79 * the instance. `US-CENTRAL1` is accepted and normalized to
80 * `us-central1`.
81 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
82 */
83 location?: string;
84 /**
85 * Instance id (the `{instance}` segment of
86 * `.../clusters/{cluster}/instances/{instance}`). If omitted, a unique
87 * RFC1035 name is generated. Must match
88 * `[a-z]([a-z0-9-]{0,61}[a-z0-9])?`. Immutable — changing it replaces
89 * the instance.
90 */
91 instanceId?: string;
92 /**
93 * Instance type. `SECONDARY` uses `instances.createsecondary`.
94 * Immutable — changing it replaces the instance.
95 * @default "PRIMARY"
96 */
97 instanceType?: alloydb.InstanceInstanceTypeEnum | (string & {});
98 /**
99 * User-facing display name.
100 */
101 displayName?: string;
102 /**
103 * User labels. Alchemy ownership labels are merged in automatically.
104 */
105 labels?: Record<string, string>;
106 /**
107 * Arbitrary client annotations (distinct from labels).
108 */
109 annotations?: Record<string, string>;
110 /**
111 * VM shape for the database engine. Defaults to 2 vCPU.
112 */
113 machineConfig?: MachineConfig;
114 /**
115 * Availability type. PRIMARY defaults to `REGIONAL`. `gceZone` is only
116 * valid with `ZONAL`.
117 */
118 availabilityType?: alloydb.InstanceAvailabilityTypeEnum | (string & {});
119 /**
120 * Compute Engine zone for `ZONAL` instances (e.g. `us-central1-a`).
121 */
122 gceZone?: string;
123 /**
124 * Whether the instance should run. `NEVER` stops it; `ALWAYS` starts
125 * it.
126 */
127 activationPolicy?: alloydb.InstanceActivationPolicyEnum | (string & {});
128 /**
129 * Database flags (`"key": "value"`). Booleans use `on` / `off`.
130 */
131 databaseFlags?: Record<string, string>;
132 /**
133 * Read-pool size. Required when `instanceType` is `READ_POOL`.
134 */
135 readPoolConfig?: ReadPoolConfig;
136 /**
137 * Query Insights configuration.
138 */
139 queryInsightsConfig?: QueryInsightsConfig;
140 /**
141 * Observability configuration.
142 */
143 observabilityConfig?: ObservabilityConfig;
144 /**
145 * Client connection and SSL settings.
146 */
147 clientConnectionConfig?: ClientConnectionConfig;
148 /**
149 * Instance-level network configuration (public IP, authorized CIDRs).
150 */
151 networkConfig?: InstanceNetworkConfig;
152 /**
153 * Private Service Connect configuration for this instance.
154 */
155 pscInstanceConfig?: PscInstanceConfig;
156 /**
157 * Managed Connection Pool (MCP) configuration.
158 */
159 connectionPoolConfig?: ConnectionPoolConfig;
160 /**
161 * Controls the Data API (`executeSql`). `migrations` run through it, so
162 * leave it enabled on an instance that applies them.
163 */
164 dataApiAccess?: alloydb.InstanceDataApiAccessEnum | (string & {});
165 /**
166 * SQL migrations to apply on deploy, once this instance is ready.
167 * PRIMARY instances only. Accepts a directory path, a `{ dir, table }`
168 * object, or a `Drizzle.Schema` output. Only pending migrations run;
169 * editing or removing an applied file fails the deploy.
170 *
171 * Migrations run through the AlloyDB Data API (`executeSql`), so the
172 * deploying machine needs no route to the instance. Bookkeeping lives in
173 * Alchemy's `__alchemy_migrations` table; each migration commits with
174 * its bookkeeping row.
175 */
176 migrations?: MigrationsInput;
177 /**
178 * Database that `migrations` target.
179 * @default "postgres"
180 */
181 migrationsDatabase?: string;
182 /**
183 * Built-in database user that `migrations` log in as — e.g. the
184 * cluster's `initialUser`. It owns the tables the migrations create.
185 */
186 migrationsUser?: string;
187 /**
188 * Password of `migrationsUser`.
189 */
190 migrationsPassword?: string | Redacted.Redacted<string>;
191 };
192
193 export type Instance = Resource<
194 "GCP.AlloyDB.Instance",
195 InstanceProps,
196 {
197 /** Full resource name `projects/{project}/locations/{location}/clusters/{cluster}/instances/{instance}`. */
198 name: string;
199 /** Instance id (last path segment). */
200 instanceId: string;
201 /** Parent cluster id. */
202 clusterId: string;
203 /** Parent cluster resource name. */
204 clusterName: string;
205 /** Project id. */
206 project: string;
207 /** Region id (`us-central1`, …). */
208 location: string;
209 /** Instance type (`PRIMARY`, `READ_POOL`, `SECONDARY`). */
210 instanceType: string;
211 /** User-facing display name. */
212 displayName: string | undefined;
213 /** User labels (Alchemy ownership labels stripped). */
214 labels: Record<string, string>;
215 /** Client annotations. */
216 annotations: Record<string, string>;
217 /** Serving state (`READY`, `CREATING`, `STOPPED`, …). */
218 state: string | undefined;
219 /** Private IP address. */
220 ipAddress: string | undefined;
221 /** Public IP address, if enabled. */
222 publicIpAddress: string | undefined;
223 /** Machine configuration currently applied. */
224 machineConfig: MachineConfig | undefined;
225 /** Availability type (`ZONAL`, `REGIONAL`). */
226 availabilityType: string | undefined;
227 /** Zone for ZONAL instances. */
228 gceZone: string | undefined;
229 /** Activation policy (`ALWAYS`, `NEVER`). */
230 activationPolicy: string | undefined;
231 /** Database flags currently applied. */
232 databaseFlags: Record<string, string>;
233 /** Read-pool configuration, if any. */
234 readPoolConfig: ReadPoolConfig | undefined;
235 /** Whether the service is reconciling intended vs actual state. */
236 reconciling: boolean;
237 /** System-generated UID. */
238 uid: string | undefined;
239 /** RFC3339 creation timestamp. */
240 createTime: string | undefined;
241 /** RFC3339 last-update timestamp. */
242 updateTime: string | undefined;
243 /** Directory of the applied SQL migrations. */
244 migrationsDir: string | undefined;
245 /** Migration bookkeeping table. */
246 migrationsTable: string | undefined;
247 /** Applied migration content hashes. */
248 migrationsHashes: Record<string, string>;
249 },
250 never,
251 Providers
252 >;
253
254 /**
255 * An AlloyDB instance (compute attached to a cluster).
256 *
257 * Changing `cluster`, `location`, `instanceId`, or `instanceType`
258 * replaces the instance. Machine size, flags, labels, activation policy,
259 * and read-pool size update in place.
260 *
261 * A cluster may exist without instances (`EMPTY`). Creating a PRIMARY
262 * instance typically takes several minutes.
263 *
264 * ### Creating an Instance
265 * **Example:** Primary instance on a cluster
266 * ```typescript
267 * const cluster = yield* GCP.AlloyDB.Cluster("AppDb", {
268 * pscConfig: { pscEnabled: true },
269 * });
270 * const primary = yield* GCP.AlloyDB.Instance("Primary", {
271 * cluster: cluster.name,
272 * instanceType: "PRIMARY",
273 * machineConfig: { cpuCount: 2 },
274 * });
275 * ```
276 *
277 * **Example:** Explicit id, display name, and labels
278 * ```typescript
279 * const primary = yield* GCP.AlloyDB.Instance("Primary", {
280 * cluster: cluster.name,
281 * instanceId: "app-db-primary",
282 * displayName: "app-db-primary",
283 * labels: { env: "prod" },
284 * machineConfig: { cpuCount: 2 },
285 * });
286 * ```
287 *
288 * ### Read Pool
289 * **Example:** Two-node read pool
290 * ```typescript
291 * const pool = yield* GCP.AlloyDB.Instance("Reads", {
292 * cluster: cluster.name,
293 * instanceType: "READ_POOL",
294 * readPoolConfig: { nodeCount: 2 },
295 * machineConfig: { cpuCount: 2 },
296 * });
297 * ```
298 *
299 * ### Migrations
300 * **Example:** Apply migrations on the primary
301 * ```typescript
302 * const cluster = yield* GCP.AlloyDB.Cluster("AppDb", {
303 * pscConfig: { pscEnabled: true },
304 * initialUser: { user: "postgres", password: "change-me" },
305 * });
306 * const primary = yield* GCP.AlloyDB.Instance("Primary", {
307 * cluster: cluster.name,
308 * instanceType: "PRIMARY",
309 * dataApiAccess: "ENABLED",
310 * migrations: "./migrations",
311 * migrationsUser: "postgres",
312 * migrationsPassword: "change-me",
313 * });
314 * ```
315 *
316 * Migrations run through the AlloyDB Data API, so the deploying machine
317 * needs no route to the cluster's network. The deploying identity needs
318 * `alloydb.instances.executeSql`. Set `migrationsDatabase` to target a
319 * database other than `postgres`.
320 *
321 * @resource
322 * @category AlloyDB
323 */
324 export const Instance = Resource<Instance>("GCP.AlloyDB.Instance");
325
326 export class InstanceNotResolved extends Data.TaggedError("GCP.AlloyDB.InstanceNotResolved")<{
327 resourceName: string;
328 }> {}
329
330 export class InstanceClusterMissing extends Data.TaggedError("GCP.AlloyDB.InstanceClusterMissing")<{
331 message: string;
332 }> {}
333
334 export class InstanceNotReady extends Data.TaggedError("GCP.AlloyDB.InstanceNotReady")<{
335 resourceName: string;
336 state: string;
337 }> {}
338
339 export class InstanceFailed extends Data.TaggedError("GCP.AlloyDB.InstanceFailed")<{
340 resourceName: string;
341 state: string;
342 }> {}
343
344 export class InstanceStillExists extends Data.TaggedError("GCP.AlloyDB.InstanceStillExists")<{
345 resourceName: string;
346 }> {}
347
348 const lastSegment = (value: string | undefined) => {
349 if (value === undefined || value.length === 0) return "";
350 const trimmed = value.replace(/\/+$/, "");
351 const parts = trimmed.split("/");
352 return parts[parts.length - 1] || trimmed;
353 };
354
355 const normalizeLocation = (location: string) => lastSegment(location).toLowerCase();
356
357 const normalizeInstanceType = (type: string | undefined) => {
358 const value = (type ?? DEFAULT_INSTANCE_TYPE).toUpperCase();
359 return value === "INSTANCE_TYPE_UNSPECIFIED" ? DEFAULT_INSTANCE_TYPE : value;
360 };
361
362 const rfc1035 = (name: string): string => {
363 let next = name
364 .toLowerCase()
365 .replace(/[^a-z0-9-]/g, "-")
366 .replace(/-+/g, "-")
367 .replace(/^-+|-+$/g, "");
368 if (!/^[a-z]/.test(next)) next = `i${next}`;
369 next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/g, "");
370 if (next.length === 0) return "instance";
371 if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, MAX_NAME_LENGTH - 1)}0`;
372 return next.slice(0, MAX_NAME_LENGTH);
373 };
374
375 const resourceName = (project: string, location: string, clusterId: string, instanceId: string) =>
376 `projects/${project}/locations/${location}/clusters/${clusterId}/instances/${instanceId}`;
377
378 const clusterNameOf = (project: string, location: string, clusterId: string) =>
379 `projects/${project}/locations/${location}/clusters/${clusterId}`;
380
381 const parseName = (name: string) => {
382 const parts = name.split("/").filter((part) => part.length > 0);
383 const instancesAt = parts.lastIndexOf("instances");
384 const clustersAt = parts.lastIndexOf("clusters");
385 const locationsAt = parts.lastIndexOf("locations");
386 const projectsAt = parts.lastIndexOf("projects");
387 return {
388 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
389 location: locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : "",
390 clusterId: clustersAt >= 0 && parts[clustersAt + 1] ? parts[clustersAt + 1]! : "",
391 instanceId:
392 instancesAt >= 0 && parts[instancesAt + 1] ? parts[instancesAt + 1]! : lastSegment(name),
393 };
394 };
395
396 const parseClusterRef = (cluster: string, fallbackProject: string, fallbackLocation: string) => {
397 const trimmed = cluster.trim();
398 if (trimmed.length === 0) {
399 return {
400 project: fallbackProject,
401 location: normalizeLocation(fallbackLocation),
402 clusterId: "",
403 };
404 }
405 if (trimmed.includes("/clusters/") || trimmed.includes("projects/")) {
406 const parsed = parseName(trimmed.includes("/instances/") ? trimmed : `${trimmed}/instances/_`);
407 return {
408 project: parsed.project || fallbackProject,
409 location: normalizeLocation(parsed.location || fallbackLocation),
410 clusterId: parsed.clusterId === "_" ? "" : parsed.clusterId,
411 };
412 }
413 return {
414 project: fallbackProject,
415 location: normalizeLocation(fallbackLocation),
416 clusterId: lastSegment(trimmed),
417 };
418 };
419
420 const userLabels = (
421 labels: Record<string, string | undefined> | null | undefined,
422 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
423
424 const stringMapOf = (
425 map: Record<string, string | undefined> | null | undefined,
426 ): Record<string, string> =>
427 Object.fromEntries(
428 Object.entries(map ?? {}).filter(
429 (entry): entry is [string, string] => entry[1] !== undefined && entry[1].length > 0,
430 ),
431 );
432
433 const toId = (id: string, instanceId: string | undefined, existing?: string) =>
434 Effect.gen(function* () {
435 return (
436 instanceId ??
437 existing ??
438 rfc1035(
439 yield* createPhysicalName({
440 id,
441 maxLength: MAX_NAME_LENGTH,
442 lowercase: true,
443 }),
444 )
445 );
446 });
447
448 const canonical = (value: unknown): unknown => {
449 if (value === undefined || value === null) return undefined;
450 if (typeof value === "boolean" || typeof value === "number") return value;
451 if (typeof value === "string") {
452 return value.length === 0 ? undefined : value;
453 }
454 if (Array.isArray(value)) {
455 const items = value.map(canonical).filter((item) => item !== undefined);
456 return items.length === 0 ? undefined : items;
457 }
458 if (typeof value === "object") {
459 const entries = Object.entries(value as Record<string, unknown>)
460 .map(([key, item]) => [key, canonical(item)] as const)
461 .filter(([, item]) => item !== undefined)
462 .sort(([left], [right]) => left.localeCompare(right));
463 if (entries.length === 0) return undefined;
464 return Object.fromEntries(entries);
465 }
466 return undefined;
467 };
468
469 const fingerprint = (value: unknown): string => JSON.stringify(canonical(value) ?? null);
470
471 const specifiedChanged = (
472 desired: Record<string, unknown> | undefined,
473 observed: Record<string, unknown> | undefined,
474 ) => {
475 if (desired === undefined) return false;
476 const picked: Record<string, unknown> = {};
477 const current: Record<string, unknown> = {};
478 for (const [key, value] of Object.entries(desired)) {
479 if (value === undefined) continue;
480 picked[key] = value;
481 current[key] = observed?.[key];
482 }
483 return fingerprint(picked) !== fingerprint(current);
484 };
485
486 const toMachineConfig = (
487 config: alloydb.MachineConfig | MachineConfig | undefined,
488 ): MachineConfig | undefined => {
489 if (config === undefined) return undefined;
490 if (config.cpuCount === undefined && config.machineType === undefined) {
491 return undefined;
492 }
493 return {
494 cpuCount: config.cpuCount,
495 machineType: config.machineType,
496 };
497 };
498
499 const toReadPoolConfig = (
500 config: alloydb.ReadPoolConfig | ReadPoolConfig | undefined,
501 ): ReadPoolConfig | undefined => {
502 if (config === undefined || config.nodeCount === undefined) return undefined;
503 return { nodeCount: config.nodeCount };
504 };
505
506 const desiredMachineConfig = (news: InstanceProps): MachineConfig => ({
507 cpuCount: news.machineConfig?.cpuCount ?? DEFAULT_CPU_COUNT,
508 machineType: news.machineConfig?.machineType,
509 });
510
511 const desiredReadPoolConfig = (
512 news: InstanceProps,
513 instanceType: string,
514 ): ReadPoolConfig | undefined => {
515 if (instanceType !== "READ_POOL") return news.readPoolConfig;
516 return {
517 nodeCount: news.readPoolConfig?.nodeCount ?? DEFAULT_READ_POOL_NODES,
518 };
519 };
520
521 const isAvailable = (state: string | undefined) => {
522 const value = (state ?? "").toUpperCase();
523 return value === "READY" || value === "STOPPED";
524 };
525
526 const isFailed = (state: string | undefined) => (state ?? "").toUpperCase() === "FAILED";
527
528 const toAttrs = (
529 instance: alloydb.Instance,
530 project: string,
531 migrations?: Partial<MigrationAttributes>,
532 ): Instance["Attributes"] => {
533 const name = instance.name ?? "";
534 const parsed = parseName(name);
535 const clusterId = parsed.clusterId;
536 const location = parsed.location;
537 const resolvedProject = parsed.project || project;
538 return {
539 name,
540 instanceId: parsed.instanceId,
541 clusterId,
542 clusterName: clusterNameOf(resolvedProject, location, clusterId),
543 project: resolvedProject,
544 location,
545 instanceType: normalizeInstanceType(instance.instanceType),
546 displayName: instance.displayName,
547 labels: userLabels(instance.labels),
548 annotations: stringMapOf(instance.annotations),
549 state: instance.state,
550 ipAddress: instance.ipAddress,
551 publicIpAddress: instance.publicIpAddress,
552 machineConfig: toMachineConfig(instance.machineConfig),
553 availabilityType: instance.availabilityType,
554 gceZone: instance.gceZone,
555 activationPolicy: instance.activationPolicy,
556 databaseFlags: stringMapOf(instance.databaseFlags),
557 readPoolConfig: toReadPoolConfig(instance.readPoolConfig),
558 reconciling: instance.reconciling === true,
559 uid: instance.uid,
560 createTime: instance.createTime,
561 updateTime: instance.updateTime,
562 ...migrationAttributesOf(migrations),
563 };
564 };
565
566 const isPlaceholder = (instance: alloydb.Instance) => {
567 const name = instance.name ?? "";
568 return name.endsWith("/instances/-") || name.endsWith("/instances/");
569 };
570
571 const getByName = (name: string) =>
572 alloydb
573 .getProjectsLocationsClustersInstances({ name })
574 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
575
576 const waitUntilExists = (name: string) =>
577 getByName(name).pipe(
578 Effect.flatMap((instance) =>
579 instance
580 ? Effect.succeed(instance)
581 : Effect.fail(new InstanceNotResolved({ resourceName: name })),
582 ),
583 Effect.retry({
584 while: (error) => error._tag === "GCP.AlloyDB.InstanceNotResolved",
585 times: 8,
586 schedule: Schedule.spaced("1 second"),
587 }),
588 );
589
590 const waitUntilReady = (name: string) =>
591 Effect.gen(function* () {
592 const instance = yield* getByName(name);
593 if (instance === undefined) {
594 return yield* new InstanceNotReady({ resourceName: name, state: "MISSING" });
595 }
596 if (isFailed(instance.state)) {
597 return yield* new InstanceFailed({
598 resourceName: name,
599 state: instance.state ?? "FAILED",
600 });
601 }
602 if (!(isAvailable(instance.state) && instance.reconciling !== true)) {
603 return yield* new InstanceNotReady({
604 resourceName: name,
605 state: instance.state ?? "STATE_UNSPECIFIED",
606 });
607 }
608 return instance;
609 }).pipe(
610 Effect.retry({
611 while: (error) => error._tag === "GCP.AlloyDB.InstanceNotReady",
612 times: 120,
613 schedule: Schedule.spaced("15 seconds"),
614 }),
615 );
616
617 const waitUntilGone = (name: string) =>
618 getByName(name).pipe(
619 Effect.flatMap((instance) =>
620 instance === undefined
621 ? Effect.void
622 : Effect.fail(new InstanceStillExists({ resourceName: name })),
623 ),
624 Effect.retry({
625 while: (error) => error._tag === "GCP.AlloyDB.InstanceStillExists",
626 times: 120,
627 schedule: Schedule.spaced("15 seconds"),
628 }),
629 );
630
631 const toCreateBody = (
632 news: InstanceProps,
633 desiredLabels: Record<string, string>,
634 instanceType: string,
635 machineConfig: MachineConfig,
636 readPoolConfig: ReadPoolConfig | undefined,
637 ): alloydb.Instance => {
638 const body: alloydb.Instance = {
639 instanceType,
640 displayName: news.displayName,
641 labels: desiredLabels,
642 machineConfig,
643 };
644 if (news.annotations !== undefined) {
645 body.annotations = news.annotations;
646 }
647 if (news.availabilityType !== undefined) {
648 body.availabilityType = news.availabilityType;
649 }
650 if (news.gceZone !== undefined) {
651 body.gceZone = news.gceZone;
652 }
653 if (news.activationPolicy !== undefined) {
654 body.activationPolicy = news.activationPolicy;
655 }
656 if (news.databaseFlags !== undefined) {
657 body.databaseFlags = news.databaseFlags;
658 }
659 if (readPoolConfig !== undefined) {
660 body.readPoolConfig = readPoolConfig;
661 }
662 if (news.queryInsightsConfig !== undefined) {
663 body.queryInsightsConfig = news.queryInsightsConfig;
664 }
665 if (news.observabilityConfig !== undefined) {
666 body.observabilityConfig = news.observabilityConfig;
667 }
668 if (news.clientConnectionConfig !== undefined) {
669 body.clientConnectionConfig = news.clientConnectionConfig;
670 }
671 if (news.networkConfig !== undefined) {
672 body.networkConfig = news.networkConfig;
673 }
674 if (news.pscInstanceConfig !== undefined) {
675 body.pscInstanceConfig = news.pscInstanceConfig;
676 }
677 if (news.connectionPoolConfig !== undefined) {
678 body.connectionPoolConfig = news.connectionPoolConfig;
679 }
680 if (news.dataApiAccess !== undefined) {
681 body.dataApiAccess = news.dataApiAccess;
682 }
683 return body;
684 };
685
686 export const InstanceProvider = () =>
687 Provider.succeed(Instance, {
688 stables: [
689 "name",
690 "instanceId",
691 "clusterId",
692 "clusterName",
693 "project",
694 "location",
695 "uid",
696 "createTime",
697 ],
698
699 diff: Effect.fn(function* ({ news, olds, output }) {
700 if (!isResolved(news)) return undefined;
701 const env = yield* GcpEnvironment.current;
702
703 const previousId = olds?.instanceId ?? output?.instanceId;
704 const nextId = news.instanceId ?? previousId;
705 const previousCluster = lastSegment(olds?.cluster ?? output?.clusterId);
706 const nextCluster = lastSegment(news.cluster ?? previousCluster);
707 const previousLocation = normalizeLocation(olds?.location ?? output?.location ?? env.region);
708 const nextLocation = normalizeLocation(news.location ?? output?.location ?? env.region);
709 const previousType = normalizeInstanceType(olds?.instanceType ?? output?.instanceType);
710 const nextType = normalizeInstanceType(news.instanceType ?? output?.instanceType);
711
712 const replace =
713 (previousId !== undefined && nextId !== undefined && nextId !== previousId) ||
714 (previousCluster.length > 0 && nextCluster.length > 0 && previousCluster !== nextCluster) ||
715 previousLocation !== nextLocation ||
716 previousType !== nextType;
717
718 if (!replace) {
719 if (yield* diffMigrations({ news, output })) {
720 return { action: "update" as const };
721 }
722 return undefined;
723 }
724 return {
725 action: "replace" as const,
726 deleteFirst:
727 previousLocation === nextLocation &&
728 previousCluster === nextCluster &&
729 previousId !== undefined &&
730 nextId === previousId,
731 };
732 }),
733
734 read: Effect.fn(function* ({ id, olds, output }) {
735 const env = yield* GcpEnvironment.current;
736 if (output?.name) {
737 const existing = yield* getByName(output.name);
738 if (existing === undefined) return undefined;
739 const attrs = toAttrs(existing, env.project, output);
740 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
741 }
742 const instanceId = yield* toId(id, olds?.instanceId, output?.instanceId);
743 const ref = parseClusterRef(
744 olds?.cluster ?? output?.clusterName ?? output?.clusterId ?? "",
745 env.project,
746 olds?.location ?? output?.location ?? env.region,
747 );
748 if (ref.clusterId.length === 0) return undefined;
749 const name = resourceName(ref.project, ref.location, ref.clusterId, instanceId);
750 const existing = yield* getByName(name);
751 if (existing === undefined) return undefined;
752 const attrs = toAttrs(existing, env.project, output);
753 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
754 }),
755
756 list: () =>
757 Effect.gen(function* () {
758 const env = yield* GcpEnvironment.current;
759 return yield* alloydb.listProjectsLocationsClustersInstances
760 .pages({
761 parent: `projects/${env.project}/locations/-/clusters/-`,
762 pageSize: 1000,
763 })
764 .pipe(
765 Stream.flatMap((page) => Stream.fromIterable(page.instances ?? [])),
766 Stream.filter(
767 (instance) =>
768 !isPlaceholder(instance) &&
769 Object.keys(instance.labels ?? {}).some((key) => key.startsWith("alchemy-")),
770 ),
771 Stream.map((instance) => toAttrs(instance, env.project)),
772 Stream.runCollect,
773 Effect.map((chunk) => Array.from(chunk)),
774 Effect.catchTag("NotFound", () => Effect.succeed([])),
775 );
776 }),
777
778 reconcile: Effect.fn(function* ({ id, news, olds, output }) {
779 const env = yield* GcpEnvironment.current;
780 const instanceId = yield* toId(id, news.instanceId, output?.instanceId);
781 const ref = parseClusterRef(
782 news.cluster,
783 env.project,
784 news.location ?? output?.location ?? env.region,
785 );
786 if (ref.clusterId.length === 0) {
787 return yield* new InstanceClusterMissing({
788 message: "GCP.AlloyDB.Instance requires `cluster` (cluster id or full resource name)",
789 });
790 }
791 const name = resourceName(ref.project, ref.location, ref.clusterId, instanceId);
792 const parent = clusterNameOf(ref.project, ref.location, ref.clusterId);
793 const instanceType = normalizeInstanceType(news.instanceType);
794 const machineConfig = desiredMachineConfig(news);
795 const readPoolConfig = desiredReadPoolConfig(news, instanceType);
796 const desiredLabels = {
797 ...toLabels(news.labels),
798 ...(yield* createInternalLabels(id)),
799 };
800
801 let current = yield* getByName(output?.name ?? name);
802
803 if (current === undefined) {
804 const body = toCreateBody(news, desiredLabels, instanceType, machineConfig, readPoolConfig);
805 yield* (
806 instanceType === "SECONDARY"
807 ? alloydb.createsecondaryProjectsLocationsClustersInstances({
808 parent,
809 instanceId,
810 body,
811 })
812 : alloydb.createProjectsLocationsClustersInstances({
813 parent,
814 instanceId,
815 body,
816 })
817 ).pipe(
818 Effect.catchTag("Conflict", () => Effect.undefined),
819 Effect.tap((created) =>
820 created === undefined ? Effect.void : waitForOperation(created),
821 ),
822 // AlloyDB intermittently fails the create operation with INTERNAL
823 // ("an internal error has occurred") after a long provisioning wait;
824 // the failed instance is removed, so the create is retried.
825 Effect.retry({
826 while: (error) => error._tag === "GCP.OperationFailed" && error.code === 13,
827 times: 2,
828 schedule: Schedule.spaced("30 seconds"),
829 }),
830 );
831 current = yield* waitUntilExists(name);
832 }
833
834 if (current === undefined) {
835 return yield* new InstanceNotResolved({ resourceName: name });
836 }
837
838 if (!isAvailable(current.state) || current.reconciling === true) {
839 current = yield* waitUntilReady(name);
840 }
841
842 const observedLabels = tagRecord(current.labels);
843 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
844 const labelsChanged = upsert.length > 0 || removed.length > 0;
845 // AlloyDB omits `displayName` from instance reads (as for clusters and
846 // backups); compare against the last applied value instead.
847 const observedDisplayName = current.displayName ?? output?.displayName ?? olds?.displayName;
848 const displayNameChanged = (observedDisplayName ?? "") !== (news.displayName ?? "");
849 const annotationsChanged =
850 news.annotations !== undefined &&
851 fingerprint(stringMapOf(current.annotations)) !== fingerprint(news.annotations);
852 const machineChanged = specifiedChanged(
853 news.machineConfig as Record<string, unknown> | undefined,
854 current.machineConfig as Record<string, unknown> | undefined,
855 );
856 const availabilityChanged =
857 news.availabilityType !== undefined &&
858 (current.availabilityType ?? "") !== news.availabilityType;
859 const zoneChanged = news.gceZone !== undefined && (current.gceZone ?? "") !== news.gceZone;
860 const activationChanged =
861 news.activationPolicy !== undefined &&
862 (current.activationPolicy ?? "") !== news.activationPolicy;
863 const flagsChanged =
864 news.databaseFlags !== undefined &&
865 fingerprint(stringMapOf(current.databaseFlags)) !== fingerprint(news.databaseFlags);
866 const readPoolChanged =
867 news.readPoolConfig !== undefined &&
868 (current.readPoolConfig?.nodeCount ?? 0) !==
869 (news.readPoolConfig.nodeCount ?? current.readPoolConfig?.nodeCount ?? 0);
870 const insightsChanged = specifiedChanged(
871 news.queryInsightsConfig as Record<string, unknown> | undefined,
872 current.queryInsightsConfig as Record<string, unknown> | undefined,
873 );
874 const observabilityChanged = specifiedChanged(
875 news.observabilityConfig as Record<string, unknown> | undefined,
876 current.observabilityConfig as Record<string, unknown> | undefined,
877 );
878 const clientChanged = specifiedChanged(
879 news.clientConnectionConfig as Record<string, unknown> | undefined,
880 current.clientConnectionConfig as Record<string, unknown> | undefined,
881 );
882 const networkChanged = specifiedChanged(
883 news.networkConfig as Record<string, unknown> | undefined,
884 current.networkConfig as Record<string, unknown> | undefined,
885 );
886 const pscChanged = specifiedChanged(
887 news.pscInstanceConfig as Record<string, unknown> | undefined,
888 current.pscInstanceConfig as Record<string, unknown> | undefined,
889 );
890 const poolChanged = specifiedChanged(
891 news.connectionPoolConfig as Record<string, unknown> | undefined,
892 current.connectionPoolConfig as Record<string, unknown> | undefined,
893 );
894 const dataApiChanged =
895 news.dataApiAccess !== undefined && (current.dataApiAccess ?? "") !== news.dataApiAccess;
896
897 if (
898 labelsChanged ||
899 displayNameChanged ||
900 annotationsChanged ||
901 machineChanged ||
902 availabilityChanged ||
903 zoneChanged ||
904 activationChanged ||
905 flagsChanged ||
906 readPoolChanged ||
907 insightsChanged ||
908 observabilityChanged ||
909 clientChanged ||
910 networkChanged ||
911 pscChanged ||
912 poolChanged ||
913 dataApiChanged
914 ) {
915 const updateMask = [
916 labelsChanged ? "labels" : undefined,
917 displayNameChanged ? "displayName" : undefined,
918 annotationsChanged ? "annotations" : undefined,
919 machineChanged ? "machineConfig" : undefined,
920 availabilityChanged ? "availabilityType" : undefined,
921 zoneChanged ? "gceZone" : undefined,
922 activationChanged ? "activationPolicy" : undefined,
923 flagsChanged ? "databaseFlags" : undefined,
924 readPoolChanged ? "readPoolConfig" : undefined,
925 insightsChanged ? "queryInsightsConfig" : undefined,
926 observabilityChanged ? "observabilityConfig" : undefined,
927 clientChanged ? "clientConnectionConfig" : undefined,
928 networkChanged ? "networkConfig" : undefined,
929 pscChanged ? "pscInstanceConfig" : undefined,
930 poolChanged ? "connectionPoolConfig" : undefined,
931 dataApiChanged ? "dataApiAccess" : undefined,
932 ].filter((field): field is string => field !== undefined);
933
934 const patched = yield* alloydb
935 .patchProjectsLocationsClustersInstances({
936 name,
937 updateMask: updateMask.join(","),
938 body: {
939 name,
940 labels: desiredLabels,
941 displayName: news.displayName,
942 annotations: news.annotations,
943 machineConfig,
944 availabilityType: news.availabilityType,
945 gceZone: news.gceZone,
946 activationPolicy: news.activationPolicy,
947 databaseFlags: news.databaseFlags,
948 readPoolConfig,
949 queryInsightsConfig: news.queryInsightsConfig,
950 observabilityConfig: news.observabilityConfig,
951 clientConnectionConfig: news.clientConnectionConfig,
952 networkConfig: news.networkConfig,
953 pscInstanceConfig: news.pscInstanceConfig,
954 connectionPoolConfig: news.connectionPoolConfig,
955 dataApiAccess: news.dataApiAccess,
956 },
957 })
958 .pipe(
959 Effect.retry({
960 while: (error) => error._tag === "Conflict",
961 times: 40,
962 schedule: Schedule.spaced("15 seconds"),
963 }),
964 );
965 yield* waitForOperation(patched);
966 current = yield* waitUntilReady(name);
967 }
968
969 // Sync migrations — the shared pipeline skips already-applied ones.
970 const migrationsInput = migrationsInputOf(news);
971 if (migrationsInput && normalizeInstanceType(current.instanceType) !== "PRIMARY") {
972 return yield* new MigrationError({
973 message: `migrations run on PRIMARY instances; '${name}' is ${normalizeInstanceType(current.instanceType)}`,
974 });
975 }
976 const migrations = migrationsInput
977 ? yield* runInstanceMigrations({
978 instance: name,
979 database: news.migrationsDatabase ?? "postgres",
980 input: migrationsInput,
981 stamped: stampedOf(output),
982 user: news.migrationsUser,
983 password: news.migrationsPassword,
984 })
985 : undefined;
986
987 return toAttrs(
988 { ...current, displayName: current.displayName ?? news.displayName },
989 env.project,
990 migrationsAttrs({ input: migrationsInput, run: migrations, output }),
991 );
992 }),
993
994 delete: Effect.fn(function* ({ output }) {
995 const operation = yield* alloydb
996 .deleteProjectsLocationsClustersInstances({ name: output.name })
997 .pipe(
998 Effect.catchTag("NotFound", () => Effect.undefined),
999 Effect.retry({
1000 while: (error) => error._tag === "Conflict",
1001 times: 40,
1002 schedule: Schedule.spaced("15 seconds"),
1003 }),
1004 );
1005 if (operation !== undefined) {
1006 yield* waitForDeleteOperation(operation);
1007 }
1008 yield* waitUntilGone(output.name);
1009 }),
1010 });