The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as redshift from "@distilled.cloud/aws/redshift"; |
| 2 | import { Unowned } 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 { createInternalTags, diffTags, hasAlchemyTags } from "alchemy/Tags"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import type * as Duration from "effect/Duration"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import type * as Redacted from "effect/Redacted"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import * as Stream from "effect/Stream"; |
| 14 | import { AWSEnvironment } from "../Environment.ts"; |
| 15 | import { durationToDays, unwrapRedactedString } from "../IAM/common.ts"; |
| 16 | import type { Providers } from "../Providers.ts"; |
| 17 | import { applyRedshiftTagDelta, redshiftArn, sameStringSet, toTagRecord } from "./internal.ts"; |
| 18 | |
| 19 | export class RedshiftClusterNotReady extends Data.TaggedError("RedshiftClusterNotReady")<{ |
| 20 | readonly message: string; |
| 21 | readonly cause?: unknown; |
| 22 | }> {} |
| 23 | |
| 24 | export class RedshiftClusterUnexpectedResponse extends Data.TaggedError( |
| 25 | "RedshiftClusterUnexpectedResponse", |
| 26 | )<{ |
| 27 | readonly message: string; |
| 28 | readonly cause?: unknown; |
| 29 | }> {} |
| 30 | |
| 31 | /** |
| 32 | * Creating a provisioned Redshift cluster requires either a |
| 33 | * `masterUserPassword` or `manageMasterPassword: true`. |
| 34 | */ |
| 35 | export class RedshiftMasterPasswordRequired extends Data.TaggedError( |
| 36 | "RedshiftMasterPasswordRequired", |
| 37 | )<{ |
| 38 | readonly clusterIdentifier: string; |
| 39 | }> {} |
| 40 | |
| 41 | export interface ClusterProps { |
| 42 | /** |
| 43 | * Unique identifier of the cluster. Must be 1-63 lowercase alphanumeric |
| 44 | * characters or hyphens, starting with a letter. If omitted, a |
| 45 | * deterministic physical name is generated. Changing the identifier |
| 46 | * replaces the cluster. |
| 47 | */ |
| 48 | clusterIdentifier?: string; |
| 49 | /** |
| 50 | * Node type of the cluster, e.g. `ra3.large`, `ra3.xlplus` or |
| 51 | * `dc2.large`. Changing the node type triggers an in-place resize. |
| 52 | * @default "ra3.large" |
| 53 | */ |
| 54 | nodeType?: string; |
| 55 | /** |
| 56 | * Number of compute nodes. `1` provisions a single-node cluster; any |
| 57 | * larger value provisions a multi-node cluster. Changing the count |
| 58 | * triggers an in-place resize. |
| 59 | * @default 1 |
| 60 | */ |
| 61 | numberOfNodes?: number; |
| 62 | /** |
| 63 | * Admin username for the cluster database. Changing the username replaces |
| 64 | * the cluster. |
| 65 | * @default "awsuser" |
| 66 | */ |
| 67 | masterUsername?: string; |
| 68 | /** |
| 69 | * Admin password. Must be 8-64 characters with at least one uppercase |
| 70 | * letter, one lowercase letter and one number. Provide this or set |
| 71 | * `manageMasterPassword`. |
| 72 | */ |
| 73 | masterUserPassword?: Redacted.Redacted<string>; |
| 74 | /** |
| 75 | * Let Amazon Redshift manage the admin password in Secrets Manager |
| 76 | * instead of supplying `masterUserPassword`. The secret ARN is surfaced |
| 77 | * as the `masterPasswordSecretArn` attribute. |
| 78 | * @default false |
| 79 | */ |
| 80 | manageMasterPassword?: boolean; |
| 81 | /** |
| 82 | * Name of the initial database created in the cluster. Changing the |
| 83 | * database name replaces the cluster. |
| 84 | * @default "dev" |
| 85 | */ |
| 86 | dbName?: string; |
| 87 | /** |
| 88 | * Name of the {@link ClusterSubnetGroup} the cluster's nodes are placed |
| 89 | * into. Changing the subnet group replaces the cluster. |
| 90 | * @default the default cluster subnet group |
| 91 | */ |
| 92 | clusterSubnetGroupName?: string; |
| 93 | /** |
| 94 | * VPC security group IDs that control network access to the cluster |
| 95 | * endpoint. |
| 96 | * @default the VPC's default security group |
| 97 | */ |
| 98 | vpcSecurityGroupIds?: string[]; |
| 99 | /** |
| 100 | * Name of the {@link ClusterParameterGroup} applied to the cluster. |
| 101 | * @default the family's default parameter group |
| 102 | */ |
| 103 | clusterParameterGroupName?: string; |
| 104 | /** |
| 105 | * Whether the cluster endpoint is reachable from the public internet. |
| 106 | * @default false |
| 107 | */ |
| 108 | publiclyAccessible?: boolean; |
| 109 | /** |
| 110 | * Whether data at rest is encrypted. RA3 node types are always encrypted. |
| 111 | * @default true |
| 112 | */ |
| 113 | encrypted?: boolean; |
| 114 | /** |
| 115 | * Customer-managed KMS key used for encryption at rest. |
| 116 | * @default AWS-owned Redshift key |
| 117 | */ |
| 118 | kmsKeyId?: string; |
| 119 | /** |
| 120 | * Port the cluster database accepts connections on. Changing the port |
| 121 | * replaces the cluster. |
| 122 | * @default 5439 |
| 123 | */ |
| 124 | port?: number; |
| 125 | /** |
| 126 | * Availability Zone the cluster is provisioned in. Changing the AZ |
| 127 | * replaces the cluster. |
| 128 | * @default chosen by Redshift |
| 129 | */ |
| 130 | availabilityZone?: string; |
| 131 | /** |
| 132 | * Weekly maintenance window, e.g. `sun:05:00-sun:05:30` (UTC). |
| 133 | */ |
| 134 | preferredMaintenanceWindow?: string; |
| 135 | /** |
| 136 | * How long automated snapshots are retained, e.g. `"7 days"` or |
| 137 | * `Duration.days(7)` (a bare number is milliseconds). Rounded to whole |
| 138 | * days on the wire; zero disables automated snapshots. |
| 139 | */ |
| 140 | automatedSnapshotRetentionPeriod?: Duration.Input; |
| 141 | /** |
| 142 | * Whether major engine upgrades may be applied during the maintenance |
| 143 | * window. |
| 144 | * @default true |
| 145 | */ |
| 146 | allowVersionUpgrade?: boolean; |
| 147 | /** |
| 148 | * Whether enhanced VPC routing forces COPY/UNLOAD traffic through the |
| 149 | * VPC. |
| 150 | * @default false |
| 151 | */ |
| 152 | enhancedVpcRouting?: boolean; |
| 153 | /** |
| 154 | * IAM role ARNs the cluster can assume for COPY/UNLOAD and federated |
| 155 | * queries (max 50). |
| 156 | */ |
| 157 | iamRoles?: string[]; |
| 158 | /** |
| 159 | * User-defined tags for the cluster. |
| 160 | */ |
| 161 | tags?: Record<string, string>; |
| 162 | } |
| 163 | |
| 164 | export interface Cluster extends Resource< |
| 165 | "AWS.Redshift.Cluster", |
| 166 | ClusterProps, |
| 167 | { |
| 168 | /** |
| 169 | * Unique identifier of the cluster. |
| 170 | */ |
| 171 | clusterIdentifier: string; |
| 172 | /** |
| 173 | * ARN of the cluster. |
| 174 | */ |
| 175 | clusterArn: string; |
| 176 | /** |
| 177 | * ARN of the cluster's namespace (used by datashares and the Data API). |
| 178 | */ |
| 179 | clusterNamespaceArn: string | undefined; |
| 180 | /** |
| 181 | * Current cluster status (e.g. `"available"`). |
| 182 | */ |
| 183 | clusterStatus: string; |
| 184 | /** |
| 185 | * Node type of the cluster (e.g. `"ra3.large"`). |
| 186 | */ |
| 187 | nodeType: string; |
| 188 | /** |
| 189 | * Number of compute nodes in the cluster. |
| 190 | */ |
| 191 | numberOfNodes: number; |
| 192 | /** |
| 193 | * Name of the initial database. |
| 194 | */ |
| 195 | dbName: string; |
| 196 | /** |
| 197 | * Admin (master) user name. |
| 198 | */ |
| 199 | masterUsername: string | undefined; |
| 200 | /** |
| 201 | * DNS address of the cluster endpoint (pgwire host). |
| 202 | */ |
| 203 | endpointAddress: string | undefined; |
| 204 | /** |
| 205 | * Port of the cluster endpoint (5439 by default). |
| 206 | */ |
| 207 | endpointPort: number | undefined; |
| 208 | /** |
| 209 | * ID of the VPC the cluster runs in. |
| 210 | */ |
| 211 | vpcId: string | undefined; |
| 212 | /** |
| 213 | * Availability zone the cluster is placed in. |
| 214 | */ |
| 215 | availabilityZone: string | undefined; |
| 216 | /** |
| 217 | * Name of the cluster subnet group the cluster is placed in, if any. |
| 218 | */ |
| 219 | clusterSubnetGroupName: string | undefined; |
| 220 | /** |
| 221 | * Whether the cluster endpoint is reachable from the public internet. |
| 222 | */ |
| 223 | publiclyAccessible: boolean | undefined; |
| 224 | /** |
| 225 | * Whether the cluster's data is encrypted at rest. |
| 226 | */ |
| 227 | encrypted: boolean | undefined; |
| 228 | /** |
| 229 | * KMS key encrypting the cluster, if any. |
| 230 | */ |
| 231 | kmsKeyId: string | undefined; |
| 232 | /** |
| 233 | * ARN of the Secrets Manager secret holding the admin password when |
| 234 | * `manageMasterPassword` is enabled. |
| 235 | */ |
| 236 | masterPasswordSecretArn: string | undefined; |
| 237 | /** |
| 238 | * Tags on the cluster (including internal Alchemy tags). |
| 239 | */ |
| 240 | tags: Record<string, string>; |
| 241 | }, |
| 242 | never, |
| 243 | Providers |
| 244 | > {} |
| 245 | |
| 246 | /** |
| 247 | * A provisioned Amazon Redshift data-warehouse cluster. |
| 248 | * |
| 249 | * Clusters take roughly 5-10 minutes to provision and are billed hourly per |
| 250 | * node while they exist (`ra3.large` and `dc2.large` are the smallest node |
| 251 | * types). For serverless data warehousing see the `RedshiftServerless` |
| 252 | * namespace instead. Destroy clusters you are not using. |
| 253 | * ### Creating a Cluster |
| 254 | * **Example:** Single-Node Cluster |
| 255 | * ```typescript |
| 256 | * const cluster = yield* Redshift.Cluster("Warehouse", { |
| 257 | * nodeType: "ra3.large", |
| 258 | * numberOfNodes: 1, |
| 259 | * masterUsername: "admin", |
| 260 | * masterUserPassword: warehousePassword, |
| 261 | * dbName: "analytics", |
| 262 | * }); |
| 263 | * ``` |
| 264 | * **Example:** Cluster in a VPC Subnet Group |
| 265 | * ```typescript |
| 266 | * const subnetGroup = yield* Redshift.ClusterSubnetGroup("WarehouseSubnets", { |
| 267 | * subnetIds: [subnetA.subnetId, subnetB.subnetId], |
| 268 | * }); |
| 269 | * const cluster = yield* Redshift.Cluster("Warehouse", { |
| 270 | * nodeType: "ra3.large", |
| 271 | * numberOfNodes: 2, |
| 272 | * masterUsername: "admin", |
| 273 | * manageMasterPassword: true, |
| 274 | * clusterSubnetGroupName: subnetGroup.clusterSubnetGroupName, |
| 275 | * publiclyAccessible: false, |
| 276 | * encrypted: true, |
| 277 | * }); |
| 278 | * ``` |
| 279 | * |
| 280 | * @resource |
| 281 | */ |
| 282 | export const Cluster = Resource<Cluster>("AWS.Redshift.Cluster"); |
| 283 | |
| 284 | const DEFAULT_NODE_TYPE = "ra3.large"; |
| 285 | const DEFAULT_MASTER_USERNAME = "awsuser"; |
| 286 | const DEFAULT_DB_NAME = "dev"; |
| 287 | |
| 288 | /** |
| 289 | * Retry an effect while the cluster is mid-transition |
| 290 | * (`InvalidClusterStateFault`), bounded to ~5 minutes. |
| 291 | * |
| 292 | * Expressed as an explicitly-typed module-scope helper: inlining |
| 293 | * `Effect.retry` in a lifecycle operation leaves `Retry.Return`'s |
| 294 | * conditional type unresolved in the provider's inferred layer type, which |
| 295 | * declaration emit widens to an `unknown` R — poisoning `AWS.providers()` |
| 296 | * for every downstream consumer. |
| 297 | */ |
| 298 | const retryWhileClusterTransitioning = <A, E extends { _tag: string }, R>( |
| 299 | self: Effect.Effect<A, E, R>, |
| 300 | ): Effect.Effect<A, E, R> => |
| 301 | Effect.retry(self, { |
| 302 | while: (e) => e._tag === "InvalidClusterStateFault", |
| 303 | schedule: Schedule.max([Schedule.fixed("15 seconds"), Schedule.recurs(20)]), |
| 304 | }); |
| 305 | |
| 306 | /** |
| 307 | * Bounded status polling (~20 minutes; cluster provisioning/resizing |
| 308 | * typically completes in 5-10). Same explicit-annotation rationale as |
| 309 | * {@link retryWhileClusterTransitioning}. |
| 310 | */ |
| 311 | const retryUntilSettled = <A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> => |
| 312 | Effect.retry(self, { |
| 313 | schedule: Schedule.max([Schedule.fixed("15 seconds"), Schedule.recurs(80)]), |
| 314 | }); |
| 315 | |
| 316 | export const ClusterProvider = () => |
| 317 | Provider.effect( |
| 318 | Cluster, |
| 319 | Effect.gen(function* () { |
| 320 | const toName = (id: string, props: Partial<ClusterProps>) => |
| 321 | props.clusterIdentifier |
| 322 | ? Effect.succeed(props.clusterIdentifier) |
| 323 | : createPhysicalName({ id, maxLength: 63, lowercase: true }); |
| 324 | |
| 325 | const readCluster = Effect.fn(function* (identifier: string) { |
| 326 | const response = yield* redshift |
| 327 | .describeClusters({ ClusterIdentifier: identifier }) |
| 328 | .pipe(Effect.catchTag("ClusterNotFoundFault", () => Effect.undefined)); |
| 329 | return response?.Clusters?.[0]; |
| 330 | }); |
| 331 | |
| 332 | const waitForAvailable = Effect.fn(function* (identifier: string) { |
| 333 | return yield* retryUntilSettled( |
| 334 | readCluster(identifier).pipe( |
| 335 | Effect.flatMap((cluster) => { |
| 336 | if (cluster === undefined) { |
| 337 | return Effect.fail( |
| 338 | new RedshiftClusterNotReady({ message: `Cluster '${identifier}' not found` }), |
| 339 | ); |
| 340 | } |
| 341 | if (cluster.ClusterStatus !== "available") { |
| 342 | return Effect.fail( |
| 343 | new RedshiftClusterNotReady({ |
| 344 | message: `Cluster '${identifier}' not available (status: ${cluster.ClusterStatus})`, |
| 345 | }), |
| 346 | ); |
| 347 | } |
| 348 | return Effect.succeed(cluster); |
| 349 | }), |
| 350 | ), |
| 351 | ); |
| 352 | }); |
| 353 | |
| 354 | // Wait for a cluster to leave a transitional state before delete. |
| 355 | // Ends when the cluster is available, deleting, or gone. |
| 356 | const waitUntilSettled = Effect.fn(function* (identifier: string) { |
| 357 | return yield* retryUntilSettled( |
| 358 | readCluster(identifier).pipe( |
| 359 | Effect.flatMap((cluster) => { |
| 360 | if ( |
| 361 | cluster !== undefined && |
| 362 | cluster.ClusterStatus !== "available" && |
| 363 | cluster.ClusterStatus !== "deleting" |
| 364 | ) { |
| 365 | return Effect.fail( |
| 366 | new RedshiftClusterNotReady({ |
| 367 | message: `Cluster '${identifier}' still settling (status: ${cluster.ClusterStatus})`, |
| 368 | }), |
| 369 | ); |
| 370 | } |
| 371 | return Effect.succeed(cluster); |
| 372 | }), |
| 373 | ), |
| 374 | ); |
| 375 | }); |
| 376 | |
| 377 | const toAttrs = Effect.fn(function* (cluster: redshift.Cluster) { |
| 378 | const { accountId, region } = yield* AWSEnvironment.current; |
| 379 | if (!cluster.ClusterIdentifier) { |
| 380 | return yield* new RedshiftClusterUnexpectedResponse({ |
| 381 | message: "Cluster is missing its identifier", |
| 382 | }); |
| 383 | } |
| 384 | return { |
| 385 | clusterIdentifier: cluster.ClusterIdentifier, |
| 386 | clusterArn: redshiftArn(region, accountId, "cluster", cluster.ClusterIdentifier), |
| 387 | clusterNamespaceArn: cluster.ClusterNamespaceArn, |
| 388 | clusterStatus: cluster.ClusterStatus ?? "available", |
| 389 | nodeType: cluster.NodeType ?? DEFAULT_NODE_TYPE, |
| 390 | numberOfNodes: cluster.NumberOfNodes ?? 1, |
| 391 | dbName: cluster.DBName ?? DEFAULT_DB_NAME, |
| 392 | masterUsername: cluster.MasterUsername, |
| 393 | endpointAddress: cluster.Endpoint?.Address, |
| 394 | endpointPort: cluster.Endpoint?.Port, |
| 395 | vpcId: cluster.VpcId, |
| 396 | availabilityZone: cluster.AvailabilityZone, |
| 397 | clusterSubnetGroupName: cluster.ClusterSubnetGroupName, |
| 398 | publiclyAccessible: cluster.PubliclyAccessible, |
| 399 | encrypted: cluster.Encrypted, |
| 400 | kmsKeyId: cluster.KmsKeyId, |
| 401 | masterPasswordSecretArn: cluster.MasterPasswordSecretArn, |
| 402 | tags: toTagRecord(cluster.Tags), |
| 403 | }; |
| 404 | }); |
| 405 | |
| 406 | return { |
| 407 | stables: ["clusterIdentifier", "clusterArn", "clusterNamespaceArn"], |
| 408 | |
| 409 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 410 | if (!isResolved(news)) return undefined; |
| 411 | const n = news ?? {}; |
| 412 | const o = olds ?? {}; |
| 413 | if ((yield* toName(id, o)) !== (yield* toName(id, n))) { |
| 414 | return { action: "replace" } as const; |
| 415 | } |
| 416 | // Create-only properties force a replacement. |
| 417 | if ((n.dbName ?? DEFAULT_DB_NAME) !== (o.dbName ?? DEFAULT_DB_NAME)) { |
| 418 | return { action: "replace" } as const; |
| 419 | } |
| 420 | if ( |
| 421 | (n.masterUsername ?? DEFAULT_MASTER_USERNAME) !== |
| 422 | (o.masterUsername ?? DEFAULT_MASTER_USERNAME) |
| 423 | ) { |
| 424 | return { action: "replace" } as const; |
| 425 | } |
| 426 | if ( |
| 427 | n.clusterSubnetGroupName !== undefined && |
| 428 | o.clusterSubnetGroupName !== undefined && |
| 429 | n.clusterSubnetGroupName !== o.clusterSubnetGroupName |
| 430 | ) { |
| 431 | return { action: "replace" } as const; |
| 432 | } |
| 433 | if ((n.port ?? undefined) !== (o.port ?? undefined)) { |
| 434 | return { action: "replace" } as const; |
| 435 | } |
| 436 | if ((n.availabilityZone ?? undefined) !== (o.availabilityZone ?? undefined)) { |
| 437 | return { action: "replace" } as const; |
| 438 | } |
| 439 | }), |
| 440 | |
| 441 | read: Effect.fn(function* ({ id, olds, output }) { |
| 442 | const identifier = output?.clusterIdentifier ?? (yield* toName(id, olds ?? {})); |
| 443 | const cluster = yield* readCluster(identifier); |
| 444 | if (cluster === undefined) return undefined; |
| 445 | const attrs = yield* toAttrs(cluster); |
| 446 | return (yield* hasAlchemyTags(id, attrs.tags)) ? attrs : Unowned(attrs); |
| 447 | }), |
| 448 | |
| 449 | reconcile: Effect.fn(function* ({ id, news, olds, output, session }) { |
| 450 | const identifier = output?.clusterIdentifier ?? (yield* toName(id, news)); |
| 451 | const internalTags = yield* createInternalTags(id); |
| 452 | const desiredTags = { ...internalTags, ...news.tags }; |
| 453 | const desiredNodes = news.numberOfNodes ?? 1; |
| 454 | const desiredNodeType = news.nodeType ?? DEFAULT_NODE_TYPE; |
| 455 | const clusterType = desiredNodes > 1 ? "multi-node" : "single-node"; |
| 456 | |
| 457 | // 1. Observe — cloud state is authoritative. |
| 458 | let observed = yield* readCluster(identifier); |
| 459 | |
| 460 | // 2. Ensure — create if missing; tolerate AlreadyExists as a race. |
| 461 | if (observed === undefined) { |
| 462 | if (!news.masterUserPassword && !news.manageMasterPassword) { |
| 463 | return yield* new RedshiftMasterPasswordRequired({ |
| 464 | clusterIdentifier: identifier, |
| 465 | }); |
| 466 | } |
| 467 | yield* redshift |
| 468 | .createCluster({ |
| 469 | ClusterIdentifier: identifier, |
| 470 | NodeType: desiredNodeType, |
| 471 | ClusterType: clusterType, |
| 472 | NumberOfNodes: desiredNodes > 1 ? desiredNodes : undefined, |
| 473 | MasterUsername: news.masterUsername ?? DEFAULT_MASTER_USERNAME, |
| 474 | MasterUserPassword: news.manageMasterPassword ? undefined : news.masterUserPassword, |
| 475 | ManageMasterPassword: news.manageMasterPassword, |
| 476 | DBName: news.dbName ?? DEFAULT_DB_NAME, |
| 477 | ClusterSubnetGroupName: news.clusterSubnetGroupName, |
| 478 | VpcSecurityGroupIds: news.vpcSecurityGroupIds, |
| 479 | ClusterParameterGroupName: news.clusterParameterGroupName, |
| 480 | PubliclyAccessible: news.publiclyAccessible ?? false, |
| 481 | Encrypted: news.encrypted ?? true, |
| 482 | KmsKeyId: news.kmsKeyId, |
| 483 | Port: news.port, |
| 484 | AvailabilityZone: news.availabilityZone, |
| 485 | PreferredMaintenanceWindow: news.preferredMaintenanceWindow, |
| 486 | AutomatedSnapshotRetentionPeriod: durationToDays( |
| 487 | news.automatedSnapshotRetentionPeriod, |
| 488 | ), |
| 489 | AllowVersionUpgrade: news.allowVersionUpgrade, |
| 490 | EnhancedVpcRouting: news.enhancedVpcRouting, |
| 491 | IamRoles: news.iamRoles, |
| 492 | Tags: Object.entries(desiredTags).map(([Key, Value]) => ({ |
| 493 | Key, |
| 494 | Value, |
| 495 | })), |
| 496 | }) |
| 497 | .pipe(Effect.catchTag("ClusterAlreadyExistsFault", () => Effect.undefined)); |
| 498 | } |
| 499 | |
| 500 | // Provisioning and in-flight modifications both surface as a |
| 501 | // non-available status; wait (bounded) so modify calls do not hit |
| 502 | // InvalidClusterStateFault. |
| 503 | observed = yield* waitForAvailable(identifier); |
| 504 | |
| 505 | // 3. Sync — compute the update delta from OBSERVED state. |
| 506 | const update: redshift.ModifyClusterMessage = { |
| 507 | ClusterIdentifier: identifier, |
| 508 | }; |
| 509 | let mutated = false; |
| 510 | if ( |
| 511 | desiredNodeType !== observed.NodeType || |
| 512 | desiredNodes !== (observed.NumberOfNodes ?? 1) |
| 513 | ) { |
| 514 | // Resizes must specify node type, count and cluster type |
| 515 | // together even when only one of them changes. |
| 516 | update.NodeType = desiredNodeType; |
| 517 | update.NumberOfNodes = desiredNodes; |
| 518 | update.ClusterType = clusterType; |
| 519 | mutated = true; |
| 520 | } |
| 521 | const observedSecurityGroups = (observed.VpcSecurityGroups ?? []).flatMap((group) => |
| 522 | group.VpcSecurityGroupId ? [group.VpcSecurityGroupId] : [], |
| 523 | ); |
| 524 | if ( |
| 525 | news.vpcSecurityGroupIds !== undefined && |
| 526 | !sameStringSet(news.vpcSecurityGroupIds, observedSecurityGroups) |
| 527 | ) { |
| 528 | update.VpcSecurityGroupIds = news.vpcSecurityGroupIds; |
| 529 | mutated = true; |
| 530 | } |
| 531 | if ( |
| 532 | news.clusterParameterGroupName !== undefined && |
| 533 | !(observed.ClusterParameterGroups ?? []).some( |
| 534 | (group) => group.ParameterGroupName === news.clusterParameterGroupName, |
| 535 | ) |
| 536 | ) { |
| 537 | update.ClusterParameterGroupName = news.clusterParameterGroupName; |
| 538 | mutated = true; |
| 539 | } |
| 540 | if ( |
| 541 | news.publiclyAccessible !== undefined && |
| 542 | news.publiclyAccessible !== observed.PubliclyAccessible |
| 543 | ) { |
| 544 | update.PubliclyAccessible = news.publiclyAccessible; |
| 545 | mutated = true; |
| 546 | } |
| 547 | if (news.encrypted !== undefined && news.encrypted !== observed.Encrypted) { |
| 548 | update.Encrypted = news.encrypted; |
| 549 | update.KmsKeyId = news.kmsKeyId; |
| 550 | mutated = true; |
| 551 | } else if (news.kmsKeyId !== undefined && news.kmsKeyId !== observed.KmsKeyId) { |
| 552 | update.Encrypted = news.encrypted ?? true; |
| 553 | update.KmsKeyId = news.kmsKeyId; |
| 554 | mutated = true; |
| 555 | } |
| 556 | if ( |
| 557 | news.preferredMaintenanceWindow !== undefined && |
| 558 | news.preferredMaintenanceWindow !== observed.PreferredMaintenanceWindow |
| 559 | ) { |
| 560 | update.PreferredMaintenanceWindow = news.preferredMaintenanceWindow; |
| 561 | mutated = true; |
| 562 | } |
| 563 | const desiredRetentionDays = durationToDays(news.automatedSnapshotRetentionPeriod); |
| 564 | if ( |
| 565 | desiredRetentionDays !== undefined && |
| 566 | desiredRetentionDays !== observed.AutomatedSnapshotRetentionPeriod |
| 567 | ) { |
| 568 | update.AutomatedSnapshotRetentionPeriod = desiredRetentionDays; |
| 569 | mutated = true; |
| 570 | } |
| 571 | if ( |
| 572 | news.allowVersionUpgrade !== undefined && |
| 573 | news.allowVersionUpgrade !== observed.AllowVersionUpgrade |
| 574 | ) { |
| 575 | update.AllowVersionUpgrade = news.allowVersionUpgrade; |
| 576 | mutated = true; |
| 577 | } |
| 578 | if ( |
| 579 | news.enhancedVpcRouting !== undefined && |
| 580 | news.enhancedVpcRouting !== observed.EnhancedVpcRouting |
| 581 | ) { |
| 582 | update.EnhancedVpcRouting = news.enhancedVpcRouting; |
| 583 | mutated = true; |
| 584 | } |
| 585 | // The live password is unobservable — apply it only when the |
| 586 | // declared value actually changed (olds is a hint, never truth). |
| 587 | if ( |
| 588 | olds !== undefined && |
| 589 | news.masterUserPassword !== undefined && |
| 590 | (olds.masterUserPassword === undefined || |
| 591 | unwrapRedactedString(news.masterUserPassword) !== |
| 592 | unwrapRedactedString(olds.masterUserPassword)) |
| 593 | ) { |
| 594 | update.MasterUserPassword = news.masterUserPassword; |
| 595 | mutated = true; |
| 596 | } |
| 597 | if ( |
| 598 | news.manageMasterPassword === true && |
| 599 | observed.MasterPasswordSecretArn === undefined |
| 600 | ) { |
| 601 | update.ManageMasterPassword = true; |
| 602 | mutated = true; |
| 603 | } |
| 604 | |
| 605 | if (mutated) { |
| 606 | yield* retryWhileClusterTransitioning(redshift.modifyCluster(update)); |
| 607 | observed = yield* waitForAvailable(identifier); |
| 608 | } |
| 609 | |
| 610 | // 3b. Sync tags — diff against OBSERVED cloud tags (describe |
| 611 | // surfaces them inline). |
| 612 | const { accountId, region } = yield* AWSEnvironment.current; |
| 613 | const arn = redshiftArn(region, accountId, "cluster", identifier); |
| 614 | const { removed, upsert } = diffTags(toTagRecord(observed.Tags), desiredTags); |
| 615 | yield* applyRedshiftTagDelta({ arn, upsert, removed }); |
| 616 | |
| 617 | yield* session.note(arn); |
| 618 | const attrs = yield* toAttrs(observed); |
| 619 | return { ...attrs, tags: desiredTags }; |
| 620 | }), |
| 621 | |
| 622 | delete: Effect.fn(function* ({ output }) { |
| 623 | const identifier = output.clusterIdentifier; |
| 624 | // A cluster mid-create/modify rejects deletion with |
| 625 | // InvalidClusterStateFault — wait (bounded) for it to settle |
| 626 | // first. A cluster already deleting (or gone) is success. |
| 627 | yield* waitUntilSettled(identifier).pipe(Effect.orElseSucceed(() => undefined)); |
| 628 | yield* retryWhileClusterTransitioning( |
| 629 | redshift |
| 630 | .deleteCluster({ |
| 631 | ClusterIdentifier: identifier, |
| 632 | SkipFinalClusterSnapshot: true, |
| 633 | }) |
| 634 | .pipe(Effect.catchTag("ClusterNotFoundFault", () => Effect.undefined)), |
| 635 | ).pipe(Effect.catchTag("InvalidClusterStateFault", () => Effect.void)); |
| 636 | }), |
| 637 | |
| 638 | list: () => |
| 639 | // Top-level account/region collection: exhaustively paginate |
| 640 | // describeClusters; tags come inline on each cluster. |
| 641 | redshift.describeClusters.pages({}).pipe( |
| 642 | Stream.runCollect, |
| 643 | Effect.map((chunk) => |
| 644 | Array.from(chunk).flatMap((page) => |
| 645 | (page.Clusters ?? []).filter((cluster) => cluster.ClusterIdentifier !== undefined), |
| 646 | ), |
| 647 | ), |
| 648 | Effect.flatMap( |
| 649 | Effect.forEach((cluster) => toAttrs(cluster), { |
| 650 | concurrency: 4, |
| 651 | }), |
| 652 | ), |
| 653 | ), |
| 654 | }; |
| 655 | }), |
| 656 | ); |