@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 84 lines 3.09 KB
Raw
1 import * as logging from "@distilled.cloud/gcp/logging_v2";
2 import * as Effect from "effect/Effect";
3 import * as Stream from "effect/Stream";
4 import { GcpEnvironment } from "../Environment.ts";
5 import { waitForOperation as waitForLongRunning, type LongRunningOperation } from "../Operation.ts";
6
7 const getOperation = (name: string) =>
8 name.includes("/billingAccounts/") || name.startsWith("billingAccounts/")
9 ? logging.getBillingAccountsLocationsOperations({ name })
10 : name.includes("/folders/") || name.startsWith("folders/")
11 ? logging.getFoldersLocationsOperations({ name })
12 : name.includes("/organizations/") || name.startsWith("organizations/")
13 ? logging.getOrganizationsLocationsOperations({ name })
14 : name.includes("/projects/") || name.startsWith("projects/")
15 ? logging.getProjectsLocationsOperations({ name })
16 : logging.getLocationsOperations({ name });
17
18 export const deleteBucketLinks = (bucketName: string) =>
19 logging.listLocationsBucketsLinks.pages({ parent: bucketName, pageSize: 100 }).pipe(
20 Stream.flatMap((page) => Stream.fromIterable(page.links ?? [])),
21 Stream.runCollect,
22 Effect.flatMap((links) =>
23 Effect.forEach(
24 links,
25 (link) => {
26 const name = link.name;
27 if (name === undefined) return Effect.void;
28 return logging.deleteLocationsBucketsLinks({ name }).pipe(
29 Effect.catchTag(["NotFound", "Conflict"], () => Effect.void),
30 Effect.asVoid,
31 );
32 },
33 { concurrency: 4 },
34 ),
35 ),
36 Effect.catchTag("NotFound", () => Effect.void),
37 Effect.asVoid,
38 );
39
40 export const listProjectBuckets = () =>
41 Effect.gen(function* () {
42 const env = yield* GcpEnvironment.current;
43 return yield* logging.listProjectsLocationsBuckets
44 .pages({
45 parent: `projects/${env.project}/locations/-`,
46 pageSize: 1000,
47 })
48 .pipe(
49 Stream.flatMap((page) => Stream.fromIterable(page.buckets ?? [])),
50 Stream.filter((bucket) => (bucket.name ?? "").length > 0),
51 Stream.runCollect,
52 Effect.map((chunk) => Array.from(chunk)),
53 );
54 });
55
56 /** Linked-dataset creation and bucket analytics upgrades take several minutes. */
57 const OPERATION_BUDGET = "15 minutes";
58
59 /**
60 * Wait for a long-running operation. ALREADY_EXISTS (code 6) means a
61 * concurrent create won the race; reconcile observes the resource next.
62 */
63 export const waitForOperation = (operation: LongRunningOperation) =>
64 waitForLongRunning(operation, getOperation, {
65 budget: OPERATION_BUDGET,
66 }).pipe(
67 Effect.catchIf(
68 (error) => error._tag === "GCP.OperationFailed" && error.code === 6,
69 () => Effect.succeed(operation),
70 ),
71 );
72
73 /**
74 * Wait for a delete operation. A vanished operation or NOT_FOUND (code 5)
75 * means the resource is already gone.
76 */
77 export const waitForDeleteOperation = (operation: LongRunningOperation) =>
78 waitForOperation(operation).pipe(
79 Effect.catchIf(
80 (error) =>
81 error._tag === "NotFound" || (error._tag === "GCP.OperationFailed" && error.code === 5),
82 () => Effect.succeed(operation),
83 ),
84 );