@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 546 lines 18.5 KB
Raw
1 import * as looker from "@distilled.cloud/gcp/looker_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import * as Stream from "effect/Stream";
11 import { GcpEnvironment } from "../Environment.ts";
12 import type { Providers } from "../Providers.ts";
13 import { waitForOperation } from "./operations.ts";
14
15 const MAX_NAME_LENGTH = 63;
16 const OWNERSHIP_PREFIX = "alch-";
17 const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
18
19 export type EncryptionConfig = {
20 /** CMEK key resource name. */
21 kmsKeyName?: string;
22 /** Full CMEK key version currently in use. */
23 kmsKeyNameVersion?: string;
24 /** CMEK key status (`VALID`, `REVOKED`, …). */
25 kmsKeyState?: string;
26 };
27
28 export type InstancesBackupProps = {
29 /**
30 * Parent Looker instance. Full name
31 * `projects/{project}/locations/{location}/instances/{instance}` or
32 * the instance id (combined with `location`). Immutable — changing it
33 * replaces the backup.
34 */
35 instance: string;
36 /**
37 * Region of the parent instance when `instance` is a bare id.
38 * Ignored when `instance` is a full resource name. Immutable —
39 * changing it replaces the backup. `US-CENTRAL1` is accepted and
40 * normalized to `us-central1`.
41 * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`)
42 */
43 location?: string;
44 /**
45 * Backup id (the `{backup}` segment of
46 * `.../instances/{instance}/backups/{backup}`). If omitted, a unique
47 * RFC1035 name is generated with an `alch-` prefix so `list` / nuke
48 * can find it. The backups API has no labels or description field;
49 * ownership is stamped in the backup id. Immutable — changing it
50 * replaces the backup. The API may ignore a requested id and assign
51 * a UUID; Alchemy then tracks the server-assigned name.
52 */
53 backupId?: string;
54 };
55
56 export type InstancesBackup = Resource<
57 "GCP.Looker.InstancesBackup",
58 InstancesBackupProps,
59 {
60 /** Full resource name `projects/{project}/locations/{location}/instances/{instance}/backups/{backup}`. */
61 name: string;
62 /** Backup id (last path segment). */
63 backupId: string;
64 /** Parent instance resource name. */
65 instance: string;
66 /** Parent instance id. */
67 instanceId: string;
68 /** Project id. */
69 project: string;
70 /** Region id (`us-central1`, …). */
71 location: string;
72 /** Server-reported state (`ACTIVE`, `CREATING`, `FAILED`, …). */
73 state: string | undefined;
74 /** Customer-managed encryption status, if any. */
75 encryptionConfig: EncryptionConfig | undefined;
76 /** RFC3339 time when the backup was started. */
77 createTime: string | undefined;
78 /** RFC3339 time when the backup will be deleted. */
79 expireTime: string | undefined;
80 },
81 never,
82 Providers
83 >;
84
85 /**
86 * A Looker (Google Cloud core) instance backup.
87 *
88 * Backups are existence-only: the API exposes no mutable fields. Changing
89 * `instance`, `location`, or `backupId` replaces the backup. Looker
90 * instance backups have no labels or description, so Alchemy stamps
91 * ownership into a generated `alch-` backup id for `list` / nuke.
92 *
93 * Create and delete are long-running operations. Provisioning a parent
94 * Looker instance is slow and billed; live lifecycle tests skipIf-gate
95 * behind `FAST` and the typed Looker API entitlement error.
96 *
97 * ### Creating a Backup
98 * **Example:** Generated id
99 * ```typescript
100 * const backup = yield* GCP.Looker.InstancesBackup("Nightly", {
101 * instance: instance.name,
102 * });
103 * ```
104 *
105 * **Example:** Explicit id and parent
106 * ```typescript
107 * const backup = yield* GCP.Looker.InstancesBackup("Nightly", {
108 * instance: "projects/my-project/locations/us-central1/instances/analytics",
109 * backupId: "alch-nightly",
110 * });
111 * ```
112 *
113 * @resource
114 * @category Looker
115 */
116 export const InstancesBackup = Resource<InstancesBackup>("GCP.Looker.InstancesBackup");
117
118 export class InstancesBackupNotResolved extends Data.TaggedError(
119 "GCP.Looker.InstancesBackupNotResolved",
120 )<{
121 resourceName: string;
122 }> {}
123
124 export class InstancesBackupInstanceMissing extends Data.TaggedError(
125 "GCP.Looker.InstancesBackupInstanceMissing",
126 )<{
127 message: string;
128 }> {}
129
130 export class InstancesBackupNotReady extends Data.TaggedError(
131 "GCP.Looker.InstancesBackupNotReady",
132 )<{
133 resourceName: string;
134 state: string;
135 }> {}
136
137 export class InstancesBackupFailed extends Data.TaggedError("GCP.Looker.InstancesBackupFailed")<{
138 resourceName: string;
139 state: string;
140 }> {}
141
142 export class InstancesBackupStillExists extends Data.TaggedError(
143 "GCP.Looker.InstancesBackupStillExists",
144 )<{
145 resourceName: string;
146 }> {}
147
148 const lastSegment = (value: string | undefined) => {
149 if (value === undefined || value.length === 0) return "";
150 const trimmed = value.replace(/\/+$/, "");
151 const parts = trimmed.split("/");
152 return parts[parts.length - 1] || trimmed;
153 };
154
155 const rfc1035 = (name: string): string => {
156 let next = name
157 .toLowerCase()
158 .replace(/[^a-z0-9-]/g, "-")
159 .replace(/-+/g, "-")
160 .replace(/^-+|-+$/g, "");
161 if (!/^[a-z]/.test(next)) next = `b${next}`;
162 next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/g, "");
163 if (next.length === 0) return "backup";
164 if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, MAX_NAME_LENGTH - 1)}0`;
165 return next.slice(0, MAX_NAME_LENGTH);
166 };
167
168 const normalizeLocation = (location: string | undefined, fallback: string) =>
169 lastSegment(location ?? fallback).toLowerCase();
170
171 const instanceNameOf = (project: string, location: string, instanceId: string) =>
172 `projects/${project}/locations/${location}/instances/${instanceId}`;
173
174 const resourceName = (instance: string, backupId: string) => `${instance}/backups/${backupId}`;
175
176 const parseBackupName = (name: string, fallbackLocation: string) => {
177 const parts = name.split("/").filter((part) => part.length > 0);
178 const backupsAt = parts.lastIndexOf("backups");
179 const instancesAt = parts.lastIndexOf("instances");
180 const locationsAt = parts.lastIndexOf("locations");
181 const projectsAt = parts.lastIndexOf("projects");
182 const instanceId = instancesAt >= 0 && parts[instancesAt + 1] ? parts[instancesAt + 1]! : "";
183 const project = projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "";
184 const location =
185 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : fallbackLocation;
186 return {
187 project,
188 location,
189 instanceId,
190 instance: instanceId.length > 0 ? instanceNameOf(project, location, instanceId) : "",
191 backupId: backupsAt >= 0 && parts[backupsAt + 1] ? parts[backupsAt + 1]! : lastSegment(name),
192 };
193 };
194
195 const parseInstanceRef = (
196 instance: string,
197 fallbackProject: string,
198 fallbackLocation: string | undefined,
199 region: string,
200 ) => {
201 const trimmed = instance.trim();
202 if (trimmed.length === 0) {
203 return {
204 project: fallbackProject,
205 location: normalizeLocation(fallbackLocation, region),
206 instanceId: "",
207 instanceName: "",
208 };
209 }
210 if (trimmed.includes("/instances/") || trimmed.includes("projects/")) {
211 const parsed = parseBackupName(
212 trimmed.includes("/backups/") ? trimmed : `${trimmed}/backups/-`,
213 "",
214 );
215 const location = normalizeLocation(parsed.location || fallbackLocation, region);
216 const project = parsed.project || fallbackProject;
217 return {
218 project,
219 location,
220 instanceId: parsed.instanceId,
221 instanceName: instanceNameOf(project, location, parsed.instanceId),
222 };
223 }
224 const location = normalizeLocation(fallbackLocation, region);
225 const instanceId = lastSegment(trimmed);
226 return {
227 project: fallbackProject,
228 location,
229 instanceId,
230 instanceName: instanceNameOf(fallbackProject, location, instanceId),
231 };
232 };
233
234 const toEncryptionConfig = (
235 config: looker.EncryptionConfig | undefined,
236 ): EncryptionConfig | undefined => {
237 if (config === undefined) return undefined;
238 if (
239 (config.kmsKeyName === undefined || config.kmsKeyName.length === 0) &&
240 (config.kmsKeyNameVersion === undefined || config.kmsKeyNameVersion.length === 0) &&
241 (config.kmsKeyState === undefined || config.kmsKeyState.length === 0)
242 ) {
243 return undefined;
244 }
245 return {
246 kmsKeyName: config.kmsKeyName,
247 kmsKeyNameVersion: config.kmsKeyNameVersion,
248 kmsKeyState: config.kmsKeyState,
249 };
250 };
251
252 const isUuid = (value: string) => UUID_RE.test(value);
253
254 const isOwnedBackupId = (backupId: string) =>
255 backupId.startsWith(OWNERSHIP_PREFIX) && !isUuid(backupId);
256
257 const isPlaceholder = (backup: looker.InstanceBackup) => {
258 const name = backup.name ?? "";
259 return name.length === 0 || name.endsWith("/backups/-") || name.endsWith("/backups/");
260 };
261
262 const isAvailable = (state: string | undefined) => (state ?? "").toUpperCase() === "ACTIVE";
263
264 const isFailed = (state: string | undefined) => (state ?? "").toUpperCase() === "FAILED";
265
266 const toAttrs = (backup: looker.InstanceBackup, project: string, region: string) => {
267 const name = backup.name ?? "";
268 const parsed = parseBackupName(name, region);
269 return {
270 name,
271 backupId: parsed.backupId,
272 instance: parsed.instance,
273 instanceId: parsed.instanceId,
274 project: parsed.project || project,
275 location: parsed.location,
276 state: backup.state,
277 encryptionConfig: toEncryptionConfig(backup.encryptionConfig),
278 createTime: backup.createTime,
279 expireTime: backup.expireTime,
280 };
281 };
282
283 const toId = (id: string, backupId: string | undefined, existing?: string) =>
284 Effect.gen(function* () {
285 if (backupId !== undefined && backupId.length > 0) return backupId;
286 if (existing !== undefined && existing.length > 0) return existing;
287 const generated = yield* createPhysicalName({
288 id,
289 maxLength: MAX_NAME_LENGTH - OWNERSHIP_PREFIX.length,
290 lowercase: true,
291 });
292 return rfc1035(`${OWNERSHIP_PREFIX}${generated}`);
293 });
294
295 const getByName = (name: string) =>
296 name.length === 0 || name.includes("//") || name.endsWith("/backups/-")
297 ? Effect.undefined
298 : looker
299 .getProjectsLocationsInstancesBackups({ name })
300 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
301
302 const waitUntilExists = (name: string) =>
303 getByName(name).pipe(
304 Effect.flatMap((backup) =>
305 backup
306 ? Effect.succeed(backup)
307 : Effect.fail(new InstancesBackupNotResolved({ resourceName: name })),
308 ),
309 Effect.retry({
310 while: (error) => error._tag === "GCP.Looker.InstancesBackupNotResolved",
311 times: 8,
312 schedule: Schedule.spaced("1 second"),
313 }),
314 );
315
316 const waitUntilReady = (name: string) =>
317 Effect.gen(function* () {
318 const backup = yield* getByName(name);
319 if (backup === undefined) {
320 return yield* new InstancesBackupNotReady({ resourceName: name, state: "MISSING" });
321 }
322 if (isFailed(backup.state)) {
323 return yield* new InstancesBackupFailed({
324 resourceName: name,
325 state: backup.state ?? "FAILED",
326 });
327 }
328 if (!isAvailable(backup.state)) {
329 return yield* new InstancesBackupNotReady({
330 resourceName: name,
331 state: backup.state ?? "STATE_UNSPECIFIED",
332 });
333 }
334 return backup;
335 }).pipe(
336 Effect.retry({
337 while: (error) => error._tag === "GCP.Looker.InstancesBackupNotReady",
338 times: 10,
339 schedule: Schedule.spaced("8 seconds"),
340 }),
341 );
342
343 const waitUntilGone = (name: string) =>
344 getByName(name).pipe(
345 Effect.flatMap((backup) =>
346 backup === undefined
347 ? Effect.void
348 : Effect.fail(new InstancesBackupStillExists({ resourceName: name })),
349 ),
350 Effect.retry({
351 while: (error) => error._tag === "GCP.Looker.InstancesBackupStillExists",
352 times: 10,
353 schedule: Schedule.spaced("8 seconds"),
354 }),
355 );
356
357 const listInstanceBackups = (parent: string) =>
358 looker.listProjectsLocationsInstancesBackups
359 .pages({
360 parent,
361 pageSize: 1000,
362 })
363 .pipe(
364 Stream.flatMap((page) => Stream.fromIterable(page.instanceBackups ?? [])),
365 Stream.runCollect,
366 Effect.map((chunk) => Array.from(chunk)),
367 Effect.catchTag("NotFound", () => Effect.succeed([])),
368 );
369
370 const listOwned = (project: string, region: string) =>
371 looker.listProjectsLocationsInstances
372 .pages({
373 parent: `projects/${project}/locations/-`,
374 pageSize: 1000,
375 })
376 .pipe(
377 Stream.flatMap((page) => Stream.fromIterable(page.instances ?? [])),
378 Stream.filter((instance) => (instance.name ?? "").length > 0),
379 Stream.runCollect,
380 Effect.map((chunk) => Array.from(chunk)),
381 Effect.catchTag("NotFound", () => Effect.succeed([] as looker.Instance[])),
382 Effect.flatMap((instances) =>
383 instances.length > 0
384 ? Effect.succeed(instances)
385 : looker.listProjectsLocationsInstances
386 .pages({
387 parent: `projects/${project}/locations/${region}`,
388 pageSize: 1000,
389 })
390 .pipe(
391 Stream.flatMap((page) => Stream.fromIterable(page.instances ?? [])),
392 Stream.runCollect,
393 Effect.map((chunk) => Array.from(chunk)),
394 Effect.catchTag("NotFound", () => Effect.succeed([] as looker.Instance[])),
395 ),
396 ),
397 Effect.flatMap((instances) =>
398 Effect.forEach(
399 instances.filter((instance) => (instance.name ?? "").length > 0),
400 (instance) => listInstanceBackups(instance.name!),
401 { concurrency: 4 },
402 ),
403 ),
404 Effect.map((pages) =>
405 pages
406 .flat()
407 .filter(
408 (backup) =>
409 !isPlaceholder(backup) &&
410 isOwnedBackupId(parseBackupName(backup.name ?? "", region).backupId),
411 ),
412 ),
413 );
414
415 export const InstancesBackupProvider = () =>
416 Provider.succeed(InstancesBackup, {
417 stables: ["name", "backupId", "instance", "instanceId", "project", "location", "createTime"],
418
419 diff: Effect.fn(function* ({ news, olds, output }) {
420 if (!isResolved(news)) return undefined;
421 const env = yield* GcpEnvironment.current;
422
423 const previousId = olds?.backupId ?? output?.backupId;
424 const nextId = news.backupId ?? previousId;
425 const previousInstance = lastSegment(
426 olds?.instance ?? output?.instance ?? output?.instanceId,
427 );
428 const nextInstance = lastSegment(news.instance ?? previousInstance);
429 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
430 const nextLocation = normalizeLocation(news.location ?? output?.location, env.region);
431
432 const replace =
433 (previousId !== undefined && nextId !== undefined && nextId !== previousId) ||
434 (previousInstance.length > 0 &&
435 nextInstance.length > 0 &&
436 previousInstance !== nextInstance) ||
437 previousLocation !== nextLocation;
438
439 if (!replace) return undefined;
440 return {
441 action: "replace" as const,
442 deleteFirst:
443 previousLocation === nextLocation &&
444 previousInstance === nextInstance &&
445 previousId !== undefined &&
446 nextId === previousId,
447 };
448 }),
449
450 read: Effect.fn(function* ({ id, olds, output }) {
451 const env = yield* GcpEnvironment.current;
452 if (output?.name) {
453 const existing = yield* getByName(output.name);
454 if (existing === undefined) return undefined;
455 const attrs = toAttrs(existing, env.project, env.region);
456 const owned =
457 isOwnedBackupId(attrs.backupId) ||
458 attrs.name === output.name ||
459 attrs.backupId === (olds?.backupId ?? output.backupId);
460 return owned ? attrs : Unowned(attrs);
461 }
462 const backupId = yield* toId(id, olds?.backupId, output?.backupId);
463 const ref = parseInstanceRef(
464 olds?.instance ?? output?.instance ?? output?.instanceId ?? "",
465 env.project,
466 olds?.location ?? output?.location,
467 env.region,
468 );
469 if (ref.instanceId.length === 0) return undefined;
470 const name = resourceName(ref.instanceName, backupId);
471 const existing = yield* getByName(name);
472 if (existing === undefined) return undefined;
473 const attrs = toAttrs(existing, env.project, env.region);
474 return isOwnedBackupId(attrs.backupId) || attrs.backupId === backupId
475 ? attrs
476 : Unowned(attrs);
477 }),
478
479 list: () =>
480 Effect.gen(function* () {
481 const env = yield* GcpEnvironment.current;
482 const items = yield* listOwned(env.project, env.region);
483 return items.map((item) => toAttrs(item, env.project, env.region));
484 }),
485
486 reconcile: Effect.fn(function* ({ id, news, output }) {
487 const env = yield* GcpEnvironment.current;
488 const backupId = yield* toId(id, news.backupId, output?.backupId);
489 const ref = parseInstanceRef(
490 news.instance,
491 env.project,
492 news.location ?? output?.location,
493 env.region,
494 );
495 if (ref.instanceId.length === 0) {
496 return yield* new InstancesBackupInstanceMissing({
497 message:
498 "GCP.Looker.InstancesBackup requires `instance` (instance id or full resource name)",
499 });
500 }
501 const name = resourceName(ref.instanceName, backupId);
502
503 let current = yield* getByName(output?.name ?? name);
504
505 if (current === undefined) {
506 const created = yield* looker
507 .createProjectsLocationsInstancesBackups({
508 parent: ref.instanceName,
509 body: { name },
510 })
511 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
512 if (created !== undefined) {
513 yield* waitForOperation(created);
514 }
515 current = yield* waitUntilExists(output?.name ?? name);
516 }
517
518 if (current === undefined) {
519 return yield* new InstancesBackupNotResolved({ resourceName: name });
520 }
521
522 const currentName = current.name ?? name;
523 if (!isAvailable(current.state)) {
524 current = yield* waitUntilReady(currentName);
525 }
526
527 return toAttrs(current, env.project, env.region);
528 }),
529
530 delete: Effect.fn(function* ({ output }) {
531 const operation = yield* looker
532 .deleteProjectsLocationsInstancesBackups({ name: output.name })
533 .pipe(
534 Effect.catchTag("NotFound", () => Effect.undefined),
535 Effect.retry({
536 while: (error) => error._tag === "Conflict",
537 times: 8,
538 schedule: Schedule.spaced("5 seconds"),
539 }),
540 );
541 if (operation !== undefined) {
542 yield* waitForOperation(operation, { notFoundOk: true });
543 }
544 yield* waitUntilGone(output.name);
545 }),
546 });