The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as networkservices from "@distilled.cloud/gcp/networkservices_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | DEFAULT_GLOBAL, |
| 14 | changedFields, |
| 15 | collectPages, |
| 16 | hasAlchemyLabelKeys, |
| 17 | normalizeLocation, |
| 18 | parentOf, |
| 19 | parseName, |
| 20 | resourceName, |
| 21 | rfc1035, |
| 22 | sameNumberList, |
| 23 | sameStringList, |
| 24 | toPhysicalId, |
| 25 | userLabels, |
| 26 | waitForOperation, |
| 27 | waitUntilGone, |
| 28 | waitUntilPresent, |
| 29 | } from "./internal.ts"; |
| 30 | |
| 31 | const COLLECTION = "gateways"; |
| 32 | const DEFAULT_TYPE = "OPEN_MESH"; |
| 33 | const DEFAULT_PORTS = [443]; |
| 34 | |
| 35 | export type GatewayType = networkservices.GatewayTypeEnum | (string & {}); |
| 36 | export type GatewayIpVersion = networkservices.GatewayIpVersionEnum | (string & {}); |
| 37 | export type GatewayEnvoyHeaders = networkservices.GatewayEnvoyHeadersEnum | (string & {}); |
| 38 | export type GatewayRoutingMode = networkservices.GatewayRoutingModeEnum | (string & {}); |
| 39 | |
| 40 | export type GatewayProps = { |
| 41 | /** |
| 42 | * Gateway id (the `{gateway}` segment of |
| 43 | * `projects/{project}/locations/{location}/gateways/{gateway}`). If |
| 44 | * omitted, a unique name is generated from the stack, stage, and |
| 45 | * logical id. Must be 1-63 characters and not start with a number. |
| 46 | * Immutable — changing it replaces the gateway. |
| 47 | */ |
| 48 | gatewayId?: string; |
| 49 | /** |
| 50 | * Location (`global`, `us-central1`, …). Immutable — changing it |
| 51 | * replaces the gateway. `US-CENTRAL1` is accepted and normalized to |
| 52 | * `us-central1`. |
| 53 | * @default "global" |
| 54 | */ |
| 55 | location?: string; |
| 56 | /** |
| 57 | * Customer-managed gateway type. Immutable — changing it replaces the |
| 58 | * gateway. |
| 59 | * @default "OPEN_MESH" |
| 60 | */ |
| 61 | type?: GatewayType; |
| 62 | /** |
| 63 | * Ports (1-65535) the proxy binds. `SECURE_WEB_GATEWAY` is limited to |
| 64 | * 5 ports. Mutually exclusive with `allPorts`. |
| 65 | * @default [443] |
| 66 | */ |
| 67 | ports?: number[]; |
| 68 | /** |
| 69 | * Listen on every port. Mutually exclusive with `ports`. Only applies |
| 70 | * to `SECURE_WEB_GATEWAY`. |
| 71 | * @default false |
| 72 | */ |
| 73 | allPorts?: boolean; |
| 74 | /** |
| 75 | * Zero or one IPv4/IPv6 address. Empty allocates from the subnetwork. |
| 76 | * Only applies to `SECURE_WEB_GATEWAY`. |
| 77 | */ |
| 78 | addresses?: string[]; |
| 79 | /** |
| 80 | * Merge key for configuration across Gateway instances. Max 64 |
| 81 | * characters; must start with a letter. Defaults to the gateway id. |
| 82 | * Immutable — changing it replaces the gateway. |
| 83 | */ |
| 84 | scope?: string; |
| 85 | /** |
| 86 | * ServerTlsPolicy URL used to terminate TLS. Empty disables TLS |
| 87 | * termination. |
| 88 | */ |
| 89 | serverTlsPolicy?: string; |
| 90 | /** |
| 91 | * Certificate Manager certificate URLs presented during TLS. Only |
| 92 | * applies to `SECURE_WEB_GATEWAY`. |
| 93 | */ |
| 94 | certificateUrls?: string[]; |
| 95 | /** |
| 96 | * GatewaySecurityPolicy URL applied to inbound connections. Only |
| 97 | * applies to `SECURE_WEB_GATEWAY`. |
| 98 | */ |
| 99 | gatewaySecurityPolicy?: string; |
| 100 | /** |
| 101 | * VPC network resource name, e.g. |
| 102 | * `projects/{project}/global/networks/{network}`. Immutable. Only |
| 103 | * applies to `SECURE_WEB_GATEWAY`. |
| 104 | */ |
| 105 | network?: string; |
| 106 | /** |
| 107 | * Subnetwork resource name, e.g. |
| 108 | * `projects/{project}/regions/{region}/subnetworks/{subnetwork}`. |
| 109 | * Immutable. Only applies to `SECURE_WEB_GATEWAY`. |
| 110 | */ |
| 111 | subnetwork?: string; |
| 112 | /** |
| 113 | * IP version (`IPV4` or `IPV6`). |
| 114 | */ |
| 115 | ipVersion?: GatewayIpVersion; |
| 116 | /** |
| 117 | * Whether Envoy inserts internal debug headers on upstream requests. |
| 118 | */ |
| 119 | envoyHeaders?: GatewayEnvoyHeaders; |
| 120 | /** |
| 121 | * Routing mode. Required for `SECURE_WEB_GATEWAY`. |
| 122 | */ |
| 123 | routingMode?: GatewayRoutingMode; |
| 124 | /** |
| 125 | * Allow clients outside the gateway region. Only applies to |
| 126 | * `SECURE_WEB_GATEWAY`. |
| 127 | * @default false |
| 128 | */ |
| 129 | allowGlobalAccess?: boolean; |
| 130 | /** |
| 131 | * Human-readable description. Max length 1024 characters. |
| 132 | */ |
| 133 | description?: string; |
| 134 | /** |
| 135 | * User labels. Alchemy ownership labels are merged in automatically. |
| 136 | */ |
| 137 | labels?: Record<string, string>; |
| 138 | }; |
| 139 | |
| 140 | export type Gateway = Resource< |
| 141 | "GCP.NetworkServices.Gateway", |
| 142 | GatewayProps, |
| 143 | { |
| 144 | /** Full resource name `projects/{project}/locations/{location}/gateways/{gateway}`. */ |
| 145 | name: string; |
| 146 | /** Gateway id (last path segment). */ |
| 147 | gatewayId: string; |
| 148 | /** Project id. */ |
| 149 | project: string; |
| 150 | /** Location id (`global`, `us-central1`, …). */ |
| 151 | location: string; |
| 152 | /** Server-defined resource URL. */ |
| 153 | selfLink: string | undefined; |
| 154 | /** Gateway type. */ |
| 155 | type: string; |
| 156 | /** Bound ports. */ |
| 157 | ports: number[]; |
| 158 | /** Whether the gateway listens on every port. */ |
| 159 | allPorts: boolean; |
| 160 | /** Bound addresses. */ |
| 161 | addresses: string[]; |
| 162 | /** Configuration merge scope. */ |
| 163 | scope: string | undefined; |
| 164 | /** ServerTlsPolicy URL, if set. */ |
| 165 | serverTlsPolicy: string | undefined; |
| 166 | /** Certificate Manager certificate URLs. */ |
| 167 | certificateUrls: string[]; |
| 168 | /** GatewaySecurityPolicy URL, if set. */ |
| 169 | gatewaySecurityPolicy: string | undefined; |
| 170 | /** VPC network resource name, if set. */ |
| 171 | network: string | undefined; |
| 172 | /** Subnetwork resource name, if set. */ |
| 173 | subnetwork: string | undefined; |
| 174 | /** IP version, if set. */ |
| 175 | ipVersion: string | undefined; |
| 176 | /** Envoy debug-header setting, if set. */ |
| 177 | envoyHeaders: string | undefined; |
| 178 | /** Routing mode, if set. */ |
| 179 | routingMode: string | undefined; |
| 180 | /** Whether clients outside the region are allowed. */ |
| 181 | allowGlobalAccess: boolean; |
| 182 | /** User-provided description. */ |
| 183 | description: string | undefined; |
| 184 | /** User labels (Alchemy ownership labels stripped). */ |
| 185 | labels: Record<string, string>; |
| 186 | /** RFC3339 creation timestamp. */ |
| 187 | createTime: string | undefined; |
| 188 | /** RFC3339 last-update timestamp. */ |
| 189 | updateTime: string | undefined; |
| 190 | }, |
| 191 | never, |
| 192 | Providers |
| 193 | >; |
| 194 | |
| 195 | /** |
| 196 | * A Network Services Gateway — the ip:port a Mesh or Secure Web Gateway |
| 197 | * proxy listens on, plus TLS and routing policy. |
| 198 | * |
| 199 | * Changing `gatewayId`, `location`, `type`, `network`, or `subnetwork` |
| 200 | * replaces the gateway. Description, labels, ports, scope, TLS, and |
| 201 | * routing fields update in place. |
| 202 | * |
| 203 | * ### Creating a Gateway |
| 204 | * **Example:** Open mesh gateway |
| 205 | * ```typescript |
| 206 | * const gateway = yield* GCP.NetworkServices.Gateway("Mesh", { |
| 207 | * type: "OPEN_MESH", |
| 208 | * ports: [443], |
| 209 | * }); |
| 210 | * ``` |
| 211 | * |
| 212 | * **Example:** Named gateway with labels |
| 213 | * ```typescript |
| 214 | * const gateway = yield* GCP.NetworkServices.Gateway("Mesh", { |
| 215 | * gatewayId: "app-mesh", |
| 216 | * description: "prod mesh", |
| 217 | * labels: { env: "prod" }, |
| 218 | * type: "OPEN_MESH", |
| 219 | * ports: [80, 443], |
| 220 | * scope: "prod", |
| 221 | * }); |
| 222 | * ``` |
| 223 | * |
| 224 | * @resource |
| 225 | * @category NetworkServices |
| 226 | */ |
| 227 | export const Gateway = Resource<Gateway>("GCP.NetworkServices.Gateway"); |
| 228 | |
| 229 | const toAttrs = (gateway: networkservices.Gateway, project: string) => { |
| 230 | const name = gateway.name ?? ""; |
| 231 | const parsed = parseName(name, COLLECTION, DEFAULT_GLOBAL); |
| 232 | return { |
| 233 | name, |
| 234 | gatewayId: parsed.id, |
| 235 | project: parsed.project || project, |
| 236 | location: parsed.location || DEFAULT_GLOBAL, |
| 237 | selfLink: gateway.selfLink, |
| 238 | type: gateway.type ?? DEFAULT_TYPE, |
| 239 | ports: gateway.ports ?? [], |
| 240 | allPorts: gateway.allPorts === true, |
| 241 | addresses: gateway.addresses ?? [], |
| 242 | scope: gateway.scope, |
| 243 | serverTlsPolicy: gateway.serverTlsPolicy, |
| 244 | certificateUrls: gateway.certificateUrls ?? [], |
| 245 | gatewaySecurityPolicy: gateway.gatewaySecurityPolicy, |
| 246 | network: gateway.network, |
| 247 | subnetwork: gateway.subnetwork, |
| 248 | ipVersion: gateway.ipVersion, |
| 249 | envoyHeaders: gateway.envoyHeaders, |
| 250 | routingMode: gateway.routingMode, |
| 251 | allowGlobalAccess: gateway.allowGlobalAccess === true, |
| 252 | description: gateway.description, |
| 253 | labels: userLabels(gateway.labels), |
| 254 | createTime: gateway.createTime, |
| 255 | updateTime: gateway.updateTime, |
| 256 | }; |
| 257 | }; |
| 258 | |
| 259 | const getByName = (name: string) => |
| 260 | networkservices |
| 261 | .getProjectsLocationsGateways({ name }) |
| 262 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 263 | |
| 264 | export const GatewayProvider = () => |
| 265 | Provider.succeed(Gateway, { |
| 266 | stables: ["name", "gatewayId", "project", "location", "selfLink", "createTime"], |
| 267 | |
| 268 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 269 | if (!isResolved(news)) return undefined; |
| 270 | const previousId = olds?.gatewayId ?? output?.gatewayId; |
| 271 | const nextId = news.gatewayId ? rfc1035(news.gatewayId, "gateway") : previousId; |
| 272 | const previousLocation = normalizeLocation( |
| 273 | olds?.location ?? output?.location, |
| 274 | DEFAULT_GLOBAL, |
| 275 | ); |
| 276 | const nextLocation = normalizeLocation( |
| 277 | news.location ?? olds?.location ?? output?.location, |
| 278 | DEFAULT_GLOBAL, |
| 279 | ); |
| 280 | const previousType = olds?.type ?? output?.type ?? DEFAULT_TYPE; |
| 281 | const nextType = news.type ?? previousType; |
| 282 | const previousNetwork = olds?.network ?? output?.network ?? ""; |
| 283 | const nextNetwork = news.network ?? previousNetwork; |
| 284 | const previousSubnetwork = olds?.subnetwork ?? output?.subnetwork ?? ""; |
| 285 | const nextSubnetwork = news.subnetwork ?? previousSubnetwork; |
| 286 | const previousScope = olds?.scope ?? output?.scope ?? previousId ?? ""; |
| 287 | const nextScope = news.scope ?? previousScope; |
| 288 | if ( |
| 289 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 290 | previousLocation !== nextLocation || |
| 291 | previousType !== nextType || |
| 292 | previousNetwork !== nextNetwork || |
| 293 | previousSubnetwork !== nextSubnetwork || |
| 294 | (previousScope.length > 0 && nextScope !== previousScope) |
| 295 | ) { |
| 296 | return { action: "replace" as const }; |
| 297 | } |
| 298 | return undefined; |
| 299 | }), |
| 300 | |
| 301 | read: Effect.fn(function* ({ id, olds, output }) { |
| 302 | const env = yield* GcpEnvironment.current; |
| 303 | const gatewayId = yield* toPhysicalId(id, olds?.gatewayId, output?.gatewayId, "gateway"); |
| 304 | const location = normalizeLocation(olds?.location ?? output?.location, DEFAULT_GLOBAL); |
| 305 | const name = output?.name ?? resourceName(env.project, location, COLLECTION, gatewayId); |
| 306 | const existing = yield* getByName(name); |
| 307 | if (existing === undefined) return undefined; |
| 308 | const attrs = toAttrs(existing, env.project); |
| 309 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 310 | }), |
| 311 | |
| 312 | list: () => |
| 313 | Effect.gen(function* () { |
| 314 | const env = yield* GcpEnvironment.current; |
| 315 | const items = yield* collectPages( |
| 316 | networkservices.listProjectsLocationsGateways.pages({ |
| 317 | parent: parentOf(env.project, "-"), |
| 318 | pageSize: 1000, |
| 319 | }), |
| 320 | (page) => page.gateways, |
| 321 | ); |
| 322 | return items |
| 323 | .filter((item) => hasAlchemyLabelKeys(item.labels)) |
| 324 | .map((item) => toAttrs(item, env.project)); |
| 325 | }), |
| 326 | |
| 327 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 328 | const env = yield* GcpEnvironment.current; |
| 329 | const gatewayId = yield* toPhysicalId(id, news.gatewayId, output?.gatewayId, "gateway"); |
| 330 | const location = normalizeLocation(news.location ?? output?.location, DEFAULT_GLOBAL); |
| 331 | const name = resourceName(env.project, location, COLLECTION, gatewayId); |
| 332 | const desiredLabels = { |
| 333 | ...toLabels(news.labels), |
| 334 | ...(yield* createInternalLabels(id)), |
| 335 | }; |
| 336 | const type = news.type ?? DEFAULT_TYPE; |
| 337 | const allPorts = news.allPorts === true; |
| 338 | const ports = allPorts ? undefined : (news.ports ?? DEFAULT_PORTS); |
| 339 | const allowGlobalAccess = news.allowGlobalAccess === true; |
| 340 | const scope = news.scope ?? gatewayId; |
| 341 | |
| 342 | let current = yield* getByName(name); |
| 343 | |
| 344 | if (current === undefined) { |
| 345 | const created = yield* networkservices |
| 346 | .createProjectsLocationsGateways({ |
| 347 | parent: parentOf(env.project, location), |
| 348 | gatewayId, |
| 349 | body: { |
| 350 | type, |
| 351 | labels: desiredLabels, |
| 352 | description: news.description, |
| 353 | ports, |
| 354 | allPorts: allPorts ? true : undefined, |
| 355 | addresses: news.addresses, |
| 356 | scope, |
| 357 | serverTlsPolicy: news.serverTlsPolicy, |
| 358 | certificateUrls: news.certificateUrls, |
| 359 | gatewaySecurityPolicy: news.gatewaySecurityPolicy, |
| 360 | network: news.network, |
| 361 | subnetwork: news.subnetwork, |
| 362 | ipVersion: news.ipVersion, |
| 363 | envoyHeaders: news.envoyHeaders, |
| 364 | routingMode: news.routingMode, |
| 365 | allowGlobalAccess: allowGlobalAccess ? true : undefined, |
| 366 | }, |
| 367 | }) |
| 368 | .pipe( |
| 369 | Effect.retry({ |
| 370 | while: (error) => error._tag === "Conflict", |
| 371 | times: 5, |
| 372 | schedule: Schedule.spaced("2 seconds"), |
| 373 | }), |
| 374 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 375 | ); |
| 376 | if (created !== undefined) { |
| 377 | yield* waitForOperation(created); |
| 378 | } |
| 379 | current = yield* waitUntilPresent(getByName(name), name); |
| 380 | } |
| 381 | |
| 382 | const observedLabels = tagRecord(current.labels); |
| 383 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 384 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 385 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 386 | const portsChanged = !sameNumberList(current.ports, ports ?? []); |
| 387 | const allPortsChanged = (current.allPorts === true) !== allPorts; |
| 388 | const addressesChanged = !sameStringList(current.addresses, news.addresses); |
| 389 | const scopeChanged = (current.scope ?? "") !== scope; |
| 390 | const serverTlsChanged = (current.serverTlsPolicy ?? "") !== (news.serverTlsPolicy ?? ""); |
| 391 | const certsChanged = !sameStringList(current.certificateUrls, news.certificateUrls); |
| 392 | const securityChanged = |
| 393 | (current.gatewaySecurityPolicy ?? "") !== (news.gatewaySecurityPolicy ?? ""); |
| 394 | const ipVersionChanged = (current.ipVersion ?? "") !== (news.ipVersion ?? ""); |
| 395 | const envoyChanged = (current.envoyHeaders ?? "") !== (news.envoyHeaders ?? ""); |
| 396 | const routingChanged = (current.routingMode ?? "") !== (news.routingMode ?? ""); |
| 397 | const globalAccessChanged = (current.allowGlobalAccess === true) !== allowGlobalAccess; |
| 398 | |
| 399 | const updateMask = changedFields([ |
| 400 | ["labels", labelsChanged], |
| 401 | ["description", descriptionChanged], |
| 402 | ["ports", portsChanged], |
| 403 | ["allPorts", allPortsChanged], |
| 404 | ["addresses", addressesChanged], |
| 405 | ["scope", scopeChanged], |
| 406 | ["serverTlsPolicy", serverTlsChanged], |
| 407 | ["certificateUrls", certsChanged], |
| 408 | ["gatewaySecurityPolicy", securityChanged], |
| 409 | ["ipVersion", ipVersionChanged], |
| 410 | ["envoyHeaders", envoyChanged], |
| 411 | ["routingMode", routingChanged], |
| 412 | ["allowGlobalAccess", globalAccessChanged], |
| 413 | ]); |
| 414 | |
| 415 | if (updateMask.length > 0) { |
| 416 | const operation = yield* networkservices.patchProjectsLocationsGateways({ |
| 417 | name: current.name ?? name, |
| 418 | updateMask: updateMask.join(","), |
| 419 | body: { |
| 420 | name: current.name ?? name, |
| 421 | type, |
| 422 | labels: desiredLabels, |
| 423 | description: news.description, |
| 424 | ports, |
| 425 | allPorts: allPorts ? true : false, |
| 426 | addresses: news.addresses ?? [], |
| 427 | scope, |
| 428 | serverTlsPolicy: news.serverTlsPolicy, |
| 429 | certificateUrls: news.certificateUrls ?? [], |
| 430 | gatewaySecurityPolicy: news.gatewaySecurityPolicy, |
| 431 | ipVersion: news.ipVersion, |
| 432 | envoyHeaders: news.envoyHeaders, |
| 433 | routingMode: news.routingMode, |
| 434 | allowGlobalAccess, |
| 435 | }, |
| 436 | }); |
| 437 | yield* waitForOperation(operation); |
| 438 | current = yield* waitUntilPresent(getByName(current.name ?? name), current.name ?? name); |
| 439 | } |
| 440 | |
| 441 | return toAttrs(current, env.project); |
| 442 | }), |
| 443 | |
| 444 | delete: Effect.fn(function* ({ output }) { |
| 445 | const operation = yield* networkservices |
| 446 | .deleteProjectsLocationsGateways({ name: output.name }) |
| 447 | .pipe( |
| 448 | Effect.retry({ |
| 449 | while: (error) => error._tag === "Conflict", |
| 450 | times: 8, |
| 451 | schedule: Schedule.spaced("2 seconds"), |
| 452 | }), |
| 453 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 454 | ); |
| 455 | if (operation !== undefined) { |
| 456 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 457 | } |
| 458 | yield* waitUntilGone(getByName(output.name), output.name); |
| 459 | }), |
| 460 | }); |