The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as oracle from "@distilled.cloud/gcp/oracledatabase_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 { GcpEnvironment } from "../Environment.ts"; |
| 9 | import { createInternalLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | OracleDatabaseNotResolved, |
| 13 | collectPages, |
| 14 | expandNetwork, |
| 15 | hasAlchemyLabelMap, |
| 16 | listAtLocation, |
| 17 | normalizeLocation, |
| 18 | parentOf, |
| 19 | parseName, |
| 20 | replaceOnIdentity, |
| 21 | resourceNameOf, |
| 22 | retryConflict, |
| 23 | retryQuota, |
| 24 | type TimeZone, |
| 25 | toPhysicalId, |
| 26 | userLabels, |
| 27 | waitUntilExists, |
| 28 | waitUntilGone, |
| 29 | waitUntilReady, |
| 30 | } from "./internal.ts"; |
| 31 | import { waitForOperation } from "./operations.ts"; |
| 32 | |
| 33 | const COLLECTION = "cloudVmClusters"; |
| 34 | const FALLBACK_ID = "vmcluster"; |
| 35 | |
| 36 | export type DataCollectionOptions = { |
| 37 | /** Enable incident logs and traces. */ |
| 38 | incidentLogsEnabled?: boolean; |
| 39 | /** Enable diagnostic collection. */ |
| 40 | diagnosticsEventsEnabled?: boolean; |
| 41 | /** Enable health monitoring. */ |
| 42 | healthMonitoringEnabled?: boolean; |
| 43 | }; |
| 44 | |
| 45 | export type CloudVmClusterPropertiesInput = { |
| 46 | /** License type. Required on create. */ |
| 47 | licenseType?: oracle.CloudVmClusterPropertiesLicenseTypeEnum | (string & {}); |
| 48 | /** Enabled CPU cores. Required on create. */ |
| 49 | cpuCoreCount?: number; |
| 50 | /** OCPU count per VM. */ |
| 51 | ocpuCount?: number; |
| 52 | /** Node count. */ |
| 53 | nodeCount?: number; |
| 54 | /** Grid Infrastructure version. */ |
| 55 | giVersion?: string; |
| 56 | /** OS image version. */ |
| 57 | systemVersion?: string; |
| 58 | /** Memory in GB. */ |
| 59 | memorySizeGb?: number; |
| 60 | /** Local storage per VM in GB. */ |
| 61 | dbNodeStorageSizeGb?: number; |
| 62 | /** DATA disk group size in TB. */ |
| 63 | dataStorageSizeTb?: number; |
| 64 | /** Hostname prefix. */ |
| 65 | hostnamePrefix?: string; |
| 66 | /** SSH public keys. */ |
| 67 | sshPublicKeys?: string[]; |
| 68 | /** SCAN listener TCP port. */ |
| 69 | scanListenerPortTcp?: number; |
| 70 | /** SCAN listener TLS port. */ |
| 71 | scanListenerPortTcpSsl?: number; |
| 72 | /** Disk redundancy (`HIGH`, `NORMAL`). */ |
| 73 | diskRedundancy?: oracle.CloudVmClusterPropertiesDiskRedundancyEnum | (string & {}); |
| 74 | /** Time zone. */ |
| 75 | timeZone?: TimeZone; |
| 76 | /** OCI cluster name. */ |
| 77 | clusterName?: string; |
| 78 | /** Use local backup. */ |
| 79 | localBackupEnabled?: boolean; |
| 80 | /** Use sparse disk group. */ |
| 81 | sparseDiskgroupEnabled?: boolean; |
| 82 | /** Diagnostics collection options. */ |
| 83 | diagnosticsDataCollectionOptions?: DataCollectionOptions; |
| 84 | /** Database server OCIDs. */ |
| 85 | dbServerOcids?: string[]; |
| 86 | }; |
| 87 | |
| 88 | export type CloudVmClusterProps = { |
| 89 | /** |
| 90 | * VM Cluster id. If omitted, a unique RFC1035 name is generated. |
| 91 | * Immutable. |
| 92 | */ |
| 93 | cloudVmClusterId?: string; |
| 94 | /** |
| 95 | * Region. Immutable. |
| 96 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 97 | */ |
| 98 | location?: string; |
| 99 | /** |
| 100 | * Exadata Infrastructure |
| 101 | * (`projects/{project}/locations/{region}/cloudExadataInfrastructures/{id}`). |
| 102 | * Immutable. |
| 103 | */ |
| 104 | exadataInfrastructure?: string; |
| 105 | /** |
| 106 | * Display name. |
| 107 | */ |
| 108 | displayName?: string; |
| 109 | /** |
| 110 | * VPC network. Immutable. Required unless `odbSubnet` is set. |
| 111 | */ |
| 112 | network?: string; |
| 113 | /** |
| 114 | * Cluster IP CIDR. Immutable. |
| 115 | */ |
| 116 | cidr?: string; |
| 117 | /** |
| 118 | * Backup subnet CIDR. Immutable. |
| 119 | */ |
| 120 | backupSubnetCidr?: string; |
| 121 | /** |
| 122 | * ODB Network. Immutable. |
| 123 | */ |
| 124 | odbNetwork?: string; |
| 125 | /** |
| 126 | * ODB Subnet for IP allocation. Immutable. |
| 127 | */ |
| 128 | odbSubnet?: string; |
| 129 | /** |
| 130 | * Backup ODB Subnet. Immutable. |
| 131 | */ |
| 132 | backupOdbSubnet?: string; |
| 133 | /** |
| 134 | * Exascale storage vault. Immutable. |
| 135 | */ |
| 136 | exascaleDbStorageVault?: string; |
| 137 | /** |
| 138 | * User labels. Alchemy ownership labels are merged in automatically. |
| 139 | */ |
| 140 | labels?: Record<string, string>; |
| 141 | /** |
| 142 | * VM cluster properties. |
| 143 | */ |
| 144 | properties?: CloudVmClusterPropertiesInput; |
| 145 | /** License type. Convenience alias for `properties.licenseType`. */ |
| 146 | licenseType?: oracle.CloudVmClusterPropertiesLicenseTypeEnum | (string & {}); |
| 147 | /** CPU core count. Convenience alias for `properties.cpuCoreCount`. */ |
| 148 | cpuCoreCount?: number; |
| 149 | /** Grid Infrastructure version. Convenience alias for `properties.giVersion`. */ |
| 150 | giVersion?: string; |
| 151 | /** Hostname prefix. Convenience alias for `properties.hostnamePrefix`. */ |
| 152 | hostnamePrefix?: string; |
| 153 | /** SSH public keys. Convenience alias for `properties.sshPublicKeys`. */ |
| 154 | sshPublicKeys?: string[]; |
| 155 | }; |
| 156 | |
| 157 | export type CloudVmCluster = Resource< |
| 158 | "GCP.OracleDatabase.CloudVmCluster", |
| 159 | CloudVmClusterProps, |
| 160 | { |
| 161 | /** Full resource name. */ |
| 162 | name: string; |
| 163 | /** VM Cluster id. */ |
| 164 | cloudVmClusterId: string; |
| 165 | /** Project id. */ |
| 166 | project: string; |
| 167 | /** Location id. */ |
| 168 | location: string; |
| 169 | /** Exadata Infrastructure. */ |
| 170 | exadataInfrastructure: string | undefined; |
| 171 | /** Display name. */ |
| 172 | displayName: string | undefined; |
| 173 | /** VPC network. */ |
| 174 | network: string | undefined; |
| 175 | /** Cluster CIDR. */ |
| 176 | cidr: string | undefined; |
| 177 | /** Backup subnet CIDR. */ |
| 178 | backupSubnetCidr: string | undefined; |
| 179 | /** ODB Network. */ |
| 180 | odbNetwork: string | undefined; |
| 181 | /** ODB Subnet. */ |
| 182 | odbSubnet: string | undefined; |
| 183 | /** Backup ODB Subnet. */ |
| 184 | backupOdbSubnet: string | undefined; |
| 185 | /** User labels (Alchemy ownership labels stripped). */ |
| 186 | labels: Record<string, string>; |
| 187 | /** GCP Oracle zone. */ |
| 188 | gcpOracleZone: string | undefined; |
| 189 | /** Lifecycle state. */ |
| 190 | state: string | undefined; |
| 191 | /** License type. */ |
| 192 | licenseType: string | undefined; |
| 193 | /** CPU cores. */ |
| 194 | cpuCoreCount: number | undefined; |
| 195 | /** Hostname. */ |
| 196 | hostname: string | undefined; |
| 197 | /** OCID. */ |
| 198 | ocid: string | undefined; |
| 199 | /** RFC3339 creation timestamp. */ |
| 200 | createTime: string | undefined; |
| 201 | }, |
| 202 | never, |
| 203 | Providers |
| 204 | >; |
| 205 | |
| 206 | /** |
| 207 | * An Oracle Exadata VM Cluster on Google Cloud. |
| 208 | * |
| 209 | * Changing `cloudVmClusterId`, `location`, `exadataInfrastructure`, |
| 210 | * network identity (`network`/`cidr`/`odbSubnet`), or license type |
| 211 | * replaces the cluster. There is no patch API in the distilled SDK, so |
| 212 | * labels are applied at create. |
| 213 | * |
| 214 | * Delete sends `force=true` so child resources do not block teardown. |
| 215 | * |
| 216 | * ### Creating a VM Cluster |
| 217 | * **Example:** Attach to Exadata Infrastructure |
| 218 | * ```typescript |
| 219 | * const cluster = yield* GCP.OracleDatabase.CloudVmCluster("Vms", { |
| 220 | * exadataInfrastructure: infra.name, |
| 221 | * network: "default", |
| 222 | * cidr: "10.10.0.0/24", |
| 223 | * backupSubnetCidr: "10.10.1.0/24", |
| 224 | * licenseType: "LICENSE_INCLUDED", |
| 225 | * cpuCoreCount: 4, |
| 226 | * giVersion: "19.0.0.0", |
| 227 | * hostnamePrefix: "exa", |
| 228 | * sshPublicKeys: [publicKey], |
| 229 | * }); |
| 230 | * ``` |
| 231 | * |
| 232 | * @resource |
| 233 | * @category OracleDatabase |
| 234 | */ |
| 235 | export const CloudVmCluster = Resource<CloudVmCluster>("GCP.OracleDatabase.CloudVmCluster"); |
| 236 | |
| 237 | const mergedProperties = (news: CloudVmClusterProps): CloudVmClusterPropertiesInput => ({ |
| 238 | ...news.properties, |
| 239 | licenseType: news.licenseType ?? news.properties?.licenseType, |
| 240 | cpuCoreCount: news.cpuCoreCount ?? news.properties?.cpuCoreCount, |
| 241 | giVersion: news.giVersion ?? news.properties?.giVersion, |
| 242 | hostnamePrefix: news.hostnamePrefix ?? news.properties?.hostnamePrefix, |
| 243 | sshPublicKeys: news.sshPublicKeys ?? news.properties?.sshPublicKeys, |
| 244 | }); |
| 245 | |
| 246 | const toCreateBody = ( |
| 247 | news: CloudVmClusterProps, |
| 248 | desiredLabels: Record<string, string>, |
| 249 | project: string, |
| 250 | ): oracle.CloudVmCluster => { |
| 251 | const props = mergedProperties(news); |
| 252 | const properties: oracle.CloudVmClusterProperties = {}; |
| 253 | if (props.licenseType !== undefined) properties.licenseType = props.licenseType; |
| 254 | if (props.cpuCoreCount !== undefined) { |
| 255 | properties.cpuCoreCount = props.cpuCoreCount; |
| 256 | } |
| 257 | if (props.ocpuCount !== undefined) properties.ocpuCount = props.ocpuCount; |
| 258 | if (props.nodeCount !== undefined) properties.nodeCount = props.nodeCount; |
| 259 | if (props.giVersion !== undefined) properties.giVersion = props.giVersion; |
| 260 | if (props.systemVersion !== undefined) { |
| 261 | properties.systemVersion = props.systemVersion; |
| 262 | } |
| 263 | if (props.memorySizeGb !== undefined) { |
| 264 | properties.memorySizeGb = props.memorySizeGb; |
| 265 | } |
| 266 | if (props.dbNodeStorageSizeGb !== undefined) { |
| 267 | properties.dbNodeStorageSizeGb = props.dbNodeStorageSizeGb; |
| 268 | } |
| 269 | if (props.dataStorageSizeTb !== undefined) { |
| 270 | properties.dataStorageSizeTb = props.dataStorageSizeTb; |
| 271 | } |
| 272 | if (props.hostnamePrefix !== undefined) { |
| 273 | properties.hostnamePrefix = props.hostnamePrefix; |
| 274 | } |
| 275 | if (props.sshPublicKeys !== undefined) { |
| 276 | properties.sshPublicKeys = props.sshPublicKeys; |
| 277 | } |
| 278 | if (props.scanListenerPortTcp !== undefined) { |
| 279 | properties.scanListenerPortTcp = props.scanListenerPortTcp; |
| 280 | } |
| 281 | if (props.scanListenerPortTcpSsl !== undefined) { |
| 282 | properties.scanListenerPortTcpSsl = props.scanListenerPortTcpSsl; |
| 283 | } |
| 284 | if (props.diskRedundancy !== undefined) { |
| 285 | properties.diskRedundancy = props.diskRedundancy; |
| 286 | } |
| 287 | if (props.timeZone !== undefined) properties.timeZone = props.timeZone; |
| 288 | if (props.clusterName !== undefined) properties.clusterName = props.clusterName; |
| 289 | if (props.localBackupEnabled !== undefined) { |
| 290 | properties.localBackupEnabled = props.localBackupEnabled; |
| 291 | } |
| 292 | if (props.sparseDiskgroupEnabled !== undefined) { |
| 293 | properties.sparseDiskgroupEnabled = props.sparseDiskgroupEnabled; |
| 294 | } |
| 295 | if (props.diagnosticsDataCollectionOptions !== undefined) { |
| 296 | properties.diagnosticsDataCollectionOptions = props.diagnosticsDataCollectionOptions; |
| 297 | } |
| 298 | if (props.dbServerOcids !== undefined) { |
| 299 | properties.dbServerOcids = props.dbServerOcids; |
| 300 | } |
| 301 | const body: oracle.CloudVmCluster = { |
| 302 | labels: desiredLabels, |
| 303 | properties, |
| 304 | }; |
| 305 | if (news.exadataInfrastructure !== undefined) { |
| 306 | body.exadataInfrastructure = news.exadataInfrastructure; |
| 307 | } |
| 308 | if (news.displayName !== undefined) body.displayName = news.displayName; |
| 309 | const network = expandNetwork(project, news.network); |
| 310 | if (network !== undefined) body.network = network; |
| 311 | if (news.cidr !== undefined) body.cidr = news.cidr; |
| 312 | if (news.backupSubnetCidr !== undefined) { |
| 313 | body.backupSubnetCidr = news.backupSubnetCidr; |
| 314 | } |
| 315 | if (news.odbNetwork !== undefined) body.odbNetwork = news.odbNetwork; |
| 316 | if (news.odbSubnet !== undefined) body.odbSubnet = news.odbSubnet; |
| 317 | if (news.backupOdbSubnet !== undefined) { |
| 318 | body.backupOdbSubnet = news.backupOdbSubnet; |
| 319 | } |
| 320 | if (news.exascaleDbStorageVault !== undefined) { |
| 321 | body.exascaleDbStorageVault = news.exascaleDbStorageVault; |
| 322 | } |
| 323 | return body; |
| 324 | }; |
| 325 | |
| 326 | const toAttrs = (cluster: oracle.CloudVmCluster, project: string) => { |
| 327 | const name = cluster.name ?? ""; |
| 328 | const parsed = parseName(name, COLLECTION); |
| 329 | return { |
| 330 | name, |
| 331 | cloudVmClusterId: parsed.id, |
| 332 | project: parsed.project || project, |
| 333 | location: parsed.location, |
| 334 | exadataInfrastructure: cluster.exadataInfrastructure, |
| 335 | displayName: cluster.displayName, |
| 336 | network: cluster.network, |
| 337 | cidr: cluster.cidr, |
| 338 | backupSubnetCidr: cluster.backupSubnetCidr, |
| 339 | odbNetwork: cluster.odbNetwork, |
| 340 | odbSubnet: cluster.odbSubnet, |
| 341 | backupOdbSubnet: cluster.backupOdbSubnet, |
| 342 | labels: userLabels(cluster.labels), |
| 343 | gcpOracleZone: cluster.gcpOracleZone, |
| 344 | state: cluster.properties?.state, |
| 345 | licenseType: cluster.properties?.licenseType, |
| 346 | cpuCoreCount: cluster.properties?.cpuCoreCount, |
| 347 | hostname: cluster.properties?.hostname, |
| 348 | ocid: cluster.properties?.ocid, |
| 349 | createTime: cluster.createTime, |
| 350 | }; |
| 351 | }; |
| 352 | |
| 353 | const getByName = (name: string) => |
| 354 | retryQuota(oracle.getProjectsLocationsCloudVmClusters({ name })).pipe( |
| 355 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 356 | ); |
| 357 | |
| 358 | const listClusters = (project: string, region: string) => { |
| 359 | const collect = (parent: string) => |
| 360 | collectPages( |
| 361 | oracle.listProjectsLocationsCloudVmClusters.pages({ |
| 362 | parent, |
| 363 | pageSize: 1000, |
| 364 | }), |
| 365 | (page) => page.cloudVmClusters, |
| 366 | ).pipe(Effect.map((items) => items.filter((item) => hasAlchemyLabelMap(item.labels)))); |
| 367 | return listAtLocation(project, region, collect).pipe( |
| 368 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 369 | ); |
| 370 | }; |
| 371 | |
| 372 | export const CloudVmClusterProvider = () => |
| 373 | Provider.succeed(CloudVmCluster, { |
| 374 | stables: ["name", "cloudVmClusterId", "project", "location", "createTime", "ocid"], |
| 375 | |
| 376 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 377 | if (!isResolved(news)) return undefined; |
| 378 | const env = yield* GcpEnvironment.current; |
| 379 | const previousInfra = olds?.exadataInfrastructure ?? output?.exadataInfrastructure ?? ""; |
| 380 | const nextInfra = news.exadataInfrastructure ?? previousInfra; |
| 381 | const previousNetwork = olds?.network ?? output?.network ?? ""; |
| 382 | const nextNetwork = news.network ?? previousNetwork; |
| 383 | const previousOdb = olds?.odbSubnet ?? output?.odbSubnet ?? ""; |
| 384 | const nextOdb = news.odbSubnet ?? previousOdb; |
| 385 | return replaceOnIdentity({ |
| 386 | previousId: olds?.cloudVmClusterId ?? output?.cloudVmClusterId, |
| 387 | nextId: news.cloudVmClusterId ?? olds?.cloudVmClusterId ?? output?.cloudVmClusterId, |
| 388 | previousLocation: normalizeLocation(olds?.location ?? output?.location ?? env.region), |
| 389 | nextLocation: normalizeLocation( |
| 390 | news.location ?? olds?.location ?? output?.location ?? env.region, |
| 391 | ), |
| 392 | extra: |
| 393 | nextInfra !== previousInfra || nextNetwork !== previousNetwork || nextOdb !== previousOdb, |
| 394 | }); |
| 395 | }), |
| 396 | |
| 397 | read: Effect.fn(function* ({ id, olds, output }) { |
| 398 | const env = yield* GcpEnvironment.current; |
| 399 | const cloudVmClusterId = yield* toPhysicalId( |
| 400 | id, |
| 401 | olds?.cloudVmClusterId, |
| 402 | output?.cloudVmClusterId, |
| 403 | FALLBACK_ID, |
| 404 | ); |
| 405 | const location = normalizeLocation(olds?.location ?? output?.location ?? env.region); |
| 406 | const name = |
| 407 | output?.name ?? resourceNameOf(env.project, location, COLLECTION, cloudVmClusterId); |
| 408 | const existing = yield* getByName(name); |
| 409 | if (existing === undefined) return undefined; |
| 410 | const attrs = toAttrs(existing, env.project); |
| 411 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 412 | }), |
| 413 | |
| 414 | list: () => |
| 415 | Effect.gen(function* () { |
| 416 | const env = yield* GcpEnvironment.current; |
| 417 | const items = yield* listClusters(env.project, env.region); |
| 418 | return items.map((item) => toAttrs(item, env.project)); |
| 419 | }), |
| 420 | |
| 421 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 422 | const env = yield* GcpEnvironment.current; |
| 423 | const cloudVmClusterId = yield* toPhysicalId( |
| 424 | id, |
| 425 | news.cloudVmClusterId, |
| 426 | output?.cloudVmClusterId, |
| 427 | FALLBACK_ID, |
| 428 | ); |
| 429 | const location = normalizeLocation(news.location ?? output?.location ?? env.region); |
| 430 | const name = resourceNameOf(env.project, location, COLLECTION, cloudVmClusterId); |
| 431 | const desiredLabels = { |
| 432 | ...toLabels(news.labels), |
| 433 | ...(yield* createInternalLabels(id)), |
| 434 | }; |
| 435 | |
| 436 | let current = yield* getByName(output?.name ?? name); |
| 437 | |
| 438 | if (current === undefined) { |
| 439 | const created = yield* oracle |
| 440 | .createProjectsLocationsCloudVmClusters({ |
| 441 | parent: parentOf(env.project, location), |
| 442 | cloudVmClusterId, |
| 443 | body: toCreateBody(news, desiredLabels, env.project), |
| 444 | }) |
| 445 | .pipe( |
| 446 | retryQuota, |
| 447 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 448 | ); |
| 449 | if (created !== undefined) { |
| 450 | yield* waitForOperation(created); |
| 451 | } |
| 452 | current = yield* waitUntilExists(getByName(name), name); |
| 453 | } |
| 454 | |
| 455 | if (current === undefined) { |
| 456 | return yield* new OracleDatabaseNotResolved({ resourceName: name }); |
| 457 | } |
| 458 | |
| 459 | const ready = yield* waitUntilReady( |
| 460 | getByName(current.name ?? name), |
| 461 | current.name ?? name, |
| 462 | (value) => value.properties?.state, |
| 463 | ); |
| 464 | |
| 465 | return toAttrs(ready, env.project); |
| 466 | }), |
| 467 | |
| 468 | delete: Effect.fn(function* ({ output }) { |
| 469 | const operation = yield* oracle |
| 470 | .deleteProjectsLocationsCloudVmClusters({ |
| 471 | name: output.name, |
| 472 | force: true, |
| 473 | }) |
| 474 | .pipe( |
| 475 | retryConflict, |
| 476 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 477 | ); |
| 478 | if (operation !== undefined) { |
| 479 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 480 | } |
| 481 | yield* waitUntilGone(getByName(output.name), output.name); |
| 482 | }), |
| 483 | }); |