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

main
ts 418 lines 13.7 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 * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import { GcpEnvironment } from "../Environment.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 clusterIdOf,
13 clusterNameOf,
14 instanceIdOf,
15 listAlchemyInstances,
16 MAX_BACKUP_ID_LENGTH,
17 parentOwned,
18 parseResourceName,
19 tableNameOf,
20 toPhysicalId,
21 waitForOperation,
22 collectPages,
23 } from "./operations.ts";
24
25 const DEFAULT_EXPIRE_MS = 7 * 24 * 60 * 60 * 1000;
26
27 export type InstancesClustersBackupProps = {
28 /**
29 * Parent instance. Full name `projects/{project}/instances/{instance}`
30 * or the instance id. Immutable — changing it replaces the backup.
31 */
32 instance: string;
33 /**
34 * Cluster that stores the backup. Full name
35 * `projects/{project}/instances/{instance}/clusters/{cluster}` or the
36 * cluster id. Immutable — changing it replaces the backup.
37 */
38 cluster: string;
39 /**
40 * Source table. Full name
41 * `projects/{project}/instances/{instance}/tables/{table}` or the table
42 * id. Immutable — changing it replaces the backup.
43 */
44 sourceTable: string;
45 /**
46 * Backup id (the `{backup}` segment of
47 * `.../clusters/{cluster}/backups/{backup}`). If omitted, a unique name
48 * is generated from the stack, stage, and logical id. Must be 1-50
49 * characters. Immutable — changing it replaces the backup.
50 */
51 backupId?: string;
52 /**
53 * RFC3339 expiration. Must be at least 6 hours and at most 90 days
54 * after creation. Defaults to 7 days from reconcile time when omitted
55 * on create.
56 */
57 expireTime?: string;
58 /**
59 * Backup type (`STANDARD` or `HOT`). Immutable — changing it replaces
60 * the backup.
61 */
62 backupType?: bigtable.BackupBackupTypeEnum | (string & {});
63 /**
64 * Time at which a hot backup converts to standard. Only valid for
65 * `HOT` backups; must be at least 24 hours after creation.
66 */
67 hotToStandardTime?: string;
68 };
69
70 export type InstancesClustersBackup = Resource<
71 "GCP.Bigtable.InstancesClustersBackup",
72 InstancesClustersBackupProps,
73 {
74 /** Full resource name `.../clusters/{cluster}/backups/{backup}`. */
75 name: string;
76 /** Backup id (last path segment). */
77 backupId: string;
78 /** Parent cluster resource name. */
79 cluster: string;
80 /** Parent cluster id. */
81 clusterId: string;
82 /** Parent instance resource name. */
83 instance: string;
84 /** Parent instance id. */
85 instanceId: string;
86 /** Project id. */
87 project: string;
88 /** Source table resource name. */
89 sourceTable: string | undefined;
90 /** Copied-from backup name, if this backup is a copy. */
91 sourceBackup: string | undefined;
92 /** RFC3339 expiration. */
93 expireTime: string | undefined;
94 /** Backup type (`STANDARD`, `HOT`). */
95 backupType: string | undefined;
96 /** Hot-to-standard conversion time. */
97 hotToStandardTime: string | undefined;
98 /** Server-reported state (`CREATING`, `READY`). */
99 state: string | undefined;
100 /** RFC3339 start time. */
101 startTime: string | undefined;
102 /** RFC3339 end time. */
103 endTime: string | undefined;
104 /** Size in bytes (decimal string). */
105 sizeBytes: string | undefined;
106 /** Encryption info reported by the API. */
107 encryptionInfo: bigtable.EncryptionInfo | undefined;
108 },
109 never,
110 Providers
111 >;
112
113 /**
114 * A Cloud Bigtable backup of a table, stored on one cluster.
115 *
116 * The parent instance, cluster, and source table must already exist.
117 * Changing `backupId`, `instance`, `cluster`, `sourceTable`, or
118 * `backupType` replaces the backup. `expireTime` and
119 * `hotToStandardTime` update in place.
120 *
121 * Backups have no labels field. Alchemy treats a backup as owned when
122 * its parent instance carries Alchemy labels, so `list` /
123 * `pnpm nuke:gcp` can find it.
124 *
125 * ### Creating a Backup
126 * **Example:** 7-day backup of an existing table
127 * ```typescript
128 * const backup = yield* GCP.Bigtable.InstancesClustersBackup("Nightly", {
129 * instance: instance.name,
130 * cluster: "cluster",
131 * sourceTable: table.name,
132 * });
133 * ```
134 *
135 * **Example:** Explicit id and expiration
136 * ```typescript
137 * const backup = yield* GCP.Bigtable.InstancesClustersBackup("Nightly", {
138 * instance: instance.name,
139 * cluster: replica.clusterId,
140 * sourceTable: table.name,
141 * backupId: "nightly",
142 * expireTime: "2026-12-31T00:00:00Z",
143 * backupType: "STANDARD",
144 * });
145 * ```
146 *
147 * @resource
148 * @category Bigtable
149 */
150 export const InstancesClustersBackup = Resource<InstancesClustersBackup>(
151 "GCP.Bigtable.InstancesClustersBackup",
152 );
153
154 export class BackupNotResolved extends Data.TaggedError("GCP.Bigtable.BackupNotResolved")<{
155 resourceName: string;
156 }> {}
157
158 export class BackupNotReady extends Data.TaggedError("GCP.Bigtable.BackupNotReady")<{
159 resourceName: string;
160 state: string;
161 }> {}
162
163 export class BackupStillExists extends Data.TaggedError("GCP.Bigtable.BackupStillExists")<{
164 resourceName: string;
165 }> {}
166
167 const toId = (id: string, backupId: string | undefined, existing?: string) =>
168 toPhysicalId(id, backupId, existing, MAX_BACKUP_ID_LENGTH);
169
170 const normalizeType = (value: string | undefined) => {
171 const next = (value ?? "").toUpperCase();
172 return next === "BACKUP_TYPE_UNSPECIFIED" ? "" : next;
173 };
174
175 const defaultExpireTime = () =>
176 Effect.sync(() => new Date(Date.now() + DEFAULT_EXPIRE_MS).toISOString());
177
178 const toAttrs = (backup: bigtable.Backup, project: string) => {
179 const name = backup.name ?? "";
180 const parsed = parseResourceName(name);
181 return {
182 name,
183 backupId: parsed.backupId,
184 cluster: parsed.cluster,
185 clusterId: parsed.clusterId,
186 instance: parsed.instance,
187 instanceId: parsed.instanceId,
188 project: parsed.project || project,
189 sourceTable: backup.sourceTable,
190 sourceBackup: backup.sourceBackup,
191 expireTime: backup.expireTime,
192 backupType: backup.backupType,
193 hotToStandardTime: backup.hotToStandardTime,
194 state: backup.state,
195 startTime: backup.startTime,
196 endTime: backup.endTime,
197 sizeBytes: backup.sizeBytes,
198 encryptionInfo: backup.encryptionInfo,
199 };
200 };
201
202 const getByName = (name: string) =>
203 bigtable
204 .getProjectsInstancesClustersBackups({ name })
205 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
206
207 const isBusy = (state: string | undefined) =>
208 state === "CREATING" || state === "STATE_UNSPECIFIED" || state === undefined;
209
210 const waitUntilExists = (name: string) =>
211 getByName(name).pipe(
212 Effect.flatMap((backup) =>
213 backup ? Effect.succeed(backup) : Effect.fail(new BackupNotResolved({ resourceName: name })),
214 ),
215 Effect.retry({
216 while: (error) => error._tag === "GCP.Bigtable.BackupNotResolved",
217 times: 8,
218 schedule: Schedule.spaced("1 second"),
219 }),
220 );
221
222 const waitUntilReady = (name: string) =>
223 getByName(name).pipe(
224 Effect.filterOrFail(
225 (backup): backup is bigtable.Backup => backup !== undefined,
226 () => new BackupNotResolved({ resourceName: name }),
227 ),
228 Effect.filterOrFail(
229 (backup) => !isBusy(backup.state),
230 (backup) =>
231 new BackupNotReady({
232 resourceName: name,
233 state: backup.state ?? "STATE_UNSPECIFIED",
234 }),
235 ),
236 Effect.retry({
237 while: (error) =>
238 error._tag === "GCP.Bigtable.BackupNotReady" ||
239 error._tag === "GCP.Bigtable.BackupNotResolved",
240 times: 10,
241 schedule: Schedule.spaced("8 seconds"),
242 }),
243 );
244
245 const waitUntilGone = (name: string) =>
246 getByName(name).pipe(
247 Effect.flatMap((backup) =>
248 backup === undefined
249 ? Effect.void
250 : Effect.fail(new BackupStillExists({ resourceName: name })),
251 ),
252 Effect.retry({
253 while: (error) => error._tag === "GCP.Bigtable.BackupStillExists",
254 times: 10,
255 schedule: Schedule.spaced("5 seconds"),
256 }),
257 );
258
259 export const InstancesClustersBackupProvider = () =>
260 Provider.succeed(InstancesClustersBackup, {
261 stables: [
262 "name",
263 "backupId",
264 "cluster",
265 "clusterId",
266 "instance",
267 "instanceId",
268 "project",
269 "sourceTable",
270 "startTime",
271 ],
272
273 diff: Effect.fn(function* ({ news, olds, output }) {
274 if (!isResolved(news)) return undefined;
275 const previousId = olds?.backupId ?? output?.backupId;
276 const nextId = news.backupId ?? previousId;
277 const previousInstance = instanceIdOf(
278 olds?.instance ?? output?.instance ?? output?.instanceId ?? "",
279 );
280 const nextInstance = instanceIdOf(news.instance);
281 const previousCluster = clusterIdOf(
282 olds?.cluster ?? output?.cluster ?? output?.clusterId ?? "",
283 );
284 const nextCluster = clusterIdOf(news.cluster);
285 const previousTable = parseResourceName(
286 olds?.sourceTable ?? output?.sourceTable ?? "",
287 ).tableId;
288 const nextTable = parseResourceName(
289 news.sourceTable.includes("/tables/")
290 ? news.sourceTable
291 : `projects/_/instances/_/tables/${news.sourceTable}`,
292 ).tableId;
293 const previousType = normalizeType(olds?.backupType ?? output?.backupType);
294 const nextType = normalizeType(news.backupType ?? output?.backupType);
295 if (
296 (previousId !== undefined && nextId !== undefined && previousId !== nextId) ||
297 (previousInstance.length > 0 && previousInstance !== nextInstance) ||
298 (previousCluster.length > 0 && previousCluster !== nextCluster) ||
299 (previousTable.length > 0 && previousTable !== nextTable) ||
300 (news.backupType !== undefined &&
301 previousType.length > 0 &&
302 nextType.length > 0 &&
303 previousType !== nextType)
304 ) {
305 return { action: "replace" as const };
306 }
307 return undefined;
308 }),
309
310 read: Effect.fn(function* ({ id, olds, output }) {
311 const env = yield* GcpEnvironment.current;
312 const backupId = yield* toId(id, olds?.backupId, output?.backupId);
313 const instanceRef = olds?.instance ?? output?.instance;
314 const clusterRef = olds?.cluster ?? output?.cluster;
315 const name =
316 output?.name ??
317 (instanceRef && clusterRef
318 ? `${clusterNameOf(env.project, instanceRef, clusterRef)}/backups/${backupId}`
319 : undefined);
320 if (name === undefined) return undefined;
321 const existing = yield* getByName(name);
322 if (existing === undefined) return undefined;
323 const attrs = toAttrs(existing, env.project);
324 return (yield* parentOwned(attrs.instance)) ? attrs : Unowned(attrs);
325 }),
326
327 list: () =>
328 Effect.gen(function* () {
329 const env = yield* GcpEnvironment.current;
330 const instances = (yield* listAlchemyInstances(env.project)).filter(
331 (instance): instance is bigtable.Instance & { name: string } =>
332 typeof instance.name === "string" && instance.name.length > 0,
333 );
334 const pages = yield* Effect.forEach(
335 instances,
336 (instance) =>
337 collectPages(
338 bigtable.listProjectsInstancesClustersBackups.pages({
339 parent: `${instance.name}/clusters/-`,
340 pageSize: 1000,
341 }),
342 (page) => page.backups,
343 ).pipe(Effect.catchTag("NotFound", () => Effect.succeed([] as bigtable.Backup[]))),
344 { concurrency: 4 },
345 );
346 return pages.flat().map((backup) => toAttrs(backup, env.project));
347 }),
348
349 reconcile: Effect.fn(function* ({ id, news, output }) {
350 const env = yield* GcpEnvironment.current;
351 const backupId = yield* toId(id, news.backupId, output?.backupId);
352 const parent = clusterNameOf(env.project, news.instance, news.cluster);
353 const name = `${parent}/backups/${backupId}`;
354 const sourceTable = tableNameOf(env.project, news.instance, news.sourceTable);
355 const expireTime = news.expireTime ?? (yield* defaultExpireTime());
356
357 let current = yield* getByName(output?.name ?? name);
358
359 if (current === undefined) {
360 const created = yield* bigtable
361 .createProjectsInstancesClustersBackups({
362 parent,
363 backupId,
364 body: {
365 sourceTable,
366 expireTime,
367 backupType: news.backupType,
368 hotToStandardTime: news.hotToStandardTime,
369 },
370 })
371 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
372 if (created !== undefined) {
373 yield* waitForOperation(created, { alreadyExistsOk: true });
374 }
375 current = yield* waitUntilExists(name);
376 }
377
378 if (isBusy(current.state)) {
379 current = yield* waitUntilReady(name);
380 }
381
382 const mask: string[] = [];
383 const patchBody: bigtable.Backup = {};
384 if (news.expireTime !== undefined && (current.expireTime ?? "") !== news.expireTime) {
385 patchBody.expireTime = news.expireTime;
386 mask.push("expire_time");
387 }
388 if (
389 news.hotToStandardTime !== undefined &&
390 (current.hotToStandardTime ?? "") !== news.hotToStandardTime
391 ) {
392 patchBody.hotToStandardTime = news.hotToStandardTime;
393 mask.push("hot_to_standard_time");
394 }
395
396 if (mask.length > 0) {
397 current = yield* bigtable.patchProjectsInstancesClustersBackups({
398 name,
399 updateMask: mask.join(","),
400 body: patchBody,
401 });
402 }
403
404 return toAttrs(current, env.project);
405 }),
406
407 delete: Effect.fn(function* ({ output }) {
408 yield* bigtable.deleteProjectsInstancesClustersBackups({ name: output.name }).pipe(
409 Effect.catchTag("NotFound", () => Effect.void),
410 Effect.retry({
411 while: (error) => error._tag === "Conflict",
412 times: 8,
413 schedule: Schedule.spaced("2 seconds"),
414 }),
415 );
416 yield* waitUntilGone(output.name);
417 }),
418 });