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 { ignoredCodes } from "./internal.ts"; |
| 22 | import { waitRegionOperation } from "./operations.ts"; |
| 23 | |
| 24 | const DEFAULT_TYPE = "PARTNER"; |
| 25 | const DEFAULT_ENCRYPTION = "NONE"; |
| 26 | const DEFAULT_STACK = "IPV4_ONLY"; |
| 27 | const MAX_NAME_LENGTH = 63; |
| 28 | |
| 29 | export type InterconnectAttachmentType = compute.InterconnectAttachmentTypeEnum | (string & {}); |
| 30 | export type InterconnectAttachmentBandwidth = |
| 31 | | compute.InterconnectAttachmentBandwidthEnum |
| 32 | | (string & {}); |
| 33 | export type InterconnectAttachmentEncryption = |
| 34 | | compute.InterconnectAttachmentEncryptionEnum |
| 35 | | (string & {}); |
| 36 | export type InterconnectAttachmentStackType = |
| 37 | | compute.InterconnectAttachmentStackTypeEnum |
| 38 | | (string & {}); |
| 39 | export type InterconnectAttachmentEdgeAvailabilityDomain = |
| 40 | | compute.InterconnectAttachmentEdgeAvailabilityDomainEnum |
| 41 | | (string & {}); |
| 42 | export type InterconnectAttachmentPartnerMetadata = compute.InterconnectAttachmentPartnerMetadata; |
| 43 | export type InterconnectAttachmentL2Forwarding = compute.InterconnectAttachmentL2Forwarding; |
| 44 | |
| 45 | export type InterconnectAttachmentProps = { |
| 46 | /** |
| 47 | * Attachment name (RFC1035, 1-63 characters). If omitted, a unique name |
| 48 | * is generated from the stack, stage, and logical id. Immutable — |
| 49 | * changing it replaces the attachment. |
| 50 | */ |
| 51 | interconnectAttachmentName?: string; |
| 52 | /** |
| 53 | * Region the attachment lives in. Immutable — changing it replaces the |
| 54 | * attachment. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 55 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 56 | */ |
| 57 | region?: string; |
| 58 | /** |
| 59 | * Cloud Router URL or name in the same region. Required. Immutable — |
| 60 | * changing it replaces the attachment. |
| 61 | */ |
| 62 | router: string; |
| 63 | /** |
| 64 | * Attachment type (`DEDICATED`, `PARTNER`, `PARTNER_PROVIDER`, |
| 65 | * `L2_DEDICATED`). Immutable — changing it replaces the attachment. |
| 66 | * @default "PARTNER" |
| 67 | */ |
| 68 | type?: InterconnectAttachmentType; |
| 69 | /** |
| 70 | * Underlying Interconnect URL or name. Required for `DEDICATED` and |
| 71 | * `L2_DEDICATED`. Immutable — changing it replaces the attachment. |
| 72 | */ |
| 73 | interconnect?: string; |
| 74 | /** |
| 75 | * Desired availability domain for `PARTNER` attachments. Immutable — |
| 76 | * changing it replaces the attachment. |
| 77 | */ |
| 78 | edgeAvailabilityDomain?: InterconnectAttachmentEdgeAvailabilityDomain; |
| 79 | /** |
| 80 | * Optional description. Updated in place via `patch`. |
| 81 | */ |
| 82 | description?: string; |
| 83 | /** |
| 84 | * Administrative status. When `false`, the attachment carries no |
| 85 | * packets. Updated in place. |
| 86 | * @default true |
| 87 | */ |
| 88 | adminEnabled?: boolean; |
| 89 | /** |
| 90 | * Provisioned bandwidth. Mutable for `DEDICATED` and |
| 91 | * `PARTNER_PROVIDER`. |
| 92 | */ |
| 93 | bandwidth?: InterconnectAttachmentBandwidth; |
| 94 | /** |
| 95 | * Packet MTU in bytes (`1440`, `1460`, `1500`, `8896`). Updated in |
| 96 | * place. |
| 97 | */ |
| 98 | mtu?: number; |
| 99 | /** |
| 100 | * IP stack (`IPV4_ONLY` or `IPV4_IPV6`). Updated in place. |
| 101 | * @default "IPV4_ONLY" |
| 102 | */ |
| 103 | stackType?: InterconnectAttachmentStackType; |
| 104 | /** |
| 105 | * VLAN encryption. Immutable — changing it replaces the attachment. |
| 106 | * @default "NONE" |
| 107 | */ |
| 108 | encryption?: InterconnectAttachmentEncryption; |
| 109 | /** |
| 110 | * IEEE 802.1Q VLAN tag (2-4093). Create-time only. Immutable — |
| 111 | * changing it replaces the attachment. |
| 112 | */ |
| 113 | vlanTag8021q?: number; |
| 114 | /** |
| 115 | * Reserved internal addresses for IPsec-encrypted attachments. |
| 116 | * Immutable — changing them replaces the attachment. |
| 117 | */ |
| 118 | ipsecInternalAddresses?: string[]; |
| 119 | /** |
| 120 | * Partner metadata displayed to customers. Mutable for |
| 121 | * `PARTNER_PROVIDER`. |
| 122 | */ |
| 123 | partnerMetadata?: InterconnectAttachmentPartnerMetadata; |
| 124 | /** |
| 125 | * L2 forwarding config. Required when `type` is `L2_DEDICATED`. |
| 126 | */ |
| 127 | l2Forwarding?: InterconnectAttachmentL2Forwarding; |
| 128 | /** |
| 129 | * IPv4 subnet mask length (`29` or `30`). Create-time only. |
| 130 | */ |
| 131 | subnetLength?: number; |
| 132 | /** |
| 133 | * Candidate link-local prefixes for Cloud Router IPs. Create-time |
| 134 | * only. |
| 135 | */ |
| 136 | candidateSubnets?: string[]; |
| 137 | /** |
| 138 | * User labels. Alchemy ownership labels are merged in automatically |
| 139 | * and synced via `setLabels` (labels cannot be set on insert). |
| 140 | */ |
| 141 | labels?: Record<string, string>; |
| 142 | }; |
| 143 | |
| 144 | export type InterconnectAttachment = Resource< |
| 145 | "GCP.Compute.InterconnectAttachment", |
| 146 | InterconnectAttachmentProps, |
| 147 | { |
| 148 | /** Attachment name. */ |
| 149 | interconnectAttachmentName: string; |
| 150 | /** Project id. */ |
| 151 | project: string; |
| 152 | /** Region short name (`us-central1`). */ |
| 153 | region: string; |
| 154 | /** Cloud Router URL. */ |
| 155 | router: string | undefined; |
| 156 | /** Attachment type. */ |
| 157 | type: string | undefined; |
| 158 | /** Underlying Interconnect URL. */ |
| 159 | interconnect: string | undefined; |
| 160 | /** Description. */ |
| 161 | description: string | undefined; |
| 162 | /** Administrative status. */ |
| 163 | adminEnabled: boolean; |
| 164 | /** Provisioned bandwidth. */ |
| 165 | bandwidth: string | undefined; |
| 166 | /** Packet MTU. */ |
| 167 | mtu: number | undefined; |
| 168 | /** IP stack. */ |
| 169 | stackType: string | undefined; |
| 170 | /** VLAN encryption. */ |
| 171 | encryption: string | undefined; |
| 172 | /** VLAN tag. */ |
| 173 | vlanTag8021q: number | undefined; |
| 174 | /** Availability domain. */ |
| 175 | edgeAvailabilityDomain: string | undefined; |
| 176 | /** Partner pairing key (PARTNER type). */ |
| 177 | pairingKey: string | undefined; |
| 178 | /** Attachment state. */ |
| 179 | state: string | undefined; |
| 180 | /** Operational status. */ |
| 181 | operationalStatus: string | undefined; |
| 182 | /** Cloud Router IPv4 address. */ |
| 183 | cloudRouterIpAddress: string | undefined; |
| 184 | /** Customer router IPv4 address. */ |
| 185 | customerRouterIpAddress: string | undefined; |
| 186 | /** Cloud Router IPv6 address. */ |
| 187 | cloudRouterIpv6Address: string | undefined; |
| 188 | /** Customer router IPv6 address. */ |
| 189 | customerRouterIpv6Address: string | undefined; |
| 190 | /** User labels (Alchemy ownership labels stripped). */ |
| 191 | labels: Record<string, string>; |
| 192 | /** Partner metadata. */ |
| 193 | partnerMetadata: InterconnectAttachmentPartnerMetadata | undefined; |
| 194 | /** L2 forwarding config. */ |
| 195 | l2Forwarding: InterconnectAttachmentL2Forwarding | undefined; |
| 196 | /** Attachment group URL. */ |
| 197 | attachmentGroup: string | undefined; |
| 198 | /** Server-assigned numeric id. */ |
| 199 | interconnectAttachmentId: string | undefined; |
| 200 | /** Resource self-link. */ |
| 201 | selfLink: string | undefined; |
| 202 | /** Label fingerprint for `setLabels`. */ |
| 203 | labelFingerprint: string | undefined; |
| 204 | /** RFC3339 creation timestamp. */ |
| 205 | creationTimestamp: string | undefined; |
| 206 | /** Resource kind. */ |
| 207 | kind: string | undefined; |
| 208 | }, |
| 209 | never, |
| 210 | Providers |
| 211 | >; |
| 212 | |
| 213 | /** |
| 214 | * A regional Compute Engine Interconnect VLAN attachment. |
| 215 | * |
| 216 | * VLAN attachments connect a Cloud Router to a Dedicated or Partner |
| 217 | * Interconnect. Name, region, router, type, interconnect, encryption, |
| 218 | * VLAN tag, and availability domain are immutable. Description, admin |
| 219 | * status, bandwidth, MTU, and stack type update in place via |
| 220 | * `interconnectAttachments.patch`. Labels are applied with `setLabels` |
| 221 | * after the attachment exists. |
| 222 | * |
| 223 | * ### Creating an Interconnect Attachment |
| 224 | * **Example:** Partner attachment |
| 225 | * ```typescript |
| 226 | * const attachment = yield* GCP.Compute.InterconnectAttachment("Vlan", { |
| 227 | * region: "us-central1", |
| 228 | * router: router.routerName, |
| 229 | * type: "PARTNER", |
| 230 | * edgeAvailabilityDomain: "AVAILABILITY_DOMAIN_1", |
| 231 | * mtu: 1500, |
| 232 | * }); |
| 233 | * ``` |
| 234 | * |
| 235 | * **Example:** Dedicated attachment |
| 236 | * ```typescript |
| 237 | * const attachment = yield* GCP.Compute.InterconnectAttachment("Vlan", { |
| 238 | * interconnectAttachmentName: "app-vlan", |
| 239 | * router: router.selfLink, |
| 240 | * type: "DEDICATED", |
| 241 | * interconnect: interconnect.selfLink, |
| 242 | * vlanTag8021q: 100, |
| 243 | * bandwidth: "BPS_1G", |
| 244 | * }); |
| 245 | * ``` |
| 246 | * |
| 247 | * @resource |
| 248 | * @category Compute |
| 249 | */ |
| 250 | export const InterconnectAttachment = Resource<InterconnectAttachment>( |
| 251 | "GCP.Compute.InterconnectAttachment", |
| 252 | ); |
| 253 | |
| 254 | export class InterconnectAttachmentNotResolved extends Data.TaggedError( |
| 255 | "GCP.Compute.InterconnectAttachmentNotResolved", |
| 256 | )<{ |
| 257 | interconnectAttachmentName: string; |
| 258 | region: string; |
| 259 | }> {} |
| 260 | |
| 261 | export class InterconnectAttachmentStillExists extends Data.TaggedError( |
| 262 | "GCP.Compute.InterconnectAttachmentStillExists", |
| 263 | )<{ |
| 264 | interconnectAttachmentName: string; |
| 265 | }> {} |
| 266 | |
| 267 | const lastSegment = (value: string | undefined): string => { |
| 268 | if (value === undefined || value.length === 0) return ""; |
| 269 | const trimmed = value.replace(/\/+$/, ""); |
| 270 | const parts = trimmed.split("/"); |
| 271 | return parts[parts.length - 1] || trimmed; |
| 272 | }; |
| 273 | |
| 274 | const normalizeRegion = (region: string | undefined, defaultRegion: string) => |
| 275 | lastSegment(region ?? defaultRegion).toLowerCase(); |
| 276 | |
| 277 | const rfc1035 = (name: string): string => { |
| 278 | let next = name |
| 279 | .toLowerCase() |
| 280 | .replace(/[^a-z0-9-]/g, "-") |
| 281 | .replace(/-+/g, "-") |
| 282 | .replace(/^-+/, "") |
| 283 | .replace(/-+$/, ""); |
| 284 | if (!/^[a-z]/.test(next)) next = `a${next}`; |
| 285 | next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/, ""); |
| 286 | return next.length > 0 ? next : "attachment"; |
| 287 | }; |
| 288 | |
| 289 | const toName = (id: string, name: string | undefined, existing?: string) => |
| 290 | Effect.gen(function* () { |
| 291 | if (name !== undefined) return name; |
| 292 | if (existing !== undefined) return existing; |
| 293 | return rfc1035( |
| 294 | yield* createPhysicalName({ |
| 295 | id, |
| 296 | maxLength: MAX_NAME_LENGTH, |
| 297 | lowercase: true, |
| 298 | }), |
| 299 | ); |
| 300 | }); |
| 301 | |
| 302 | const typeOf = (value: string | undefined) => (value ?? DEFAULT_TYPE).toUpperCase(); |
| 303 | |
| 304 | const encryptionOf = (value: string | undefined) => (value ?? DEFAULT_ENCRYPTION).toUpperCase(); |
| 305 | |
| 306 | const stackOf = (value: string | undefined) => (value ?? DEFAULT_STACK).toUpperCase(); |
| 307 | |
| 308 | const routerUrl = (project: string, region: string, value: string) => { |
| 309 | if (value.includes("/")) return value; |
| 310 | return `projects/${project}/regions/${region}/routers/${value}`; |
| 311 | }; |
| 312 | |
| 313 | const interconnectUrl = (project: string, value: string) => { |
| 314 | if (value.includes("/")) return value; |
| 315 | return `projects/${project}/global/interconnects/${value}`; |
| 316 | }; |
| 317 | |
| 318 | const userLabels = ( |
| 319 | labels: Record<string, string | undefined> | null | undefined, |
| 320 | ): Record<string, string> => stripInternalLabels(tagRecord(labels)); |
| 321 | |
| 322 | const toAttrs = ( |
| 323 | attachment: compute.InterconnectAttachment, |
| 324 | project: string, |
| 325 | ): InterconnectAttachment["Attributes"] => ({ |
| 326 | interconnectAttachmentName: attachment.name ?? "", |
| 327 | project, |
| 328 | region: lastSegment(attachment.region).toLowerCase(), |
| 329 | router: attachment.router, |
| 330 | type: attachment.type, |
| 331 | interconnect: attachment.interconnect, |
| 332 | description: attachment.description, |
| 333 | adminEnabled: attachment.adminEnabled !== false, |
| 334 | bandwidth: attachment.bandwidth, |
| 335 | mtu: attachment.mtu !== undefined ? Number(attachment.mtu) : undefined, |
| 336 | stackType: attachment.stackType, |
| 337 | encryption: attachment.encryption, |
| 338 | vlanTag8021q: attachment.vlanTag8021q, |
| 339 | edgeAvailabilityDomain: attachment.edgeAvailabilityDomain, |
| 340 | pairingKey: attachment.pairingKey, |
| 341 | state: attachment.state, |
| 342 | operationalStatus: attachment.operationalStatus, |
| 343 | cloudRouterIpAddress: attachment.cloudRouterIpAddress, |
| 344 | customerRouterIpAddress: attachment.customerRouterIpAddress, |
| 345 | cloudRouterIpv6Address: attachment.cloudRouterIpv6Address, |
| 346 | customerRouterIpv6Address: attachment.customerRouterIpv6Address, |
| 347 | labels: userLabels(attachment.labels), |
| 348 | partnerMetadata: attachment.partnerMetadata, |
| 349 | l2Forwarding: attachment.l2Forwarding, |
| 350 | attachmentGroup: attachment.attachmentGroup, |
| 351 | interconnectAttachmentId: attachment.id, |
| 352 | selfLink: attachment.selfLink, |
| 353 | labelFingerprint: attachment.labelFingerprint, |
| 354 | creationTimestamp: attachment.creationTimestamp, |
| 355 | kind: attachment.kind, |
| 356 | }); |
| 357 | |
| 358 | const getByName = (project: string, region: string, interconnectAttachment: string) => |
| 359 | compute |
| 360 | .getInterconnectAttachments({ |
| 361 | project, |
| 362 | region, |
| 363 | interconnectAttachment, |
| 364 | }) |
| 365 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 366 | |
| 367 | const awaitResource = (project: string, region: string, interconnectAttachmentName: string) => |
| 368 | getByName(project, region, interconnectAttachmentName).pipe( |
| 369 | Effect.flatMap((attachment) => |
| 370 | attachment !== undefined |
| 371 | ? Effect.succeed(attachment) |
| 372 | : Effect.fail( |
| 373 | new InterconnectAttachmentNotResolved({ |
| 374 | interconnectAttachmentName, |
| 375 | region, |
| 376 | }), |
| 377 | ), |
| 378 | ), |
| 379 | Effect.retry({ |
| 380 | while: (error) => error._tag === "GCP.Compute.InterconnectAttachmentNotResolved", |
| 381 | times: 8, |
| 382 | schedule: Schedule.spaced("1 second"), |
| 383 | }), |
| 384 | ); |
| 385 | |
| 386 | const waitUntilGone = (project: string, region: string, interconnectAttachmentName: string) => |
| 387 | getByName(project, region, interconnectAttachmentName).pipe( |
| 388 | Effect.flatMap((attachment) => |
| 389 | attachment === undefined |
| 390 | ? Effect.void |
| 391 | : Effect.fail( |
| 392 | new InterconnectAttachmentStillExists({ |
| 393 | interconnectAttachmentName, |
| 394 | }), |
| 395 | ), |
| 396 | ), |
| 397 | Effect.retry({ |
| 398 | while: (error) => error._tag === "GCP.Compute.InterconnectAttachmentStillExists", |
| 399 | times: 10, |
| 400 | schedule: Schedule.spaced("2 seconds"), |
| 401 | }), |
| 402 | Effect.catchTag("GCP.Compute.InterconnectAttachmentStillExists", () => Effect.void), |
| 403 | ); |
| 404 | |
| 405 | const runOp = <E extends { readonly _tag: string }, R>( |
| 406 | project: string, |
| 407 | region: string, |
| 408 | interconnectAttachmentName: string, |
| 409 | start: Effect.Effect<compute.Operation, E, R>, |
| 410 | options?: { ignoreAlreadyExists?: boolean; ignoreNotFound?: boolean }, |
| 411 | ) => |
| 412 | start.pipe( |
| 413 | Effect.flatMap((operation) => |
| 414 | waitRegionOperation(project, region, operation, { |
| 415 | ignore: ignoredCodes(options), |
| 416 | }), |
| 417 | ), |
| 418 | Effect.retry({ |
| 419 | while: (error) => error._tag === "Conflict", |
| 420 | times: 5, |
| 421 | schedule: Schedule.spaced("1 second"), |
| 422 | }), |
| 423 | ); |
| 424 | |
| 425 | export const InterconnectAttachmentProvider = () => |
| 426 | Provider.succeed(InterconnectAttachment, { |
| 427 | stables: [ |
| 428 | "interconnectAttachmentName", |
| 429 | "project", |
| 430 | "region", |
| 431 | "interconnectAttachmentId", |
| 432 | "selfLink", |
| 433 | "type", |
| 434 | "router", |
| 435 | "interconnect", |
| 436 | "creationTimestamp", |
| 437 | ], |
| 438 | |
| 439 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 440 | if (!isResolved(news)) return undefined; |
| 441 | const env = yield* GcpEnvironment.current; |
| 442 | const previousName = olds?.interconnectAttachmentName ?? output?.interconnectAttachmentName; |
| 443 | const nextName = news.interconnectAttachmentName ?? previousName; |
| 444 | const nameChanged = |
| 445 | previousName !== undefined && nextName !== undefined && previousName !== nextName; |
| 446 | const previousRegion = normalizeRegion(olds?.region ?? output?.region, env.region); |
| 447 | const nextRegion = normalizeRegion(news.region ?? previousRegion, env.region); |
| 448 | const previousRouter = lastSegment(olds?.router ?? output?.router); |
| 449 | const nextRouter = lastSegment(news.router); |
| 450 | const previousType = typeOf(olds?.type ?? output?.type); |
| 451 | const nextType = typeOf(news.type ?? previousType); |
| 452 | const previousIx = lastSegment(olds?.interconnect ?? output?.interconnect); |
| 453 | const nextIx = lastSegment(news.interconnect ?? previousIx); |
| 454 | const previousEnc = encryptionOf(olds?.encryption ?? output?.encryption); |
| 455 | const nextEnc = encryptionOf(news.encryption ?? previousEnc); |
| 456 | const previousVlan = olds?.vlanTag8021q ?? output?.vlanTag8021q; |
| 457 | const nextVlan = news.vlanTag8021q ?? previousVlan; |
| 458 | const previousDomain = olds?.edgeAvailabilityDomain ?? output?.edgeAvailabilityDomain; |
| 459 | const nextDomain = news.edgeAvailabilityDomain ?? previousDomain; |
| 460 | |
| 461 | const immutableChanged = |
| 462 | previousRouter !== nextRouter || |
| 463 | previousType !== nextType || |
| 464 | previousIx !== nextIx || |
| 465 | previousEnc !== nextEnc || |
| 466 | previousVlan !== nextVlan || |
| 467 | previousDomain !== nextDomain; |
| 468 | |
| 469 | if (nameChanged || previousRegion !== nextRegion) { |
| 470 | return { action: "replace" as const, deleteFirst: false }; |
| 471 | } |
| 472 | if (immutableChanged) { |
| 473 | return { action: "replace" as const, deleteFirst: true }; |
| 474 | } |
| 475 | return undefined; |
| 476 | }), |
| 477 | |
| 478 | read: Effect.fn(function* ({ id, olds, output, previous }) { |
| 479 | const env = yield* GcpEnvironment.current; |
| 480 | const interconnectAttachmentName = yield* toName( |
| 481 | id, |
| 482 | olds?.interconnectAttachmentName, |
| 483 | output?.interconnectAttachmentName, |
| 484 | ); |
| 485 | const region = normalizeRegion(olds?.region ?? output?.region, env.region); |
| 486 | const existing = yield* getByName(env.project, region, interconnectAttachmentName); |
| 487 | if (existing === undefined) return undefined; |
| 488 | const attrs = toAttrs(existing, env.project); |
| 489 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) |
| 490 | ? attrs |
| 491 | : unownedUnlessPrevious(attrs, previous, "interconnectAttachmentName"); |
| 492 | }), |
| 493 | |
| 494 | list: () => |
| 495 | Effect.gen(function* () { |
| 496 | const env = yield* GcpEnvironment.current; |
| 497 | const pages = yield* compute.aggregatedListInterconnectAttachments |
| 498 | .pages({ |
| 499 | project: env.project, |
| 500 | filter: "labels.alchemy-id:*", |
| 501 | returnPartialSuccess: true, |
| 502 | maxResults: 500, |
| 503 | }) |
| 504 | .pipe(Stream.runCollect); |
| 505 | return Array.from(pages).flatMap((page) => |
| 506 | Object.values(page.items ?? {}).flatMap((scoped) => |
| 507 | (scoped?.interconnectAttachments ?? []) |
| 508 | .filter((item) => |
| 509 | Object.keys(item.labels ?? {}).some((key) => key.startsWith("alchemy-")), |
| 510 | ) |
| 511 | .map((item) => toAttrs(item, env.project)), |
| 512 | ), |
| 513 | ); |
| 514 | }), |
| 515 | |
| 516 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 517 | const env = yield* GcpEnvironment.current; |
| 518 | const interconnectAttachmentName = yield* toName( |
| 519 | id, |
| 520 | news.interconnectAttachmentName, |
| 521 | output?.interconnectAttachmentName, |
| 522 | ); |
| 523 | const region = normalizeRegion(news.region ?? output?.region, env.region); |
| 524 | const desiredLabels = { |
| 525 | ...toLabels(news.labels), |
| 526 | ...(yield* createInternalLabels(id)), |
| 527 | }; |
| 528 | const router = routerUrl(env.project, region, news.router); |
| 529 | const type = typeOf(news.type); |
| 530 | const encryption = encryptionOf(news.encryption); |
| 531 | const stackType = stackOf(news.stackType); |
| 532 | const adminEnabled = news.adminEnabled !== false; |
| 533 | |
| 534 | let current = yield* getByName(env.project, region, interconnectAttachmentName); |
| 535 | |
| 536 | if (current === undefined) { |
| 537 | yield* compute |
| 538 | .insertInterconnectAttachments({ |
| 539 | project: env.project, |
| 540 | region, |
| 541 | body: { |
| 542 | name: interconnectAttachmentName, |
| 543 | router, |
| 544 | type, |
| 545 | interconnect: news.interconnect |
| 546 | ? interconnectUrl(env.project, news.interconnect) |
| 547 | : undefined, |
| 548 | edgeAvailabilityDomain: news.edgeAvailabilityDomain, |
| 549 | description: news.description, |
| 550 | adminEnabled, |
| 551 | bandwidth: news.bandwidth, |
| 552 | mtu: news.mtu, |
| 553 | stackType, |
| 554 | encryption, |
| 555 | vlanTag8021q: news.vlanTag8021q, |
| 556 | ipsecInternalAddresses: news.ipsecInternalAddresses, |
| 557 | partnerMetadata: news.partnerMetadata, |
| 558 | l2Forwarding: news.l2Forwarding, |
| 559 | subnetLength: news.subnetLength, |
| 560 | candidateSubnets: news.candidateSubnets, |
| 561 | }, |
| 562 | }) |
| 563 | .pipe( |
| 564 | Effect.flatMap((operation) => |
| 565 | waitRegionOperation(env.project, region, operation, { |
| 566 | ignore: ["RESOURCE_ALREADY_EXISTS"], |
| 567 | }), |
| 568 | ), |
| 569 | Effect.catchTag("Conflict", () => Effect.void), |
| 570 | ); |
| 571 | current = yield* awaitResource(env.project, region, interconnectAttachmentName); |
| 572 | } |
| 573 | |
| 574 | const observedMtu = current.mtu !== undefined ? Number(current.mtu) : undefined; |
| 575 | const needsPatch = |
| 576 | (current.description ?? "") !== (news.description ?? "") || |
| 577 | (current.adminEnabled !== false) !== adminEnabled || |
| 578 | (news.bandwidth !== undefined && (current.bandwidth ?? "") !== news.bandwidth) || |
| 579 | (news.mtu !== undefined && observedMtu !== news.mtu) || |
| 580 | (news.stackType !== undefined && stackOf(current.stackType) !== stackType) || |
| 581 | (news.partnerMetadata !== undefined && |
| 582 | JSON.stringify(current.partnerMetadata ?? null) !== |
| 583 | JSON.stringify(news.partnerMetadata ?? null)); |
| 584 | |
| 585 | if (needsPatch) { |
| 586 | yield* runOp( |
| 587 | env.project, |
| 588 | region, |
| 589 | interconnectAttachmentName, |
| 590 | compute.patchInterconnectAttachments({ |
| 591 | project: env.project, |
| 592 | region, |
| 593 | interconnectAttachment: interconnectAttachmentName, |
| 594 | body: { |
| 595 | description: news.description, |
| 596 | adminEnabled, |
| 597 | bandwidth: news.bandwidth, |
| 598 | mtu: news.mtu, |
| 599 | stackType, |
| 600 | partnerMetadata: news.partnerMetadata, |
| 601 | }, |
| 602 | }), |
| 603 | ); |
| 604 | current = (yield* getByName(env.project, region, interconnectAttachmentName)) ?? current; |
| 605 | } |
| 606 | |
| 607 | const observedLabels = tagRecord(current.labels); |
| 608 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 609 | if (upsert.length > 0 || removed.length > 0) { |
| 610 | yield* Effect.gen(function* () { |
| 611 | const latest = |
| 612 | (yield* getByName(env.project, region, interconnectAttachmentName)) ?? current; |
| 613 | if (latest === undefined) { |
| 614 | return yield* new InterconnectAttachmentNotResolved({ |
| 615 | interconnectAttachmentName, |
| 616 | region, |
| 617 | }); |
| 618 | } |
| 619 | yield* runOp( |
| 620 | env.project, |
| 621 | region, |
| 622 | interconnectAttachmentName, |
| 623 | compute.setLabelsInterconnectAttachments({ |
| 624 | project: env.project, |
| 625 | region, |
| 626 | resource: interconnectAttachmentName, |
| 627 | body: { |
| 628 | labels: desiredLabels, |
| 629 | labelFingerprint: latest.labelFingerprint, |
| 630 | }, |
| 631 | }), |
| 632 | ); |
| 633 | }).pipe( |
| 634 | Effect.retry({ |
| 635 | while: (error) => error._tag === "Conflict", |
| 636 | times: 5, |
| 637 | schedule: Schedule.spaced("1 second"), |
| 638 | }), |
| 639 | ); |
| 640 | current = (yield* getByName(env.project, region, interconnectAttachmentName)) ?? current; |
| 641 | } |
| 642 | |
| 643 | return toAttrs(current, env.project); |
| 644 | }), |
| 645 | |
| 646 | delete: Effect.fn(function* ({ output }) { |
| 647 | if (!output.interconnectAttachmentName) return; |
| 648 | const env = yield* GcpEnvironment.current; |
| 649 | const project = output.project || env.project; |
| 650 | const region = normalizeRegion(output.region, env.region); |
| 651 | yield* compute |
| 652 | .deleteInterconnectAttachments({ |
| 653 | project, |
| 654 | region, |
| 655 | interconnectAttachment: output.interconnectAttachmentName, |
| 656 | }) |
| 657 | .pipe( |
| 658 | Effect.flatMap((operation) => |
| 659 | waitRegionOperation(project, region, operation, { |
| 660 | ignore: ["RESOURCE_NOT_FOUND"], |
| 661 | }), |
| 662 | ), |
| 663 | Effect.catchTag("NotFound", () => Effect.void), |
| 664 | Effect.retry({ |
| 665 | while: (error) => error._tag === "Conflict", |
| 666 | times: 8, |
| 667 | schedule: Schedule.spaced("2 seconds"), |
| 668 | }), |
| 669 | ); |
| 670 | yield* waitUntilGone(project, region, output.interconnectAttachmentName); |
| 671 | }), |
| 672 | }); |