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 { Vpc, VpcPeeringConnection } from "@alchemy.run/aws/EC2"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Test from "./VpcTest.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: AWS.providers() }, 2); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | const DEAD = new Set(["deleted", "deleting", "rejected", "failed", "expired"]); |
| 17 | |
| 18 | class PeeringStillLive extends Data.TaggedError("PeeringStillLive") {} |
| 19 | |
| 20 | const assertDeleted = Effect.fn(function* (pcxId: string) { |
| 21 | yield* EC2.describeVpcPeeringConnections({ |
| 22 | VpcPeeringConnectionIds: [pcxId], |
| 23 | }).pipe( |
| 24 | Effect.flatMap((r) => { |
| 25 | const code = r.VpcPeeringConnections?.[0]?.Status?.Code; |
| 26 | return code === undefined || DEAD.has(code) |
| 27 | ? Effect.void |
| 28 | : Effect.fail(new PeeringStillLive()); |
| 29 | }), |
| 30 | Effect.retry({ |
| 31 | while: (e) => e instanceof PeeringStillLive, |
| 32 | schedule: Schedule.max([Schedule.exponential(300), Schedule.recurs(8)]), |
| 33 | }), |
| 34 | Effect.catchTag("InvalidVpcPeeringConnectionID.NotFound", () => Effect.void), |
| 35 | Effect.catchTag("InvalidVpcPeeringConnectionId.NotFound", () => Effect.void), |
| 36 | ); |
| 37 | }); |
| 38 | |
| 39 | test.provider( |
| 40 | "create, auto-accept, delete same-account peering connection", |
| 41 | (stack) => |
| 42 | Effect.gen(function* () { |
| 43 | yield* stack.destroy(); |
| 44 | |
| 45 | const { peering, vpcA, vpcB } = yield* stack.deploy( |
| 46 | Effect.gen(function* () { |
| 47 | const vpcA = yield* Vpc("PeeringVpcA", { |
| 48 | cidrBlock: "10.10.0.0/16", |
| 49 | }); |
| 50 | const vpcB = yield* Vpc("PeeringVpcB", { |
| 51 | cidrBlock: "10.20.0.0/16", |
| 52 | }); |
| 53 | const peering = yield* VpcPeeringConnection("Peering", { |
| 54 | vpcId: vpcA.vpcId, |
| 55 | peerVpcId: vpcB.vpcId, |
| 56 | }); |
| 57 | return { peering, vpcA, vpcB }; |
| 58 | }), |
| 59 | ); |
| 60 | |
| 61 | expect(peering.vpcPeeringConnectionId).toMatch(/^pcx-/); |
| 62 | expect(peering.status).toEqual("active"); |
| 63 | expect(peering.requesterVpcId).toEqual(vpcA.vpcId); |
| 64 | expect(peering.accepterVpcId).toEqual(vpcB.vpcId); |
| 65 | |
| 66 | // Verify out-of-band that it is ACTIVE. |
| 67 | const described = yield* EC2.describeVpcPeeringConnections({ |
| 68 | VpcPeeringConnectionIds: [peering.vpcPeeringConnectionId], |
| 69 | }); |
| 70 | const pcx = described.VpcPeeringConnections?.[0]; |
| 71 | expect(pcx?.Status?.Code).toEqual("active"); |
| 72 | expect(pcx?.RequesterVpcInfo?.VpcId).toEqual(vpcA.vpcId); |
| 73 | expect(pcx?.AccepterVpcInfo?.VpcId).toEqual(vpcB.vpcId); |
| 74 | |
| 75 | yield* stack.destroy(); |
| 76 | yield* assertDeleted(peering.vpcPeeringConnectionId); |
| 77 | }).pipe(logLevel), |
| 78 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 240_000 }, |
| 79 | ); |
| 80 | |
| 81 | test.provider( |
| 82 | "list enumerates the deployed peering connection", |
| 83 | (stack) => |
| 84 | Effect.gen(function* () { |
| 85 | yield* stack.destroy(); |
| 86 | |
| 87 | const { peering } = yield* stack.deploy( |
| 88 | Effect.gen(function* () { |
| 89 | const vpcA = yield* Vpc("ListPeeringVpcA", { |
| 90 | cidrBlock: "10.30.0.0/16", |
| 91 | }); |
| 92 | const vpcB = yield* Vpc("ListPeeringVpcB", { |
| 93 | cidrBlock: "10.40.0.0/16", |
| 94 | }); |
| 95 | const peering = yield* VpcPeeringConnection("ListPeering", { |
| 96 | vpcId: vpcA.vpcId, |
| 97 | peerVpcId: vpcB.vpcId, |
| 98 | }); |
| 99 | return { peering }; |
| 100 | }), |
| 101 | ); |
| 102 | |
| 103 | const provider = yield* Provider.findProvider(VpcPeeringConnection); |
| 104 | const all = yield* provider.list(); |
| 105 | expect(all.some((x) => x.vpcPeeringConnectionId === peering.vpcPeeringConnectionId)).toBe( |
| 106 | true, |
| 107 | ); |
| 108 | |
| 109 | yield* stack.destroy(); |
| 110 | yield* assertDeleted(peering.vpcPeeringConnectionId); |
| 111 | }).pipe(logLevel), |
| 112 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 240_000 }, |
| 113 | ); |