The best way to control Claude Code, Codex and more from any computer or phone

main
ts 153 lines 5.48 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { JobTemplate } from "./JobTemplate.ts";
5 import type { VirtualCluster } from "./VirtualCluster.ts";
6
7 /**
8 * Shared scaffolding for Amazon EMR on EKS (`emr-containers`) HTTP bindings.
9 *
10 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
11 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
12 * below. Everything except the operation, the identifier injection, and the
13 * IAM action list is boilerplate.
14 *
15 * EMR on EKS authorizes virtual-cluster-addressed actions against the
16 * virtual cluster ARN (`arn:…:/virtualclusters/vc-id`) and sub-resource
17 * actions (job runs, managed endpoints) against child ARNs
18 * (`…/virtualclusters/vc-id/jobruns/id`, `…/endpoints/id`), so the
19 * virtual-cluster builders grant on both the bound cluster's ARN and its
20 * sub-resource pattern.
21 */
22 export const virtualClusterPolicyStatement = (
23 virtualCluster: VirtualCluster,
24 actions: readonly string[],
25 ) => ({
26 Effect: "Allow" as const,
27 Action: [...actions],
28 Resource: [
29 Output.interpolate`${virtualCluster.virtualClusterArn}`,
30 Output.map(virtualCluster.virtualClusterArn, (arn) => `${arn}/*`),
31 ],
32 });
33
34 /**
35 * Build the impl Effect for an operation whose input carries a
36 * `virtualClusterId` field: the runtime callable injects the bound
37 * {@link VirtualCluster}'s ID and the deploy-time half grants `actions` on
38 * the virtual cluster ARN (and its sub-resource pattern).
39 */
40 export const makeEMRContainersVirtualClusterHttpBinding = <
41 I extends { virtualClusterId?: string },
42 A,
43 E,
44 R,
45 >(options: {
46 /** Fully-qualified binding tag, e.g. `AWS.EMRContainers.StartJobRun`. */
47 tag: string;
48 /** The distilled operation; `virtualClusterId` is injected. */
49 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
50 /** IAM actions granted on the virtual cluster ARN + sub-resource pattern. */
51 actions: readonly string[];
52 }) =>
53 Effect.gen(function* () {
54 const bind = yield* AWSBindings.Bind;
55 const op = yield* AWSBindings.withCredentials(options.operation);
56
57 return Effect.fn(function* (virtualCluster: VirtualCluster) {
58 // Outputs yield a DEFERRED effect — resolve again per invocation below.
59 const VirtualClusterId = yield* virtualCluster.virtualClusterId;
60 if (!globalThis.__ALCHEMY_RUNTIME__) {
61 yield* bind`${options.tag}(${virtualCluster})`({
62 policyStatements: [virtualClusterPolicyStatement(virtualCluster, options.actions)],
63 });
64 }
65 return Effect.fn(`${options.tag}(${virtualCluster.LogicalId})`)(function* (
66 request?: Omit<I, "virtualClusterId">,
67 ) {
68 return yield* op({
69 ...request,
70 virtualClusterId: yield* VirtualClusterId,
71 } as I);
72 });
73 });
74 });
75
76 /**
77 * Build the impl Effect for an operation whose input carries an `id` field
78 * addressing a {@link JobTemplate}: the runtime callable injects the bound
79 * template's ID and the deploy-time half grants `actions` on the template
80 * ARN.
81 */
82 export const makeEMRContainersJobTemplateHttpBinding = <
83 I extends { id?: string },
84 A,
85 E,
86 R,
87 >(options: {
88 /** Fully-qualified binding tag, e.g. `AWS.EMRContainers.DescribeJobTemplate`. */
89 tag: string;
90 /** The distilled operation; the template `id` is injected. */
91 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
92 /** IAM actions granted on the job template ARN. */
93 actions: readonly string[];
94 }) =>
95 Effect.gen(function* () {
96 const bind = yield* AWSBindings.Bind;
97 const op = yield* AWSBindings.withCredentials(options.operation);
98
99 return Effect.fn(function* (template: JobTemplate) {
100 const JobTemplateId = yield* template.jobTemplateId;
101 if (!globalThis.__ALCHEMY_RUNTIME__) {
102 yield* bind`${options.tag}(${template})`({
103 policyStatements: [
104 {
105 Effect: "Allow",
106 Action: [...options.actions],
107 Resource: [template.jobTemplateArn],
108 },
109 ],
110 });
111 }
112 return Effect.fn(`${options.tag}(${template.LogicalId})`)(function* (
113 request?: Omit<I, "id">,
114 ) {
115 return yield* op({ ...request, id: yield* JobTemplateId } as I);
116 });
117 });
118 });
119
120 /**
121 * Build the impl Effect for an account-level operation (e.g. enumerating the
122 * account's job templates). The deploy-time half grants `actions` on `*` —
123 * account-level list actions are not scoped to a single resource.
124 */
125 export const makeEMRContainersAccountHttpBinding = <I, A, E, R>(options: {
126 /** Fully-qualified binding tag, e.g. `AWS.EMRContainers.ListJobTemplates`. */
127 tag: string;
128 /** The distilled operation, invoked with the caller's request as-is. */
129 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
130 /** IAM actions granted on `*`. */
131 actions: readonly string[];
132 }) =>
133 Effect.gen(function* () {
134 const bind = yield* AWSBindings.Bind;
135 const op = yield* AWSBindings.withCredentials(options.operation);
136
137 return Effect.fn(function* () {
138 if (!globalThis.__ALCHEMY_RUNTIME__) {
139 yield* bind`${options.tag}()`({
140 policyStatements: [
141 {
142 Effect: "Allow",
143 Action: [...options.actions],
144 Resource: ["*"],
145 },
146 ],
147 });
148 }
149 return Effect.fn(options.tag)(function* (request?: I) {
150 return yield* op((request ?? {}) as I);
151 });
152 });
153 });