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

main
ts 176 lines 6.13 KB
Raw
1 import * as BCMDataExports from "@alchemy.run/aws/BCMDataExports";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import { Bucket } from "@alchemy.run/aws/S3/Bucket";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 const bucketName = "alchemy-test-bcm-bindings-dest";
13 export const fixtureExportName = "alchemy-test-bcm-bindings-export";
14
15 // A well-formed-but-nonexistent execution id — drives the typed error path
16 // for GetExecution (a fresh export has no executions to read yet).
17 const NONEXISTENT_EXECUTION_ID = "00000000-0000-0000-0000-000000000000";
18
19 const queryStatement =
20 "SELECT identity_line_item_id, identity_time_interval, line_item_unblended_cost FROM COST_AND_USAGE_REPORT";
21
22 export class BCMDataExportsTestFunction extends Lambda.Function<Lambda.Function>()(
23 "BCMDataExportsTestFunction",
24 ) {}
25
26 export default BCMDataExportsTestFunction.make(
27 {
28 main,
29 functionUrl: true,
30 },
31 Effect.gen(function* () {
32 // Destination bucket must let the Data Exports service principals write
33 // to it and read its policy.
34 const bucket = yield* Bucket("BindingsExportDest", {
35 bucketName,
36 forceDestroy: true,
37 policy: [
38 {
39 Sid: "EnableAWSDataExportsToWriteToS3AndCheckPolicy",
40 Effect: "Allow",
41 Principal: {
42 Service: ["billingreports.amazonaws.com", "bcm-data-exports.amazonaws.com"],
43 },
44 Action: ["s3:PutObject", "s3:GetBucketPolicy"],
45 Resource: [`arn:aws:s3:::${bucketName}`, `arn:aws:s3:::${bucketName}/*`],
46 },
47 ],
48 });
49
50 const dataExport = yield* BCMDataExports.Export("BindingsExport", {
51 exportName: fixtureExportName,
52 description: "alchemy bcm-data-exports bindings fixture",
53 dataQuery: {
54 queryStatement,
55 tableConfigurations: {
56 COST_AND_USAGE_REPORT: {
57 TIME_GRANULARITY: "HOURLY",
58 INCLUDE_RESOURCES: "FALSE",
59 INCLUDE_MANUAL_DISCOUNT_COMPATIBILITY: "FALSE",
60 INCLUDE_SPLIT_COST_ALLOCATION_DATA: "FALSE",
61 },
62 },
63 },
64 s3Destination: {
65 s3Bucket: bucket.bucketName,
66 s3Prefix: "bindings",
67 s3Region: "us-east-1",
68 },
69 });
70
71 // --- export-scoped bindings ---
72 const getExport = yield* BCMDataExports.GetExport(dataExport);
73 const getExecution = yield* BCMDataExports.GetExecution(dataExport);
74 const listExecutions = yield* BCMDataExports.ListExecutions(dataExport);
75
76 // --- account-level bindings ---
77 const listExports = yield* BCMDataExports.ListExports();
78 const getTable = yield* BCMDataExports.GetTable();
79 const listTables = yield* BCMDataExports.ListTables();
80
81 const bound = {
82 getExport,
83 getExecution,
84 listExecutions,
85 listExports,
86 getTable,
87 listTables,
88 };
89
90 return {
91 fetch: Effect.gen(function* () {
92 const request = yield* HttpServerRequest;
93 const url = new URL(request.originalUrl);
94 const pathname = url.pathname;
95
96 if (request.method === "GET" && pathname === "/bindings") {
97 return yield* HttpServerResponse.json({
98 bound: Object.keys(bound),
99 });
100 }
101
102 if (request.method === "GET" && pathname === "/export") {
103 const result = yield* getExport();
104 return yield* HttpServerResponse.json({
105 name: result.Export?.Name ?? null,
106 prefix: result.Export?.DestinationConfigurations.S3Destination.S3Prefix ?? null,
107 frequency: result.Export?.RefreshCadence.Frequency ?? null,
108 });
109 }
110
111 if (request.method === "GET" && pathname === "/exports") {
112 const result = yield* listExports({ MaxResults: 100 });
113 return yield* HttpServerResponse.json({
114 names: (result.Exports ?? []).map((e) => e.ExportName ?? null),
115 });
116 }
117
118 if (request.method === "GET" && pathname === "/executions") {
119 const result = yield* listExecutions({ MaxResults: 25 });
120 return yield* HttpServerResponse.json({
121 count: (result.Executions ?? []).length,
122 });
123 }
124
125 if (request.method === "GET" && pathname === "/execution-not-found") {
126 // Exercises `ExportArn` injection + the typed error path — a fresh
127 // export has no executions, so the nonexistent id must surface a
128 // typed tag (an IAM gap would surface AccessDeniedException and
129 // fail the route with an opaque 500 instead).
130 const tag = yield* getExecution({
131 ExecutionId: NONEXISTENT_EXECUTION_ID,
132 }).pipe(
133 Effect.map(() => "Found"),
134 Effect.catchTag(["ResourceNotFoundException", "ValidationException"], (e) =>
135 Effect.succeed(e._tag),
136 ),
137 );
138 return yield* HttpServerResponse.json({ tag });
139 }
140
141 if (request.method === "GET" && pathname === "/table") {
142 const result = yield* getTable({
143 TableName: "COST_AND_USAGE_REPORT",
144 });
145 return yield* HttpServerResponse.json({
146 tableName: result.TableName ?? null,
147 columnCount: (result.Schema ?? []).length,
148 });
149 }
150
151 if (request.method === "GET" && pathname === "/tables") {
152 const result = yield* listTables({ MaxResults: 100 });
153 return yield* HttpServerResponse.json({
154 names: (result.Tables ?? []).map((t) => t.TableName ?? null),
155 });
156 }
157
158 return yield* HttpServerResponse.json(
159 { error: "Not found", method: request.method, pathname },
160 { status: 404 },
161 );
162 }).pipe(Effect.orDie),
163 };
164 }).pipe(
165 Effect.provide(
166 Layer.mergeAll(
167 BCMDataExports.GetExportHttp,
168 BCMDataExports.GetExecutionHttp,
169 BCMDataExports.ListExecutionsHttp,
170 BCMDataExports.ListExportsHttp,
171 BCMDataExports.GetTableHttp,
172 BCMDataExports.ListTablesHttp,
173 ),
174 ),
175 ),
176 );