The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 2 | import * as compute from "@distilled.cloud/gcp/compute_v1"; |
| 3 | import * as servicenetworking from "@distilled.cloud/gcp/servicenetworking_v1"; |
| 4 | import { isResolved } from "alchemy/Diff"; |
| 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 { waitGlobalOperation } from "../Compute/operations.ts"; |
| 12 | import { GcpEnvironment } from "../Environment.ts"; |
| 13 | import { waitForOperation as waitForLongRunning } from "../Operation.ts"; |
| 14 | import type { Providers } from "../Providers.ts"; |
| 15 | |
| 16 | const DEFAULT_SERVICE = "servicenetworking.googleapis.com"; |
| 17 | const DEFAULT_PEERING = "servicenetworking-googleapis-com"; |
| 18 | |
| 19 | export type ConnectionProps = { |
| 20 | /** |
| 21 | * Consumer VPC network. Accepts a name (`main`), a resource path |
| 22 | * (`projects/{project}/global/networks/{network}`), or a Compute |
| 23 | * self-link. Immutable — changing it replaces the connection. |
| 24 | */ |
| 25 | network: string; |
| 26 | /** |
| 27 | * Peering service that manages producer connectivity. For Google |
| 28 | * services this is `servicenetworking.googleapis.com`. A `services/` |
| 29 | * prefix is accepted. Immutable — changing it replaces the connection. |
| 30 | * @default "servicenetworking.googleapis.com" |
| 31 | */ |
| 32 | service?: string; |
| 33 | /** |
| 34 | * Names of allocated `VPC_PEERING` global addresses reserved for this |
| 35 | * producer. Updating the list PATCHes the connection (`force` is sent |
| 36 | * when a previously allocated range is dropped). |
| 37 | */ |
| 38 | reservedPeeringRanges: string[]; |
| 39 | /** |
| 40 | * When `ABANDON`, destroy removes the resource from state without |
| 41 | * calling the API. Use this when Cloud SQL (or another managed |
| 42 | * service) still depends on the peering. |
| 43 | */ |
| 44 | deletionPolicy?: "ABANDON" | "DELETE" | (string & {}); |
| 45 | }; |
| 46 | |
| 47 | export type Connection = Resource< |
| 48 | "GCP.ServiceNetworking.Connection", |
| 49 | ConnectionProps, |
| 50 | { |
| 51 | /** |
| 52 | * Consumer network path |
| 53 | * `projects/{projectNumber}/global/networks/{network}`. |
| 54 | */ |
| 55 | network: string; |
| 56 | /** VPC network id (last path segment). */ |
| 57 | networkName: string; |
| 58 | /** Peering service, as `services/{service}`. */ |
| 59 | service: string; |
| 60 | /** VPC Network Peering name created by the producer. */ |
| 61 | peering: string; |
| 62 | /** Allocated IP range names assigned to this connection. */ |
| 63 | reservedPeeringRanges: ReadonlyArray<string>; |
| 64 | /** |
| 65 | * Resource name |
| 66 | * `services/{service}/connections/{peering}`. |
| 67 | */ |
| 68 | name: string; |
| 69 | /** Project id. */ |
| 70 | project: string; |
| 71 | /** Numeric project used in Service Networking paths. */ |
| 72 | projectNumber: string; |
| 73 | }, |
| 74 | never, |
| 75 | Providers |
| 76 | >; |
| 77 | |
| 78 | /** |
| 79 | * A private services access connection (VPC Network Peering to a |
| 80 | * Google or third-party service producer). |
| 81 | * |
| 82 | * The API has no labels or description. Alchemy treats existence at |
| 83 | * `(network, service)` as ownership. `list` returns connections whose |
| 84 | * consumer VPC carries Alchemy ownership markers so `pnpm nuke:gcp` |
| 85 | * can still clean leaks. |
| 86 | * |
| 87 | * `network` and `service` are immutable. `reservedPeeringRanges` |
| 88 | * update in place. |
| 89 | * |
| 90 | * ### Creating a Connection |
| 91 | * **Example:** Private services access on a VPC |
| 92 | * ```typescript |
| 93 | * const network = yield* GCP.Compute.Network("Vpc", { |
| 94 | * autoCreateSubnetworks: false, |
| 95 | * }); |
| 96 | * const range = yield* GCP.Compute.GlobalAddress("PsaRange", { |
| 97 | * addressType: "INTERNAL", |
| 98 | * purpose: "VPC_PEERING", |
| 99 | * network: network.selfLink, |
| 100 | * prefixLength: 24, |
| 101 | * }); |
| 102 | * const connection = yield* GCP.ServiceNetworking.Connection("Psa", { |
| 103 | * network: network.networkName, |
| 104 | * reservedPeeringRanges: [range.addressName], |
| 105 | * }); |
| 106 | * ``` |
| 107 | * |
| 108 | * **Example:** Explicit service and additional ranges |
| 109 | * ```typescript |
| 110 | * const connection = yield* GCP.ServiceNetworking.Connection("Psa", { |
| 111 | * network: network.networkName, |
| 112 | * service: "servicenetworking.googleapis.com", |
| 113 | * reservedPeeringRanges: [range.addressName, extra.addressName], |
| 114 | * }); |
| 115 | * ``` |
| 116 | * |
| 117 | * @resource |
| 118 | * @category ServiceNetworking |
| 119 | */ |
| 120 | export const Connection = Resource<Connection>("GCP.ServiceNetworking.Connection"); |
| 121 | |
| 122 | export class ConnectionNotResolved extends Data.TaggedError( |
| 123 | "GCP.ServiceNetworking.ConnectionNotResolved", |
| 124 | )<{ |
| 125 | network: string; |
| 126 | service: string; |
| 127 | }> {} |
| 128 | |
| 129 | export class ConnectionNetworkMissing extends Data.TaggedError( |
| 130 | "GCP.ServiceNetworking.ConnectionNetworkMissing", |
| 131 | )<{ |
| 132 | message: string; |
| 133 | }> {} |
| 134 | |
| 135 | export class ConnectionRangesMissing extends Data.TaggedError( |
| 136 | "GCP.ServiceNetworking.ConnectionRangesMissing", |
| 137 | )<{ |
| 138 | network: string; |
| 139 | message: string; |
| 140 | }> {} |
| 141 | |
| 142 | export class ConnectionProjectNumberMissing extends Data.TaggedError( |
| 143 | "GCP.ServiceNetworking.ConnectionProjectNumberMissing", |
| 144 | )<{ |
| 145 | project: string; |
| 146 | }> {} |
| 147 | |
| 148 | export class ConnectionStillExists extends Data.TaggedError( |
| 149 | "GCP.ServiceNetworking.ConnectionStillExists", |
| 150 | )<{ |
| 151 | network: string; |
| 152 | }> {} |
| 153 | |
| 154 | const lastSegment = (value: string) => { |
| 155 | const trimmed = value.replace(/\/+$/, ""); |
| 156 | const parts = trimmed.split("/"); |
| 157 | return parts[parts.length - 1] || trimmed; |
| 158 | }; |
| 159 | |
| 160 | const linkKey = (value: string | undefined) => |
| 161 | value === undefined || value === "" ? "" : lastSegment(value).toLowerCase(); |
| 162 | |
| 163 | const stripServicesPrefix = (service: string) => |
| 164 | service.startsWith("services/") ? service.slice("services/".length) : service; |
| 165 | |
| 166 | const parentServiceOf = (service: string | undefined) => |
| 167 | `services/${stripServicesPrefix(service || DEFAULT_SERVICE)}`; |
| 168 | |
| 169 | const serviceIdOf = (service: string | undefined) => |
| 170 | stripServicesPrefix(service || DEFAULT_SERVICE); |
| 171 | |
| 172 | const consumerNetworkOf = (projectNumber: string, networkName: string) => |
| 173 | `projects/${projectNumber}/global/networks/${networkName}`; |
| 174 | |
| 175 | const connectionNameOf = (parent: string, peering: string | undefined) => |
| 176 | `${parent}/connections/${peering && peering.length > 0 ? peering : "-"}`; |
| 177 | |
| 178 | const rangeNamesOf = (ranges: readonly string[] | undefined) => |
| 179 | [...(ranges ?? [])].map((range) => lastSegment(range)).filter((range) => range.length > 0); |
| 180 | |
| 181 | const rangesKey = (ranges: readonly string[] | undefined) => |
| 182 | [...rangeNamesOf(ranges)].sort().join("\n"); |
| 183 | |
| 184 | const isAlchemyNetwork = (network: compute.Network) => |
| 185 | (network.description ?? "").includes("alchemy-id="); |
| 186 | |
| 187 | const toAttrs = ( |
| 188 | connection: servicenetworking.Connection, |
| 189 | project: string, |
| 190 | projectNumber: string, |
| 191 | fallbackNetwork: string, |
| 192 | fallbackService: string, |
| 193 | ) => { |
| 194 | const network = connection.network ?? fallbackNetwork; |
| 195 | const service = connection.service ?? parentServiceOf(fallbackService); |
| 196 | const peering = connection.peering ?? DEFAULT_PEERING; |
| 197 | return { |
| 198 | network, |
| 199 | networkName: lastSegment(network), |
| 200 | service, |
| 201 | peering, |
| 202 | reservedPeeringRanges: connection.reservedPeeringRanges ?? [], |
| 203 | name: connectionNameOf(parentServiceOf(service), peering), |
| 204 | project, |
| 205 | projectNumber, |
| 206 | }; |
| 207 | }; |
| 208 | |
| 209 | const getProjectNumber = (project: string) => |
| 210 | Effect.gen(function* () { |
| 211 | if (/^\d+$/.test(project)) return project; |
| 212 | const resource = yield* resourcemanager.getProjects({ |
| 213 | name: `projects/${project}`, |
| 214 | }); |
| 215 | const number = lastSegment(resource.name ?? ""); |
| 216 | if (!/^\d+$/.test(number)) { |
| 217 | return yield* new ConnectionProjectNumberMissing({ project }); |
| 218 | } |
| 219 | return number; |
| 220 | }); |
| 221 | |
| 222 | const listForNetwork = (parent: string, consumerNetwork: string) => |
| 223 | servicenetworking |
| 224 | .listServicesConnections({ |
| 225 | parent, |
| 226 | network: consumerNetwork, |
| 227 | }) |
| 228 | .pipe( |
| 229 | Effect.map((page) => page.connections ?? []), |
| 230 | Effect.catchTag("NotFound", () => Effect.succeed([] as servicenetworking.Connection[])), |
| 231 | ); |
| 232 | |
| 233 | const getByNetwork = (parent: string, consumerNetwork: string) => |
| 234 | listForNetwork(parent, consumerNetwork).pipe( |
| 235 | Effect.map((connections): servicenetworking.Connection | undefined => { |
| 236 | if (connections.length === 0) { |
| 237 | return undefined; |
| 238 | } |
| 239 | const match = connections.find( |
| 240 | (connection) => |
| 241 | linkKey(connection.network) === linkKey(consumerNetwork) || connections.length === 1, |
| 242 | ); |
| 243 | return match ?? connections[0]; |
| 244 | }), |
| 245 | ); |
| 246 | |
| 247 | /** |
| 248 | * Wait for a Service Networking operation. Peering changes settle within a |
| 249 | * few minutes. `ALREADY_EXISTS` always succeeds; `notFoundOk` accepts |
| 250 | * `NOT_FOUND` (deletes) and `ignoreRangeConflict` accepts |
| 251 | * `FAILED_PRECONDITION` (another writer already changed the ranges). |
| 252 | */ |
| 253 | const waitForOperation = ( |
| 254 | operation: servicenetworking.Operation, |
| 255 | options?: { notFoundOk?: boolean; ignoreRangeConflict?: boolean }, |
| 256 | ) => |
| 257 | waitForLongRunning(operation, (name) => servicenetworking.getOperations({ name }), { |
| 258 | budget: "10 minutes", |
| 259 | }).pipe( |
| 260 | Effect.catchIf( |
| 261 | (error) => |
| 262 | error._tag === "GCP.OperationFailed" && |
| 263 | (error.code === 6 || |
| 264 | (options?.notFoundOk === true && error.code === 5) || |
| 265 | (options?.ignoreRangeConflict === true && error.code === 9)), |
| 266 | () => Effect.void, |
| 267 | ), |
| 268 | Effect.catchIf( |
| 269 | (error) => options?.notFoundOk === true && error._tag === "NotFound", |
| 270 | () => Effect.void, |
| 271 | ), |
| 272 | ); |
| 273 | |
| 274 | const removeConsumerPeering = (project: string, network: string, peering: string) => |
| 275 | compute |
| 276 | .removePeeringNetworks({ |
| 277 | project, |
| 278 | network, |
| 279 | body: { name: peering || DEFAULT_PEERING }, |
| 280 | }) |
| 281 | .pipe( |
| 282 | Effect.flatMap((operation) => |
| 283 | waitGlobalOperation(project, operation, { ignore: ["RESOURCE_NOT_FOUND"] }), |
| 284 | ), |
| 285 | Effect.catchTag("NotFound", () => Effect.void), |
| 286 | Effect.asVoid, |
| 287 | ); |
| 288 | |
| 289 | const waitUntilPresent = (parent: string, consumerNetwork: string) => |
| 290 | getByNetwork(parent, consumerNetwork).pipe( |
| 291 | Effect.filterOrFail( |
| 292 | (connection): connection is servicenetworking.Connection => connection !== undefined, |
| 293 | () => |
| 294 | new ConnectionNotResolved({ |
| 295 | network: consumerNetwork, |
| 296 | service: parent, |
| 297 | }), |
| 298 | ), |
| 299 | Effect.retry({ |
| 300 | while: (error) => error._tag === "GCP.ServiceNetworking.ConnectionNotResolved", |
| 301 | times: 8, |
| 302 | schedule: Schedule.spaced("2 seconds"), |
| 303 | }), |
| 304 | ); |
| 305 | |
| 306 | const waitUntilGone = (parent: string, consumerNetwork: string) => |
| 307 | getByNetwork(parent, consumerNetwork).pipe( |
| 308 | Effect.flatMap((connection) => |
| 309 | connection === undefined |
| 310 | ? Effect.void |
| 311 | : Effect.fail( |
| 312 | new ConnectionStillExists({ |
| 313 | network: consumerNetwork, |
| 314 | }), |
| 315 | ), |
| 316 | ), |
| 317 | Effect.retry({ |
| 318 | while: (error) => error._tag === "GCP.ServiceNetworking.ConnectionStillExists", |
| 319 | times: 10, |
| 320 | schedule: Schedule.spaced("8 seconds"), |
| 321 | }), |
| 322 | ); |
| 323 | |
| 324 | const patchRanges = ( |
| 325 | parent: string, |
| 326 | peering: string | undefined, |
| 327 | consumerNetwork: string, |
| 328 | ranges: readonly string[], |
| 329 | ) => |
| 330 | Effect.gen(function* () { |
| 331 | const operation = yield* servicenetworking.patchServicesConnections({ |
| 332 | name: connectionNameOf(parent, peering), |
| 333 | updateMask: "reservedPeeringRanges", |
| 334 | force: true, |
| 335 | body: { |
| 336 | network: consumerNetwork, |
| 337 | reservedPeeringRanges: [...ranges], |
| 338 | }, |
| 339 | }); |
| 340 | yield* waitForOperation(operation); |
| 341 | }); |
| 342 | |
| 343 | export const ConnectionProvider = () => |
| 344 | Provider.succeed(Connection, { |
| 345 | stables: ["network", "networkName", "service", "peering", "name", "project", "projectNumber"], |
| 346 | |
| 347 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 348 | if (!isResolved(news)) return undefined; |
| 349 | const previousNetwork = linkKey(olds?.network ?? output?.network); |
| 350 | const nextNetwork = linkKey(news.network ?? output?.network); |
| 351 | const previousService = serviceIdOf(olds?.service ?? output?.service); |
| 352 | const nextService = serviceIdOf(news.service ?? output?.service); |
| 353 | if ( |
| 354 | (previousNetwork.length > 0 && nextNetwork !== previousNetwork) || |
| 355 | (output?.service !== undefined && nextService !== previousService) |
| 356 | ) { |
| 357 | return { action: "replace" as const, deleteFirst: false }; |
| 358 | } |
| 359 | return undefined; |
| 360 | }), |
| 361 | |
| 362 | read: Effect.fn(function* ({ olds, output }) { |
| 363 | const env = yield* GcpEnvironment.current; |
| 364 | const networkName = lastSegment( |
| 365 | olds?.network ?? output?.networkName ?? output?.network ?? "", |
| 366 | ); |
| 367 | if (networkName.length === 0) return undefined; |
| 368 | const parent = parentServiceOf(olds?.service ?? output?.service); |
| 369 | const projectNumber = output?.projectNumber ?? (yield* getProjectNumber(env.project)); |
| 370 | const consumerNetwork = consumerNetworkOf(projectNumber, networkName); |
| 371 | const existing = yield* getByNetwork(parent, consumerNetwork); |
| 372 | if (existing === undefined) return undefined; |
| 373 | // Connections have no labels. Existence at (network, service) is |
| 374 | // ownership — the API allows only one PSA peering per pair. |
| 375 | return toAttrs(existing, env.project, projectNumber, consumerNetwork, parent); |
| 376 | }), |
| 377 | |
| 378 | list: () => |
| 379 | Effect.gen(function* () { |
| 380 | const env = yield* GcpEnvironment.current; |
| 381 | const projectNumber = yield* getProjectNumber(env.project); |
| 382 | const networks = yield* compute.listNetworks |
| 383 | .items({ project: env.project, maxResults: 500 }) |
| 384 | .pipe( |
| 385 | Stream.filter(isAlchemyNetwork), |
| 386 | Stream.runCollect, |
| 387 | Effect.map((chunk) => Array.from(chunk)), |
| 388 | ); |
| 389 | const pages = yield* Effect.forEach( |
| 390 | networks, |
| 391 | (network) => { |
| 392 | const networkName = network.name; |
| 393 | if (!networkName) { |
| 394 | return Effect.succeed([] as ReturnType<typeof toAttrs>[]); |
| 395 | } |
| 396 | const consumerNetwork = consumerNetworkOf(projectNumber, networkName); |
| 397 | return listForNetwork("services/-", consumerNetwork).pipe( |
| 398 | Effect.map((connections) => |
| 399 | connections.map((connection) => |
| 400 | toAttrs( |
| 401 | connection, |
| 402 | env.project, |
| 403 | projectNumber, |
| 404 | consumerNetwork, |
| 405 | connection.service ?? DEFAULT_SERVICE, |
| 406 | ), |
| 407 | ), |
| 408 | ), |
| 409 | ); |
| 410 | }, |
| 411 | { concurrency: 4 }, |
| 412 | ); |
| 413 | return pages.flat(); |
| 414 | }), |
| 415 | |
| 416 | reconcile: Effect.fn(function* ({ news, output }) { |
| 417 | const env = yield* GcpEnvironment.current; |
| 418 | const networkName = lastSegment(news.network ?? output?.network ?? ""); |
| 419 | if (networkName.length === 0) { |
| 420 | return yield* new ConnectionNetworkMissing({ |
| 421 | message: "Service Networking connections require `network` (VPC name or URL).", |
| 422 | }); |
| 423 | } |
| 424 | const parent = parentServiceOf(news.service ?? output?.service); |
| 425 | const projectNumber = output?.projectNumber ?? (yield* getProjectNumber(env.project)); |
| 426 | const consumerNetwork = consumerNetworkOf(projectNumber, networkName); |
| 427 | const desiredRanges = rangeNamesOf(news.reservedPeeringRanges); |
| 428 | |
| 429 | let current = yield* getByNetwork(parent, consumerNetwork); |
| 430 | |
| 431 | if (current === undefined) { |
| 432 | if (desiredRanges.length === 0) { |
| 433 | return yield* new ConnectionRangesMissing({ |
| 434 | network: consumerNetwork, |
| 435 | message: "Service Networking connections require `reservedPeeringRanges`.", |
| 436 | }); |
| 437 | } |
| 438 | const created = yield* servicenetworking |
| 439 | .createServicesConnections({ |
| 440 | parent, |
| 441 | body: { |
| 442 | network: consumerNetwork, |
| 443 | reservedPeeringRanges: [...desiredRanges], |
| 444 | }, |
| 445 | }) |
| 446 | .pipe(Effect.catchTag(["Conflict", "ConnectionAlreadyExists"], () => Effect.undefined)); |
| 447 | if (created !== undefined) { |
| 448 | yield* waitForOperation(created, { ignoreRangeConflict: true }); |
| 449 | } |
| 450 | current = yield* waitUntilPresent(parent, consumerNetwork).pipe( |
| 451 | Effect.catchTag("GCP.ServiceNetworking.ConnectionNotResolved", () => Effect.undefined), |
| 452 | ); |
| 453 | if ( |
| 454 | current !== undefined && |
| 455 | desiredRanges.length > 0 && |
| 456 | rangesKey(current.reservedPeeringRanges) !== rangesKey(desiredRanges) |
| 457 | ) { |
| 458 | yield* patchRanges(parent, current.peering, consumerNetwork, desiredRanges).pipe( |
| 459 | Effect.catchTag(["Conflict", "ConnectionAlreadyExists"], () => Effect.void), |
| 460 | ); |
| 461 | current = yield* waitUntilPresent(parent, consumerNetwork).pipe( |
| 462 | Effect.catchTag("GCP.ServiceNetworking.ConnectionNotResolved", () => Effect.undefined), |
| 463 | ); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | if (current === undefined) { |
| 468 | return yield* new ConnectionNotResolved({ |
| 469 | network: consumerNetwork, |
| 470 | service: parent, |
| 471 | }); |
| 472 | } |
| 473 | |
| 474 | if ( |
| 475 | desiredRanges.length > 0 && |
| 476 | rangesKey(current.reservedPeeringRanges) !== rangesKey(desiredRanges) |
| 477 | ) { |
| 478 | yield* patchRanges(parent, current.peering, consumerNetwork, desiredRanges); |
| 479 | current = yield* waitUntilPresent(parent, consumerNetwork); |
| 480 | } |
| 481 | |
| 482 | if (current === undefined) { |
| 483 | return yield* new ConnectionNotResolved({ |
| 484 | network: consumerNetwork, |
| 485 | service: parent, |
| 486 | }); |
| 487 | } |
| 488 | |
| 489 | return toAttrs(current, env.project, projectNumber, consumerNetwork, parent); |
| 490 | }), |
| 491 | |
| 492 | delete: Effect.fn(function* ({ olds, output }) { |
| 493 | if ((olds?.deletionPolicy ?? "DELETE").toUpperCase() === "ABANDON") { |
| 494 | return; |
| 495 | } |
| 496 | const parent = parentServiceOf(output.service); |
| 497 | const consumerNetwork = output.network; |
| 498 | const operation = yield* servicenetworking |
| 499 | .deleteConnectionServicesConnections({ |
| 500 | name: output.name || connectionNameOf(parent, output.peering), |
| 501 | body: { consumerNetwork }, |
| 502 | }) |
| 503 | .pipe( |
| 504 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 505 | Effect.retry({ |
| 506 | while: (error) => error._tag === "Conflict", |
| 507 | times: 8, |
| 508 | schedule: Schedule.spaced("8 seconds"), |
| 509 | }), |
| 510 | ); |
| 511 | if (operation !== undefined) { |
| 512 | yield* waitForOperation(operation, { notFoundOk: true }).pipe( |
| 513 | // `FAILED_PRECONDITION`: a producer (Cloud SQL keeps its side for |
| 514 | // a while after the instance is deleted) still uses the |
| 515 | // connection. Removing the peering from the consumer VPC is |
| 516 | // always allowed and drops the connection with it. |
| 517 | Effect.catchIf( |
| 518 | (error) => error._tag === "GCP.OperationFailed" && error.code === 9, |
| 519 | () => removeConsumerPeering(output.project, output.networkName, output.peering), |
| 520 | ), |
| 521 | ); |
| 522 | } |
| 523 | yield* waitUntilGone(parent, consumerNetwork); |
| 524 | }), |
| 525 | }); |