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