| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Binding from "../../Binding.ts"; |
| 3 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 4 | import type { LifecyclePolicy } from "./LifecyclePolicy.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Shared scaffolding for Amazon Data Lifecycle Manager (DLM) HTTP bindings. |
| 8 | * |
| 9 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 10 | * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders |
| 11 | * below. Everything except the operation and the IAM action list is |
| 12 | * boilerplate. |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | * Build the impl Effect for a DLM operation scoped to a |
| 17 | * {@link LifecyclePolicy}: the deploy-time half grants `actions` on the bound |
| 18 | * policy's ARN, and the runtime half injects the policy's `PolicyId` into |
| 19 | * every request. |
| 20 | */ |
| 21 | export const makeDlmPolicyHttpBinding = < |
| 22 | I extends { PolicyId: string }, |
| 23 | A, |
| 24 | E, |
| 25 | R, |
| 26 | >(options: { |
| 27 | /** Fully-qualified binding tag, e.g. `AWS.DLM.GetLifecyclePolicy`. */ |
| 28 | tag: string; |
| 29 | /** The distilled operation. */ |
| 30 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 31 | /** IAM actions granted on the lifecycle policy ARN. */ |
| 32 | actions: readonly string[]; |
| 33 | }) => |
| 34 | Effect.gen(function* () { |
| 35 | const op = yield* options.operation; |
| 36 | |
| 37 | return Effect.fn(function* (policy: LifecyclePolicy) { |
| 38 | const PolicyId = yield* policy.policyId; |
| 39 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 40 | const host = yield* Binding.Host; |
| 41 | if (isBindingHost(host)) { |
| 42 | yield* host.bind`Allow(${host}, ${options.tag}(${policy}))`({ |
| 43 | policyStatements: [ |
| 44 | { |
| 45 | Effect: "Allow", |
| 46 | Action: [...options.actions], |
| 47 | Resource: [policy.policyArn], |
| 48 | }, |
| 49 | ], |
| 50 | }); |
| 51 | } |
| 52 | } |
| 53 | return Effect.fn(`${options.tag}(${policy.LogicalId})`)(function* ( |
| 54 | request?: Omit<I, "PolicyId">, |
| 55 | ) { |
| 56 | const policyId = yield* PolicyId; |
| 57 | return yield* op({ ...request, PolicyId: policyId } as I); |
| 58 | }); |
| 59 | }); |
| 60 | }); |
| 61 | |
| 62 | /** |
| 63 | * Build the impl Effect for an account-level DLM operation (enumerating the |
| 64 | * account's lifecycle policies). The deploy-time half grants `actions` on |
| 65 | * `*` — `dlm:GetLifecyclePolicies` is a list action that is not scoped to a |
| 66 | * single policy resource. |
| 67 | */ |
| 68 | export const makeDlmAccountHttpBinding = <I, A, E, R>(options: { |
| 69 | /** Fully-qualified binding tag, e.g. `AWS.DLM.GetLifecyclePolicies`. */ |
| 70 | tag: string; |
| 71 | /** The distilled operation, invoked with the caller's request as-is. */ |
| 72 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 73 | /** IAM actions granted on `*`. */ |
| 74 | actions: readonly string[]; |
| 75 | }) => |
| 76 | Effect.gen(function* () { |
| 77 | const op = yield* options.operation; |
| 78 | |
| 79 | return Effect.fn(function* () { |
| 80 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 81 | const host = yield* Binding.Host; |
| 82 | if (isBindingHost(host)) { |
| 83 | yield* host.bind`Allow(${host}, ${options.tag}())`({ |
| 84 | policyStatements: [ |
| 85 | { |
| 86 | Effect: "Allow", |
| 87 | Action: [...options.actions], |
| 88 | Resource: ["*"], |
| 89 | }, |
| 90 | ], |
| 91 | }); |
| 92 | } |
| 93 | } |
| 94 | return Effect.fn(options.tag)(function* (request?: I) { |
| 95 | return yield* op((request ?? {}) as I); |
| 96 | }); |
| 97 | }); |
| 98 | }); |