The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as networkconnectivity from "@distilled.cloud/gcp/networkconnectivity_v1"; |
| 2 | import { Unowned } 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 { tagRecord } from "alchemy/Tags"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import { GcpEnvironment } from "../Environment.ts"; |
| 13 | import { |
| 14 | createInternalLabels, |
| 15 | diffLabels, |
| 16 | hasAlchemyLabels, |
| 17 | stripInternalLabels, |
| 18 | toLabels, |
| 19 | } from "../Labels.ts"; |
| 20 | import type { Providers } from "../Providers.ts"; |
| 21 | import { waitForOperation } from "./internal.ts"; |
| 22 | |
| 23 | const MAX_NAME_LENGTH = 63; |
| 24 | |
| 25 | export type TransportBandwidth = networkconnectivity.TransportBandwidthEnum | (string & {}); |
| 26 | export type TransportStackType = networkconnectivity.TransportStackTypeEnum | (string & {}); |
| 27 | export type TransportState = networkconnectivity.TransportStateEnum | (string & {}); |
| 28 | |
| 29 | export type TransportProps = { |
| 30 | /** |
| 31 | * Transport id (the `{transport}` segment of |
| 32 | * `projects/{project}/locations/{location}/transports/{transport}`). |
| 33 | * If omitted, a unique RFC1035 name is generated from the stack, stage, |
| 34 | * and logical id. Must be 1-63 characters and match |
| 35 | * `[a-z]([-a-z0-9]*[a-z0-9])?`. Immutable — changing it replaces the |
| 36 | * transport. |
| 37 | */ |
| 38 | transportId?: string; |
| 39 | /** |
| 40 | * Region of the transport (e.g. `us-east4`). Remote transport profiles |
| 41 | * are location-scoped, so this must match the profile's location. |
| 42 | * `US-CENTRAL1` is accepted and normalized to `us-central1`. Immutable |
| 43 | * — changing it replaces the transport. |
| 44 | * @default the stack's GCP region (`GCP.Region`, else the profile |
| 45 | * region, else `us-central1`) |
| 46 | */ |
| 47 | location?: string; |
| 48 | /** |
| 49 | * VPC network peered with this transport. Accepts a name (`app-vpc`), a |
| 50 | * resource path (`projects/{project}/global/networks/{network}`), or a |
| 51 | * Compute self-link. Immutable — changing it replaces the transport. |
| 52 | */ |
| 53 | network: string; |
| 54 | /** |
| 55 | * Remote transport profile this transport connects to. Accepts a |
| 56 | * profile id (`aws-us-east-1`), a resource name, or a full API URL. |
| 57 | * Immutable — changing it replaces the transport. Required when |
| 58 | * `providedActivationKey` is omitted. |
| 59 | */ |
| 60 | remoteProfile?: string; |
| 61 | /** |
| 62 | * Partner-supplied activation key (INPUT key flow). Only valid while |
| 63 | * the transport is `PENDING_KEY`. Immutable — changing it replaces the |
| 64 | * transport. Required when `remoteProfile` and `bandwidth` are omitted. |
| 65 | */ |
| 66 | providedActivationKey?: string; |
| 67 | /** |
| 68 | * Cloud-service-provider account id associated with `remoteProfile`. |
| 69 | * Immutable — changing it replaces the transport. |
| 70 | */ |
| 71 | remoteAccountId?: string; |
| 72 | /** |
| 73 | * Bandwidth. Must be one of the profile's supported values, and is |
| 74 | * required when no activation key is provided. |
| 75 | */ |
| 76 | bandwidth?: TransportBandwidth; |
| 77 | /** |
| 78 | * IP version stack for the established connectivity. |
| 79 | */ |
| 80 | stackType?: TransportStackType; |
| 81 | /** |
| 82 | * IPv4 and IPv6 prefixes advertised to the remote provider. |
| 83 | */ |
| 84 | advertisedRoutes?: string[]; |
| 85 | /** |
| 86 | * Human-readable description of the transport. |
| 87 | */ |
| 88 | description?: string; |
| 89 | /** |
| 90 | * User labels. Alchemy ownership labels are merged in automatically. |
| 91 | */ |
| 92 | labels?: Record<string, string>; |
| 93 | }; |
| 94 | |
| 95 | export type Transport = Resource< |
| 96 | "GCP.NetworkConnectivity.Transport", |
| 97 | TransportProps, |
| 98 | { |
| 99 | /** |
| 100 | * Full resource name |
| 101 | * `projects/{project}/locations/{location}/transports/{transport}`. |
| 102 | */ |
| 103 | name: string; |
| 104 | /** Transport id (last path segment). */ |
| 105 | transportId: string; |
| 106 | /** Project id. */ |
| 107 | project: string; |
| 108 | /** Location id (region). */ |
| 109 | location: string; |
| 110 | /** Network URI as reported by the API. */ |
| 111 | network: string | undefined; |
| 112 | /** VPC network id (last path segment). */ |
| 113 | networkName: string | undefined; |
| 114 | /** Remote transport profile URI as reported by the API. */ |
| 115 | remoteProfile: string | undefined; |
| 116 | /** Remote profile id (last path segment). */ |
| 117 | remoteProfileId: string | undefined; |
| 118 | /** Cloud-service-provider account id. */ |
| 119 | remoteAccountId: string | undefined; |
| 120 | /** Configured bandwidth enum. */ |
| 121 | bandwidth: string | undefined; |
| 122 | /** Configured IP stack. */ |
| 123 | stackType: string | undefined; |
| 124 | /** Prefixes advertised to the remote provider. */ |
| 125 | advertisedRoutes: ReadonlyArray<string>; |
| 126 | /** User-provided description. */ |
| 127 | description: string | undefined; |
| 128 | /** User labels (Alchemy ownership labels stripped). */ |
| 129 | labels: Record<string, string>; |
| 130 | /** VPC created for peering to `network` (output-only). */ |
| 131 | peeringNetwork: string | undefined; |
| 132 | /** Packet MTU in bytes (output-only). */ |
| 133 | mtuLimit: number | undefined; |
| 134 | /** |
| 135 | * Google-generated activation key (OUTPUT key flow). Only present |
| 136 | * while the transport is `PENDING_KEY`. |
| 137 | */ |
| 138 | generatedActivationKey: string | undefined; |
| 139 | /** Server-reported lifecycle state (`PENDING_KEY`, `ACTIVE`, …). */ |
| 140 | state: string | undefined; |
| 141 | /** RFC3339 creation timestamp. */ |
| 142 | createTime: string | undefined; |
| 143 | /** RFC3339 last-update timestamp. */ |
| 144 | updateTime: string | undefined; |
| 145 | }, |
| 146 | never, |
| 147 | Providers |
| 148 | >; |
| 149 | |
| 150 | /** |
| 151 | * A Network Connectivity Partner Cross-Cloud Interconnect transport. |
| 152 | * |
| 153 | * Transports attach a VPC (or NCC hub, on the beta API) to a remote |
| 154 | * cloud-service-provider profile such as AWS or Azure. `transportId`, |
| 155 | * `location`, `network`, `remoteProfile`, `providedActivationKey`, and |
| 156 | * `remoteAccountId` are immutable. Description, labels, bandwidth, |
| 157 | * stack type, and advertised routes update in place. Creating a |
| 158 | * transport without an activation key typically leaves it in |
| 159 | * `PENDING_KEY` until the remote provider accepts the generated key. |
| 160 | * |
| 161 | * ### Creating a Transport |
| 162 | * **Example:** Generated name with an AWS profile |
| 163 | * ```typescript |
| 164 | * const network = yield* GCP.Compute.Network("Vpc", { |
| 165 | * autoCreateSubnetworks: false, |
| 166 | * }); |
| 167 | * const transport = yield* GCP.NetworkConnectivity.Transport("Aws", { |
| 168 | * location: "us-east4", |
| 169 | * network: network.selfLink ?? network.networkName, |
| 170 | * remoteProfile: "aws-us-east-1", |
| 171 | * bandwidth: "BPS_1G", |
| 172 | * remoteAccountId: "123456789012", |
| 173 | * advertisedRoutes: ["10.0.0.0/8"], |
| 174 | * }); |
| 175 | * ``` |
| 176 | * |
| 177 | * **Example:** Named transport with labels |
| 178 | * ```typescript |
| 179 | * const transport = yield* GCP.NetworkConnectivity.Transport("Aws", { |
| 180 | * transportId: "app-aws", |
| 181 | * location: "us-east4", |
| 182 | * network: "projects/{project}/global/networks/app-vpc", |
| 183 | * remoteProfile: "aws-us-east-1", |
| 184 | * bandwidth: "BPS_1G", |
| 185 | * remoteAccountId: "123456789012", |
| 186 | * description: "prod aws interconnect", |
| 187 | * labels: { env: "prod" }, |
| 188 | * }); |
| 189 | * ``` |
| 190 | * |
| 191 | * ### Activation key |
| 192 | * **Example:** Accept a partner-supplied key |
| 193 | * ```typescript |
| 194 | * const transport = yield* GCP.NetworkConnectivity.Transport("Azure", { |
| 195 | * location: "us-east4", |
| 196 | * network: network.networkName, |
| 197 | * providedActivationKey: "ABC1234", |
| 198 | * }); |
| 199 | * ``` |
| 200 | * |
| 201 | * ### Updating a Transport |
| 202 | * **Example:** Description, labels, and advertised routes |
| 203 | * ```typescript |
| 204 | * const transport = yield* GCP.NetworkConnectivity.Transport("Aws", { |
| 205 | * transportId: "app-aws", |
| 206 | * location: "us-east4", |
| 207 | * network: "app-vpc", |
| 208 | * remoteProfile: "aws-us-east-1", |
| 209 | * bandwidth: "BPS_2G", |
| 210 | * remoteAccountId: "123456789012", |
| 211 | * advertisedRoutes: ["10.0.0.0/8", "192.168.0.0/16"], |
| 212 | * description: "prod aws interconnect v2", |
| 213 | * labels: { env: "prod", role: "cci" }, |
| 214 | * }); |
| 215 | * ``` |
| 216 | * |
| 217 | * @resource |
| 218 | * @category NetworkConnectivity |
| 219 | */ |
| 220 | export const Transport = Resource<Transport>("GCP.NetworkConnectivity.Transport"); |
| 221 | |
| 222 | export class TransportNotResolved extends Data.TaggedError( |
| 223 | "GCP.NetworkConnectivity.TransportNotResolved", |
| 224 | )<{ |
| 225 | resourceName: string; |
| 226 | }> {} |
| 227 | |
| 228 | export class TransportFailed extends Data.TaggedError("GCP.NetworkConnectivity.TransportFailed")<{ |
| 229 | resourceName: string; |
| 230 | state: string | undefined; |
| 231 | }> {} |
| 232 | |
| 233 | export class TransportStillExists extends Data.TaggedError( |
| 234 | "GCP.NetworkConnectivity.TransportStillExists", |
| 235 | )<{ |
| 236 | resourceName: string; |
| 237 | }> {} |
| 238 | |
| 239 | const lastSegment = (value: string) => { |
| 240 | const trimmed = value.replace(/\/+$/, ""); |
| 241 | const parts = trimmed.split("/"); |
| 242 | return parts[parts.length - 1] || trimmed; |
| 243 | }; |
| 244 | |
| 245 | const rfc1035 = (name: string): string => { |
| 246 | let next = name |
| 247 | .toLowerCase() |
| 248 | .replace(/[^a-z0-9-]/g, "-") |
| 249 | .replace(/-+/g, "-") |
| 250 | .replace(/^-+/, "") |
| 251 | .replace(/-+$/, ""); |
| 252 | if (!/^[a-z]/.test(next)) { |
| 253 | next = `t${next}`; |
| 254 | } |
| 255 | next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/, ""); |
| 256 | return next.length > 0 ? next : "transport"; |
| 257 | }; |
| 258 | |
| 259 | const normalizeLocation = (location: string | undefined, defaultLocation: string) => |
| 260 | lastSegment(location ?? defaultLocation).toLowerCase(); |
| 261 | |
| 262 | const networkNameOf = (network: string | undefined) => |
| 263 | network === undefined || network.length === 0 ? undefined : lastSegment(network); |
| 264 | |
| 265 | const profileIdOf = (profile: string | undefined) => |
| 266 | profile === undefined || profile.length === 0 ? undefined : lastSegment(profile); |
| 267 | |
| 268 | const toNetworkResource = (project: string, network: string) => { |
| 269 | const trimmed = network.trim(); |
| 270 | if (trimmed.includes("/")) return trimmed; |
| 271 | return `projects/${project}/global/networks/${trimmed}`; |
| 272 | }; |
| 273 | |
| 274 | const toRemoteProfileResource = ( |
| 275 | project: string, |
| 276 | location: string, |
| 277 | profile: string | undefined, |
| 278 | ) => { |
| 279 | if (profile === undefined || profile.length === 0) return undefined; |
| 280 | const trimmed = profile.trim(); |
| 281 | if (trimmed.includes("/")) return trimmed; |
| 282 | return `projects/${project}/locations/${location}/remoteTransportProfiles/${trimmed}`; |
| 283 | }; |
| 284 | |
| 285 | const resourceName = (project: string, location: string, transportId: string) => |
| 286 | `projects/${project}/locations/${location}/transports/${transportId}`; |
| 287 | |
| 288 | const parentOf = (project: string, location: string) => `projects/${project}/locations/${location}`; |
| 289 | |
| 290 | const parseName = (name: string, defaultLocation: string) => { |
| 291 | const parts = name.split("/").filter((part) => part.length > 0); |
| 292 | const transportsAt = parts.lastIndexOf("transports"); |
| 293 | const locationsAt = parts.lastIndexOf("locations"); |
| 294 | const projectsAt = parts.lastIndexOf("projects"); |
| 295 | return { |
| 296 | project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "", |
| 297 | location: |
| 298 | locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : defaultLocation, |
| 299 | transportId: |
| 300 | transportsAt >= 0 && parts[transportsAt + 1] ? parts[transportsAt + 1]! : lastSegment(name), |
| 301 | }; |
| 302 | }; |
| 303 | |
| 304 | const userLabels = ( |
| 305 | labels: Record<string, string | undefined> | null | undefined, |
| 306 | ): Record<string, string> => stripInternalLabels(tagRecord(labels)); |
| 307 | |
| 308 | const toId = (id: string, transportId: string | undefined, existing?: string) => |
| 309 | Effect.gen(function* () { |
| 310 | if (transportId !== undefined) return transportId; |
| 311 | if (existing !== undefined) return existing; |
| 312 | return rfc1035( |
| 313 | yield* createPhysicalName({ |
| 314 | id, |
| 315 | maxLength: MAX_NAME_LENGTH, |
| 316 | lowercase: true, |
| 317 | }), |
| 318 | ); |
| 319 | }); |
| 320 | |
| 321 | const sameList = (left?: readonly string[], right?: readonly string[]) => |
| 322 | [...(left ?? [])].sort().join("\0") === [...(right ?? [])].sort().join("\0"); |
| 323 | |
| 324 | const normalizeBandwidth = (value: string | undefined) => |
| 325 | value === undefined || value.length === 0 || value === "BANDWIDTH_UNSPECIFIED" ? "" : value; |
| 326 | |
| 327 | const normalizeStackType = (value: string | undefined) => |
| 328 | value === undefined || value.length === 0 || value === "STACK_TYPE_UNSPECIFIED" ? "" : value; |
| 329 | |
| 330 | const toAttrs = (transport: networkconnectivity.Transport, project: string, region: string) => { |
| 331 | const name = transport.name ?? ""; |
| 332 | const parsed = parseName(name, region); |
| 333 | return { |
| 334 | name, |
| 335 | transportId: parsed.transportId, |
| 336 | project: parsed.project || project, |
| 337 | location: parsed.location || region, |
| 338 | network: transport.network, |
| 339 | networkName: networkNameOf(transport.network), |
| 340 | remoteProfile: transport.remoteProfile, |
| 341 | remoteProfileId: profileIdOf(transport.remoteProfile), |
| 342 | remoteAccountId: transport.remoteAccountId, |
| 343 | bandwidth: transport.bandwidth, |
| 344 | stackType: transport.stackType, |
| 345 | advertisedRoutes: transport.advertisedRoutes ?? [], |
| 346 | description: transport.description, |
| 347 | labels: userLabels(transport.labels), |
| 348 | peeringNetwork: transport.peeringNetwork, |
| 349 | mtuLimit: transport.mtuLimit, |
| 350 | generatedActivationKey: transport.generatedActivationKey, |
| 351 | state: transport.state, |
| 352 | createTime: transport.createTime, |
| 353 | updateTime: transport.updateTime, |
| 354 | }; |
| 355 | }; |
| 356 | |
| 357 | const getByName = (name: string) => |
| 358 | networkconnectivity |
| 359 | .getProjectsLocationsTransports({ name }) |
| 360 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 361 | |
| 362 | const isPendingState = (state: string | undefined) => |
| 363 | state === "CREATING" || |
| 364 | state === "DELETING" || |
| 365 | state === "STATE_UNSPECIFIED" || |
| 366 | state === undefined; |
| 367 | |
| 368 | const waitUntilReady = (name: string) => |
| 369 | getByName(name).pipe( |
| 370 | Effect.filterOrFail( |
| 371 | (transport): transport is networkconnectivity.Transport => transport !== undefined, |
| 372 | () => new TransportNotResolved({ resourceName: name }), |
| 373 | ), |
| 374 | Effect.filterOrFail( |
| 375 | (transport) => transport.state !== "DEPROVISIONED", |
| 376 | (transport) => new TransportFailed({ resourceName: name, state: transport.state }), |
| 377 | ), |
| 378 | Effect.filterOrFail( |
| 379 | (transport) => !isPendingState(transport.state), |
| 380 | () => new TransportNotResolved({ resourceName: name }), |
| 381 | ), |
| 382 | Effect.retry({ |
| 383 | while: (error) => error._tag === "GCP.NetworkConnectivity.TransportNotResolved", |
| 384 | times: 10, |
| 385 | schedule: Schedule.spaced("3 seconds"), |
| 386 | }), |
| 387 | ); |
| 388 | |
| 389 | const waitUntilGone = (name: string) => |
| 390 | getByName(name).pipe( |
| 391 | Effect.flatMap((transport) => |
| 392 | transport === undefined |
| 393 | ? Effect.void |
| 394 | : Effect.fail(new TransportStillExists({ resourceName: name })), |
| 395 | ), |
| 396 | Effect.retry({ |
| 397 | while: (error) => error._tag === "GCP.NetworkConnectivity.TransportStillExists", |
| 398 | times: 10, |
| 399 | schedule: Schedule.spaced("3 seconds"), |
| 400 | }), |
| 401 | ); |
| 402 | |
| 403 | const listOwnedTransports = (parent: string, project: string, region: string) => |
| 404 | networkconnectivity.listProjectsLocationsTransports |
| 405 | .pages({ |
| 406 | parent, |
| 407 | pageSize: 1000, |
| 408 | }) |
| 409 | .pipe( |
| 410 | Stream.flatMap((page) => Stream.fromIterable(page.transports ?? [])), |
| 411 | Stream.filter((transport) => |
| 412 | Object.keys(transport.labels ?? {}).some((key) => key.startsWith("alchemy-")), |
| 413 | ), |
| 414 | Stream.map((transport) => toAttrs(transport, project, region)), |
| 415 | Stream.runCollect, |
| 416 | Effect.map((chunk) => Array.from(chunk)), |
| 417 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 418 | ); |
| 419 | |
| 420 | const toCreateBody = ( |
| 421 | news: TransportProps, |
| 422 | project: string, |
| 423 | location: string, |
| 424 | desiredLabels: Record<string, string>, |
| 425 | ): networkconnectivity.Transport => ({ |
| 426 | network: toNetworkResource(project, news.network), |
| 427 | remoteProfile: toRemoteProfileResource(project, location, news.remoteProfile), |
| 428 | providedActivationKey: news.providedActivationKey, |
| 429 | remoteAccountId: news.remoteAccountId, |
| 430 | bandwidth: news.bandwidth, |
| 431 | stackType: news.stackType, |
| 432 | advertisedRoutes: news.advertisedRoutes, |
| 433 | description: news.description, |
| 434 | labels: desiredLabels, |
| 435 | }); |
| 436 | |
| 437 | export const TransportProvider = () => |
| 438 | Provider.succeed(Transport, { |
| 439 | stables: [ |
| 440 | "name", |
| 441 | "transportId", |
| 442 | "project", |
| 443 | "location", |
| 444 | "networkName", |
| 445 | "remoteProfileId", |
| 446 | "remoteAccountId", |
| 447 | "createTime", |
| 448 | ], |
| 449 | |
| 450 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 451 | if (!isResolved(news)) return undefined; |
| 452 | const env = yield* GcpEnvironment.current; |
| 453 | |
| 454 | const previousId = olds?.transportId ?? output?.transportId; |
| 455 | const nextId = news.transportId ?? previousId; |
| 456 | const idChanged = previousId !== undefined && nextId !== undefined && nextId !== previousId; |
| 457 | |
| 458 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 459 | const nextLocation = normalizeLocation( |
| 460 | news.location ?? olds?.location ?? output?.location, |
| 461 | env.region, |
| 462 | ); |
| 463 | const locationChanged = previousLocation !== nextLocation; |
| 464 | |
| 465 | const previousNetwork = networkNameOf( |
| 466 | olds?.network ?? output?.networkName ?? output?.network, |
| 467 | ); |
| 468 | const nextNetwork = networkNameOf(news.network ?? previousNetwork); |
| 469 | const networkChanged = |
| 470 | previousNetwork !== undefined && |
| 471 | nextNetwork !== undefined && |
| 472 | previousNetwork !== nextNetwork; |
| 473 | |
| 474 | const previousProfile = profileIdOf( |
| 475 | olds?.remoteProfile ?? output?.remoteProfileId ?? output?.remoteProfile, |
| 476 | ); |
| 477 | const nextProfile = profileIdOf(news.remoteProfile ?? previousProfile); |
| 478 | const profileChanged = |
| 479 | previousProfile !== undefined && |
| 480 | nextProfile !== undefined && |
| 481 | previousProfile !== nextProfile; |
| 482 | |
| 483 | const previousAccount = olds?.remoteAccountId ?? output?.remoteAccountId ?? ""; |
| 484 | const nextAccount = news.remoteAccountId ?? previousAccount; |
| 485 | const accountChanged = previousAccount !== nextAccount; |
| 486 | |
| 487 | const previousKey = olds?.providedActivationKey ?? ""; |
| 488 | const nextKey = news.providedActivationKey ?? previousKey; |
| 489 | const keyChanged = previousKey.length > 0 && nextKey.length > 0 && previousKey !== nextKey; |
| 490 | |
| 491 | if ( |
| 492 | !idChanged && |
| 493 | !locationChanged && |
| 494 | !networkChanged && |
| 495 | !profileChanged && |
| 496 | !accountChanged && |
| 497 | !keyChanged |
| 498 | ) { |
| 499 | return undefined; |
| 500 | } |
| 501 | |
| 502 | return { |
| 503 | action: "replace" as const, |
| 504 | deleteFirst: |
| 505 | !idChanged && !locationChanged && previousId !== undefined && nextId === previousId, |
| 506 | }; |
| 507 | }), |
| 508 | |
| 509 | read: Effect.fn(function* ({ id, olds, output }) { |
| 510 | const env = yield* GcpEnvironment.current; |
| 511 | const transportId = yield* toId(id, olds?.transportId, output?.transportId); |
| 512 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 513 | const name = output?.name ?? resourceName(env.project, location, transportId); |
| 514 | const existing = yield* getByName(name); |
| 515 | if (existing === undefined) return undefined; |
| 516 | const attrs = toAttrs(existing, env.project, env.region); |
| 517 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 518 | }), |
| 519 | |
| 520 | list: () => |
| 521 | Effect.gen(function* () { |
| 522 | const env = yield* GcpEnvironment.current; |
| 523 | const aggregated = yield* listOwnedTransports( |
| 524 | parentOf(env.project, "-"), |
| 525 | env.project, |
| 526 | env.region, |
| 527 | ); |
| 528 | if (aggregated.length > 0) return aggregated; |
| 529 | return yield* listOwnedTransports( |
| 530 | parentOf(env.project, env.region), |
| 531 | env.project, |
| 532 | env.region, |
| 533 | ); |
| 534 | }), |
| 535 | |
| 536 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 537 | const env = yield* GcpEnvironment.current; |
| 538 | const transportId = yield* toId(id, news.transportId, output?.transportId); |
| 539 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 540 | const name = resourceName(env.project, location, transportId); |
| 541 | const desiredLabels = { |
| 542 | ...toLabels(news.labels), |
| 543 | ...(yield* createInternalLabels(id)), |
| 544 | }; |
| 545 | |
| 546 | let current = yield* getByName(name); |
| 547 | if (current?.state === "DELETING") { |
| 548 | yield* waitUntilGone(name); |
| 549 | current = undefined; |
| 550 | } |
| 551 | |
| 552 | if (current === undefined) { |
| 553 | const created = yield* networkconnectivity |
| 554 | .createProjectsLocationsTransports({ |
| 555 | parent: parentOf(env.project, location), |
| 556 | transportId, |
| 557 | body: toCreateBody(news, env.project, location, desiredLabels), |
| 558 | }) |
| 559 | .pipe( |
| 560 | Effect.retry({ |
| 561 | while: (error) => error._tag === "Conflict", |
| 562 | times: 5, |
| 563 | schedule: Schedule.spaced("2 seconds"), |
| 564 | }), |
| 565 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 566 | ); |
| 567 | if (created !== undefined) { |
| 568 | yield* waitForOperation(created); |
| 569 | } |
| 570 | current = yield* waitUntilReady(name); |
| 571 | } |
| 572 | |
| 573 | if (current === undefined) { |
| 574 | return yield* new TransportNotResolved({ resourceName: name }); |
| 575 | } |
| 576 | |
| 577 | const observedLabels = tagRecord(current.labels); |
| 578 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 579 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 580 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 581 | const bandwidthChanged = |
| 582 | news.bandwidth !== undefined && |
| 583 | normalizeBandwidth(current.bandwidth) !== normalizeBandwidth(news.bandwidth); |
| 584 | const stackTypeChanged = |
| 585 | news.stackType !== undefined && |
| 586 | normalizeStackType(current.stackType) !== normalizeStackType(news.stackType); |
| 587 | const routesChanged = |
| 588 | news.advertisedRoutes !== undefined && |
| 589 | !sameList(news.advertisedRoutes, current.advertisedRoutes); |
| 590 | |
| 591 | if ( |
| 592 | labelsChanged || |
| 593 | descriptionChanged || |
| 594 | bandwidthChanged || |
| 595 | stackTypeChanged || |
| 596 | routesChanged |
| 597 | ) { |
| 598 | const updateMask = [ |
| 599 | labelsChanged ? "labels" : undefined, |
| 600 | descriptionChanged ? "description" : undefined, |
| 601 | bandwidthChanged ? "bandwidth" : undefined, |
| 602 | stackTypeChanged ? "stackType" : undefined, |
| 603 | routesChanged ? "advertisedRoutes" : undefined, |
| 604 | ].filter((field): field is string => field !== undefined); |
| 605 | |
| 606 | const operation = yield* networkconnectivity.patchProjectsLocationsTransports({ |
| 607 | name: current.name ?? name, |
| 608 | updateMask: updateMask.join(","), |
| 609 | body: { |
| 610 | name: current.name ?? name, |
| 611 | labels: desiredLabels, |
| 612 | description: news.description, |
| 613 | bandwidth: news.bandwidth, |
| 614 | stackType: news.stackType, |
| 615 | advertisedRoutes: news.advertisedRoutes, |
| 616 | }, |
| 617 | }); |
| 618 | yield* waitForOperation(operation); |
| 619 | current = yield* waitUntilReady(current.name ?? name); |
| 620 | } |
| 621 | |
| 622 | return toAttrs(current, env.project, env.region); |
| 623 | }), |
| 624 | |
| 625 | delete: Effect.fn(function* ({ output }) { |
| 626 | const operation = yield* networkconnectivity |
| 627 | .deleteProjectsLocationsTransports({ name: output.name }) |
| 628 | .pipe( |
| 629 | Effect.retry({ |
| 630 | while: (error) => error._tag === "Conflict", |
| 631 | times: 8, |
| 632 | schedule: Schedule.spaced("2 seconds"), |
| 633 | }), |
| 634 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 635 | ); |
| 636 | if (operation !== undefined) { |
| 637 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 638 | } |
| 639 | yield* waitUntilGone(output.name); |
| 640 | }), |
| 641 | }); |