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

main
ts 154 lines 5.39 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 { DBCluster } from "./DBCluster.ts";
5 import type { DBInstance } from "./DBInstance.ts";
6
7 /**
8 * Shared scaffolding for Amazon Neptune 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, the identifier resolver, and the
13 * IAM action list is boilerplate.
14 *
15 * Neptune shares the RDS control plane, so its management IAM actions live
16 * under the `rds:` service prefix (`rds:FailoverDBCluster`,
17 * `rds:DescribeDBClusters`, …) and its ARNs use the `rds` service segment.
18 */
19
20 /**
21 * Build the impl Effect for an account-level operation (cluster discovery,
22 * event history, snapshot administration). The deploy-time half grants
23 * `actions` on `*` — these operations span every cluster in the account and
24 * the identifiers they filter on are runtime data.
25 */
26 export const makeNeptuneAccountHttpBinding = <I, A, E, R>(options: {
27 /** Fully-qualified binding tag, e.g. `AWS.Neptune.DescribeEvents`. */
28 tag: string;
29 /** The distilled operation, invoked with the caller's request as-is. */
30 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
31 /** IAM actions granted on `*`. */
32 actions: readonly string[];
33 }) =>
34 Effect.gen(function* () {
35 const bind = yield* AWSBindings.Bind;
36 const op = yield* AWSBindings.withCredentials(options.operation);
37
38 return Effect.fn(function* () {
39 if (!globalThis.__ALCHEMY_RUNTIME__) {
40 yield* bind`${options.tag}()`({
41 policyStatements: [
42 {
43 Effect: "Allow",
44 Action: [...options.actions],
45 Resource: ["*"],
46 },
47 ],
48 });
49 }
50 return Effect.fn(options.tag)(function* (request?: I) {
51 return yield* op((request ?? {}) as I);
52 });
53 });
54 });
55
56 /**
57 * Build the impl Effect for a cluster-scoped operation: the runtime callable
58 * injects the bound {@link DBCluster}'s identifier as `DBClusterIdentifier`
59 * and the deploy-time half grants `actions` on the cluster ARN (plus any
60 * `extraResources`, e.g. the `cluster-snapshot` ARN pattern for snapshot
61 * creation).
62 */
63 export const makeNeptuneClusterHttpBinding = <
64 I extends { DBClusterIdentifier?: string },
65 A,
66 E,
67 R,
68 >(options: {
69 /** Fully-qualified binding tag, e.g. `AWS.Neptune.FailoverDBCluster`. */
70 tag: string;
71 /** The distilled operation; `DBClusterIdentifier` is injected. */
72 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
73 /** IAM actions granted on the cluster ARN. */
74 actions: readonly string[];
75 /** Additional IAM resource ARNs derived from the cluster ARN. */
76 extraResources?: (clusterArn: string) => string[];
77 }) =>
78 Effect.gen(function* () {
79 const bind = yield* AWSBindings.Bind;
80 const op = yield* AWSBindings.withCredentials(options.operation);
81
82 return Effect.fn(function* (cluster: DBCluster) {
83 const Identifier = yield* cluster.dbClusterIdentifier;
84 if (!globalThis.__ALCHEMY_RUNTIME__) {
85 const resources = options.extraResources;
86 yield* bind`${options.tag}(${cluster})`({
87 policyStatements: [
88 {
89 Effect: "Allow",
90 Action: [...options.actions],
91 Resource: resources
92 ? Output.map(cluster.dbClusterArn, (arn) => [arn, ...resources(arn)])
93 : [Output.interpolate`${cluster.dbClusterArn}`],
94 },
95 ],
96 });
97 }
98 return Effect.fn(`${options.tag}(${cluster.LogicalId})`)(function* (
99 request?: Omit<I, "DBClusterIdentifier">,
100 ) {
101 return yield* op({
102 ...request,
103 DBClusterIdentifier: yield* Identifier,
104 } as I);
105 });
106 });
107 });
108
109 /**
110 * Build the impl Effect for an instance-scoped operation: the runtime
111 * callable injects the bound {@link DBInstance}'s identifier as
112 * `DBInstanceIdentifier` and the deploy-time half grants `actions` on the
113 * instance ARN.
114 */
115 export const makeNeptuneInstanceHttpBinding = <
116 I extends { DBInstanceIdentifier?: string },
117 A,
118 E,
119 R,
120 >(options: {
121 /** Fully-qualified binding tag, e.g. `AWS.Neptune.RebootDBInstance`. */
122 tag: string;
123 /** The distilled operation; `DBInstanceIdentifier` is injected. */
124 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
125 /** IAM actions granted on the instance ARN. */
126 actions: readonly string[];
127 }) =>
128 Effect.gen(function* () {
129 const bind = yield* AWSBindings.Bind;
130 const op = yield* AWSBindings.withCredentials(options.operation);
131
132 return Effect.fn(function* (instance: DBInstance) {
133 const Identifier = yield* instance.dbInstanceIdentifier;
134 if (!globalThis.__ALCHEMY_RUNTIME__) {
135 yield* bind`${options.tag}(${instance})`({
136 policyStatements: [
137 {
138 Effect: "Allow",
139 Action: [...options.actions],
140 Resource: [Output.interpolate`${instance.dbInstanceArn}`],
141 },
142 ],
143 });
144 }
145 return Effect.fn(`${options.tag}(${instance.LogicalId})`)(function* (
146 request?: Omit<I, "DBInstanceIdentifier">,
147 ) {
148 return yield* op({
149 ...request,
150 DBInstanceIdentifier: yield* Identifier,
151 } as I);
152 });
153 });
154 });