| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Binding from "../../Binding.ts"; |
| 3 | import * as Output from "../../Output.ts"; |
| 4 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 5 | import type { Cluster } from "./Cluster.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Shared scaffolding for AWS DocDBElastic HTTP bindings. |
| 9 | * |
| 10 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a |
| 11 | * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders |
| 12 | * below. Everything except the operation and the IAM action list is |
| 13 | * boilerplate. |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * Elastic-cluster snapshot ARNs embed a server-generated UUID that is only |
| 18 | * known at runtime, so snapshot-scoped grants use this wildcard. |
| 19 | */ |
| 20 | export const SNAPSHOT_ARN_WILDCARD = |
| 21 | "arn:aws:docdb-elastic:*:*:cluster-snapshot/*"; |
| 22 | |
| 23 | /** |
| 24 | * Build the impl Effect for an account-level operation (snapshot management, |
| 25 | * restore, pending maintenance). The deploy-time half grants `actions` on |
| 26 | * `resources` (default `*`) — these operations address snapshots and |
| 27 | * clusters by ARNs that are runtime data. |
| 28 | */ |
| 29 | export const makeDocDBElasticAccountHttpBinding = <I, A, E, R>(options: { |
| 30 | /** Fully-qualified binding tag, e.g. `AWS.DocDBElastic.GetClusterSnapshot`. */ |
| 31 | tag: string; |
| 32 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 33 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 34 | /** IAM actions granted. */ |
| 35 | actions: readonly string[]; |
| 36 | /** |
| 37 | * IAM resources the actions are granted on. |
| 38 | * @default ["*"] |
| 39 | */ |
| 40 | resources?: readonly string[]; |
| 41 | }) => |
| 42 | Effect.gen(function* () { |
| 43 | const op = yield* options.operation; |
| 44 | |
| 45 | return Effect.fn(function* () { |
| 46 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 47 | const host = yield* Binding.Host; |
| 48 | if (isBindingHost(host)) { |
| 49 | yield* host.bind`Allow(${host}, ${options.tag}())`({ |
| 50 | policyStatements: [ |
| 51 | { |
| 52 | Effect: "Allow", |
| 53 | Action: [...options.actions], |
| 54 | Resource: [...(options.resources ?? ["*"])], |
| 55 | }, |
| 56 | ], |
| 57 | }); |
| 58 | } |
| 59 | } |
| 60 | return Effect.fn(options.tag)(function* (request?: I) { |
| 61 | return yield* op((request ?? {}) as I); |
| 62 | }); |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | /** |
| 67 | * Build the impl Effect for a cluster-scoped operation: the runtime callable |
| 68 | * injects the bound {@link Cluster}'s ARN as `clusterArn` and the deploy-time |
| 69 | * half grants `actions` on the cluster ARN (plus any `extraResources`). |
| 70 | */ |
| 71 | export const makeDocDBElasticClusterHttpBinding = < |
| 72 | I extends { clusterArn: string }, |
| 73 | A, |
| 74 | E, |
| 75 | R, |
| 76 | >(options: { |
| 77 | /** Fully-qualified binding tag, e.g. `AWS.DocDBElastic.StopCluster`. */ |
| 78 | tag: string; |
| 79 | /** The distilled operation; `clusterArn` is injected from the cluster. */ |
| 80 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 81 | /** IAM actions granted on the cluster ARN. */ |
| 82 | actions: readonly string[]; |
| 83 | /** Static IAM resources granted in addition to the cluster ARN. */ |
| 84 | extraResources?: readonly string[]; |
| 85 | }) => |
| 86 | Effect.gen(function* () { |
| 87 | const op = yield* options.operation; |
| 88 | |
| 89 | return Effect.fn(function* (cluster: Cluster) { |
| 90 | const clusterArn = yield* cluster.clusterArn; |
| 91 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 92 | const host = yield* Binding.Host; |
| 93 | if (isBindingHost(host)) { |
| 94 | yield* host.bind`Allow(${host}, ${options.tag}(${cluster}))`({ |
| 95 | policyStatements: [ |
| 96 | { |
| 97 | Effect: "Allow", |
| 98 | Action: [...options.actions], |
| 99 | Resource: [ |
| 100 | Output.interpolate`${cluster.clusterArn}`, |
| 101 | ...(options.extraResources ?? []), |
| 102 | ], |
| 103 | }, |
| 104 | ], |
| 105 | }); |
| 106 | } |
| 107 | } |
| 108 | return Effect.fn(`${options.tag}(${cluster.LogicalId})`)(function* ( |
| 109 | request?: Omit<I, "clusterArn">, |
| 110 | ) { |
| 111 | return yield* op({ |
| 112 | ...request, |
| 113 | clusterArn: yield* clusterArn, |
| 114 | } as I); |
| 115 | }); |
| 116 | }); |
| 117 | }); |