The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as workstations from "@distilled.cloud/gcp/workstations_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 | expandParent, |
| 14 | fieldMask, |
| 15 | fingerprint, |
| 16 | listAtNested, |
| 17 | listLabeledPages, |
| 18 | normalizeLocation, |
| 19 | parseName, |
| 20 | replaceOnIdentity, |
| 21 | ResourceNotResolved, |
| 22 | sameBool, |
| 23 | sameNumber, |
| 24 | sameText, |
| 25 | stringMap, |
| 26 | toPhysicalId, |
| 27 | userLabels, |
| 28 | waitForOperation, |
| 29 | waitUntilExists, |
| 30 | waitUntilGone, |
| 31 | } from "./internal.ts"; |
| 32 | |
| 33 | export type Container = { |
| 34 | /** |
| 35 | * Docker image that defines the workstation environment. Public images |
| 36 | * or Artifact Registry images the host service account can pull. |
| 37 | */ |
| 38 | image?: string; |
| 39 | /** |
| 40 | * Environment variables passed to the container entrypoint. |
| 41 | */ |
| 42 | env?: Record<string, string>; |
| 43 | /** |
| 44 | * Override the image ENTRYPOINT. |
| 45 | */ |
| 46 | command?: string[]; |
| 47 | /** |
| 48 | * Arguments passed to the entrypoint. |
| 49 | */ |
| 50 | args?: string[]; |
| 51 | /** |
| 52 | * Override the image working directory. |
| 53 | */ |
| 54 | workingDir?: string; |
| 55 | /** |
| 56 | * Run the container as this uid. |
| 57 | */ |
| 58 | runAsUser?: number; |
| 59 | }; |
| 60 | |
| 61 | export type ReadinessCheck = { |
| 62 | /** Path the readiness probe requests. */ |
| 63 | path?: string; |
| 64 | /** Port the readiness probe requests. */ |
| 65 | port?: number; |
| 66 | }; |
| 67 | |
| 68 | export type Accelerator = { |
| 69 | /** Accelerator type (`nvidia-tesla-p100`, …). */ |
| 70 | type?: string; |
| 71 | /** Number of accelerator cards. */ |
| 72 | count?: number; |
| 73 | }; |
| 74 | |
| 75 | export type GceShieldedInstanceConfig = { |
| 76 | /** Enable Secure Boot. */ |
| 77 | enableSecureBoot?: boolean; |
| 78 | /** Enable vTPM. */ |
| 79 | enableVtpm?: boolean; |
| 80 | /** Enable integrity monitoring. */ |
| 81 | enableIntegrityMonitoring?: boolean; |
| 82 | }; |
| 83 | |
| 84 | export type GceConfidentialInstanceConfig = { |
| 85 | /** Enable Confidential Compute. */ |
| 86 | enableConfidentialCompute?: boolean; |
| 87 | }; |
| 88 | |
| 89 | export type BoostConfig = { |
| 90 | /** Boost configuration id. Required when listed. */ |
| 91 | id?: string; |
| 92 | /** Machine type for boosted VMs. */ |
| 93 | machineType?: string; |
| 94 | /** Idle boost VMs to keep ready. */ |
| 95 | poolSize?: number; |
| 96 | /** Boot disk size in GB (minimum 30). */ |
| 97 | bootDiskSizeGb?: number; |
| 98 | /** Enable nested virtualization on boosted VMs. */ |
| 99 | enableNestedVirtualization?: boolean; |
| 100 | /** Accelerators attached to boosted VMs. */ |
| 101 | accelerators?: Accelerator[]; |
| 102 | }; |
| 103 | |
| 104 | export type GceInstance = { |
| 105 | /** |
| 106 | * Machine type (`e2-standard-4`, …). |
| 107 | */ |
| 108 | machineType?: string; |
| 109 | /** |
| 110 | * Boot disk size in GB. Minimum 30, default 50. |
| 111 | */ |
| 112 | bootDiskSizeGb?: number; |
| 113 | /** |
| 114 | * Idle VMs kept ready for fast start. Default 0. |
| 115 | */ |
| 116 | poolSize?: number; |
| 117 | /** |
| 118 | * Disable public IPs. Requires Private Google Access or Cloud NAT. |
| 119 | * @default false |
| 120 | */ |
| 121 | disablePublicIpAddresses?: boolean; |
| 122 | /** |
| 123 | * Disable SSH access to the VM. |
| 124 | */ |
| 125 | disableSsh?: boolean; |
| 126 | /** |
| 127 | * Enable nested virtualization (N1/N2 machine series only). |
| 128 | */ |
| 129 | enableNestedVirtualization?: boolean; |
| 130 | /** |
| 131 | * Service account email for workstation VMs. |
| 132 | */ |
| 133 | serviceAccount?: string; |
| 134 | /** |
| 135 | * Scopes granted to `serviceAccount`. |
| 136 | */ |
| 137 | serviceAccountScopes?: string[]; |
| 138 | /** |
| 139 | * Network tags applied to workstation VMs. |
| 140 | */ |
| 141 | tags?: string[]; |
| 142 | /** |
| 143 | * Resource Manager tags bound to the VM |
| 144 | * (`tagKeys/{id}` → `tagValues/{id}`). |
| 145 | */ |
| 146 | vmTags?: Record<string, string>; |
| 147 | /** |
| 148 | * Custom instance metadata. |
| 149 | */ |
| 150 | instanceMetadata?: Record<string, string>; |
| 151 | /** |
| 152 | * GCS URI of a startup script (`gs://{bucket}/{object}`). |
| 153 | */ |
| 154 | startupScriptUri?: string; |
| 155 | /** |
| 156 | * Shielded VM options. |
| 157 | */ |
| 158 | shieldedInstanceConfig?: GceShieldedInstanceConfig; |
| 159 | /** |
| 160 | * Confidential VM options. |
| 161 | */ |
| 162 | confidentialInstanceConfig?: GceConfidentialInstanceConfig; |
| 163 | /** |
| 164 | * Accelerators attached to the VM. |
| 165 | */ |
| 166 | accelerators?: Accelerator[]; |
| 167 | /** |
| 168 | * Boost configurations users may choose at start. |
| 169 | */ |
| 170 | boostConfigs?: BoostConfig[]; |
| 171 | }; |
| 172 | |
| 173 | export type Host = { |
| 174 | /** Compute Engine VM that backs workstations. */ |
| 175 | gceInstance?: GceInstance; |
| 176 | }; |
| 177 | |
| 178 | export type CustomerEncryptionKey = { |
| 179 | /** |
| 180 | * Cloud KMS key |
| 181 | * (`projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}`). |
| 182 | * Immutable. |
| 183 | */ |
| 184 | kmsKey?: string; |
| 185 | /** |
| 186 | * Service account used with the KMS key. Immutable. |
| 187 | */ |
| 188 | kmsKeyServiceAccount?: string; |
| 189 | }; |
| 190 | |
| 191 | export type GcePersistentDisk = { |
| 192 | /** Source disk image. Mutually exclusive with `sourceSnapshot`. */ |
| 193 | sourceImage?: string; |
| 194 | /** Source snapshot. Mutually exclusive with `sourceImage`. */ |
| 195 | sourceSnapshot?: string; |
| 196 | /** When true the disk is read-only and may be shared. */ |
| 197 | readOnly?: boolean; |
| 198 | /** |
| 199 | * Disk type. |
| 200 | * @default "pd-standard" |
| 201 | */ |
| 202 | diskType?: string; |
| 203 | }; |
| 204 | |
| 205 | export type EphemeralDirectory = { |
| 206 | /** Mount path inside the workstation. */ |
| 207 | mountPath?: string; |
| 208 | /** Persistent-disk backing for the ephemeral directory. */ |
| 209 | gcePd?: GcePersistentDisk; |
| 210 | }; |
| 211 | |
| 212 | export type GceRegionalPersistentDisk = { |
| 213 | /** |
| 214 | * Capacity in GB (`10`, `50`, `100`, `200`, `500`, or `1000`). |
| 215 | * Default 200. |
| 216 | */ |
| 217 | sizeGb?: number; |
| 218 | /** |
| 219 | * Disk type (`pd-standard`, `pd-balanced`, `pd-ssd`). |
| 220 | * @default "pd-standard" |
| 221 | */ |
| 222 | diskType?: string; |
| 223 | /** |
| 224 | * File system type. |
| 225 | * @default "ext4" |
| 226 | */ |
| 227 | fsType?: string; |
| 228 | /** |
| 229 | * Whether to delete the disk when the workstation is deleted. |
| 230 | * @default "DELETE" |
| 231 | */ |
| 232 | reclaimPolicy?: workstations.GceRegionalPersistentDiskReclaimPolicyEnum | (string & {}); |
| 233 | /** Snapshot used as the disk source. */ |
| 234 | sourceSnapshot?: string; |
| 235 | /** |
| 236 | * Seconds to wait before archiving the disk as a snapshot (`"0s"` |
| 237 | * disables archival). |
| 238 | */ |
| 239 | archiveTimeout?: string; |
| 240 | /** Maximum resize size in GB. */ |
| 241 | maxSizeGb?: number; |
| 242 | }; |
| 243 | |
| 244 | export type GceHyperdiskBalancedHighAvailability = { |
| 245 | /** |
| 246 | * Capacity in GB (`10`, `50`, `100`, `200`, `500`, or `1000`). |
| 247 | * Default 200. |
| 248 | */ |
| 249 | sizeGb?: number; |
| 250 | /** |
| 251 | * Whether to delete the disk when the workstation is deleted. |
| 252 | * @default "DELETE" |
| 253 | */ |
| 254 | reclaimPolicy?: |
| 255 | | workstations.GceHyperdiskBalancedHighAvailabilityReclaimPolicyEnum |
| 256 | | (string & {}); |
| 257 | /** Snapshot used as the disk source. */ |
| 258 | sourceSnapshot?: string; |
| 259 | /** |
| 260 | * Seconds to wait before archiving the disk as a snapshot. |
| 261 | */ |
| 262 | archiveTimeout?: string; |
| 263 | /** Maximum resize size in GB. */ |
| 264 | maxSizeGb?: number; |
| 265 | }; |
| 266 | |
| 267 | export type PersistentDirectory = { |
| 268 | /** Mount path inside the workstation. */ |
| 269 | mountPath?: string; |
| 270 | /** Regional persistent disk backing. */ |
| 271 | gcePd?: GceRegionalPersistentDisk; |
| 272 | /** Hyperdisk Balanced HA backing. */ |
| 273 | gceHd?: GceHyperdiskBalancedHighAvailability; |
| 274 | }; |
| 275 | |
| 276 | export type PortRange = { |
| 277 | /** Inclusive start port (22, 80, or 1024-65535). */ |
| 278 | first?: number; |
| 279 | /** Inclusive end port (22, 80, or 1024-65535). */ |
| 280 | last?: number; |
| 281 | }; |
| 282 | |
| 283 | export type WorkstationClustersWorkstationConfigProps = { |
| 284 | /** |
| 285 | * Parent workstation cluster. Full name |
| 286 | * `projects/{project}/locations/{location}/workstationClusters/{workstationCluster}` |
| 287 | * or a cluster id (combined with `location`). Immutable — changing it |
| 288 | * replaces the configuration. |
| 289 | */ |
| 290 | workstationCluster: string; |
| 291 | /** |
| 292 | * Configuration id (the `{workstationConfig}` segment). If omitted, a |
| 293 | * unique RFC1035 name is generated from the stack, stage, and logical |
| 294 | * id. Immutable — changing it replaces the configuration. |
| 295 | */ |
| 296 | workstationConfigId?: string; |
| 297 | /** |
| 298 | * Region used when `workstationCluster` is a bare id. |
| 299 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 300 | */ |
| 301 | location?: string; |
| 302 | /** |
| 303 | * Human-readable display name. |
| 304 | */ |
| 305 | displayName?: string; |
| 306 | /** |
| 307 | * User labels. Alchemy ownership labels are merged in automatically. |
| 308 | */ |
| 309 | labels?: Record<string, string>; |
| 310 | /** |
| 311 | * Client-specified annotations. |
| 312 | */ |
| 313 | annotations?: Record<string, string>; |
| 314 | /** |
| 315 | * Container that runs on each workstation using this configuration. |
| 316 | */ |
| 317 | container?: Container; |
| 318 | /** |
| 319 | * Runtime host for workstations created from this configuration. |
| 320 | */ |
| 321 | host?: Host; |
| 322 | /** |
| 323 | * Readiness checks that must return 200 before the workstation is |
| 324 | * marked running. |
| 325 | */ |
| 326 | readinessChecks?: ReadinessCheck[]; |
| 327 | /** |
| 328 | * Idle timeout before automatic stop (`"1200s"` default, `"0s"` never). |
| 329 | */ |
| 330 | idleTimeout?: string; |
| 331 | /** |
| 332 | * Maximum running time (`"43200s"` default, `"0s"` never). |
| 333 | */ |
| 334 | runningTimeout?: string; |
| 335 | /** |
| 336 | * Directories persisted across workstation sessions. |
| 337 | */ |
| 338 | persistentDirectories?: PersistentDirectory[]; |
| 339 | /** |
| 340 | * Directories that are recreated on every start. |
| 341 | */ |
| 342 | ephemeralDirectories?: EphemeralDirectory[]; |
| 343 | /** |
| 344 | * Customer-managed encryption key. Immutable. |
| 345 | */ |
| 346 | encryptionKey?: CustomerEncryptionKey; |
| 347 | /** |
| 348 | * Zones used to replicate VM and disk resources. Exactly two zones in |
| 349 | * the cluster region. Immutable. |
| 350 | */ |
| 351 | replicaZones?: string[]; |
| 352 | /** |
| 353 | * Maximum workstations a user may have `workstations.workstation.use` |
| 354 | * on. `0` is unlimited. |
| 355 | */ |
| 356 | maxUsableWorkstations?: number; |
| 357 | /** |
| 358 | * Enable Linux `auditd` logging. Requires a host service account with |
| 359 | * log/metric writer roles. |
| 360 | */ |
| 361 | enableAuditAgent?: boolean; |
| 362 | /** |
| 363 | * Disable the websocket TCP relay (SSH and other plain TCP). |
| 364 | */ |
| 365 | disableTcpConnections?: boolean; |
| 366 | /** |
| 367 | * Ports exposed on the workstation. Defaults to 22, 80, and 1024-65535. |
| 368 | */ |
| 369 | allowedPorts?: PortRange[]; |
| 370 | /** |
| 371 | * Grant the creator `roles/workstations.policyAdmin` on workstations |
| 372 | * they create. |
| 373 | * @default false |
| 374 | */ |
| 375 | grantWorkstationAdminRoleOnCreate?: boolean; |
| 376 | }; |
| 377 | |
| 378 | export type WorkstationClustersWorkstationConfig = Resource< |
| 379 | "GCP.Workstations.WorkstationClustersWorkstationConfig", |
| 380 | WorkstationClustersWorkstationConfigProps, |
| 381 | { |
| 382 | /** Full resource name. */ |
| 383 | name: string; |
| 384 | /** Configuration id (last path segment). */ |
| 385 | workstationConfigId: string; |
| 386 | /** Parent cluster name. */ |
| 387 | workstationCluster: string; |
| 388 | /** Project id. */ |
| 389 | project: string; |
| 390 | /** Location id. */ |
| 391 | location: string; |
| 392 | /** Human-readable display name. */ |
| 393 | displayName: string | undefined; |
| 394 | /** User labels (Alchemy ownership labels stripped). */ |
| 395 | labels: Record<string, string>; |
| 396 | /** Client-specified annotations. */ |
| 397 | annotations: Record<string, string>; |
| 398 | /** Container configuration. */ |
| 399 | container: Container | undefined; |
| 400 | /** Runtime host configuration. */ |
| 401 | host: Host | undefined; |
| 402 | /** Readiness checks. */ |
| 403 | readinessChecks: ReadinessCheck[] | undefined; |
| 404 | /** Idle timeout. */ |
| 405 | idleTimeout: string | undefined; |
| 406 | /** Running timeout. */ |
| 407 | runningTimeout: string | undefined; |
| 408 | /** Persistent directories. */ |
| 409 | persistentDirectories: PersistentDirectory[] | undefined; |
| 410 | /** Ephemeral directories. */ |
| 411 | ephemeralDirectories: EphemeralDirectory[] | undefined; |
| 412 | /** Customer-managed encryption key. */ |
| 413 | encryptionKey: CustomerEncryptionKey | undefined; |
| 414 | /** Replica zones. */ |
| 415 | replicaZones: string[] | undefined; |
| 416 | /** Max usable workstations per user. */ |
| 417 | maxUsableWorkstations: number | undefined; |
| 418 | /** Whether auditd logging is enabled. */ |
| 419 | enableAuditAgent: boolean; |
| 420 | /** Whether the TCP relay is disabled. */ |
| 421 | disableTcpConnections: boolean; |
| 422 | /** Exposed port ranges. */ |
| 423 | allowedPorts: PortRange[] | undefined; |
| 424 | /** Whether creators get policyAdmin. */ |
| 425 | grantWorkstationAdminRoleOnCreate: boolean; |
| 426 | /** Whether the configuration is reconciling. */ |
| 427 | reconciling: boolean; |
| 428 | /** Whether the configuration is degraded. */ |
| 429 | degraded: boolean; |
| 430 | /** Server-generated resource uid. */ |
| 431 | uid: string | undefined; |
| 432 | /** RFC3339 creation timestamp. */ |
| 433 | createTime: string | undefined; |
| 434 | /** RFC3339 last-update timestamp. */ |
| 435 | updateTime: string | undefined; |
| 436 | }, |
| 437 | never, |
| 438 | Providers |
| 439 | >; |
| 440 | |
| 441 | /** |
| 442 | * A Cloud Workstations configuration — the template for workstation VMs |
| 443 | * (machine type, image, disks, timeouts). |
| 444 | * |
| 445 | * Changing `workstationConfigId`, `location`, `workstationCluster`, |
| 446 | * `replicaZones`, or `encryptionKey` replaces the configuration. Display |
| 447 | * name, labels, container, host, timeouts, directories, and ports update |
| 448 | * in place. |
| 449 | * |
| 450 | * ### Creating a Configuration |
| 451 | * **Example:** Generated name |
| 452 | * ```typescript |
| 453 | * const config = yield* GCP.Workstations.WorkstationClustersWorkstationConfig( |
| 454 | * "Code", |
| 455 | * { workstationCluster: cluster.name }, |
| 456 | * ); |
| 457 | * ``` |
| 458 | * |
| 459 | * **Example:** Custom machine type and image |
| 460 | * ```typescript |
| 461 | * const config = yield* GCP.Workstations.WorkstationClustersWorkstationConfig( |
| 462 | * "Code", |
| 463 | * { |
| 464 | * workstationCluster: cluster.name, |
| 465 | * idleTimeout: "1800s", |
| 466 | * host: { gceInstance: { machineType: "e2-standard-4", poolSize: 0 } }, |
| 467 | * labels: { env: "prod" }, |
| 468 | * }, |
| 469 | * ); |
| 470 | * ``` |
| 471 | * |
| 472 | * ### Updating a Configuration |
| 473 | * **Example:** Display name and idle timeout |
| 474 | * ```typescript |
| 475 | * // Same logical id, changed props: the engine updates it in place. |
| 476 | * const config = yield* GCP.Workstations.WorkstationClustersWorkstationConfig( |
| 477 | * "Code", |
| 478 | * { |
| 479 | * workstationCluster: cluster.name, |
| 480 | * displayName: "code v2", |
| 481 | * idleTimeout: "3600s", |
| 482 | * }, |
| 483 | * ); |
| 484 | * ``` |
| 485 | * |
| 486 | * @resource |
| 487 | * @category Workstations |
| 488 | */ |
| 489 | export const WorkstationClustersWorkstationConfig = Resource<WorkstationClustersWorkstationConfig>( |
| 490 | "GCP.Workstations.WorkstationClustersWorkstationConfig", |
| 491 | ); |
| 492 | |
| 493 | const resourceName = (cluster: string, workstationConfigId: string) => |
| 494 | `${cluster}/workstationConfigs/${workstationConfigId}`; |
| 495 | |
| 496 | const toContainer = ( |
| 497 | value: workstations.Container | Container | undefined, |
| 498 | ): Container | undefined => |
| 499 | value === undefined |
| 500 | ? undefined |
| 501 | : { |
| 502 | image: value.image, |
| 503 | env: stringMap(value.env), |
| 504 | command: value.command, |
| 505 | args: value.args, |
| 506 | workingDir: value.workingDir, |
| 507 | runAsUser: value.runAsUser, |
| 508 | }; |
| 509 | |
| 510 | const toGceInstance = ( |
| 511 | value: workstations.GceInstance | GceInstance | undefined, |
| 512 | ): GceInstance | undefined => |
| 513 | value === undefined |
| 514 | ? undefined |
| 515 | : { |
| 516 | machineType: value.machineType, |
| 517 | bootDiskSizeGb: value.bootDiskSizeGb, |
| 518 | poolSize: value.poolSize, |
| 519 | disablePublicIpAddresses: value.disablePublicIpAddresses, |
| 520 | disableSsh: value.disableSsh, |
| 521 | enableNestedVirtualization: value.enableNestedVirtualization, |
| 522 | serviceAccount: value.serviceAccount, |
| 523 | serviceAccountScopes: value.serviceAccountScopes, |
| 524 | tags: value.tags, |
| 525 | vmTags: stringMap(value.vmTags), |
| 526 | instanceMetadata: stringMap(value.instanceMetadata), |
| 527 | startupScriptUri: value.startupScriptUri, |
| 528 | shieldedInstanceConfig: value.shieldedInstanceConfig, |
| 529 | confidentialInstanceConfig: value.confidentialInstanceConfig, |
| 530 | accelerators: value.accelerators, |
| 531 | boostConfigs: value.boostConfigs, |
| 532 | }; |
| 533 | |
| 534 | const toHost = (value: workstations.Host | Host | undefined): Host | undefined => |
| 535 | value === undefined ? undefined : { gceInstance: toGceInstance(value.gceInstance) }; |
| 536 | |
| 537 | // Reads omit proto3 defaults (`poolSize: 0`, `false`) and add server defaults |
| 538 | // the user never set (scopes, disk size), so compare only the fields the user |
| 539 | // declared, treating an omitted default as equal to its zero value. |
| 540 | const isZero = (value: unknown) => |
| 541 | value === undefined || |
| 542 | value === null || |
| 543 | value === 0 || |
| 544 | value === false || |
| 545 | value === "" || |
| 546 | (Array.isArray(value) && value.length === 0) || |
| 547 | (typeof value === "object" && Object.keys(value as object).length === 0); |
| 548 | |
| 549 | const hostMatches = (observed: workstations.Host | undefined, desired: Host): boolean => { |
| 550 | const want = toGceInstance(desired.gceInstance); |
| 551 | if (want === undefined) return true; |
| 552 | const have = toGceInstance(observed?.gceInstance) ?? {}; |
| 553 | return Object.entries(want).every(([key, value]) => { |
| 554 | if (value === undefined) return true; |
| 555 | const current = have[key as keyof GceInstance]; |
| 556 | return isZero(value) ? isZero(current) : fingerprint(current) === fingerprint(value); |
| 557 | }); |
| 558 | }; |
| 559 | |
| 560 | const toPersistent = ( |
| 561 | value: workstations.PersistentDirectory | PersistentDirectory, |
| 562 | ): PersistentDirectory => ({ |
| 563 | mountPath: value.mountPath, |
| 564 | gcePd: value.gcePd, |
| 565 | gceHd: value.gceHd, |
| 566 | }); |
| 567 | |
| 568 | const toEphemeral = ( |
| 569 | value: workstations.EphemeralDirectory | EphemeralDirectory, |
| 570 | ): EphemeralDirectory => ({ |
| 571 | mountPath: value.mountPath, |
| 572 | gcePd: value.gcePd, |
| 573 | }); |
| 574 | |
| 575 | const toAttrs = (item: workstations.WorkstationConfig, project: string, region: string) => { |
| 576 | const name = item.name ?? ""; |
| 577 | const parsed = parseName(name, "workstationConfigs", region); |
| 578 | return { |
| 579 | name, |
| 580 | workstationConfigId: parsed.id, |
| 581 | workstationCluster: parsed.parent, |
| 582 | project: parsed.project || project, |
| 583 | location: parsed.location, |
| 584 | displayName: item.displayName, |
| 585 | labels: userLabels(item.labels), |
| 586 | annotations: stringMap(item.annotations) ?? {}, |
| 587 | container: toContainer(item.container), |
| 588 | host: toHost(item.host), |
| 589 | readinessChecks: item.readinessChecks, |
| 590 | idleTimeout: item.idleTimeout, |
| 591 | runningTimeout: item.runningTimeout, |
| 592 | persistentDirectories: item.persistentDirectories?.map(toPersistent), |
| 593 | ephemeralDirectories: item.ephemeralDirectories?.map(toEphemeral), |
| 594 | encryptionKey: item.encryptionKey, |
| 595 | replicaZones: item.replicaZones, |
| 596 | maxUsableWorkstations: item.maxUsableWorkstations, |
| 597 | enableAuditAgent: item.enableAuditAgent === true, |
| 598 | disableTcpConnections: item.disableTcpConnections === true, |
| 599 | allowedPorts: item.allowedPorts, |
| 600 | grantWorkstationAdminRoleOnCreate: item.grantWorkstationAdminRoleOnCreate === true, |
| 601 | reconciling: item.reconciling === true, |
| 602 | degraded: item.degraded === true, |
| 603 | uid: item.uid, |
| 604 | createTime: item.createTime, |
| 605 | updateTime: item.updateTime, |
| 606 | }; |
| 607 | }; |
| 608 | |
| 609 | const getByName = (name: string) => |
| 610 | workstations |
| 611 | .getProjectsLocationsWorkstationClustersWorkstationConfigs({ name }) |
| 612 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 613 | |
| 614 | const listOwned = (project: string, region: string) => |
| 615 | listAtNested(project, region, "workstationClusters/-", (parent) => |
| 616 | listLabeledPages( |
| 617 | workstations.listProjectsLocationsWorkstationClustersWorkstationConfigs.pages({ |
| 618 | parent, |
| 619 | pageSize: 1000, |
| 620 | }), |
| 621 | (page) => page.workstationConfigs, |
| 622 | (item) => item.labels, |
| 623 | ), |
| 624 | ); |
| 625 | |
| 626 | const replicaKey = (zones: string[] | undefined) => |
| 627 | fingerprint([...(zones ?? [])].map((zone) => zone.toLowerCase()).sort()); |
| 628 | |
| 629 | export const WorkstationClustersWorkstationConfigProvider = () => |
| 630 | Provider.succeed(WorkstationClustersWorkstationConfig, { |
| 631 | stables: [ |
| 632 | "name", |
| 633 | "workstationConfigId", |
| 634 | "workstationCluster", |
| 635 | "project", |
| 636 | "location", |
| 637 | "uid", |
| 638 | "createTime", |
| 639 | ], |
| 640 | |
| 641 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 642 | if (!isResolved(news)) return undefined; |
| 643 | const env = yield* GcpEnvironment.current; |
| 644 | const previousKey = olds?.encryptionKey ?? output?.encryptionKey; |
| 645 | const previousZones = olds?.replicaZones ?? output?.replicaZones; |
| 646 | return replaceOnIdentity({ |
| 647 | previousId: olds?.workstationConfigId ?? output?.workstationConfigId, |
| 648 | nextId: |
| 649 | news.workstationConfigId ?? olds?.workstationConfigId ?? output?.workstationConfigId, |
| 650 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 651 | nextLocation: normalizeLocation( |
| 652 | news.location ?? olds?.location ?? output?.location, |
| 653 | env.region, |
| 654 | ), |
| 655 | previousParent: olds?.workstationCluster ?? output?.workstationCluster, |
| 656 | nextParent: news.workstationCluster, |
| 657 | extra: |
| 658 | (previousKey?.kmsKey !== undefined && |
| 659 | news.encryptionKey?.kmsKey !== undefined && |
| 660 | previousKey.kmsKey !== news.encryptionKey.kmsKey) || |
| 661 | (previousZones !== undefined && |
| 662 | news.replicaZones !== undefined && |
| 663 | replicaKey(previousZones) !== replicaKey(news.replicaZones)), |
| 664 | }); |
| 665 | }), |
| 666 | |
| 667 | read: Effect.fn(function* ({ id, olds, output }) { |
| 668 | const env = yield* GcpEnvironment.current; |
| 669 | const parent = olds?.workstationCluster ?? output?.workstationCluster; |
| 670 | // A config cannot exist before its cluster's name resolved. |
| 671 | if (output?.name === undefined && !parent) return undefined; |
| 672 | const workstationConfigId = yield* toPhysicalId( |
| 673 | id, |
| 674 | olds?.workstationConfigId, |
| 675 | output?.workstationConfigId, |
| 676 | "config", |
| 677 | ); |
| 678 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 679 | const cluster = expandParent(parent ?? "", env.project, location, "workstationClusters"); |
| 680 | const name = output?.name ?? resourceName(cluster, workstationConfigId); |
| 681 | const existing = yield* getByName(name); |
| 682 | if (existing === undefined) return undefined; |
| 683 | const attrs = toAttrs(existing, env.project, env.region); |
| 684 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 685 | }), |
| 686 | |
| 687 | list: () => |
| 688 | Effect.gen(function* () { |
| 689 | const env = yield* GcpEnvironment.current; |
| 690 | const items = yield* listOwned(env.project, env.region); |
| 691 | return items.map((item) => toAttrs(item, env.project, env.region)); |
| 692 | }), |
| 693 | |
| 694 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 695 | const env = yield* GcpEnvironment.current; |
| 696 | const workstationConfigId = yield* toPhysicalId( |
| 697 | id, |
| 698 | news.workstationConfigId, |
| 699 | output?.workstationConfigId, |
| 700 | "config", |
| 701 | ); |
| 702 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 703 | const cluster = expandParent( |
| 704 | news.workstationCluster, |
| 705 | env.project, |
| 706 | location, |
| 707 | "workstationClusters", |
| 708 | ); |
| 709 | const name = resourceName(cluster, workstationConfigId); |
| 710 | const desiredLabels = { |
| 711 | ...toLabels(news.labels), |
| 712 | ...(yield* createInternalLabels(id)), |
| 713 | }; |
| 714 | const desiredAnnotations = stringMap(news.annotations); |
| 715 | |
| 716 | let current = yield* getByName(output?.name ?? name); |
| 717 | |
| 718 | if (current === undefined) { |
| 719 | const created = yield* workstations |
| 720 | .createProjectsLocationsWorkstationClustersWorkstationConfigs({ |
| 721 | parent: cluster, |
| 722 | workstationConfigId, |
| 723 | body: { |
| 724 | displayName: news.displayName, |
| 725 | labels: desiredLabels, |
| 726 | annotations: desiredAnnotations, |
| 727 | container: news.container, |
| 728 | host: news.host, |
| 729 | readinessChecks: news.readinessChecks, |
| 730 | idleTimeout: news.idleTimeout, |
| 731 | runningTimeout: news.runningTimeout, |
| 732 | persistentDirectories: news.persistentDirectories, |
| 733 | ephemeralDirectories: news.ephemeralDirectories, |
| 734 | encryptionKey: news.encryptionKey, |
| 735 | replicaZones: news.replicaZones, |
| 736 | maxUsableWorkstations: news.maxUsableWorkstations, |
| 737 | enableAuditAgent: news.enableAuditAgent, |
| 738 | disableTcpConnections: news.disableTcpConnections, |
| 739 | allowedPorts: news.allowedPorts, |
| 740 | grantWorkstationAdminRoleOnCreate: news.grantWorkstationAdminRoleOnCreate, |
| 741 | }, |
| 742 | }) |
| 743 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 744 | if (created !== undefined) { |
| 745 | // ALREADY_EXISTS (6): a concurrent create won the race. |
| 746 | yield* waitForOperation(created).pipe( |
| 747 | Effect.catchTag("GCP.OperationFailed", (error) => |
| 748 | error.code === 6 ? Effect.void : Effect.fail(error), |
| 749 | ), |
| 750 | ); |
| 751 | } |
| 752 | current = yield* waitUntilExists(getByName(name), name); |
| 753 | } |
| 754 | |
| 755 | if (current === undefined) { |
| 756 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 757 | } |
| 758 | |
| 759 | const observedLabels = tagRecord(current.labels); |
| 760 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 761 | const mask = fieldMask([ |
| 762 | (upsert.length > 0 || removed.length > 0) && "labels", |
| 763 | !sameText(current.displayName, news.displayName) && "displayName", |
| 764 | fingerprint(stringMap(current.annotations)) !== fingerprint(desiredAnnotations) && |
| 765 | "annotations", |
| 766 | news.container !== undefined && |
| 767 | fingerprint(toContainer(current.container)) !== |
| 768 | fingerprint(toContainer(news.container)) && |
| 769 | "container", |
| 770 | news.host !== undefined && !hostMatches(current.host, news.host) && "host", |
| 771 | news.readinessChecks !== undefined && |
| 772 | fingerprint(current.readinessChecks) !== fingerprint(news.readinessChecks) && |
| 773 | "readinessChecks", |
| 774 | news.idleTimeout !== undefined && |
| 775 | !sameText(current.idleTimeout, news.idleTimeout) && |
| 776 | "idleTimeout", |
| 777 | news.runningTimeout !== undefined && |
| 778 | !sameText(current.runningTimeout, news.runningTimeout) && |
| 779 | "runningTimeout", |
| 780 | news.persistentDirectories !== undefined && |
| 781 | fingerprint(current.persistentDirectories?.map(toPersistent)) !== |
| 782 | fingerprint(news.persistentDirectories.map(toPersistent)) && |
| 783 | "persistentDirectories", |
| 784 | news.ephemeralDirectories !== undefined && |
| 785 | fingerprint(current.ephemeralDirectories?.map(toEphemeral)) !== |
| 786 | fingerprint(news.ephemeralDirectories.map(toEphemeral)) && |
| 787 | "ephemeralDirectories", |
| 788 | news.maxUsableWorkstations !== undefined && |
| 789 | !sameNumber(current.maxUsableWorkstations, news.maxUsableWorkstations) && |
| 790 | "maxUsableWorkstations", |
| 791 | news.enableAuditAgent !== undefined && |
| 792 | !sameBool(current.enableAuditAgent, news.enableAuditAgent) && |
| 793 | "enableAuditAgent", |
| 794 | news.disableTcpConnections !== undefined && |
| 795 | !sameBool(current.disableTcpConnections, news.disableTcpConnections) && |
| 796 | "disableTcpConnections", |
| 797 | news.allowedPorts !== undefined && |
| 798 | fingerprint(current.allowedPorts) !== fingerprint(news.allowedPorts) && |
| 799 | "allowedPorts", |
| 800 | news.grantWorkstationAdminRoleOnCreate !== undefined && |
| 801 | !sameBool( |
| 802 | current.grantWorkstationAdminRoleOnCreate, |
| 803 | news.grantWorkstationAdminRoleOnCreate, |
| 804 | ) && |
| 805 | "grantWorkstationAdminRoleOnCreate", |
| 806 | ]); |
| 807 | |
| 808 | if (mask.length > 0) { |
| 809 | const operation = yield* workstations |
| 810 | .patchProjectsLocationsWorkstationClustersWorkstationConfigs({ |
| 811 | name: current.name ?? name, |
| 812 | updateMask: mask, |
| 813 | body: { |
| 814 | etag: current.etag, |
| 815 | labels: desiredLabels, |
| 816 | displayName: news.displayName, |
| 817 | annotations: desiredAnnotations, |
| 818 | container: news.container, |
| 819 | host: news.host, |
| 820 | readinessChecks: news.readinessChecks, |
| 821 | idleTimeout: news.idleTimeout, |
| 822 | runningTimeout: news.runningTimeout, |
| 823 | persistentDirectories: news.persistentDirectories, |
| 824 | ephemeralDirectories: news.ephemeralDirectories, |
| 825 | maxUsableWorkstations: news.maxUsableWorkstations, |
| 826 | enableAuditAgent: news.enableAuditAgent, |
| 827 | disableTcpConnections: news.disableTcpConnections, |
| 828 | allowedPorts: news.allowedPorts, |
| 829 | grantWorkstationAdminRoleOnCreate: news.grantWorkstationAdminRoleOnCreate, |
| 830 | }, |
| 831 | }) |
| 832 | .pipe( |
| 833 | Effect.retry({ |
| 834 | while: (error) => error._tag === "Conflict", |
| 835 | times: 8, |
| 836 | schedule: Schedule.spaced("2 seconds"), |
| 837 | }), |
| 838 | ); |
| 839 | yield* waitForOperation(operation); |
| 840 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 841 | } |
| 842 | |
| 843 | return toAttrs(current, env.project, env.region); |
| 844 | }), |
| 845 | |
| 846 | delete: Effect.fn(function* ({ output }) { |
| 847 | const operation = yield* workstations |
| 848 | .deleteProjectsLocationsWorkstationClustersWorkstationConfigs({ |
| 849 | name: output.name, |
| 850 | force: true, |
| 851 | }) |
| 852 | .pipe( |
| 853 | Effect.retry({ |
| 854 | while: (error) => error._tag === "Conflict", |
| 855 | times: 8, |
| 856 | schedule: Schedule.spaced("2 seconds"), |
| 857 | }), |
| 858 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 859 | ); |
| 860 | if (operation !== undefined) { |
| 861 | // NOT_FOUND (5): already gone. |
| 862 | yield* waitForOperation(operation).pipe( |
| 863 | Effect.catchTag("GCP.OperationFailed", (error) => |
| 864 | error.code === 5 ? Effect.void : Effect.fail(error), |
| 865 | ), |
| 866 | ); |
| 867 | } |
| 868 | yield* waitUntilGone(getByName(output.name), output.name); |
| 869 | }), |
| 870 | }); |