1import * as Lambda from "@/AWS/Lambda";
2import * as Rbin from "@/AWS/Rbin";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import { HttpServerRequest } from "effect/http/HttpServerRequest";
7import * as HttpServerResponse from "effect/http/HttpServerResponse";
8import path from "pathe";
9
10const main = path.resolve(import.meta.dirname, "handler.ts");
11
12export class RbinTestFunction extends Lambda.Function<Lambda.Function>()(
13 "RbinTestFunction",
14) {}
15
16export default RbinTestFunction.make(
17 {
18 main,
19 functionUrl: true,
20 timeout: Duration.seconds(30),
21 },
22 Effect.gen(function* () {
23 // The retention rule the rule-scoped binding is bound to. Tag-level and
24 // targeting a tag no snapshot carries, so it never retains anything.
25 const rule = yield* Rbin.Rule("BindingRule", {
26 resourceType: "EBS_SNAPSHOT",
27 retentionPeriod: "7 days",
28 description: "alchemy rbin bindings fixture rule",
29 resourceTags: [{ key: "AlchemyRbinBindings", value: "true" }],
30 });
31
32 const getRule = yield* Rbin.GetRule(rule);
33 const listRules = yield* Rbin.ListRules();
34
35 const bound = { getRule, listRules };
36
37 return {
38 fetch: Effect.gen(function* () {
39 const request = yield* HttpServerRequest;
40 const url = new URL(request.originalUrl);
41 const pathname = url.pathname;
42
43 if (request.method === "GET" && pathname === "/bindings") {
44 return yield* HttpServerResponse.json({
45 bound: Object.keys(bound),
46 });
47 }
48
49 // Rule-scoped read: the Identifier is injected from the binding.
50 if (request.method === "GET" && pathname === "/rule") {
51 const detail = yield* getRule();
52 return yield* HttpServerResponse.json({
53 identifier: detail.Identifier,
54 resourceType: detail.ResourceType,
55 retentionDays: detail.RetentionPeriod?.RetentionPeriodValue,
56 description: detail.Description,
57 });
58 }
59
60 // Account-level list, scoped to the fixture rule's resource type.
61 if (request.method === "GET" && pathname === "/rules") {
62 const { Rules } = yield* listRules({
63 ResourceType: "EBS_SNAPSHOT",
64 });
65 return yield* HttpServerResponse.json({
66 ids: (Rules ?? []).map((summary) => summary.Identifier),
67 });
68 }
69
70 return yield* HttpServerResponse.json(
71 { error: "Not found", method: request.method, pathname },
72 { status: 404 },
73 );
74 }).pipe(Effect.orDie),
75 };
76 }).pipe(Effect.provide(Layer.mergeAll(Rbin.GetRuleHttp, Rbin.ListRulesHttp))),
77);