The best way to control Claude Code, Codex and more from any computer or phone

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