The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import type { Cluster } from "./Cluster.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared scaffolding for provisioned Amazon Redshift HTTP bindings. |
| 8 | * |
| 9 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service (except |
| 10 | * the bespoke `ConnectHttp`) is a thin `Layer.effect(Cap, make…HttpBinding({ |
| 11 | * … }))` over one of the builders below. Everything except the operation, |
| 12 | * the identifier resolver, and the IAM action list is boilerplate. |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | * Build the impl Effect for an account-level operation (cluster discovery, |
| 17 | * event history, snapshot administration). The deploy-time half grants |
| 18 | * `actions` on `*` — these operations span every cluster in the account and |
| 19 | * the identifiers they filter on are runtime data. |
| 20 | */ |
| 21 | export const makeRedshiftAccountHttpBinding = <I, A, E, R>(options: { |
| 22 | /** Fully-qualified binding tag, e.g. `AWS.Redshift.DescribeEvents`. */ |
| 23 | tag: string; |
| 24 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 25 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 26 | /** IAM actions granted on `*`. */ |
| 27 | actions: readonly string[]; |
| 28 | }) => |
| 29 | Effect.gen(function* () { |
| 30 | const bind = yield* AWSBindings.Bind; |
| 31 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 32 | |
| 33 | return Effect.fn(function* () { |
| 34 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 35 | yield* bind`${options.tag}()`({ |
| 36 | policyStatements: [ |
| 37 | { |
| 38 | Effect: "Allow", |
| 39 | Action: [...options.actions], |
| 40 | Resource: ["*"], |
| 41 | }, |
| 42 | ], |
| 43 | }); |
| 44 | } |
| 45 | return Effect.fn(options.tag)(function* (request?: I) { |
| 46 | return yield* op((request ?? {}) as I); |
| 47 | }); |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | /** |
| 52 | * Build the impl Effect for a cluster-scoped operation: the runtime callable |
| 53 | * injects the bound {@link Cluster}'s identifier as `ClusterIdentifier` and |
| 54 | * the deploy-time half grants `actions` on the cluster ARN (plus any |
| 55 | * `extraResources`, e.g. the `snapshot:{cluster}/*` ARN pattern for snapshot |
| 56 | * creation). |
| 57 | */ |
| 58 | export const makeRedshiftClusterHttpBinding = < |
| 59 | I extends { ClusterIdentifier?: string }, |
| 60 | A, |
| 61 | E, |
| 62 | R, |
| 63 | >(options: { |
| 64 | /** Fully-qualified binding tag, e.g. `AWS.Redshift.PauseCluster`. */ |
| 65 | tag: string; |
| 66 | /** The distilled operation; `ClusterIdentifier` is injected. */ |
| 67 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 68 | /** IAM actions granted on the cluster ARN. */ |
| 69 | actions: readonly string[]; |
| 70 | /** Additional IAM resource ARNs derived from the cluster ARN. */ |
| 71 | extraResources?: (clusterArn: string) => string[]; |
| 72 | }) => |
| 73 | Effect.gen(function* () { |
| 74 | const bind = yield* AWSBindings.Bind; |
| 75 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 76 | |
| 77 | return Effect.fn(function* (cluster: Cluster) { |
| 78 | const Identifier = yield* cluster.clusterIdentifier; |
| 79 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 80 | const resources = options.extraResources; |
| 81 | yield* bind`${options.tag}(${cluster})`({ |
| 82 | policyStatements: [ |
| 83 | { |
| 84 | Effect: "Allow", |
| 85 | Action: [...options.actions], |
| 86 | Resource: resources |
| 87 | ? Output.map(cluster.clusterArn, (arn) => [arn, ...resources(arn)]) |
| 88 | : [Output.interpolate`${cluster.clusterArn}`], |
| 89 | }, |
| 90 | ], |
| 91 | }); |
| 92 | } |
| 93 | return Effect.fn(`${options.tag}(${cluster.LogicalId})`)(function* ( |
| 94 | request?: Omit<I, "ClusterIdentifier">, |
| 95 | ) { |
| 96 | return yield* op({ |
| 97 | ...request, |
| 98 | ClusterIdentifier: yield* Identifier, |
| 99 | } as I); |
| 100 | }); |
| 101 | }); |
| 102 | }); |
| 103 | |
| 104 | /** |
| 105 | * The `snapshot:{cluster}/*` ARN derived from a cluster ARN — the extra IAM |
| 106 | * resource snapshot-writing operations are scoped to. |
| 107 | */ |
| 108 | export const clusterSnapshotArnPattern = (clusterArn: string): string => |
| 109 | `${clusterArn.replace(":cluster:", ":snapshot:")}/*`; |