@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 507 lines 18.6 KB
Raw
1 import * as logging from "@distilled.cloud/gcp/logging_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 * 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 DEFAULT_RETENTION_DAYS,
16 billingAccountIdOf,
17 billingAccountParent,
18 encodeDescription,
19 hasOwnershipMarker,
20 jsonEqual,
21 lastSegment,
22 lookupProjectBillingAccountId,
23 parseDescription,
24 resolveBillingAccountId,
25 toPhysicalId,
26 } from "./internal.ts";
27 import type { LogBucketCmekSettings, LogBucketIndexConfig } from "./LogBucket.ts";
28
29 export type BillingBucketIndexConfig = LogBucketIndexConfig;
30 export type BillingBucketCmekSettings = LogBucketCmekSettings;
31
32 export type BillingBucketProps = {
33 /**
34 * Bucket id (the `{bucket}` segment of
35 * `billingAccounts/{billingAccount}/locations/{location}/buckets/{bucket}`).
36 * If omitted, a unique name is generated from the stack, stage, and
37 * logical id. Limited to 100 characters: letters, digits, underscores,
38 * hyphens, periods; first character must be alphanumeric. Immutable —
39 * changing it replaces the bucket.
40 */
41 bucketId?: string;
42 /**
43 * Billing account id (`XXXXXX-XXXXXX-XXXXXX` or
44 * `billingAccounts/{id}`). If omitted, Alchemy uses the billing
45 * account linked to the current project. Immutable — changing it
46 * replaces the bucket.
47 */
48 billingAccountId?: string;
49 /**
50 * Location that stores log entries. `global` leaves placement unspecified.
51 * Immutable after create — changing it replaces the bucket.
52 * @default "global"
53 */
54 location?: string;
55 /**
56 * Human-readable description. Log buckets have no labels field, so
57 * Alchemy ownership is stored in a `[alchemy …]` prefix and stripped
58 * from attributes.
59 */
60 description?: string;
61 /**
62 * Days to retain log entries before they are deleted. Minimum 1. `0` at
63 * create uses the GCP default of 30. Cannot be changed while the bucket
64 * is locked.
65 * @default 30
66 */
67 retentionDays?: number;
68 /**
69 * When true, retention cannot be reduced and the bucket can only be
70 * deleted if empty. Locking cannot be undone.
71 * @default false
72 */
73 locked?: boolean;
74 /**
75 * Enable Log Analytics. Once enabled, it cannot be disabled.
76 * @default false
77 */
78 analyticsEnabled?: boolean;
79 /**
80 * Log entry field paths denied in this bucket (e.g. `textPayload`,
81 * `jsonPayload`).
82 */
83 restrictedFields?: string[];
84 /**
85 * Custom indexed fields. Automatically indexed paths cannot be changed.
86 */
87 indexConfigs?: BillingBucketIndexConfig[];
88 /**
89 * Customer-managed encryption for new log entries. Once configured,
90 * CMEK cannot be disabled.
91 */
92 cmekSettings?: BillingBucketCmekSettings;
93 };
94
95 export type BillingBucket = Resource<
96 "GCP.Logging.BillingBucket",
97 BillingBucketProps,
98 {
99 /** Full resource name `billingAccounts/{billingAccount}/locations/{location}/buckets/{bucketId}`. */
100 name: string;
101 /** Bucket id (last path segment). */
102 bucketId: string;
103 /** Billing account id. */
104 billingAccountId: string;
105 /** Location that stores log entries. */
106 location: string;
107 /** User description with the Alchemy ownership prefix stripped. */
108 description: string | undefined;
109 /** Retention period in days. */
110 retentionDays: number;
111 /** Whether the bucket is locked. */
112 locked: boolean;
113 /** Whether Log Analytics is enabled. */
114 analyticsEnabled: boolean;
115 /** Restricted field paths. */
116 restrictedFields: string[];
117 /** Custom index configs. */
118 indexConfigs: BillingBucketIndexConfig[];
119 /** CMEK key name, if configured. */
120 cmekSettings: BillingBucketCmekSettings | undefined;
121 /** Bucket lifecycle (`ACTIVE`, `DELETE_REQUESTED`, …). */
122 lifecycleState: string | undefined;
123 /** RFC3339 creation timestamp. Unset on GCP default buckets. */
124 createTime: string | undefined;
125 /** RFC3339 last-update timestamp. */
126 updateTime: string | undefined;
127 },
128 never,
129 Providers
130 >;
131
132 /**
133 * A Cloud Logging log bucket owned by a billing account.
134 *
135 * Log buckets have no labels field, so Alchemy stamps ownership into the
136 * description for `list` / nuke. `bucketId`, `location`, and
137 * `billingAccountId` are identity — changing any replaces the bucket.
138 * Delete marks the bucket `DELETE_REQUESTED`; GCP purges it after 7 days.
139 * Reconcile undeletes a bucket still in that grace period so the same
140 * name can be reused.
141 *
142 * ### Creating a Billing Log Bucket
143 * **Example:** Generated name in `global`
144 * ```typescript
145 * const bucket = yield* GCP.Logging.BillingBucket("AppLogs", {
146 * description: "application logs",
147 * });
148 * ```
149 *
150 * **Example:** Named bucket with custom retention
151 * ```typescript
152 * const bucket = yield* GCP.Logging.BillingBucket("AppLogs", {
153 * billingAccountId: "AAAAAA-BBBBBB-CCCCCC",
154 * bucketId: "app-logs",
155 * location: "global",
156 * description: "application logs",
157 * retentionDays: 31,
158 * });
159 * ```
160 *
161 * ### Updating a Billing Log Bucket
162 * **Example:** Change description and retention
163 * ```typescript
164 * const bucket = yield* GCP.Logging.BillingBucket("AppLogs", {
165 * location: "global",
166 * description: "retained application logs",
167 * retentionDays: 60,
168 * });
169 * ```
170 *
171 * @resource
172 * @category Logging
173 */
174 export const BillingBucket = Resource<BillingBucket>("GCP.Logging.BillingBucket");
175
176 export class BillingBucketNotResolved extends Data.TaggedError(
177 "GCP.Logging.BillingBucketNotResolved",
178 )<{
179 resourceName: string;
180 }> {}
181
182 export class BillingBucketFailed extends Data.TaggedError("GCP.Logging.BillingBucketFailed")<{
183 resourceName: string;
184 state: string | undefined;
185 }> {}
186
187 const resourceName = (billingAccountId: string, location: string, bucketId: string) =>
188 `${billingAccountParent(billingAccountId)}/locations/${location}/buckets/${bucketId}`;
189
190 const parseBucketName = (name: string) => {
191 const match = name.match(/^billingAccounts\/([^/]+)\/locations\/([^/]+)\/buckets\/([^/]+)$/);
192 if (!match) return undefined;
193 return {
194 billingAccountId: match[1]!,
195 location: match[2]!,
196 bucketId: match[3]!,
197 };
198 };
199
200 const bucketIdOf = (bucket: logging.LogBucket, fallback?: string) => {
201 const parsed = parseBucketName(bucket.name ?? "");
202 return parsed?.bucketId ?? fallback ?? lastSegment(bucket.name ?? "");
203 };
204
205 const locationOf = (bucket: logging.LogBucket, fallback: string) => {
206 const parsed = parseBucketName(bucket.name ?? "");
207 return parsed?.location ?? fallback;
208 };
209
210 const isDeleted = (bucket: logging.LogBucket | undefined): bucket is undefined =>
211 bucket === undefined || bucket.lifecycleState === "DELETE_REQUESTED";
212
213 const isPending = (state: string | undefined) => state === "CREATING" || state === "UPDATING";
214
215 const canonRestricted = (fields: readonly string[] | undefined) =>
216 [...(fields ?? [])].slice().sort();
217
218 const canonIndexConfigs = (
219 configs: readonly logging.IndexConfig[] | readonly BillingBucketIndexConfig[] | undefined,
220 ): BillingBucketIndexConfig[] =>
221 [...(configs ?? [])]
222 .flatMap((config) =>
223 config.fieldPath
224 ? [
225 {
226 fieldPath: config.fieldPath,
227 type: (config.type ?? "INDEX_TYPE_STRING") as BillingBucketIndexConfig["type"],
228 },
229 ]
230 : [],
231 )
232 .sort((left, right) => left.fieldPath.localeCompare(right.fieldPath));
233
234 const toAttrs = (bucket: logging.LogBucket, billingAccountId: string, location: string) => {
235 const parsedName = parseBucketName(bucket.name ?? "");
236 const bucketId = bucketIdOf(bucket);
237 const resolvedLocation = locationOf(bucket, location);
238 const parsed = parseDescription(bucket.description);
239 const cmekKey = bucket.cmekSettings?.kmsKeyName;
240 const account = parsedName?.billingAccountId ?? billingAccountId;
241 return {
242 name: bucket.name ?? (bucketId ? resourceName(account, resolvedLocation, bucketId) : ""),
243 bucketId,
244 billingAccountId: account,
245 location: resolvedLocation,
246 description: parsed.description,
247 retentionDays: bucket.retentionDays ?? DEFAULT_RETENTION_DAYS,
248 locked: bucket.locked === true,
249 analyticsEnabled: bucket.analyticsEnabled === true,
250 restrictedFields: [...(bucket.restrictedFields ?? [])],
251 indexConfigs: canonIndexConfigs(bucket.indexConfigs),
252 cmekSettings: cmekKey ? { kmsKeyName: cmekKey } : undefined,
253 lifecycleState: bucket.lifecycleState,
254 createTime: bucket.createTime,
255 updateTime: bucket.updateTime,
256 };
257 };
258
259 const getByName = (name: string) =>
260 logging
261 .getBillingAccountsLocationsBuckets({ name })
262 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
263
264 const undelete = (name: string) =>
265 logging
266 .undeleteBillingAccountsLocationsBuckets({ name, body: {} })
267 .pipe(Effect.catchTag(["NotFound", "Conflict"], () => Effect.void));
268
269 const waitUntilActive = (name: string) =>
270 Effect.gen(function* () {
271 const bucket = yield* getByName(name);
272 if (bucket === undefined || isDeleted(bucket)) {
273 return yield* new BillingBucketNotResolved({ resourceName: name });
274 }
275 if (bucket.lifecycleState === "FAILED") {
276 return yield* new BillingBucketFailed({
277 resourceName: name,
278 state: bucket.lifecycleState,
279 });
280 }
281 if (isPending(bucket.lifecycleState)) {
282 return yield* new BillingBucketNotResolved({ resourceName: name });
283 }
284 return bucket;
285 }).pipe(
286 Effect.retry({
287 while: (error) => error._tag === "GCP.Logging.BillingBucketNotResolved",
288 times: 8,
289 schedule: Schedule.spaced("1 second"),
290 }),
291 );
292
293 const waitUntilDeleted = (name: string) =>
294 getByName(name).pipe(
295 Effect.flatMap((bucket) =>
296 isDeleted(bucket)
297 ? Effect.void
298 : Effect.fail(new BillingBucketNotResolved({ resourceName: name })),
299 ),
300 Effect.retry({
301 while: (error) => error._tag === "GCP.Logging.BillingBucketNotResolved",
302 times: 8,
303 schedule: Schedule.spaced("1 second"),
304 }),
305 );
306
307 const toCreateBody = (props: BillingBucketProps, description: string): logging.LogBucket => ({
308 description,
309 retentionDays: props.retentionDays,
310 locked: props.locked === true ? true : undefined,
311 analyticsEnabled: props.analyticsEnabled === true ? true : undefined,
312 restrictedFields:
313 props.restrictedFields && props.restrictedFields.length > 0
314 ? [...props.restrictedFields]
315 : undefined,
316 indexConfigs:
317 props.indexConfigs && props.indexConfigs.length > 0
318 ? props.indexConfigs.map((config) => ({
319 fieldPath: config.fieldPath,
320 type: config.type,
321 }))
322 : undefined,
323 cmekSettings: props.cmekSettings ? { kmsKeyName: props.cmekSettings.kmsKeyName } : undefined,
324 });
325
326 export const BillingBucketProvider = () =>
327 Provider.succeed(BillingBucket, {
328 stables: ["name", "bucketId", "billingAccountId", "location", "createTime"],
329
330 diff: Effect.fn(function* ({ news, olds, output }) {
331 if (!isResolved(news)) return undefined;
332 const previousId = olds?.bucketId ?? output?.bucketId;
333 const idChanged =
334 previousId !== undefined && news.bucketId !== undefined && news.bucketId !== previousId;
335 const previousLocation = olds?.location ?? output?.location;
336 const locationChanged =
337 previousLocation !== undefined &&
338 news.location !== undefined &&
339 news.location !== previousLocation;
340 const previousAccount = olds?.billingAccountId ?? output?.billingAccountId;
341 const accountChanged =
342 previousAccount !== undefined &&
343 news.billingAccountId !== undefined &&
344 billingAccountIdOf(news.billingAccountId) !== billingAccountIdOf(previousAccount);
345 if (!idChanged && !locationChanged && !accountChanged) return undefined;
346 return { action: "replace" as const, deleteFirst: false };
347 }),
348
349 read: Effect.fn(function* ({ id, olds, output }) {
350 const billingAccountId = yield* resolveBillingAccountId(
351 olds?.billingAccountId,
352 output?.billingAccountId,
353 );
354 const location = olds?.location ?? output?.location ?? DEFAULT_LOCATION;
355 const bucketId = yield* toPhysicalId(id, olds?.bucketId, output?.bucketId, "b");
356 const name = output?.name ?? resourceName(billingAccountId, location, bucketId);
357 const existing = yield* getByName(name);
358 if (isDeleted(existing)) return undefined;
359 const attrs = toAttrs(existing, billingAccountId, location);
360 const { labels } = parseDescription(existing.description);
361 return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs);
362 }),
363
364 list: () =>
365 Effect.gen(function* () {
366 const env = yield* GcpEnvironment.current;
367 const billingAccountId = yield* lookupProjectBillingAccountId(env.project);
368 if (billingAccountId === undefined) return [];
369 return yield* logging.listBillingAccountsLocationsBuckets
370 .pages({
371 parent: `${billingAccountParent(billingAccountId)}/locations/-`,
372 pageSize: 1000,
373 })
374 .pipe(
375 Stream.flatMap((page) => Stream.fromIterable(page.buckets ?? [])),
376 Stream.filter((bucket) => !isDeleted(bucket) && hasOwnershipMarker(bucket.description)),
377 Stream.map((bucket) =>
378 toAttrs(bucket, billingAccountId, locationOf(bucket, DEFAULT_LOCATION)),
379 ),
380 Stream.runCollect,
381 Effect.map((chunk) => Array.from(chunk)),
382 Effect.catchTag("NotFound", () => Effect.succeed([] as BillingBucket["Attributes"][])),
383 );
384 }),
385
386 reconcile: Effect.fn(function* ({ id, news, output }) {
387 const billingAccountId = yield* resolveBillingAccountId(
388 news.billingAccountId,
389 output?.billingAccountId,
390 );
391 const location = news.location ?? output?.location ?? DEFAULT_LOCATION;
392 const bucketId = yield* toPhysicalId(id, news.bucketId, output?.bucketId, "b");
393 const name = resourceName(billingAccountId, location, bucketId);
394 const ownership = yield* createInternalLabels(id);
395 const desiredDescription = encodeDescription(ownership, news.description);
396
397 let current = yield* getByName(output?.name ?? name);
398
399 if (current !== undefined && current.lifecycleState === "DELETE_REQUESTED") {
400 yield* undelete(current.name ?? name);
401 current = yield* waitUntilActive(current.name ?? name);
402 }
403
404 if (current === undefined) {
405 const created = yield* logging
406 .createBillingAccountsLocationsBuckets({
407 parent: `${billingAccountParent(billingAccountId)}/locations/${location}`,
408 bucketId,
409 body: toCreateBody(news, desiredDescription),
410 })
411 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
412 current = created ?? undefined;
413 if (current !== undefined && current.lifecycleState === "DELETE_REQUESTED") {
414 yield* undelete(current.name ?? name);
415 current = yield* waitUntilActive(current.name ?? name);
416 } else if (current !== undefined && isPending(current.lifecycleState)) {
417 current = yield* waitUntilActive(current.name ?? name);
418 }
419 }
420
421 if (isDeleted(current)) {
422 return yield* new BillingBucketNotResolved({ resourceName: name });
423 }
424
425 const desiredLocked = news.locked === true;
426 const desiredAnalytics = news.analyticsEnabled === true;
427 const descriptionChanged = (current.description ?? "") !== desiredDescription;
428 const retentionChanged =
429 news.retentionDays !== undefined &&
430 current.locked !== true &&
431 (current.retentionDays ?? DEFAULT_RETENTION_DAYS) !== news.retentionDays;
432 const restrictedChanged =
433 news.restrictedFields !== undefined &&
434 !jsonEqual(
435 canonRestricted(current.restrictedFields),
436 canonRestricted(news.restrictedFields),
437 );
438 const indexChanged =
439 news.indexConfigs !== undefined &&
440 !jsonEqual(canonIndexConfigs(current.indexConfigs), canonIndexConfigs(news.indexConfigs));
441 const analyticsChanged =
442 news.analyticsEnabled !== undefined &&
443 desiredAnalytics &&
444 current.analyticsEnabled !== true;
445 const cmekChanged =
446 news.cmekSettings !== undefined &&
447 (current.cmekSettings?.kmsKeyName ?? "") !== news.cmekSettings.kmsKeyName;
448 const lockedChanged = news.locked !== undefined && desiredLocked && current.locked !== true;
449
450 const syncMask = [
451 descriptionChanged ? "description" : undefined,
452 retentionChanged ? "retentionDays" : undefined,
453 restrictedChanged ? "restrictedFields" : undefined,
454 indexChanged ? "indexConfigs" : undefined,
455 analyticsChanged ? "analyticsEnabled" : undefined,
456 cmekChanged ? "cmekSettings" : undefined,
457 ].filter((field): field is string => field !== undefined);
458
459 if (syncMask.length > 0) {
460 const body: logging.LogBucket = {};
461 if (descriptionChanged) body.description = desiredDescription;
462 if (retentionChanged) body.retentionDays = news.retentionDays;
463 if (restrictedChanged) {
464 body.restrictedFields = [...(news.restrictedFields ?? [])];
465 }
466 if (indexChanged) {
467 body.indexConfigs = (news.indexConfigs ?? []).map((config) => ({
468 fieldPath: config.fieldPath,
469 type: config.type,
470 }));
471 }
472 if (analyticsChanged) body.analyticsEnabled = true;
473 if (cmekChanged) {
474 body.cmekSettings = { kmsKeyName: news.cmekSettings?.kmsKeyName };
475 }
476 current = yield* logging.patchBillingAccountsLocationsBuckets({
477 name: current.name ?? name,
478 updateMask: syncMask.join(","),
479 body,
480 });
481 if (isPending(current.lifecycleState)) {
482 current = yield* waitUntilActive(current.name ?? name);
483 }
484 }
485
486 if (lockedChanged) {
487 current = yield* logging.patchBillingAccountsLocationsBuckets({
488 name: current.name ?? name,
489 updateMask: "locked",
490 body: { locked: true },
491 });
492 if (isPending(current.lifecycleState)) {
493 current = yield* waitUntilActive(current.name ?? name);
494 }
495 }
496
497 return toAttrs(current, billingAccountId, location);
498 }),
499
500 delete: Effect.fn(function* ({ output }) {
501 if (output.lifecycleState === "DELETE_REQUESTED") return;
502 yield* logging
503 .deleteBillingAccountsLocationsBuckets({ name: output.name })
504 .pipe(Effect.catchTag("NotFound", () => Effect.void));
505 yield* waitUntilDeleted(output.name);
506 }),
507 });