@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 406 lines 14.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 Stream from "effect/Stream";
9 import { GcpEnvironment } from "../Environment.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 createOwnership,
13 encodeDescription,
14 exclusionsOf,
15 hasOwnershipMarker,
16 lastSegment,
17 ownedBy,
18 parseDescription,
19 parseLoggingName,
20 sameExclusions,
21 scopeParent,
22 toExclusionsBody,
23 toPhysicalId,
24 type SinkExclusion,
25 } from "./internal.ts";
26
27 export type FolderSinkExclusion = SinkExclusion;
28
29 export type FolderSinkBigQueryOptions = {
30 /**
31 * Use BigQuery partitioned tables instead of dated tables
32 * (`syslog_20170523`). Only applies when `destination` is a BigQuery
33 * dataset.
34 * @default false
35 */
36 usePartitionedTables?: boolean;
37 };
38
39 export type FolderSinkProps = {
40 /**
41 * Folder id (`folders/{folder}` or the numeric id). When omitted, the
42 * stack project is used. Immutable — changing it replaces the sink.
43 */
44 folderId?: string;
45 /**
46 * Sink id (the `{sink}` segment of `{parent}/sinks/{sink}`). If omitted,
47 * a unique name is generated from the stack, stage, and logical id.
48 * Limited to 100 characters: letters, digits, underscores, hyphens,
49 * periods; first character must be alphanumeric. Immutable — changing
50 * it replaces the sink.
51 */
52 sinkId?: string;
53 /**
54 * Export destination. One of:
55 * `storage.googleapis.com/[GCS_BUCKET]`,
56 * `bigquery.googleapis.com/projects/[PROJECT_ID]/datasets/[DATASET]`,
57 * `pubsub.googleapis.com/projects/[PROJECT_ID]/topics/[TOPIC_ID]`,
58 * `logging.googleapis.com/projects/[PROJECT_ID]`,
59 * `logging.googleapis.com/projects/[PROJECT_ID]/locations/[LOCATION_ID]/buckets/[BUCKET_ID]`.
60 */
61 destination: string;
62 /**
63 * Advanced logs filter. Only matching entries in the sink's parent are
64 * exported. Empty or omitted exports every entry.
65 */
66 filter?: string;
67 /**
68 * Human-readable description (max 8000 characters). Logging sinks have
69 * no labels field, so Alchemy ownership is stored in a `[alchemy …]`
70 * prefix and stripped from attributes.
71 */
72 description?: string;
73 /**
74 * When true, the sink exists but does not export any log entries.
75 * @default false
76 */
77 disabled?: boolean;
78 /**
79 * Exclusion filters. An entry matching `filter` and any exclusion is
80 * not exported.
81 */
82 exclusions?: FolderSinkExclusion[];
83 /**
84 * BigQuery-specific export options. Ignored unless `destination` is a
85 * BigQuery dataset.
86 */
87 bigqueryOptions?: FolderSinkBigQueryOptions;
88 /**
89 * When true, Cloud Logging assigns a unique writer service account as
90 * `writerIdentity`. Folder sinks always receive a writer identity.
91 */
92 uniqueWriterIdentity?: boolean;
93 /**
94 * Caller-provided writer service account
95 * (`serviceAccount:some@email`). Only valid when routing to a log
96 * bucket in a different project.
97 */
98 customWriterIdentity?: string;
99 /**
100 * When true, logs from child projects, folders, and billing accounts
101 * are also eligible for export. Folder/org sinks only.
102 * @default false
103 */
104 includeChildren?: boolean;
105 /**
106 * When true (requires `includeChildren`), matching logs are intercepted
107 * and omitted from non-`_Required` child sinks. Folder/org sinks only.
108 * @default false
109 */
110 interceptChildren?: boolean;
111 };
112
113 export type FolderSink = Resource<
114 "GCP.Logging.FolderSink",
115 FolderSinkProps,
116 {
117 /** Full resource name `{parent}/sinks/{sink}`. */
118 name: string;
119 /** Sink id (last path segment). */
120 sinkId: string;
121 /** Parent resource (`folders/{folder}` or `projects/{project}`). */
122 parent: string;
123 /** Folder id when the parent is a folder. */
124 folderId: string | undefined;
125 /** Export destination. */
126 destination: string;
127 /** Advanced logs filter, if set. */
128 filter: string | undefined;
129 /** User description with the Alchemy ownership prefix stripped. */
130 description: string | undefined;
131 /** Whether the sink is disabled. */
132 disabled: boolean;
133 /** Exclusion filters. */
134 exclusions: FolderSinkExclusion[];
135 /** BigQuery export options, if set. */
136 bigqueryOptions: FolderSinkBigQueryOptions | undefined;
137 /** Whether child resources are included (org/folder sinks). */
138 includeChildren: boolean;
139 /** Whether matching child logs are intercepted (org/folder sinks). */
140 interceptChildren: boolean;
141 /**
142 * IAM identity Cloud Logging uses to write to `destination`. Empty
143 * for same-project log-bucket destinations.
144 */
145 writerIdentity: string | undefined;
146 /** RFC3339 creation timestamp. */
147 createTime: string | undefined;
148 /** RFC3339 last-update timestamp. */
149 updateTime: string | undefined;
150 },
151 never,
152 Providers
153 >;
154
155 /**
156 * A Cloud Logging sink owned by a folder (or the stack project).
157 *
158 * Logging sinks have no labels field, so Alchemy stamps ownership into
159 * the description for `list` / nuke. `folderId` and `sinkId` are identity
160 * — changing either replaces the sink. Filter, destination, description,
161 * disabled flag, exclusions, and BigQuery options update in place.
162 *
163 * ### Creating a Folder Sink
164 * **Example:** Route errors to the `_Default` log bucket
165 * ```typescript
166 * const sink = yield* GCP.Logging.FolderSink("Errors", {
167 * destination:
168 * "logging.googleapis.com/projects/my-project/locations/global/buckets/_Default",
169 * filter: "severity>=ERROR",
170 * });
171 * ```
172 *
173 * **Example:** Folder-owned sink including children
174 * ```typescript
175 * const sink = yield* GCP.Logging.FolderSink("Errors", {
176 * folderId: "123456789",
177 * destination:
178 * "logging.googleapis.com/projects/my-project/locations/global/buckets/_Default",
179 * filter: "severity>=ERROR",
180 * includeChildren: true,
181 * uniqueWriterIdentity: true,
182 * });
183 * ```
184 *
185 * ### Updating a Folder Sink
186 * **Example:** Change the filter
187 * ```typescript
188 * const sink = yield* GCP.Logging.FolderSink("Errors", {
189 * destination:
190 * "logging.googleapis.com/projects/my-project/locations/global/buckets/_Default",
191 * filter: "severity>=WARNING",
192 * description: "warnings and errors",
193 * });
194 * ```
195 *
196 * @resource
197 * @category Logging
198 */
199 export const FolderSink = Resource<FolderSink>("GCP.Logging.FolderSink");
200
201 export class FolderSinkNotResolved extends Data.TaggedError("GCP.Logging.FolderSinkNotResolved")<{
202 resourceName: string;
203 }> {}
204
205 const resourceName = (parent: string, sinkId: string) => `${parent}/sinks/${sinkId}`;
206
207 const folderIdOf = (parent: string) =>
208 parent.startsWith("folders/") ? lastSegment(parent) : undefined;
209
210 const sinkIdOf = (sink: logging.LogSink) => {
211 const raw = sink.name ?? sink.resourceName ?? "";
212 const parsed = parseLoggingName(raw);
213 return parsed.sinkId ?? (raw.includes("/") ? lastSegment(raw) : raw);
214 };
215
216 const toAttrs = (sink: logging.LogSink, parent: string) => {
217 const sinkId = sinkIdOf(sink);
218 const parsed = parseDescription(sink.description);
219 const name = sink.resourceName ?? (sinkId ? resourceName(parent, sinkId) : "");
220 const resolvedParent = parseLoggingName(name).parent || parent;
221 const bq = sink.bigqueryOptions;
222 return {
223 name,
224 sinkId,
225 parent: resolvedParent,
226 folderId: folderIdOf(resolvedParent),
227 destination: sink.destination ?? "",
228 filter: sink.filter,
229 description: parsed.description,
230 disabled: sink.disabled === true,
231 exclusions: exclusionsOf(sink.exclusions),
232 bigqueryOptions:
233 bq?.usePartitionedTables !== undefined
234 ? { usePartitionedTables: bq.usePartitionedTables }
235 : undefined,
236 includeChildren: sink.includeChildren === true,
237 interceptChildren: sink.interceptChildren === true,
238 writerIdentity: sink.writerIdentity,
239 createTime: sink.createTime,
240 updateTime: sink.updateTime,
241 };
242 };
243
244 const getByName = (name: string) =>
245 logging
246 .getFoldersSinks({ sinkName: name })
247 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
248
249 const toCreateBody = (
250 sinkId: string,
251 props: FolderSinkProps,
252 description: string,
253 ): logging.LogSink => ({
254 name: sinkId,
255 destination: props.destination,
256 filter: props.filter,
257 description,
258 disabled: props.disabled === true ? true : undefined,
259 exclusions: toExclusionsBody(props.exclusions),
260 bigqueryOptions: props.bigqueryOptions,
261 includeChildren: props.includeChildren === true ? true : undefined,
262 interceptChildren: props.interceptChildren === true ? true : undefined,
263 });
264
265 export const FolderSinkProvider = () =>
266 Provider.succeed(FolderSink, {
267 stables: ["name", "sinkId", "parent", "folderId", "createTime"],
268
269 diff: Effect.fn(function* ({ news, olds, output }) {
270 if (!isResolved(news)) return undefined;
271 const previous = olds?.sinkId ?? output?.sinkId;
272 const idChanged =
273 previous !== undefined && news.sinkId !== undefined && news.sinkId !== previous;
274 const previousFolder = olds?.folderId ?? output?.folderId;
275 const folderChanged = news.folderId !== undefined && news.folderId !== previousFolder;
276 if (!idChanged && !folderChanged) return undefined;
277 return { action: "replace" as const, deleteFirst: false };
278 }),
279
280 read: Effect.fn(function* ({ id, olds, output }) {
281 const env = yield* GcpEnvironment.current;
282 const parent = scopeParent(env.project, olds?.folderId ?? output?.folderId);
283 const sinkId = yield* toPhysicalId(id, olds?.sinkId, output?.sinkId, "s");
284 const name = output?.name ?? resourceName(parent, sinkId);
285 const existing = yield* getByName(name);
286 if (existing === undefined) return undefined;
287 const attrs = toAttrs(existing, parent);
288 const { labels } = parseDescription(existing.description);
289 return (yield* ownedBy(id, labels)) ? attrs : Unowned(attrs);
290 }),
291
292 list: () =>
293 Effect.gen(function* () {
294 const env = yield* GcpEnvironment.current;
295 return yield* logging.listFoldersSinks
296 .pages({
297 parent: `projects/${env.project}`,
298 pageSize: 1000,
299 })
300 .pipe(
301 Stream.flatMap((page) => Stream.fromIterable(page.sinks ?? [])),
302 Stream.filter((sink) => hasOwnershipMarker(sink.description)),
303 Stream.map((sink) => toAttrs(sink, `projects/${env.project}`)),
304 Stream.runCollect,
305 Effect.map((chunk) => Array.from(chunk)),
306 );
307 }),
308
309 reconcile: Effect.fn(function* ({ id, news, output }) {
310 const env = yield* GcpEnvironment.current;
311 const parent = scopeParent(env.project, news.folderId ?? output?.folderId);
312 const sinkId = yield* toPhysicalId(id, news.sinkId, output?.sinkId, "s");
313 const name = resourceName(parent, sinkId);
314 const ownership = yield* createOwnership(id);
315 const desiredDescription = encodeDescription(ownership, news.description);
316 const uniqueWriterIdentity =
317 news.uniqueWriterIdentity ?? (news.folderId !== undefined ? true : undefined);
318
319 let current = yield* getByName(output?.name ?? name);
320
321 if (current === undefined) {
322 const created = yield* logging
323 .createFoldersSinks({
324 parent,
325 uniqueWriterIdentity,
326 customWriterIdentity: news.customWriterIdentity,
327 body: toCreateBody(sinkId, news, desiredDescription),
328 })
329 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
330 current = created ?? undefined;
331 }
332
333 if (current === undefined) {
334 return yield* new FolderSinkNotResolved({ resourceName: name });
335 }
336
337 const desiredDisabled = news.disabled === true;
338 const desiredIncludeChildren = news.includeChildren === true;
339 const desiredInterceptChildren = news.interceptChildren === true;
340 const desiredBq = news.bigqueryOptions?.usePartitionedTables === true;
341 const observedBq = current.bigqueryOptions?.usePartitionedTables === true;
342
343 const destinationChanged = (current.destination ?? "") !== news.destination;
344 const filterChanged = (current.filter ?? "") !== (news.filter ?? "");
345 const descriptionChanged = (current.description ?? "") !== desiredDescription;
346 const disabledChanged = (current.disabled === true) !== desiredDisabled;
347 const exclusionsChanged = !sameExclusions(current.exclusions, news.exclusions);
348 const bqChanged = news.bigqueryOptions !== undefined && desiredBq !== observedBq;
349 const includeChildrenChanged =
350 news.includeChildren !== undefined &&
351 (current.includeChildren === true) !== desiredIncludeChildren;
352 const interceptChildrenChanged =
353 news.interceptChildren !== undefined &&
354 (current.interceptChildren === true) !== desiredInterceptChildren;
355
356 const updateMask = [
357 destinationChanged ? "destination" : undefined,
358 filterChanged ? "filter" : undefined,
359 descriptionChanged ? "description" : undefined,
360 disabledChanged ? "disabled" : undefined,
361 exclusionsChanged ? "exclusions" : undefined,
362 bqChanged ? "bigqueryOptions.usePartitionedTables" : undefined,
363 includeChildrenChanged ? "includeChildren" : undefined,
364 interceptChildrenChanged ? "interceptChildren" : undefined,
365 ].filter((field): field is string => field !== undefined);
366
367 if (updateMask.length > 0) {
368 const body: logging.LogSink = {
369 name: sinkId,
370 destination: news.destination,
371 filter: news.filter ?? "",
372 description: desiredDescription,
373 disabled: desiredDisabled,
374 };
375 if (exclusionsChanged) {
376 body.exclusions = toExclusionsBody(news.exclusions) ?? [];
377 }
378 if (bqChanged) {
379 body.bigqueryOptions = {
380 usePartitionedTables: desiredBq,
381 };
382 }
383 if (includeChildrenChanged) {
384 body.includeChildren = desiredIncludeChildren;
385 }
386 if (interceptChildrenChanged) {
387 body.interceptChildren = desiredInterceptChildren;
388 }
389 current = yield* logging.patchFoldersSinks({
390 sinkName: current.resourceName ?? name,
391 uniqueWriterIdentity,
392 customWriterIdentity: news.customWriterIdentity,
393 updateMask: updateMask.join(","),
394 body,
395 });
396 }
397
398 return toAttrs(current, parent);
399 }),
400
401 delete: Effect.fn(function* ({ output }) {
402 yield* logging
403 .deleteFoldersSinks({ sinkName: output.name })
404 .pipe(Effect.catchTag("NotFound", () => Effect.void));
405 }),
406 });