@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 358 lines 12.4 KB
Raw
1 import * as logging from "@distilled.cloud/gcp/logging_v2";
2 import { unownedUnlessPrevious } 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 * as Stream from "effect/Stream";
10 import { GcpEnvironment } from "../Environment.ts";
11 import { createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
12 import type { Providers } from "../Providers.ts";
13 import {
14 DEFAULT_LOCATION,
15 encodeDescription,
16 hasOwnershipMarker,
17 lastSegment,
18 organizationIdOf,
19 parseDescription,
20 resolveOrganization,
21 toPhysicalId,
22 tryResolveOrganization,
23 } from "./internal.ts";
24 import { waitForDeleteOperation, waitForOperation } from "./operations.ts";
25
26 export type OrganizationBucketsLinkProps = {
27 /**
28 * Link id (alphanumeric and underscores only). If omitted, a unique
29 * name is generated from the stack, stage, and logical id. Immutable —
30 * changing it replaces the link. A log bucket may contain only one
31 * link.
32 */
33 linkId?: string;
34 /**
35 * Parent organization (`organizations/{organization}` or the numeric
36 * id). Defaults to the project ancestor organization. Immutable —
37 * changing it replaces the link.
38 */
39 organization?: string;
40 /**
41 * Parent analytics-enabled log bucket resource name
42 * (`organizations/{organization}/locations/{location}/buckets/{bucket}`).
43 * Required. Immutable — changing it replaces the link.
44 */
45 bucket: string;
46 /**
47 * Location of the parent bucket. Taken from `bucket` when omitted.
48 * @default "global"
49 */
50 location?: string;
51 /**
52 * Human-readable description (max 8000 characters). Links have no
53 * labels field, so Alchemy ownership is stored in a `[alchemy …]`
54 * prefix and stripped from attributes. The Logging API cannot update a
55 * link after create.
56 */
57 description?: string;
58 };
59
60 export type OrganizationBucketsLink = Resource<
61 "GCP.Logging.OrganizationBucketsLink",
62 OrganizationBucketsLinkProps,
63 {
64 /** Full resource name `organizations/{organization}/locations/{location}/buckets/{bucket}/links/{link}`. */
65 name: string;
66 /** Link id (last path segment). */
67 linkId: string;
68 /** Parent bucket resource name. */
69 bucket: string;
70 /** Bucket id. */
71 bucketId: string;
72 /** Organization resource name. */
73 organization: string;
74 /** Organization id. */
75 organizationId: string;
76 /** Project id of the deploying stack. */
77 project: string;
78 /** Location of the parent bucket. */
79 location: string;
80 /** User description with the Alchemy ownership prefix stripped. */
81 description: string | undefined;
82 /** Linked BigQuery dataset id, if reported. */
83 datasetId: string | undefined;
84 /** Link lifecycle (`ACTIVE`, `CREATING`, …). */
85 lifecycleState: string | undefined;
86 /** RFC3339 creation timestamp. */
87 createTime: string | undefined;
88 },
89 never,
90 Providers
91 >;
92
93 /**
94 * A BigQuery dataset link for an organization Cloud Logging bucket.
95 *
96 * Analytics must be enabled on the parent bucket. A bucket may contain
97 * only one link. Links have no labels field, so Alchemy stamps
98 * ownership into the description for `list` / nuke. The API has no
99 * update method — `linkId`, organization, and bucket are identity.
100 *
101 * ### Creating an Organization Bucket Link
102 * **Example:** Link an analytics-enabled organization bucket
103 * ```typescript
104 * const bucket = yield* GCP.Logging.OrganizationLogBucket("Analytics", {
105 * analyticsEnabled: true,
106 * });
107 * const link = yield* GCP.Logging.OrganizationBucketsLink("Bq", {
108 * bucket: bucket.name,
109 * description: "log analytics dataset",
110 * });
111 * ```
112 *
113 * @resource
114 * @category Logging
115 */
116 export const OrganizationBucketsLink = Resource<OrganizationBucketsLink>(
117 "GCP.Logging.OrganizationBucketsLink",
118 );
119
120 export class OrganizationBucketsLinkNotResolved extends Data.TaggedError(
121 "GCP.Logging.OrganizationBucketsLinkNotResolved",
122 )<{
123 resourceName: string;
124 }> {}
125
126 const parseLinkName = (name: string) => {
127 const match = name.match(
128 /^(organizations\/[^/]+)\/locations\/([^/]+)\/buckets\/([^/]+)\/links\/([^/]+)$/,
129 );
130 if (!match) return undefined;
131 return {
132 organization: match[1]!,
133 location: match[2]!,
134 bucketId: match[3]!,
135 linkId: match[4]!,
136 };
137 };
138
139 const parseBucketName = (name: string) => {
140 const match = name.match(/^(organizations\/[^/]+)\/locations\/([^/]+)\/buckets\/([^/]+)$/);
141 if (!match) return undefined;
142 return {
143 organization: match[1]!,
144 location: match[2]!,
145 bucketId: match[3]!,
146 };
147 };
148
149 const resourceName = (bucket: string, linkId: string) => `${bucket}/links/${linkId}`;
150
151 const isDeleted = (link: logging.Link | undefined): link is undefined =>
152 link === undefined || link.lifecycleState === "DELETE_REQUESTED";
153
154 const isPending = (state: string | undefined) => state === "CREATING" || state === "UPDATING";
155
156 const toAttrs = (link: logging.Link, fallbackBucket: string, project: string) => {
157 const name = link.name ?? "";
158 const parsed = parseLinkName(name);
159 const bucket =
160 parsed !== undefined
161 ? `${parsed.organization}/locations/${parsed.location}/buckets/${parsed.bucketId}`
162 : fallbackBucket;
163 const bucketParsed = parseBucketName(bucket);
164 const description = parseDescription(link.description);
165 const organization = parsed?.organization ?? bucketParsed?.organization ?? "";
166 return {
167 name: name || resourceName(bucket, parsed?.linkId ?? lastSegment(name)),
168 linkId: parsed?.linkId ?? lastSegment(name),
169 bucket,
170 bucketId: parsed?.bucketId ?? bucketParsed?.bucketId ?? "",
171 organization,
172 organizationId: organizationIdOf(organization),
173 project,
174 location: parsed?.location ?? bucketParsed?.location ?? DEFAULT_LOCATION,
175 description: description.description,
176 datasetId: link.bigqueryDataset?.datasetId,
177 lifecycleState: link.lifecycleState,
178 createTime: link.createTime,
179 };
180 };
181
182 const getByName = (name: string) =>
183 logging
184 .getOrganizationsLocationsBucketsLinks({ name })
185 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
186
187 const waitUntilActive = (name: string) =>
188 Effect.gen(function* () {
189 const link = yield* getByName(name);
190 if (isDeleted(link)) {
191 return yield* new OrganizationBucketsLinkNotResolved({ resourceName: name });
192 }
193 if (link.lifecycleState === "FAILED") {
194 return yield* new OrganizationBucketsLinkNotResolved({ resourceName: name });
195 }
196 if (isPending(link.lifecycleState)) {
197 return yield* new OrganizationBucketsLinkNotResolved({ resourceName: name });
198 }
199 return link;
200 }).pipe(
201 Effect.retry({
202 while: (error) => error._tag === "GCP.Logging.OrganizationBucketsLinkNotResolved",
203 times: 10,
204 schedule: Schedule.spaced("2 seconds"),
205 }),
206 );
207
208 const waitUntilDeleted = (name: string) =>
209 getByName(name).pipe(
210 Effect.flatMap((link) =>
211 isDeleted(link)
212 ? Effect.void
213 : Effect.fail(new OrganizationBucketsLinkNotResolved({ resourceName: name })),
214 ),
215 Effect.retry({
216 while: (error) => error._tag === "GCP.Logging.OrganizationBucketsLinkNotResolved",
217 times: 10,
218 schedule: Schedule.spaced("2 seconds"),
219 }),
220 );
221
222 export const OrganizationBucketsLinkProvider = () =>
223 Provider.succeed(OrganizationBucketsLink, {
224 stables: [
225 "name",
226 "linkId",
227 "bucket",
228 "bucketId",
229 "organization",
230 "organizationId",
231 "project",
232 "location",
233 "createTime",
234 ],
235
236 diff: Effect.fn(function* ({ news, olds, output }) {
237 if (!isResolved(news)) return undefined;
238 const previousId = olds?.linkId ?? output?.linkId;
239 const idChanged =
240 previousId !== undefined && news.linkId !== undefined && news.linkId !== previousId;
241 const previousBucket = olds?.bucket ?? output?.bucket;
242 const bucketChanged = previousBucket !== undefined && news.bucket !== previousBucket;
243 const previousOrg = olds?.organization ?? output?.organization;
244 const orgChanged =
245 previousOrg !== undefined &&
246 news.organization !== undefined &&
247 news.organization !== previousOrg;
248 if (!idChanged && !bucketChanged && !orgChanged) return undefined;
249 return { action: "replace" as const, deleteFirst: true };
250 }),
251
252 read: Effect.fn(function* ({ id, olds, output, previous }) {
253 const env = yield* GcpEnvironment.current;
254 yield* resolveOrganization(olds?.organization ?? output?.organization, output?.organization);
255 const linkId = yield* toPhysicalId(id, olds?.linkId, output?.linkId, "l", {
256 delimiter: "_",
257 underscores: true,
258 });
259 const bucket = olds?.bucket ?? output?.bucket ?? "";
260 const name = output?.name ?? resourceName(bucket, linkId);
261 const existing = yield* getByName(name);
262 if (isDeleted(existing)) return undefined;
263 const attrs = toAttrs(existing, bucket, env.project);
264 const { labels } = parseDescription(existing.description);
265 return (yield* hasAlchemyLabels(id, labels))
266 ? attrs
267 : unownedUnlessPrevious(attrs, previous, "name");
268 }),
269
270 list: () =>
271 Effect.gen(function* () {
272 const env = yield* GcpEnvironment.current;
273 const organization = yield* tryResolveOrganization();
274 if (organization === undefined) return [];
275 return yield* logging.listOrganizationsLocationsBuckets
276 .pages({
277 parent: `${organization}/locations/-`,
278 pageSize: 1000,
279 })
280 .pipe(
281 Stream.flatMap((page) => Stream.fromIterable(page.buckets ?? [])),
282 Stream.flatMap(
283 (bucket) =>
284 bucket.name
285 ? logging.listOrganizationsLocationsBucketsLinks
286 .pages({
287 parent: bucket.name,
288 pageSize: 1000,
289 })
290 .pipe(
291 Stream.flatMap((page) => Stream.fromIterable(page.links ?? [])),
292 Stream.filter(
293 (link) => !isDeleted(link) && hasOwnershipMarker(link.description),
294 ),
295 Stream.map((link) => toAttrs(link, bucket.name ?? "", env.project)),
296 Stream.catchTag("NotFound", () => Stream.empty),
297 )
298 : Stream.empty,
299 { concurrency: 4 },
300 ),
301 Stream.runCollect,
302 Effect.map((chunk) => Array.from(chunk)),
303 Effect.catchTag("NotFound", () => Effect.succeed([])),
304 );
305 }),
306
307 reconcile: Effect.fn(function* ({ id, news, output }) {
308 const env = yield* GcpEnvironment.current;
309 yield* resolveOrganization(news.organization, output?.organization);
310 const linkId = yield* toPhysicalId(id, news.linkId, output?.linkId, "l", {
311 delimiter: "_",
312 underscores: true,
313 });
314 const bucket = news.bucket;
315 const name = resourceName(bucket, linkId);
316 const ownership = yield* createInternalLabels(id);
317 const desiredDescription = encodeDescription(ownership, news.description);
318
319 let current = yield* getByName(output?.name ?? name);
320
321 if (isDeleted(current)) {
322 const created = yield* logging
323 .createOrganizationsLocationsBucketsLinks({
324 parent: bucket,
325 linkId,
326 body: {
327 description: desiredDescription,
328 },
329 })
330 .pipe(
331 Effect.flatMap((operation) => waitForOperation(operation)),
332 Effect.flatMap(() => getByName(name)),
333 Effect.catchTag("Conflict", () => getByName(name)),
334 );
335 current = created ?? undefined;
336 if (current !== undefined && isPending(current.lifecycleState)) {
337 current = yield* waitUntilActive(current.name ?? name);
338 }
339 }
340
341 if (isDeleted(current)) {
342 return yield* new OrganizationBucketsLinkNotResolved({ resourceName: name });
343 }
344
345 return toAttrs(current, bucket, env.project);
346 }),
347
348 delete: Effect.fn(function* ({ output }) {
349 if (output.lifecycleState === "DELETE_REQUESTED") return;
350 const operation = yield* logging
351 .deleteOrganizationsLocationsBucketsLinks({ name: output.name })
352 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
353 if (operation !== undefined) {
354 yield* waitForDeleteOperation(operation);
355 }
356 yield* waitUntilDeleted(output.name);
357 }),
358 });