The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as redshiftserverless from "@distilled.cloud/aws/redshift-serverless"; |
| 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, hasAlchemyTags } from "alchemy/Tags"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | import { readTags, syncTags, toWireTags } from "./internal.ts"; |
| 14 | |
| 15 | export class WorkgroupUnexpectedResponse extends Data.TaggedError("WorkgroupUnexpectedResponse")<{ |
| 16 | readonly message: string; |
| 17 | readonly cause?: unknown; |
| 18 | }> {} |
| 19 | |
| 20 | export interface WorkgroupProps { |
| 21 | /** |
| 22 | * Name of the workgroup. Must be 3-64 characters, lowercase letters, |
| 23 | * numbers, and hyphens. If omitted, a deterministic physical name is |
| 24 | * generated. Changing the name replaces the workgroup. |
| 25 | */ |
| 26 | workgroupName?: string; |
| 27 | /** |
| 28 | * Name of the namespace this workgroup provides compute for. Changing the |
| 29 | * namespace replaces the workgroup. |
| 30 | */ |
| 31 | namespaceName: string; |
| 32 | /** |
| 33 | * Base compute capacity in Redshift Processing Units (RPUs), 8-1024 in |
| 34 | * multiples of 8. 8 RPU is the cheapest configuration. |
| 35 | * @default 8 |
| 36 | */ |
| 37 | baseCapacity?: number; |
| 38 | /** |
| 39 | * Maximum RPUs the workgroup can burst to. Omit to disable the ceiling. |
| 40 | */ |
| 41 | maxCapacity?: number; |
| 42 | /** |
| 43 | * Route all traffic through the VPC (enhanced VPC routing) instead of the |
| 44 | * internet. |
| 45 | * @default false |
| 46 | */ |
| 47 | enhancedVpcRouting?: boolean; |
| 48 | /** |
| 49 | * Whether the workgroup is reachable from the public internet. |
| 50 | * @default false |
| 51 | */ |
| 52 | publiclyAccessible?: boolean; |
| 53 | /** |
| 54 | * VPC subnet IDs the workgroup runs in. Must span at least three |
| 55 | * Availability Zones. Defaults to the account's default-VPC subnets. |
| 56 | */ |
| 57 | subnetIds?: string[]; |
| 58 | /** |
| 59 | * VPC security group IDs attached to the workgroup. |
| 60 | */ |
| 61 | securityGroupIds?: string[]; |
| 62 | /** |
| 63 | * Port the workgroup listens on. |
| 64 | * @default 5439 |
| 65 | */ |
| 66 | port?: number; |
| 67 | /** |
| 68 | * Redshift configuration parameters (e.g. `max_query_execution_time`, |
| 69 | * `enable_user_activity_logging`) as key/value pairs. |
| 70 | */ |
| 71 | configParameters?: Record<string, string>; |
| 72 | /** |
| 73 | * User-defined tags for the workgroup. |
| 74 | */ |
| 75 | tags?: Record<string, string>; |
| 76 | } |
| 77 | |
| 78 | export interface Workgroup extends Resource< |
| 79 | "AWS.RedshiftServerless.Workgroup", |
| 80 | WorkgroupProps, |
| 81 | { |
| 82 | /** |
| 83 | * Name of the workgroup. |
| 84 | */ |
| 85 | workgroupName: string; |
| 86 | /** |
| 87 | * ARN of the workgroup. |
| 88 | */ |
| 89 | workgroupArn: string; |
| 90 | /** |
| 91 | * Unique ID of the workgroup. |
| 92 | */ |
| 93 | workgroupId: string; |
| 94 | /** |
| 95 | * Name of the namespace the workgroup computes against. |
| 96 | */ |
| 97 | namespaceName: string; |
| 98 | /** |
| 99 | * Current workgroup status (e.g. `"AVAILABLE"`). |
| 100 | */ |
| 101 | status: string; |
| 102 | /** |
| 103 | * DNS address of the workgroup endpoint (pgwire host). |
| 104 | */ |
| 105 | endpointAddress: string | undefined; |
| 106 | /** |
| 107 | * Port of the workgroup endpoint (5439 by default). |
| 108 | */ |
| 109 | endpointPort: number | undefined; |
| 110 | /** |
| 111 | * Whether the endpoint is reachable from the public internet. |
| 112 | */ |
| 113 | publiclyAccessible: boolean | undefined; |
| 114 | }, |
| 115 | never, |
| 116 | Providers |
| 117 | > {} |
| 118 | |
| 119 | /** |
| 120 | * An Amazon Redshift Serverless workgroup — the compute half of a serverless |
| 121 | * data warehouse. |
| 122 | * |
| 123 | * A workgroup provides on-demand compute (measured in RPUs) against a |
| 124 | * {@link Namespace}'s data. Creating a workgroup is asynchronous and takes |
| 125 | * roughly 2-5 minutes; the provider waits (bounded) for it to become |
| 126 | * `AVAILABLE`. Because a running workgroup bills against its RPU floor, tear |
| 127 | * it down promptly when you are done. |
| 128 | * |
| 129 | * ### Creating a Workgroup |
| 130 | * **Example:** Minimal (Cheapest) Workgroup |
| 131 | * ```typescript |
| 132 | * const namespace = yield* RedshiftServerless.Namespace("Analytics", { |
| 133 | * adminUsername: "admin", |
| 134 | * manageAdminPassword: true, |
| 135 | * }); |
| 136 | * const workgroup = yield* RedshiftServerless.Workgroup("AnalyticsWg", { |
| 137 | * namespaceName: namespace.namespaceName, |
| 138 | * baseCapacity: 8, |
| 139 | * }); |
| 140 | * // workgroup.endpointAddress -> "<wg>.<account>.<region>.redshift-serverless.amazonaws.com" |
| 141 | * ``` |
| 142 | * |
| 143 | * ### Networking |
| 144 | * **Example:** Publicly Accessible with Explicit Subnets |
| 145 | * ```typescript |
| 146 | * const workgroup = yield* RedshiftServerless.Workgroup("AnalyticsWg", { |
| 147 | * namespaceName: namespace.namespaceName, |
| 148 | * baseCapacity: 8, |
| 149 | * publiclyAccessible: true, |
| 150 | * subnetIds: [subnetA.subnetId, subnetB.subnetId, subnetC.subnetId], |
| 151 | * securityGroupIds: [securityGroup.groupId], |
| 152 | * enhancedVpcRouting: false, |
| 153 | * }); |
| 154 | * ``` |
| 155 | * |
| 156 | * @resource |
| 157 | */ |
| 158 | export const Workgroup = Resource<Workgroup>("AWS.RedshiftServerless.Workgroup"); |
| 159 | |
| 160 | class WorkgroupNotSettled extends Data.TaggedError("WorkgroupNotSettled")<{ |
| 161 | readonly workgroupName: string; |
| 162 | readonly status: string; |
| 163 | }> {} |
| 164 | |
| 165 | const statusIs = (status: string | undefined, expected: string): boolean => |
| 166 | status?.toUpperCase() === expected; |
| 167 | |
| 168 | const sameStringSet = (a: string[], b: string[]): boolean => { |
| 169 | const as = [...a].sort(); |
| 170 | const bs = [...b].sort(); |
| 171 | return as.length === bs.length && as.every((v, i) => v === bs[i]); |
| 172 | }; |
| 173 | |
| 174 | const toConfigList = ( |
| 175 | config: Record<string, string> | undefined, |
| 176 | ): redshiftserverless.ConfigParameter[] | undefined => |
| 177 | config === undefined |
| 178 | ? undefined |
| 179 | : Object.entries(config).map(([parameterKey, parameterValue]) => ({ |
| 180 | parameterKey, |
| 181 | parameterValue, |
| 182 | })); |
| 183 | |
| 184 | export const WorkgroupProvider = () => |
| 185 | Provider.effect( |
| 186 | Workgroup, |
| 187 | Effect.gen(function* () { |
| 188 | const toName = (id: string, props: Partial<WorkgroupProps>) => |
| 189 | props.workgroupName |
| 190 | ? Effect.succeed(props.workgroupName) |
| 191 | : createPhysicalName({ id, maxLength: 64 }); |
| 192 | |
| 193 | const readWorkgroup = Effect.fn(function* (name: string) { |
| 194 | const response = yield* redshiftserverless |
| 195 | .getWorkgroup({ workgroupName: name }) |
| 196 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 197 | return response?.workgroup; |
| 198 | }); |
| 199 | |
| 200 | // Workgroup create/update converge through CREATING/MODIFYING. Creation |
| 201 | // takes ~2-5 min; budget ~8 min (96 * 5s). |
| 202 | const waitForAvailable = Effect.fn(function* (name: string) { |
| 203 | return yield* readWorkgroup(name).pipe( |
| 204 | Effect.flatMap((wg) => |
| 205 | wg !== undefined && !statusIs(wg.status, "AVAILABLE") |
| 206 | ? Effect.fail( |
| 207 | new WorkgroupNotSettled({ |
| 208 | workgroupName: name, |
| 209 | status: wg.status ?? "UNKNOWN", |
| 210 | }), |
| 211 | ) |
| 212 | : Effect.succeed(wg), |
| 213 | ), |
| 214 | Effect.retry({ |
| 215 | while: (e) => e instanceof WorkgroupNotSettled, |
| 216 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(96)]), |
| 217 | }), |
| 218 | ); |
| 219 | }); |
| 220 | |
| 221 | const waitUntilGone = Effect.fn(function* (name: string) { |
| 222 | yield* readWorkgroup(name).pipe( |
| 223 | Effect.flatMap((wg) => |
| 224 | wg === undefined |
| 225 | ? Effect.void |
| 226 | : Effect.fail( |
| 227 | new WorkgroupNotSettled({ |
| 228 | workgroupName: name, |
| 229 | status: wg.status ?? "DELETING", |
| 230 | }), |
| 231 | ), |
| 232 | ), |
| 233 | Effect.retry({ |
| 234 | while: (e) => e instanceof WorkgroupNotSettled, |
| 235 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(96)]), |
| 236 | }), |
| 237 | ); |
| 238 | }); |
| 239 | |
| 240 | const toAttrs = (wg: redshiftserverless.Workgroup) => ({ |
| 241 | workgroupName: wg.workgroupName!, |
| 242 | workgroupArn: wg.workgroupArn!, |
| 243 | workgroupId: wg.workgroupId!, |
| 244 | namespaceName: wg.namespaceName!, |
| 245 | status: wg.status ?? "UNKNOWN", |
| 246 | endpointAddress: wg.endpoint?.address, |
| 247 | endpointPort: wg.endpoint?.port, |
| 248 | publiclyAccessible: wg.publiclyAccessible, |
| 249 | }); |
| 250 | |
| 251 | // Redshift Serverless rejects multi-parameter updates: "You can't |
| 252 | // update multiple parameters in one request." Apply each drifted field |
| 253 | // as its own updateWorkgroup call, waiting for AVAILABLE between them. |
| 254 | const applyUpdate = Effect.fn(function* ( |
| 255 | name: string, |
| 256 | patch: Omit<redshiftserverless.UpdateWorkgroupRequest, "workgroupName">, |
| 257 | ) { |
| 258 | yield* redshiftserverless.updateWorkgroup({ |
| 259 | workgroupName: name, |
| 260 | ...patch, |
| 261 | }); |
| 262 | return yield* waitForAvailable(name); |
| 263 | }); |
| 264 | |
| 265 | return { |
| 266 | stables: ["workgroupName", "workgroupArn", "workgroupId"], |
| 267 | |
| 268 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 269 | if (!isResolved(news)) return undefined; |
| 270 | if ((yield* toName(id, olds ?? {})) !== (yield* toName(id, news ?? {}))) { |
| 271 | return { action: "replace" } as const; |
| 272 | } |
| 273 | // A workgroup can't be reassigned to another namespace. |
| 274 | if ((news?.namespaceName ?? undefined) !== (olds?.namespaceName ?? undefined)) { |
| 275 | return { action: "replace" } as const; |
| 276 | } |
| 277 | }), |
| 278 | |
| 279 | read: Effect.fn(function* ({ id, olds, output }) { |
| 280 | const name = output?.workgroupName ?? (yield* toName(id, olds ?? {})); |
| 281 | const wg = yield* readWorkgroup(name); |
| 282 | if (wg === undefined) return undefined; |
| 283 | const attrs = toAttrs(wg); |
| 284 | const tags = yield* readTags(attrs.workgroupArn); |
| 285 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 286 | }), |
| 287 | |
| 288 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 289 | const name = output?.workgroupName ?? (yield* toName(id, news)); |
| 290 | const internalTags = yield* createInternalTags(id); |
| 291 | const desiredTags = { ...internalTags, ...news.tags }; |
| 292 | const baseCapacity = news.baseCapacity ?? 8; |
| 293 | |
| 294 | // 1. Observe. |
| 295 | let observed = yield* readWorkgroup(name); |
| 296 | |
| 297 | // 2. Ensure — create if missing; tolerate a concurrent create. |
| 298 | if (observed === undefined) { |
| 299 | const created = yield* redshiftserverless |
| 300 | .createWorkgroup({ |
| 301 | workgroupName: name, |
| 302 | namespaceName: news.namespaceName, |
| 303 | baseCapacity, |
| 304 | maxCapacity: news.maxCapacity, |
| 305 | enhancedVpcRouting: news.enhancedVpcRouting, |
| 306 | publiclyAccessible: news.publiclyAccessible, |
| 307 | subnetIds: news.subnetIds, |
| 308 | securityGroupIds: news.securityGroupIds, |
| 309 | port: news.port, |
| 310 | configParameters: toConfigList(news.configParameters), |
| 311 | tags: toWireTags(desiredTags), |
| 312 | }) |
| 313 | .pipe( |
| 314 | Effect.map((r) => r.workgroup), |
| 315 | Effect.catchTag("ConflictException", () => |
| 316 | redshiftserverless |
| 317 | .getWorkgroup({ workgroupName: name }) |
| 318 | .pipe(Effect.map((r) => r.workgroup)), |
| 319 | ), |
| 320 | ); |
| 321 | observed = created; |
| 322 | } |
| 323 | |
| 324 | const settled = yield* waitForAvailable(name); |
| 325 | if (settled !== undefined) observed = settled; |
| 326 | if (observed === undefined) { |
| 327 | return yield* new WorkgroupUnexpectedResponse({ |
| 328 | message: `Redshift workgroup '${name}' disappeared while reconciling`, |
| 329 | }); |
| 330 | } |
| 331 | |
| 332 | // 3. Sync — one updateWorkgroup per drifted field (multi-field |
| 333 | // updates are rejected). Only mutate fields the user specified. |
| 334 | if (news.baseCapacity !== undefined && observed.baseCapacity !== baseCapacity) { |
| 335 | observed = (yield* applyUpdate(name, { baseCapacity })) ?? observed; |
| 336 | } |
| 337 | if ( |
| 338 | news.enhancedVpcRouting !== undefined && |
| 339 | observed.enhancedVpcRouting !== news.enhancedVpcRouting |
| 340 | ) { |
| 341 | observed = |
| 342 | (yield* applyUpdate(name, { |
| 343 | enhancedVpcRouting: news.enhancedVpcRouting, |
| 344 | })) ?? observed; |
| 345 | } |
| 346 | if ( |
| 347 | news.publiclyAccessible !== undefined && |
| 348 | observed.publiclyAccessible !== news.publiclyAccessible |
| 349 | ) { |
| 350 | observed = |
| 351 | (yield* applyUpdate(name, { |
| 352 | publiclyAccessible: news.publiclyAccessible, |
| 353 | })) ?? observed; |
| 354 | } |
| 355 | if (news.port !== undefined && observed.port !== news.port) { |
| 356 | observed = (yield* applyUpdate(name, { port: news.port })) ?? observed; |
| 357 | } |
| 358 | if ( |
| 359 | news.securityGroupIds !== undefined && |
| 360 | !sameStringSet(observed.securityGroupIds ?? [], news.securityGroupIds) |
| 361 | ) { |
| 362 | observed = |
| 363 | (yield* applyUpdate(name, { |
| 364 | securityGroupIds: news.securityGroupIds, |
| 365 | })) ?? observed; |
| 366 | } |
| 367 | |
| 368 | // 3b. Sync tags — diff against OBSERVED cloud tags. |
| 369 | yield* syncTags(observed.workgroupArn!, desiredTags); |
| 370 | |
| 371 | // 4. Return fresh attributes. |
| 372 | yield* session.note(name); |
| 373 | return toAttrs(observed); |
| 374 | }), |
| 375 | |
| 376 | delete: Effect.fn(function* ({ output }) { |
| 377 | yield* redshiftserverless.deleteWorkgroup({ workgroupName: output.workgroupName }).pipe( |
| 378 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 379 | // A concurrent modification may still be settling — retry. |
| 380 | Effect.retry({ |
| 381 | while: (e) => e._tag === "ConflictException", |
| 382 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(24)]), |
| 383 | }), |
| 384 | Effect.catchTag("ConflictException", () => Effect.void), |
| 385 | ); |
| 386 | yield* waitUntilGone(output.workgroupName); |
| 387 | }), |
| 388 | |
| 389 | list: () => |
| 390 | redshiftserverless.listWorkgroups.pages({}).pipe( |
| 391 | Stream.runCollect, |
| 392 | Effect.map((chunk) => |
| 393 | Array.from(chunk) |
| 394 | .flatMap((page) => page.workgroups ?? []) |
| 395 | .flatMap((wg) => |
| 396 | wg.workgroupName !== undefined && |
| 397 | wg.workgroupArn !== undefined && |
| 398 | wg.workgroupId !== undefined && |
| 399 | wg.namespaceName !== undefined |
| 400 | ? [ |
| 401 | { |
| 402 | workgroupName: wg.workgroupName, |
| 403 | workgroupArn: wg.workgroupArn, |
| 404 | workgroupId: wg.workgroupId, |
| 405 | namespaceName: wg.namespaceName, |
| 406 | status: wg.status ?? "UNKNOWN", |
| 407 | endpointAddress: wg.endpoint?.address, |
| 408 | endpointPort: wg.endpoint?.port, |
| 409 | publiclyAccessible: wg.publiclyAccessible, |
| 410 | }, |
| 411 | ] |
| 412 | : [], |
| 413 | ), |
| 414 | ), |
| 415 | ), |
| 416 | }; |
| 417 | }), |
| 418 | ); |