The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { PrefixList } from "@alchemy.run/aws/EC2"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: AWS.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | class PrefixListStillExists extends Data.TaggedError("PrefixListStillExists") {} |
| 17 | |
| 18 | const getEntries = (prefixListId: string) => |
| 19 | EC2.getManagedPrefixListEntries({ PrefixListId: prefixListId }).pipe( |
| 20 | Effect.map((r) => |
| 21 | Object.fromEntries((r.Entries ?? []).map((e) => [e.Cidr!, e.Description ?? ""])), |
| 22 | ), |
| 23 | ); |
| 24 | |
| 25 | const assertDeleted = Effect.fn(function* (prefixListId: string) { |
| 26 | yield* EC2.describeManagedPrefixLists({ PrefixListIds: [prefixListId] }).pipe( |
| 27 | Effect.flatMap((r) => { |
| 28 | const state = r.PrefixLists?.[0]?.State; |
| 29 | return state === undefined || state === "delete-complete" |
| 30 | ? Effect.void |
| 31 | : Effect.fail(new PrefixListStillExists()); |
| 32 | }), |
| 33 | Effect.retry({ |
| 34 | while: (e) => e instanceof PrefixListStillExists, |
| 35 | schedule: Schedule.max([Schedule.exponential(200), Schedule.recurs(8)]), |
| 36 | }), |
| 37 | Effect.catchTag("InvalidPrefixListID.NotFound", () => Effect.void), |
| 38 | ); |
| 39 | }); |
| 40 | |
| 41 | test.provider( |
| 42 | "create, update entries, delete managed prefix list", |
| 43 | (stack) => |
| 44 | Effect.gen(function* () { |
| 45 | yield* stack.destroy(); |
| 46 | |
| 47 | // Create with two entries. |
| 48 | const { prefixList } = yield* stack.deploy( |
| 49 | Effect.gen(function* () { |
| 50 | const prefixList = yield* PrefixList("TestPrefixList", { |
| 51 | maxEntries: 5, |
| 52 | entries: [ |
| 53 | { cidr: "10.0.0.0/16", description: "vpc-a" }, |
| 54 | { cidr: "10.1.0.0/16", description: "vpc-b" }, |
| 55 | ], |
| 56 | }); |
| 57 | return { prefixList }; |
| 58 | }), |
| 59 | ); |
| 60 | |
| 61 | expect(prefixList.prefixListId).toMatch(/^pl-/); |
| 62 | expect(prefixList.addressFamily).toEqual("IPv4"); |
| 63 | expect(prefixList.maxEntries).toEqual(5); |
| 64 | |
| 65 | // Verify out-of-band. |
| 66 | const described = yield* EC2.describeManagedPrefixLists({ |
| 67 | PrefixListIds: [prefixList.prefixListId], |
| 68 | }); |
| 69 | expect(described.PrefixLists?.[0]?.PrefixListId).toEqual(prefixList.prefixListId); |
| 70 | expect(described.PrefixLists?.[0]?.State).toEqual("create-complete"); |
| 71 | |
| 72 | const entries1 = yield* getEntries(prefixList.prefixListId); |
| 73 | expect(entries1).toEqual({ |
| 74 | "10.0.0.0/16": "vpc-a", |
| 75 | "10.1.0.0/16": "vpc-b", |
| 76 | }); |
| 77 | |
| 78 | // Update entries: drop one, add one, change a description. |
| 79 | const { prefixList: updated } = yield* stack.deploy( |
| 80 | Effect.gen(function* () { |
| 81 | const prefixList = yield* PrefixList("TestPrefixList", { |
| 82 | maxEntries: 5, |
| 83 | entries: [ |
| 84 | { cidr: "10.0.0.0/16", description: "vpc-a-renamed" }, |
| 85 | { cidr: "10.2.0.0/16", description: "vpc-c" }, |
| 86 | ], |
| 87 | }); |
| 88 | return { prefixList }; |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | // Same list (in-place modify), version bumped. |
| 93 | expect(updated.prefixListId).toEqual(prefixList.prefixListId); |
| 94 | expect(updated.version).toBeGreaterThan(prefixList.version); |
| 95 | |
| 96 | const entries2 = yield* getEntries(prefixList.prefixListId); |
| 97 | expect(entries2).toEqual({ |
| 98 | "10.0.0.0/16": "vpc-a-renamed", |
| 99 | "10.2.0.0/16": "vpc-c", |
| 100 | }); |
| 101 | |
| 102 | yield* stack.destroy(); |
| 103 | yield* assertDeleted(prefixList.prefixListId); |
| 104 | }).pipe(logLevel), |
| 105 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 240_000 }, |
| 106 | ); |
| 107 | |
| 108 | test.provider( |
| 109 | "list enumerates the deployed prefix list", |
| 110 | (stack) => |
| 111 | Effect.gen(function* () { |
| 112 | yield* stack.destroy(); |
| 113 | |
| 114 | const { prefixList } = yield* stack.deploy( |
| 115 | Effect.gen(function* () { |
| 116 | const prefixList = yield* PrefixList("ListPrefixList", { |
| 117 | maxEntries: 3, |
| 118 | entries: [{ cidr: "192.168.0.0/24" }], |
| 119 | }); |
| 120 | return { prefixList }; |
| 121 | }), |
| 122 | ); |
| 123 | |
| 124 | const provider = yield* Provider.findProvider(PrefixList); |
| 125 | const all = yield* provider.list(); |
| 126 | expect(all.some((x) => x.prefixListId === prefixList.prefixListId)).toBe(true); |
| 127 | |
| 128 | yield* stack.destroy(); |
| 129 | yield* assertDeleted(prefixList.prefixListId); |
| 130 | }).pipe(logLevel), |
| 131 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 240_000 }, |
| 132 | ); |