The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as logging from "@distilled.cloud/gcp/logging_v2"; |
| 2 | import { unownedUnlessPrevious } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { GcpEnvironment } from "../Environment.ts"; |
| 12 | import { createInternalLabels, hasAlchemyLabels } from "../Labels.ts"; |
| 13 | import type { Providers } from "../Providers.ts"; |
| 14 | import { listProjectBuckets, waitForDeleteOperation, waitForOperation } from "./operations.ts"; |
| 15 | import { |
| 16 | encodeDescription, |
| 17 | hasOwnershipMarker, |
| 18 | lastSegment, |
| 19 | parseDescription, |
| 20 | } from "./ownership.ts"; |
| 21 | |
| 22 | const MAX_NAME_LENGTH = 100; |
| 23 | const DEFAULT_LOCATION = "global"; |
| 24 | |
| 25 | export type BucketsLinkProps = { |
| 26 | /** |
| 27 | * Parent analytics-enabled log bucket. Full name |
| 28 | * `projects/{project}/locations/{location}/buckets/{bucket}` or the |
| 29 | * bucket id (combined with `location`). A bucket may have at most one |
| 30 | * link. Immutable — changing it replaces the link (delete-first). |
| 31 | */ |
| 32 | bucket: string; |
| 33 | /** |
| 34 | * Link id (the `{link}` segment of |
| 35 | * `.../buckets/{bucket}/links/{link}`). Also used as the BigQuery |
| 36 | * dataset id, so it may contain only letters, digits, and underscores |
| 37 | * and must start with a letter. If omitted, a unique name is generated. |
| 38 | * Immutable — changing it replaces the link. |
| 39 | */ |
| 40 | linkId?: string; |
| 41 | /** |
| 42 | * Bucket location used when `bucket` is a bare id. Immutable — changing |
| 43 | * it replaces the link. |
| 44 | * @default "global" |
| 45 | */ |
| 46 | location?: string; |
| 47 | /** |
| 48 | * Human-readable description (max 8000 characters). Links have no |
| 49 | * labels field and no update API, so Alchemy stamps ownership into the |
| 50 | * description at create and cannot change it in place. |
| 51 | */ |
| 52 | description?: string; |
| 53 | }; |
| 54 | |
| 55 | export type BucketsLink = Resource< |
| 56 | "GCP.Logging.BucketsLink", |
| 57 | BucketsLinkProps, |
| 58 | { |
| 59 | /** Full resource name `projects/{project}/locations/{location}/buckets/{bucket}/links/{linkId}`. */ |
| 60 | name: string; |
| 61 | /** Link id (last path segment). */ |
| 62 | linkId: string; |
| 63 | /** Parent log bucket resource name. */ |
| 64 | bucket: string; |
| 65 | /** Bucket id. */ |
| 66 | bucketId: string; |
| 67 | /** Project id. */ |
| 68 | project: string; |
| 69 | /** Location of the parent bucket. */ |
| 70 | location: string; |
| 71 | /** User description with the Alchemy ownership prefix stripped. */ |
| 72 | description: string | undefined; |
| 73 | /** BigQuery dataset id created for this link. */ |
| 74 | datasetId: string | undefined; |
| 75 | /** Link lifecycle (`ACTIVE`, `DELETE_REQUESTED`, …). */ |
| 76 | lifecycleState: string | undefined; |
| 77 | /** RFC3339 creation timestamp. */ |
| 78 | createTime: string | undefined; |
| 79 | }, |
| 80 | never, |
| 81 | Providers |
| 82 | >; |
| 83 | |
| 84 | /** |
| 85 | * A Cloud Logging link that creates a BigQuery dataset over an |
| 86 | * analytics-enabled log bucket. |
| 87 | * |
| 88 | * A bucket may have only one link. Create and delete are long-running |
| 89 | * operations; the matching BigQuery dataset is created and deleted with |
| 90 | * the link. Links have no update API and no labels field — Alchemy |
| 91 | * stamps ownership into the description at create. `linkId` and the |
| 92 | * parent bucket are identity; replacement is delete-first because a |
| 93 | * bucket cannot hold two links. |
| 94 | * |
| 95 | * ### Creating a Link |
| 96 | * **Example:** Link an analytics bucket to BigQuery |
| 97 | * ```typescript |
| 98 | * const bucket = yield* GCP.Logging.LogBucket("AppLogs", { |
| 99 | * analyticsEnabled: true, |
| 100 | * description: "analytics logs", |
| 101 | * }); |
| 102 | * const link = yield* GCP.Logging.BucketsLink("Analytics", { |
| 103 | * bucket: bucket.name, |
| 104 | * description: "bigquery analytics", |
| 105 | * }); |
| 106 | * ``` |
| 107 | * |
| 108 | * **Example:** Named link |
| 109 | * ```typescript |
| 110 | * const link = yield* GCP.Logging.BucketsLink("Analytics", { |
| 111 | * bucket: bucket.name, |
| 112 | * linkId: "app_logs", |
| 113 | * description: "bigquery analytics", |
| 114 | * }); |
| 115 | * ``` |
| 116 | * |
| 117 | * @resource |
| 118 | * @category Logging |
| 119 | */ |
| 120 | export const BucketsLink = Resource<BucketsLink>("GCP.Logging.BucketsLink"); |
| 121 | |
| 122 | export class BucketsLinkNotResolved extends Data.TaggedError("GCP.Logging.BucketsLinkNotResolved")<{ |
| 123 | resourceName: string; |
| 124 | }> {} |
| 125 | |
| 126 | export class BucketsLinkFailed extends Data.TaggedError("GCP.Logging.BucketsLinkFailed")<{ |
| 127 | resourceName: string; |
| 128 | state: string | undefined; |
| 129 | }> {} |
| 130 | |
| 131 | export class BucketsLinkStillExists extends Data.TaggedError("GCP.Logging.BucketsLinkStillExists")<{ |
| 132 | resourceName: string; |
| 133 | }> {} |
| 134 | |
| 135 | const parseLinkName = (name: string) => { |
| 136 | const match = name.match( |
| 137 | /^projects\/([^/]+)\/locations\/([^/]+)\/buckets\/([^/]+)\/links\/([^/]+)$/, |
| 138 | ); |
| 139 | if (!match) return undefined; |
| 140 | return { |
| 141 | project: match[1]!, |
| 142 | location: match[2]!, |
| 143 | bucketId: match[3]!, |
| 144 | linkId: match[4]!, |
| 145 | }; |
| 146 | }; |
| 147 | |
| 148 | const parseBucket = (bucket: string, project: string, location: string) => { |
| 149 | const match = bucket.match(/^(projects\/([^/]+)\/locations\/([^/]+)\/buckets\/([^/]+))$/); |
| 150 | if (match) { |
| 151 | return { |
| 152 | name: match[1]!, |
| 153 | project: match[2]!, |
| 154 | location: match[3]!, |
| 155 | bucketId: match[4]!, |
| 156 | }; |
| 157 | } |
| 158 | return { |
| 159 | name: `projects/${project}/locations/${location}/buckets/${bucket}`, |
| 160 | project, |
| 161 | location, |
| 162 | bucketId: lastSegment(bucket), |
| 163 | }; |
| 164 | }; |
| 165 | |
| 166 | const resourceName = (project: string, location: string, bucketId: string, linkId: string) => |
| 167 | `projects/${project}/locations/${location}/buckets/${bucketId}/links/${linkId}`; |
| 168 | |
| 169 | const linkIdOf = (link: logging.Link, fallback?: string) => { |
| 170 | const parsed = parseLinkName(link.name ?? ""); |
| 171 | return parsed?.linkId ?? fallback ?? lastSegment(link.name ?? ""); |
| 172 | }; |
| 173 | |
| 174 | const toId = (id: string, linkId: string | undefined, existing?: string) => |
| 175 | Effect.gen(function* () { |
| 176 | if (linkId !== undefined) return linkId; |
| 177 | if (existing !== undefined) return existing; |
| 178 | const generated = yield* createPhysicalName({ |
| 179 | id, |
| 180 | maxLength: MAX_NAME_LENGTH, |
| 181 | lowercase: true, |
| 182 | delimiter: "_", |
| 183 | }); |
| 184 | const cleaned = generated.replace(/-/g, "_").replace(/[^a-z0-9_]/g, "_"); |
| 185 | return /^[a-z]/.test(cleaned) ? cleaned : `l${cleaned}`.slice(0, MAX_NAME_LENGTH); |
| 186 | }); |
| 187 | |
| 188 | const isDeleted = (link: logging.Link | undefined): link is undefined => |
| 189 | link === undefined || link.lifecycleState === "DELETE_REQUESTED"; |
| 190 | |
| 191 | const isPending = (state: string | undefined) => state === "CREATING" || state === "UPDATING"; |
| 192 | |
| 193 | const toAttrs = (link: logging.Link, project: string, location: string, bucketId: string) => { |
| 194 | const linkId = linkIdOf(link); |
| 195 | const parsed = parseDescription(link.description); |
| 196 | const parsedName = parseLinkName(link.name ?? ""); |
| 197 | const resolvedLocation = parsedName?.location ?? location; |
| 198 | const resolvedBucketId = parsedName?.bucketId ?? bucketId; |
| 199 | const resolvedProject = parsedName?.project ?? project; |
| 200 | return { |
| 201 | name: |
| 202 | link.name ?? |
| 203 | (linkId ? resourceName(resolvedProject, resolvedLocation, resolvedBucketId, linkId) : ""), |
| 204 | linkId, |
| 205 | bucket: `projects/${resolvedProject}/locations/${resolvedLocation}/buckets/${resolvedBucketId}`, |
| 206 | bucketId: resolvedBucketId, |
| 207 | project: resolvedProject, |
| 208 | location: resolvedLocation, |
| 209 | description: parsed.description, |
| 210 | datasetId: link.bigqueryDataset?.datasetId, |
| 211 | lifecycleState: link.lifecycleState, |
| 212 | createTime: link.createTime, |
| 213 | }; |
| 214 | }; |
| 215 | |
| 216 | const isValidLinkName = (name: string) => |
| 217 | /^projects\/[^/]+\/locations\/[^/]+\/buckets\/[^/]+\/links\/[^/]+$/.test(name); |
| 218 | |
| 219 | const getByName = (name: string) => |
| 220 | !isValidLinkName(name) |
| 221 | ? Effect.undefined |
| 222 | : logging |
| 223 | .getProjectsLocationsBucketsLinks({ name }) |
| 224 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 225 | |
| 226 | const waitUntilActive = (name: string) => |
| 227 | Effect.gen(function* () { |
| 228 | const link = yield* getByName(name); |
| 229 | if (isDeleted(link)) { |
| 230 | return yield* new BucketsLinkNotResolved({ resourceName: name }); |
| 231 | } |
| 232 | if (link.lifecycleState === "FAILED") { |
| 233 | return yield* new BucketsLinkFailed({ |
| 234 | resourceName: name, |
| 235 | state: link.lifecycleState, |
| 236 | }); |
| 237 | } |
| 238 | if (isPending(link.lifecycleState)) { |
| 239 | return yield* new BucketsLinkNotResolved({ resourceName: name }); |
| 240 | } |
| 241 | return link; |
| 242 | }).pipe( |
| 243 | Effect.retry({ |
| 244 | while: (error) => error._tag === "GCP.Logging.BucketsLinkNotResolved", |
| 245 | times: 10, |
| 246 | schedule: Schedule.spaced("3 seconds"), |
| 247 | }), |
| 248 | Effect.catchTag("GCP.Logging.BucketsLinkNotResolved", () => |
| 249 | Effect.fail( |
| 250 | new BucketsLinkFailed({ |
| 251 | resourceName: name, |
| 252 | state: "PENDING", |
| 253 | }), |
| 254 | ), |
| 255 | ), |
| 256 | ); |
| 257 | |
| 258 | const waitUntilDeleted = (name: string) => |
| 259 | getByName(name).pipe( |
| 260 | Effect.flatMap((link) => |
| 261 | isDeleted(link) |
| 262 | ? Effect.void |
| 263 | : Effect.fail(new BucketsLinkStillExists({ resourceName: name })), |
| 264 | ), |
| 265 | Effect.retry({ |
| 266 | while: (error) => error._tag === "GCP.Logging.BucketsLinkStillExists", |
| 267 | times: 10, |
| 268 | schedule: Schedule.spaced("3 seconds"), |
| 269 | }), |
| 270 | Effect.catchTag("GCP.Logging.BucketsLinkStillExists", () => Effect.void), |
| 271 | ); |
| 272 | |
| 273 | export const BucketsLinkProvider = () => |
| 274 | Provider.succeed(BucketsLink, { |
| 275 | nuke: { |
| 276 | dependsOn: ["GCP.Logging.LogBucket"], |
| 277 | }, |
| 278 | stables: ["name", "linkId", "bucket", "bucketId", "project", "location", "createTime"], |
| 279 | |
| 280 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 281 | if (!isResolved(news)) return undefined; |
| 282 | const previousId = olds?.linkId ?? output?.linkId; |
| 283 | const idChanged = |
| 284 | previousId !== undefined && news.linkId !== undefined && news.linkId !== previousId; |
| 285 | const previousBucket = olds?.bucket ?? output?.bucket; |
| 286 | const bucketChanged = |
| 287 | previousBucket !== undefined && |
| 288 | news.bucket.includes("/buckets/") && |
| 289 | news.bucket !== previousBucket; |
| 290 | if (!idChanged && !bucketChanged) return undefined; |
| 291 | return { action: "replace" as const, deleteFirst: true }; |
| 292 | }), |
| 293 | |
| 294 | read: Effect.fn(function* ({ id, olds, output, previous }) { |
| 295 | const env = yield* GcpEnvironment.current; |
| 296 | const location = olds?.location ?? output?.location ?? DEFAULT_LOCATION; |
| 297 | const parent = parseBucket(olds?.bucket ?? output?.bucket ?? "", env.project, location); |
| 298 | const linkId = yield* toId(id, olds?.linkId, output?.linkId); |
| 299 | const name = |
| 300 | output?.name ?? resourceName(parent.project, parent.location, parent.bucketId, linkId); |
| 301 | const existing = yield* getByName(name); |
| 302 | if (isDeleted(existing)) return undefined; |
| 303 | const attrs = toAttrs(existing, parent.project, parent.location, parent.bucketId); |
| 304 | const { labels } = parseDescription(existing.description); |
| 305 | return (yield* hasAlchemyLabels(id, labels)) |
| 306 | ? attrs |
| 307 | : unownedUnlessPrevious(attrs, previous, "name"); |
| 308 | }), |
| 309 | |
| 310 | list: () => |
| 311 | Effect.gen(function* () { |
| 312 | const env = yield* GcpEnvironment.current; |
| 313 | const buckets = yield* listProjectBuckets(); |
| 314 | const pages = yield* Effect.forEach( |
| 315 | buckets, |
| 316 | (bucket) => |
| 317 | logging.listProjectsLocationsBucketsLinks |
| 318 | .pages({ |
| 319 | parent: bucket.name!, |
| 320 | pageSize: 1000, |
| 321 | }) |
| 322 | .pipe( |
| 323 | Stream.flatMap((page) => Stream.fromIterable(page.links ?? [])), |
| 324 | Stream.filter((link) => !isDeleted(link) && hasOwnershipMarker(link.description)), |
| 325 | Stream.map((link) => { |
| 326 | const parsed = parseLinkName(link.name ?? ""); |
| 327 | return toAttrs( |
| 328 | link, |
| 329 | parsed?.project ?? env.project, |
| 330 | parsed?.location ?? DEFAULT_LOCATION, |
| 331 | parsed?.bucketId ?? lastSegment(bucket.name ?? ""), |
| 332 | ); |
| 333 | }), |
| 334 | Stream.runCollect, |
| 335 | Effect.map((chunk) => Array.from(chunk)), |
| 336 | Effect.catchTag("NotFound", () => |
| 337 | Effect.succeed([] as ReturnType<typeof toAttrs>[]), |
| 338 | ), |
| 339 | ), |
| 340 | { concurrency: 4 }, |
| 341 | ); |
| 342 | return pages.flat(); |
| 343 | }), |
| 344 | |
| 345 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 346 | const env = yield* GcpEnvironment.current; |
| 347 | const location = news.location ?? output?.location ?? DEFAULT_LOCATION; |
| 348 | const parent = parseBucket(news.bucket, env.project, location); |
| 349 | if (parent.bucketId.length === 0) { |
| 350 | return yield* new BucketsLinkNotResolved({ |
| 351 | resourceName: parent.name, |
| 352 | }); |
| 353 | } |
| 354 | const linkId = yield* toId(id, news.linkId, output?.linkId); |
| 355 | const name = resourceName(parent.project, parent.location, parent.bucketId, linkId); |
| 356 | const ownership = yield* createInternalLabels(id); |
| 357 | const desiredDescription = encodeDescription(ownership, news.description); |
| 358 | |
| 359 | let current = yield* getByName(output?.name ?? name); |
| 360 | |
| 361 | if (current !== undefined && current.lifecycleState === "DELETE_REQUESTED") { |
| 362 | yield* waitUntilDeleted(current.name ?? name); |
| 363 | current = undefined; |
| 364 | } |
| 365 | |
| 366 | if (current === undefined) { |
| 367 | const created = yield* logging |
| 368 | .createProjectsLocationsBucketsLinks({ |
| 369 | parent: parent.name, |
| 370 | linkId, |
| 371 | body: { description: desiredDescription }, |
| 372 | }) |
| 373 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 374 | if (created !== undefined) { |
| 375 | yield* waitForOperation(created); |
| 376 | } |
| 377 | current = yield* waitUntilActive(name); |
| 378 | } |
| 379 | |
| 380 | if (isDeleted(current)) { |
| 381 | return yield* new BucketsLinkNotResolved({ resourceName: name }); |
| 382 | } |
| 383 | |
| 384 | return toAttrs(current, parent.project, parent.location, parent.bucketId); |
| 385 | }), |
| 386 | |
| 387 | delete: Effect.fn(function* ({ output }) { |
| 388 | if (output.lifecycleState === "DELETE_REQUESTED") return; |
| 389 | const names = isValidLinkName(output.name) |
| 390 | ? [output.name] |
| 391 | : output.linkId |
| 392 | ? yield* listProjectBuckets().pipe( |
| 393 | Effect.map((buckets) => |
| 394 | buckets.flatMap((bucket) => |
| 395 | bucket.name ? [`${bucket.name}/links/${output.linkId}`] : [], |
| 396 | ), |
| 397 | ), |
| 398 | ) |
| 399 | : []; |
| 400 | yield* Effect.forEach( |
| 401 | names, |
| 402 | (name) => |
| 403 | logging.deleteProjectsLocationsBucketsLinks({ name }).pipe( |
| 404 | Effect.flatMap((operation) => waitForDeleteOperation(operation)), |
| 405 | Effect.catchTag(["NotFound", "BadRequest"], () => Effect.void), |
| 406 | Effect.flatMap(() => waitUntilDeleted(name)), |
| 407 | ), |
| 408 | { concurrency: 4 }, |
| 409 | ); |
| 410 | }), |
| 411 | }); |