import * as Output from "alchemy/Output";
import * as Effect from "effect/Effect";
import * as AWSBindings from "../AWSBindings.ts";
import type { ImagePipeline } from "./ImagePipeline.ts";
/**
* Shared scaffolding for the EC2 Image Builder HTTP bindings.
*
* NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
* a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders
* below. Everything except the operation and the IAM action list is
* boilerplate.
*/
/**
* Build the impl Effect for an Image Builder operation scoped to a bound
* {@link ImagePipeline}: the runtime callable injects the pipeline's ARN as
* `imagePipelineArn` and the deploy-time half grants `actions` on the
* pipeline's ARN.
*
* `clientToken` (an idempotency token) is auto-generated by distilled when
* absent, so it is omitted from the runtime request shape.
*/
export const makeImageBuilderPipelineHttpBinding = <
I extends { imagePipelineArn?: string },
A,
E,
R,
>(options: {
/** Fully-qualified binding tag, e.g. `AWS.ImageBuilder.GetImagePipeline`. */
tag: string;
/** The distilled operation; `imagePipelineArn` is injected. */
operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
/** IAM actions granted on the pipeline ARN. */
actions: readonly string[];
}) =>
Effect.gen(function* () {
const bind = yield* AWSBindings.Bind;
const op = yield* AWSBindings.withCredentials(options.operation);
return Effect.fn(function* (pipeline: ImagePipeline) {
// Outputs yield a DEFERRED effect — resolve again per invocation below.
const PipelineArn = yield* pipeline.imagePipelineArn;
if (!globalThis.__ALCHEMY_RUNTIME__) {
yield* bind`${options.tag}(${pipeline})`({
policyStatements: [
{
Effect: "Allow",
Action: [...options.actions],
Resource: [Output.interpolate`${pipeline.imagePipelineArn}`],
},
],
});
}
return Effect.fn(`${options.tag}(${pipeline.LogicalId})`)(function* (
request?: Omit<I, "imagePipelineArn" | "clientToken">,
) {
return yield* op({
...request,
imagePipelineArn: yield* PipelineArn,
} as I);
});
});
});
/**
* Build the impl Effect for an account-level Image Builder operation (image
* build-version reads and control, account image listings). Image build
* versions are created dynamically by pipeline runs — their ARNs are not
* known at deploy time — so the deploy-time half grants `actions` on `*` and
* the runtime callable passes the caller's request through as-is.
*/
export const makeImageBuilderAccountHttpBinding = <I, A, E, R>(options: {
/** Fully-qualified binding tag, e.g. `AWS.ImageBuilder.GetImage`. */
tag: string;
/** The distilled operation, invoked with the caller's request as-is. */
operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
/** IAM actions granted on `*`. */
actions: readonly string[];
}) =>
Effect.gen(function* () {
const bind = yield* AWSBindings.Bind;
const op = yield* AWSBindings.withCredentials(options.operation);
return Effect.fn(function* () {
if (!globalThis.__ALCHEMY_RUNTIME__) {
yield* bind`${options.tag}()`({
policyStatements: [
{
Effect: "Allow",
Action: [...options.actions],
Resource: ["*"],
},
],
});
}
return Effect.fn(options.tag)(function* (request?: Omit<I, "clientToken">) {
return yield* op((request ?? {}) as I);
});
});
});