The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as compute from "@distilled.cloud/gcp/compute_v1"; |
| 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 { 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 { waitRegionOperation } from "./operations.ts"; |
| 22 | |
| 23 | const DEFAULT_IKE_VERSION = 2; |
| 24 | const MAX_NAME_LENGTH = 63; |
| 25 | |
| 26 | export type VpnTunnelCipherSuitePhase1 = { |
| 27 | /** Phase 1 encryption algorithms (for example `AES-CBC-256`). */ |
| 28 | encryption?: string[]; |
| 29 | /** Phase 1 integrity algorithms (for example `HMAC-SHA2-256-128`). */ |
| 30 | integrity?: string[]; |
| 31 | /** Phase 1 PRF algorithms (for example `PRF-HMAC-SHA2-256`). */ |
| 32 | prf?: string[]; |
| 33 | /** Phase 1 Diffie-Hellman groups (for example `Group-14`). */ |
| 34 | dh?: string[]; |
| 35 | }; |
| 36 | |
| 37 | export type VpnTunnelCipherSuitePhase2 = { |
| 38 | /** Phase 2 encryption algorithms (for example `AES-CBC-128`). */ |
| 39 | encryption?: string[]; |
| 40 | /** Phase 2 integrity algorithms (for example `HMAC-SHA2-256-128`). */ |
| 41 | integrity?: string[]; |
| 42 | /** Phase 2 PFS groups (for example `Group-14`). */ |
| 43 | pfs?: string[]; |
| 44 | }; |
| 45 | |
| 46 | export type VpnTunnelCipherSuiteProps = { |
| 47 | /** Cipher configuration for IKE phase 1. */ |
| 48 | phase1?: VpnTunnelCipherSuitePhase1; |
| 49 | /** Cipher configuration for IKE phase 2. */ |
| 50 | phase2?: VpnTunnelCipherSuitePhase2; |
| 51 | }; |
| 52 | |
| 53 | export type VpnTunnelProps = { |
| 54 | /** |
| 55 | * Tunnel name. If omitted, a unique RFC1035 name is generated from |
| 56 | * the stack, stage, and logical id. Immutable — changing it replaces |
| 57 | * the tunnel. |
| 58 | */ |
| 59 | vpnTunnelName?: string; |
| 60 | /** |
| 61 | * Region the tunnel lives in. Immutable — changing it replaces the |
| 62 | * tunnel. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 63 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 64 | */ |
| 65 | region?: string; |
| 66 | /** |
| 67 | * Optional description. Immutable — changing it replaces the tunnel. |
| 68 | */ |
| 69 | description?: string; |
| 70 | /** |
| 71 | * HA VPN gateway this tunnel attaches to. Accepts a name, a partial |
| 72 | * URL (`regions/{region}/vpnGateways/{name}`), or a full resource URL. |
| 73 | * Mutually exclusive with `targetVpnGateway`. Immutable. |
| 74 | */ |
| 75 | vpnGateway?: string; |
| 76 | /** |
| 77 | * Interface id on `vpnGateway` (`0` or `1`). Defaults to `0` when |
| 78 | * `vpnGateway` is set. Immutable. |
| 79 | */ |
| 80 | vpnGatewayInterface?: number; |
| 81 | /** |
| 82 | * Peer HA VPN gateway in another VPC. Exclusive with |
| 83 | * `peerExternalGateway`. Immutable. |
| 84 | */ |
| 85 | peerGcpGateway?: string; |
| 86 | /** |
| 87 | * Peer external VPN gateway (on-prem / other cloud). Exclusive with |
| 88 | * `peerGcpGateway`. Immutable. |
| 89 | */ |
| 90 | peerExternalGateway?: string; |
| 91 | /** |
| 92 | * Interface id on `peerExternalGateway` (`0`–`3`). Defaults to `0` |
| 93 | * when `peerExternalGateway` is set. Immutable. |
| 94 | */ |
| 95 | peerExternalGatewayInterface?: number; |
| 96 | /** |
| 97 | * Classic VPN target gateway. Mutually exclusive with `vpnGateway`. |
| 98 | * Immutable. |
| 99 | */ |
| 100 | targetVpnGateway?: string; |
| 101 | /** |
| 102 | * Peer VPN gateway IPv4. Classic VPN only. Immutable. |
| 103 | */ |
| 104 | peerIp?: string; |
| 105 | /** |
| 106 | * Cloud Router used for dynamic routing (required for HA VPN). |
| 107 | * Accepts a name or URL. Immutable. |
| 108 | */ |
| 109 | router?: string; |
| 110 | /** |
| 111 | * IKE pre-shared key (1–96 printable ASCII characters). Write-only — |
| 112 | * the API never returns it. Immutable — changing it replaces the |
| 113 | * tunnel. |
| 114 | */ |
| 115 | sharedSecret: string; |
| 116 | /** |
| 117 | * IKE protocol version (`1` or `2`). |
| 118 | * @default 2 |
| 119 | */ |
| 120 | ikeVersion?: number; |
| 121 | /** |
| 122 | * Local traffic selector CIDRs. Classic VPN only (output-only on HA |
| 123 | * VPN). Immutable. |
| 124 | */ |
| 125 | localTrafficSelector?: string[]; |
| 126 | /** |
| 127 | * Remote traffic selector CIDRs. Classic VPN only (output-only on HA |
| 128 | * VPN). Immutable. |
| 129 | */ |
| 130 | remoteTrafficSelector?: string[]; |
| 131 | /** |
| 132 | * User-specified IKE cipher suite. Immutable. |
| 133 | */ |
| 134 | cipherSuite?: VpnTunnelCipherSuiteProps; |
| 135 | /** |
| 136 | * User labels. Alchemy ownership labels are merged in automatically |
| 137 | * and synced via `setLabels` (labels cannot be set on insert). |
| 138 | */ |
| 139 | labels?: Record<string, string>; |
| 140 | }; |
| 141 | |
| 142 | export type VpnTunnel = Resource< |
| 143 | "GCP.Compute.VpnTunnel", |
| 144 | VpnTunnelProps, |
| 145 | { |
| 146 | /** Tunnel name. */ |
| 147 | vpnTunnelName: string; |
| 148 | /** Project id. */ |
| 149 | project: string; |
| 150 | /** Region short name (`us-central1`). */ |
| 151 | region: string; |
| 152 | /** Description. */ |
| 153 | description: string | undefined; |
| 154 | /** HA VPN gateway URL, if this is an HA tunnel. */ |
| 155 | vpnGateway: string | undefined; |
| 156 | /** HA VPN gateway interface id. */ |
| 157 | vpnGatewayInterface: number | undefined; |
| 158 | /** Peer HA VPN gateway URL. */ |
| 159 | peerGcpGateway: string | undefined; |
| 160 | /** Peer external VPN gateway URL. */ |
| 161 | peerExternalGateway: string | undefined; |
| 162 | /** Peer external VPN gateway interface id. */ |
| 163 | peerExternalGatewayInterface: number | undefined; |
| 164 | /** Classic VPN target gateway URL. */ |
| 165 | targetVpnGateway: string | undefined; |
| 166 | /** Peer IPv4 (Classic VPN). */ |
| 167 | peerIp: string | undefined; |
| 168 | /** Cloud Router URL. */ |
| 169 | router: string | undefined; |
| 170 | /** IKE protocol version. */ |
| 171 | ikeVersion: number | undefined; |
| 172 | /** Local traffic selector CIDRs. */ |
| 173 | localTrafficSelector: ReadonlyArray<string>; |
| 174 | /** Remote traffic selector CIDRs. */ |
| 175 | remoteTrafficSelector: ReadonlyArray<string>; |
| 176 | /** Configured IKE cipher suite, if any. */ |
| 177 | cipherSuite: VpnTunnelCipherSuiteProps | undefined; |
| 178 | /** Hash of the shared secret. */ |
| 179 | sharedSecretHash: string | undefined; |
| 180 | /** Server-reported tunnel status. */ |
| 181 | status: string | undefined; |
| 182 | /** Human-readable status detail. */ |
| 183 | detailedStatus: string | undefined; |
| 184 | /** Server-assigned numeric id. */ |
| 185 | vpnTunnelId: string | undefined; |
| 186 | /** Resource self-link. */ |
| 187 | selfLink: string | undefined; |
| 188 | /** User labels (Alchemy ownership labels stripped). */ |
| 189 | labels: Record<string, string>; |
| 190 | /** RFC3339 creation timestamp. */ |
| 191 | creationTimestamp: string | undefined; |
| 192 | }, |
| 193 | never, |
| 194 | Providers |
| 195 | >; |
| 196 | |
| 197 | /** |
| 198 | * A regional Compute Engine Cloud VPN tunnel. |
| 199 | * |
| 200 | * HA VPN tunnels attach to a `VpnGateway` plus a Cloud Router and a |
| 201 | * peer (`peerExternalGateway` or `peerGcpGateway`). Classic VPN |
| 202 | * tunnels attach to a `TargetVpnGateway` and a `peerIp`. Labels are |
| 203 | * the only in-place update (`vpnTunnels.setLabels`); every other |
| 204 | * field replaces the tunnel. |
| 205 | * |
| 206 | * ### Creating an HA VPN tunnel |
| 207 | * **Example:** HA VPN to an external peer |
| 208 | * ```typescript |
| 209 | * const network = yield* GCP.Compute.Network("Vpc", { |
| 210 | * autoCreateSubnetworks: false, |
| 211 | * }); |
| 212 | * const gateway = yield* GCP.Compute.VpnGateway("Gateway", { |
| 213 | * network: network.networkName, |
| 214 | * }); |
| 215 | * const router = yield* GCP.Compute.Router("Edge", { |
| 216 | * network: network.networkName, |
| 217 | * bgp: { asn: 64514 }, |
| 218 | * }); |
| 219 | * const peer = yield* GCP.Compute.ExternalVpnGateway("Peer", { |
| 220 | * redundancyType: "SINGLE_IP_INTERNALLY_REDUNDANT", |
| 221 | * interfaces: [{ id: 0, ipAddress: "15.0.0.120" }], |
| 222 | * }); |
| 223 | * const tunnel = yield* GCP.Compute.VpnTunnel("Tunnel", { |
| 224 | * vpnGateway: gateway.vpnGatewayName, |
| 225 | * vpnGatewayInterface: 0, |
| 226 | * peerExternalGateway: peer.externalVpnGatewayName, |
| 227 | * peerExternalGatewayInterface: 0, |
| 228 | * router: router.routerName, |
| 229 | * sharedSecret: "replace-me-with-a-secret", |
| 230 | * }); |
| 231 | * ``` |
| 232 | * |
| 233 | * **Example:** Named tunnel with labels |
| 234 | * ```typescript |
| 235 | * const tunnel = yield* GCP.Compute.VpnTunnel("Tunnel", { |
| 236 | * vpnTunnelName: "app-tunnel", |
| 237 | * region: "us-central1", |
| 238 | * vpnGateway: "app-vpn", |
| 239 | * vpnGatewayInterface: 0, |
| 240 | * peerGcpGateway: "peer-vpn", |
| 241 | * router: "app-router", |
| 242 | * sharedSecret: "replace-me-with-a-secret", |
| 243 | * labels: { env: "prod" }, |
| 244 | * }); |
| 245 | * ``` |
| 246 | * |
| 247 | * ### Classic VPN |
| 248 | * **Example:** Tunnel on a TargetVpnGateway |
| 249 | * ```typescript |
| 250 | * const tunnel = yield* GCP.Compute.VpnTunnel("Tunnel", { |
| 251 | * targetVpnGateway: gateway.targetVpnGatewayName, |
| 252 | * peerIp: "15.0.0.120", |
| 253 | * sharedSecret: "replace-me-with-a-secret", |
| 254 | * localTrafficSelector: ["10.0.0.0/16"], |
| 255 | * remoteTrafficSelector: ["172.16.0.0/16"], |
| 256 | * }); |
| 257 | * ``` |
| 258 | * |
| 259 | * @resource |
| 260 | * @category Compute |
| 261 | */ |
| 262 | export const VpnTunnel = Resource<VpnTunnel>("GCP.Compute.VpnTunnel"); |
| 263 | |
| 264 | export class VpnTunnelNotResolved extends Data.TaggedError("GCP.Compute.VpnTunnelNotResolved")<{ |
| 265 | vpnTunnelName: string; |
| 266 | region: string; |
| 267 | }> {} |
| 268 | |
| 269 | export class VpnTunnelPending extends Data.TaggedError("GCP.Compute.VpnTunnelPending")<{ |
| 270 | vpnTunnelName: string; |
| 271 | status: string; |
| 272 | }> {} |
| 273 | |
| 274 | const userLabels = ( |
| 275 | labels: Record<string, string | undefined> | null | undefined, |
| 276 | ): Record<string, string> => stripInternalLabels(tagRecord(labels)); |
| 277 | |
| 278 | const lastSegment = (value: string) => { |
| 279 | const trimmed = value.replace(/\/+$/, ""); |
| 280 | const parts = trimmed.split("/"); |
| 281 | return parts[parts.length - 1] || trimmed; |
| 282 | }; |
| 283 | |
| 284 | const normalizeRegion = (region: string | undefined, defaultRegion: string) => |
| 285 | lastSegment(region ?? defaultRegion).toLowerCase(); |
| 286 | |
| 287 | const resourceRefOf = (value: string | undefined) => { |
| 288 | if (!value) return ""; |
| 289 | return lastSegment(value); |
| 290 | }; |
| 291 | |
| 292 | const ikeVersionOf = (value: number | undefined) => value ?? DEFAULT_IKE_VERSION; |
| 293 | |
| 294 | const selectorsKey = (values: ReadonlyArray<string> | undefined) => |
| 295 | [...(values ?? [])] |
| 296 | .map((value) => value.trim()) |
| 297 | .filter((value) => value.length > 0) |
| 298 | .sort() |
| 299 | .join(","); |
| 300 | |
| 301 | const cipherKey = (suite: VpnTunnelCipherSuiteProps | undefined) => |
| 302 | JSON.stringify({ |
| 303 | phase1: { |
| 304 | encryption: [...(suite?.phase1?.encryption ?? [])].sort(), |
| 305 | integrity: [...(suite?.phase1?.integrity ?? [])].sort(), |
| 306 | prf: [...(suite?.phase1?.prf ?? [])].sort(), |
| 307 | dh: [...(suite?.phase1?.dh ?? [])].sort(), |
| 308 | }, |
| 309 | phase2: { |
| 310 | encryption: [...(suite?.phase2?.encryption ?? [])].sort(), |
| 311 | integrity: [...(suite?.phase2?.integrity ?? [])].sort(), |
| 312 | pfs: [...(suite?.phase2?.pfs ?? [])].sort(), |
| 313 | }, |
| 314 | }); |
| 315 | |
| 316 | const toCipherSuite = ( |
| 317 | suite: compute.VpnTunnelCipherSuite | undefined, |
| 318 | ): VpnTunnelCipherSuiteProps | undefined => { |
| 319 | if (suite === undefined) return undefined; |
| 320 | const phase1 = suite.phase1; |
| 321 | const phase2 = suite.phase2; |
| 322 | if (phase1 === undefined && phase2 === undefined) return undefined; |
| 323 | return { |
| 324 | phase1: phase1 |
| 325 | ? { |
| 326 | encryption: phase1.encryption, |
| 327 | integrity: phase1.integrity, |
| 328 | prf: phase1.prf, |
| 329 | dh: phase1.dh, |
| 330 | } |
| 331 | : undefined, |
| 332 | phase2: phase2 |
| 333 | ? { |
| 334 | encryption: phase2.encryption, |
| 335 | integrity: phase2.integrity, |
| 336 | pfs: phase2.pfs, |
| 337 | } |
| 338 | : undefined, |
| 339 | }; |
| 340 | }; |
| 341 | |
| 342 | const regionalUrl = (project: string, region: string, collection: string, value: string) => { |
| 343 | if (value.includes("/")) { |
| 344 | return value.startsWith("projects/") || value.startsWith("http") |
| 345 | ? value |
| 346 | : `projects/${project}/${value.replace(/^\//, "")}`; |
| 347 | } |
| 348 | return `projects/${project}/regions/${region}/${collection}/${value}`; |
| 349 | }; |
| 350 | |
| 351 | const globalUrl = (project: string, collection: string, value: string) => { |
| 352 | if (value.includes("/")) { |
| 353 | return value.startsWith("projects/") || value.startsWith("http") |
| 354 | ? value |
| 355 | : `projects/${project}/${value.replace(/^\//, "")}`; |
| 356 | } |
| 357 | return `projects/${project}/global/${collection}/${value}`; |
| 358 | }; |
| 359 | |
| 360 | const toName = (id: string, name: string | undefined, existing?: string) => |
| 361 | Effect.gen(function* () { |
| 362 | if (name !== undefined) return name; |
| 363 | if (existing !== undefined) return existing; |
| 364 | const generated = yield* createPhysicalName({ |
| 365 | id, |
| 366 | maxLength: MAX_NAME_LENGTH, |
| 367 | lowercase: true, |
| 368 | }); |
| 369 | const rfc = generated.replace(/^[^a-z]+/, "t").replace(/-+$/g, ""); |
| 370 | return rfc.slice(0, MAX_NAME_LENGTH); |
| 371 | }); |
| 372 | |
| 373 | const toAttrs = (tunnel: compute.VpnTunnel, project: string) => ({ |
| 374 | vpnTunnelName: tunnel.name ?? "", |
| 375 | project, |
| 376 | region: lastSegment(tunnel.region ?? "").toLowerCase(), |
| 377 | description: tunnel.description, |
| 378 | vpnGateway: tunnel.vpnGateway, |
| 379 | vpnGatewayInterface: tunnel.vpnGatewayInterface, |
| 380 | peerGcpGateway: tunnel.peerGcpGateway, |
| 381 | peerExternalGateway: tunnel.peerExternalGateway, |
| 382 | peerExternalGatewayInterface: tunnel.peerExternalGatewayInterface, |
| 383 | targetVpnGateway: tunnel.targetVpnGateway, |
| 384 | peerIp: tunnel.peerIp, |
| 385 | router: tunnel.router, |
| 386 | ikeVersion: tunnel.ikeVersion, |
| 387 | localTrafficSelector: tunnel.localTrafficSelector ?? [], |
| 388 | remoteTrafficSelector: tunnel.remoteTrafficSelector ?? [], |
| 389 | cipherSuite: toCipherSuite(tunnel.cipherSuite), |
| 390 | sharedSecretHash: tunnel.sharedSecretHash, |
| 391 | status: tunnel.status, |
| 392 | detailedStatus: tunnel.detailedStatus, |
| 393 | vpnTunnelId: tunnel.id, |
| 394 | selfLink: tunnel.selfLink, |
| 395 | labels: userLabels(tunnel.labels), |
| 396 | creationTimestamp: tunnel.creationTimestamp, |
| 397 | }); |
| 398 | |
| 399 | const toInsertBody = ( |
| 400 | project: string, |
| 401 | region: string, |
| 402 | vpnTunnelName: string, |
| 403 | news: VpnTunnelProps, |
| 404 | ): compute.VpnTunnel => { |
| 405 | const body: compute.VpnTunnel = { |
| 406 | name: vpnTunnelName, |
| 407 | sharedSecret: news.sharedSecret, |
| 408 | ikeVersion: ikeVersionOf(news.ikeVersion), |
| 409 | description: news.description, |
| 410 | }; |
| 411 | if (news.vpnGateway !== undefined) { |
| 412 | body.vpnGateway = regionalUrl(project, region, "vpnGateways", news.vpnGateway); |
| 413 | body.vpnGatewayInterface = news.vpnGatewayInterface ?? 0; |
| 414 | } |
| 415 | if (news.targetVpnGateway !== undefined) { |
| 416 | body.targetVpnGateway = regionalUrl( |
| 417 | project, |
| 418 | region, |
| 419 | "targetVpnGateways", |
| 420 | news.targetVpnGateway, |
| 421 | ); |
| 422 | } |
| 423 | if (news.peerGcpGateway !== undefined) { |
| 424 | body.peerGcpGateway = regionalUrl(project, region, "vpnGateways", news.peerGcpGateway); |
| 425 | } |
| 426 | if (news.peerExternalGateway !== undefined) { |
| 427 | body.peerExternalGateway = globalUrl(project, "externalVpnGateways", news.peerExternalGateway); |
| 428 | body.peerExternalGatewayInterface = news.peerExternalGatewayInterface ?? 0; |
| 429 | } |
| 430 | if (news.peerIp !== undefined) { |
| 431 | body.peerIp = news.peerIp; |
| 432 | } |
| 433 | if (news.router !== undefined) { |
| 434 | body.router = regionalUrl(project, region, "routers", news.router); |
| 435 | } |
| 436 | if (news.localTrafficSelector !== undefined) { |
| 437 | body.localTrafficSelector = news.localTrafficSelector; |
| 438 | } |
| 439 | if (news.remoteTrafficSelector !== undefined) { |
| 440 | body.remoteTrafficSelector = news.remoteTrafficSelector; |
| 441 | } |
| 442 | if (news.cipherSuite !== undefined) { |
| 443 | body.cipherSuite = news.cipherSuite; |
| 444 | } |
| 445 | return body; |
| 446 | }; |
| 447 | |
| 448 | const getByName = (project: string, region: string, vpnTunnel: string) => |
| 449 | compute |
| 450 | .getVpnTunnels({ project, region, vpnTunnel }) |
| 451 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 452 | |
| 453 | const requireTunnel = (project: string, region: string, vpnTunnelName: string) => |
| 454 | getByName(project, region, vpnTunnelName).pipe( |
| 455 | Effect.flatMap((tunnel) => |
| 456 | tunnel |
| 457 | ? Effect.succeed(tunnel) |
| 458 | : Effect.fail(new VpnTunnelNotResolved({ vpnTunnelName, region })), |
| 459 | ), |
| 460 | Effect.retry({ |
| 461 | while: (e) => e._tag === "GCP.Compute.VpnTunnelNotResolved", |
| 462 | schedule: Schedule.spaced("1 second"), |
| 463 | times: 8, |
| 464 | }), |
| 465 | ); |
| 466 | |
| 467 | const waitUntilGone = (project: string, region: string, vpnTunnelName: string) => |
| 468 | getByName(project, region, vpnTunnelName).pipe( |
| 469 | Effect.flatMap((tunnel) => |
| 470 | tunnel === undefined |
| 471 | ? Effect.void |
| 472 | : Effect.fail( |
| 473 | new VpnTunnelPending({ |
| 474 | vpnTunnelName, |
| 475 | status: "EXISTS", |
| 476 | }), |
| 477 | ), |
| 478 | ), |
| 479 | Effect.retry({ |
| 480 | while: (e) => e._tag === "GCP.Compute.VpnTunnelPending", |
| 481 | times: 10, |
| 482 | schedule: Schedule.spaced("2 seconds"), |
| 483 | }), |
| 484 | ); |
| 485 | |
| 486 | export const VpnTunnelProvider = () => |
| 487 | Provider.succeed(VpnTunnel, { |
| 488 | stables: [ |
| 489 | "vpnTunnelName", |
| 490 | "project", |
| 491 | "region", |
| 492 | "vpnGateway", |
| 493 | "vpnGatewayInterface", |
| 494 | "peerGcpGateway", |
| 495 | "peerExternalGateway", |
| 496 | "peerExternalGatewayInterface", |
| 497 | "targetVpnGateway", |
| 498 | "peerIp", |
| 499 | "router", |
| 500 | "ikeVersion", |
| 501 | "vpnTunnelId", |
| 502 | "selfLink", |
| 503 | "creationTimestamp", |
| 504 | ], |
| 505 | |
| 506 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 507 | if (!isResolved(news)) return undefined; |
| 508 | const env = yield* GcpEnvironment.current; |
| 509 | const previousName = olds.vpnTunnelName ?? output?.vpnTunnelName; |
| 510 | const nextName = news.vpnTunnelName ?? previousName; |
| 511 | const nameChanged = |
| 512 | previousName !== undefined && nextName !== undefined && nextName !== previousName; |
| 513 | |
| 514 | const previousRegion = normalizeRegion(olds.region ?? output?.region, env.region); |
| 515 | const nextRegion = normalizeRegion(news.region ?? output?.region, env.region); |
| 516 | const regionChanged = previousRegion !== nextRegion; |
| 517 | |
| 518 | const previousDescription = olds.description ?? output?.description ?? ""; |
| 519 | const previousVpnGateway = resourceRefOf(olds.vpnGateway ?? output?.vpnGateway); |
| 520 | const previousVpnGatewayInterface = |
| 521 | olds.vpnGatewayInterface ?? output?.vpnGatewayInterface ?? 0; |
| 522 | const previousPeerGcp = resourceRefOf(olds.peerGcpGateway ?? output?.peerGcpGateway); |
| 523 | const previousPeerExternal = resourceRefOf( |
| 524 | olds.peerExternalGateway ?? output?.peerExternalGateway, |
| 525 | ); |
| 526 | const previousPeerExternalInterface = |
| 527 | olds.peerExternalGatewayInterface ?? output?.peerExternalGatewayInterface ?? 0; |
| 528 | const previousTarget = resourceRefOf(olds.targetVpnGateway ?? output?.targetVpnGateway); |
| 529 | const previousPeerIp = olds.peerIp ?? output?.peerIp ?? ""; |
| 530 | const previousRouter = resourceRefOf(olds.router ?? output?.router); |
| 531 | const previousIke = ikeVersionOf(olds.ikeVersion ?? output?.ikeVersion); |
| 532 | const previousLocal = selectorsKey(olds.localTrafficSelector ?? output?.localTrafficSelector); |
| 533 | const previousRemote = selectorsKey( |
| 534 | olds.remoteTrafficSelector ?? output?.remoteTrafficSelector, |
| 535 | ); |
| 536 | const previousCipher = cipherKey(olds.cipherSuite ?? output?.cipherSuite); |
| 537 | |
| 538 | const immutableChanged = |
| 539 | (news.description !== undefined && (news.description ?? "") !== previousDescription) || |
| 540 | (news.vpnGateway !== undefined && resourceRefOf(news.vpnGateway) !== previousVpnGateway) || |
| 541 | (news.vpnGatewayInterface !== undefined && |
| 542 | news.vpnGatewayInterface !== previousVpnGatewayInterface) || |
| 543 | (news.peerGcpGateway !== undefined && |
| 544 | resourceRefOf(news.peerGcpGateway) !== previousPeerGcp) || |
| 545 | (news.peerExternalGateway !== undefined && |
| 546 | resourceRefOf(news.peerExternalGateway) !== previousPeerExternal) || |
| 547 | (news.peerExternalGatewayInterface !== undefined && |
| 548 | news.peerExternalGatewayInterface !== previousPeerExternalInterface) || |
| 549 | (news.targetVpnGateway !== undefined && |
| 550 | resourceRefOf(news.targetVpnGateway) !== previousTarget) || |
| 551 | (news.peerIp !== undefined && news.peerIp !== previousPeerIp) || |
| 552 | (news.router !== undefined && resourceRefOf(news.router) !== previousRouter) || |
| 553 | (news.ikeVersion !== undefined && ikeVersionOf(news.ikeVersion) !== previousIke) || |
| 554 | (olds.sharedSecret !== undefined && news.sharedSecret !== olds.sharedSecret) || |
| 555 | (news.localTrafficSelector !== undefined && |
| 556 | selectorsKey(news.localTrafficSelector) !== previousLocal) || |
| 557 | (news.remoteTrafficSelector !== undefined && |
| 558 | selectorsKey(news.remoteTrafficSelector) !== previousRemote) || |
| 559 | (news.cipherSuite !== undefined && cipherKey(news.cipherSuite) !== previousCipher); |
| 560 | |
| 561 | if (nameChanged || regionChanged) { |
| 562 | return { action: "replace" as const, deleteFirst: false }; |
| 563 | } |
| 564 | if (immutableChanged) { |
| 565 | return { action: "replace" as const, deleteFirst: true }; |
| 566 | } |
| 567 | return undefined; |
| 568 | }), |
| 569 | |
| 570 | read: Effect.fn(function* ({ id, olds, output, previous }) { |
| 571 | const env = yield* GcpEnvironment.current; |
| 572 | const vpnTunnelName = yield* toName(id, olds?.vpnTunnelName, output?.vpnTunnelName); |
| 573 | const region = normalizeRegion(olds?.region ?? output?.region, env.region); |
| 574 | const existing = yield* getByName(env.project, region, vpnTunnelName); |
| 575 | if (existing === undefined) return undefined; |
| 576 | const attrs = toAttrs(existing, env.project); |
| 577 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) |
| 578 | ? attrs |
| 579 | : unownedUnlessPrevious(attrs, previous, "vpnTunnelName"); |
| 580 | }), |
| 581 | |
| 582 | list: () => |
| 583 | Effect.gen(function* () { |
| 584 | const env = yield* GcpEnvironment.current; |
| 585 | const pages = yield* compute.aggregatedListVpnTunnels |
| 586 | .pages({ |
| 587 | project: env.project, |
| 588 | filter: "labels.alchemy-id:*", |
| 589 | returnPartialSuccess: true, |
| 590 | maxResults: 500, |
| 591 | }) |
| 592 | .pipe(Stream.runCollect); |
| 593 | return Array.from(pages).flatMap((page) => |
| 594 | Object.values(page.items ?? {}).flatMap((scoped) => |
| 595 | (scoped?.vpnTunnels ?? []) |
| 596 | .filter((tunnel) => |
| 597 | Object.keys(tunnel.labels ?? {}).some((key) => key.startsWith("alchemy-")), |
| 598 | ) |
| 599 | .map((tunnel) => toAttrs(tunnel, env.project)), |
| 600 | ), |
| 601 | ); |
| 602 | }), |
| 603 | |
| 604 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 605 | const env = yield* GcpEnvironment.current; |
| 606 | const vpnTunnelName = yield* toName(id, news.vpnTunnelName, output?.vpnTunnelName); |
| 607 | const region = normalizeRegion(news.region ?? output?.region, env.region); |
| 608 | const desiredLabels = { |
| 609 | ...toLabels(news.labels), |
| 610 | ...(yield* createInternalLabels(id)), |
| 611 | }; |
| 612 | |
| 613 | let current = yield* getByName(env.project, region, vpnTunnelName); |
| 614 | |
| 615 | if (current === undefined) { |
| 616 | const created = yield* compute |
| 617 | .insertVpnTunnels({ |
| 618 | project: env.project, |
| 619 | region, |
| 620 | body: toInsertBody(env.project, region, vpnTunnelName, news), |
| 621 | }) |
| 622 | .pipe( |
| 623 | Effect.flatMap((operation) => |
| 624 | waitRegionOperation(env.project, region, operation, { |
| 625 | ignore: ["RESOURCE_ALREADY_EXISTS"], |
| 626 | }).pipe(Effect.flatMap(() => requireTunnel(env.project, region, vpnTunnelName))), |
| 627 | ), |
| 628 | Effect.catchTag("Conflict", () => getByName(env.project, region, vpnTunnelName)), |
| 629 | ); |
| 630 | current = created ?? undefined; |
| 631 | } |
| 632 | |
| 633 | if (current === undefined) { |
| 634 | return yield* new VpnTunnelNotResolved({ vpnTunnelName, region }); |
| 635 | } |
| 636 | |
| 637 | const resolved = current; |
| 638 | const observedLabels = tagRecord(resolved.labels); |
| 639 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 640 | if (upsert.length > 0 || removed.length > 0) { |
| 641 | yield* Effect.gen(function* () { |
| 642 | const latest = (yield* getByName(env.project, region, vpnTunnelName)) ?? resolved; |
| 643 | yield* compute |
| 644 | .setLabelsVpnTunnels({ |
| 645 | project: env.project, |
| 646 | region, |
| 647 | resource: vpnTunnelName, |
| 648 | body: { |
| 649 | labels: desiredLabels, |
| 650 | labelFingerprint: latest.labelFingerprint, |
| 651 | }, |
| 652 | }) |
| 653 | .pipe( |
| 654 | Effect.flatMap((operation) => waitRegionOperation(env.project, region, operation)), |
| 655 | ); |
| 656 | }).pipe( |
| 657 | Effect.retry({ |
| 658 | while: (e) => e._tag === "Conflict", |
| 659 | times: 5, |
| 660 | schedule: Schedule.spaced("1 second"), |
| 661 | }), |
| 662 | ); |
| 663 | current = (yield* getByName(env.project, region, vpnTunnelName)) ?? resolved; |
| 664 | } |
| 665 | |
| 666 | return toAttrs(current ?? resolved, env.project); |
| 667 | }), |
| 668 | |
| 669 | delete: Effect.fn(function* ({ output }) { |
| 670 | const env = yield* GcpEnvironment.current; |
| 671 | const project = output.project || env.project; |
| 672 | const region = normalizeRegion(output.region, env.region); |
| 673 | if (!output.vpnTunnelName) return; |
| 674 | yield* compute |
| 675 | .deleteVpnTunnels({ |
| 676 | project, |
| 677 | region, |
| 678 | vpnTunnel: output.vpnTunnelName, |
| 679 | }) |
| 680 | .pipe( |
| 681 | Effect.flatMap((operation) => |
| 682 | waitRegionOperation(project, region, operation, { |
| 683 | ignore: ["RESOURCE_NOT_FOUND"], |
| 684 | }), |
| 685 | ), |
| 686 | Effect.catchTag("NotFound", () => Effect.void), |
| 687 | Effect.retry({ |
| 688 | while: (e) => e._tag === "Conflict", |
| 689 | times: 8, |
| 690 | schedule: Schedule.spaced("2 seconds"), |
| 691 | }), |
| 692 | ); |
| 693 | yield* waitUntilGone(project, region, output.vpnTunnelName); |
| 694 | }), |
| 695 | }); |