The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { Vpc } from "@alchemy.run/aws/EC2"; |
| 3 | import { normalizePolicyDocument } from "@alchemy.run/aws/IAM/Policy"; |
| 4 | import { |
| 5 | AuthPolicy, |
| 6 | ResourcePolicy, |
| 7 | Service, |
| 8 | ServiceNetwork, |
| 9 | ServiceNetworkVpcAssociation, |
| 10 | } from "@alchemy.run/aws/VpcLattice"; |
| 11 | import { expect } from "@alchemy.run/test"; |
| 12 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 13 | import * as vpclattice from "@distilled.cloud/aws/vpc-lattice"; |
| 14 | import * as Provider from "alchemy/Provider"; |
| 15 | import type { ScopedPlanStatusSession } from "alchemy/Report"; |
| 16 | import * as Data from "effect/Data"; |
| 17 | import * as Effect from "effect/Effect"; |
| 18 | import * as Result from "effect/Result"; |
| 19 | import * as Schedule from "effect/Schedule"; |
| 20 | import { getDefaultVpc } from "../DefaultVpc.ts"; |
| 21 | |
| 22 | const { test } = Test.make({ providers: AWS.providers() }); |
| 23 | |
| 24 | const stubSession = { |
| 25 | note: () => Effect.void, |
| 26 | } as unknown as ScopedPlanStatusSession; |
| 27 | |
| 28 | const findServiceNetwork = (id: string) => |
| 29 | vpclattice |
| 30 | .getServiceNetwork({ serviceNetworkIdentifier: id }) |
| 31 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined))); |
| 32 | |
| 33 | class StillExists extends Data.TaggedError("StillExists")<{ |
| 34 | readonly id: string; |
| 35 | }> {} |
| 36 | |
| 37 | class AssociationNotReady extends Data.TaggedError("AssociationNotReady")<{ |
| 38 | readonly serviceNetworkId: string; |
| 39 | readonly vpcId: string; |
| 40 | }> {} |
| 41 | |
| 42 | const assertServiceNetworkDeleted = (id: string) => |
| 43 | findServiceNetwork(id).pipe( |
| 44 | Effect.flatMap((sn) => (sn === undefined ? Effect.void : Effect.fail(new StillExists({ id })))), |
| 45 | Effect.retry({ |
| 46 | while: (e) => e._tag === "StillExists", |
| 47 | schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(15)]), |
| 48 | }), |
| 49 | ); |
| 50 | |
| 51 | const assertAssociationDeleted = (id: string) => |
| 52 | vpclattice |
| 53 | .getServiceNetworkVpcAssociation({ |
| 54 | serviceNetworkVpcAssociationIdentifier: id, |
| 55 | }) |
| 56 | .pipe( |
| 57 | Effect.flatMap((assoc) => |
| 58 | assoc.status === "DELETE_IN_PROGRESS" ? Effect.fail(new StillExists({ id })) : Effect.void, |
| 59 | ), |
| 60 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 61 | Effect.retry({ |
| 62 | while: (e) => e._tag === "StillExists", |
| 63 | schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(20)]), |
| 64 | }), |
| 65 | ); |
| 66 | |
| 67 | test.provider( |
| 68 | "create, update authType, delete a service network", |
| 69 | (stack) => |
| 70 | Effect.gen(function* () { |
| 71 | yield* stack.destroy(); |
| 72 | |
| 73 | const network = yield* stack.deploy( |
| 74 | Effect.gen(function* () { |
| 75 | return yield* ServiceNetwork("TestServiceNetwork", { |
| 76 | tags: { Environment: "test" }, |
| 77 | }); |
| 78 | }), |
| 79 | ); |
| 80 | |
| 81 | expect(network.serviceNetworkId).toMatch(/^sn-/); |
| 82 | expect(network.serviceNetworkArn).toContain(":servicenetwork/"); |
| 83 | expect(network.authType).toBe("NONE"); |
| 84 | |
| 85 | const live = yield* findServiceNetwork(network.serviceNetworkId); |
| 86 | expect(live?.arn).toBe(network.serviceNetworkArn); |
| 87 | const tags = yield* vpclattice |
| 88 | .listTagsForResource({ resourceArn: network.serviceNetworkArn }) |
| 89 | .pipe(Effect.map((r) => r.tags ?? {})); |
| 90 | expect(tags["alchemy::id"]).toBe("TestServiceNetwork"); |
| 91 | |
| 92 | // Update the auth type in place. |
| 93 | const updated = yield* stack.deploy( |
| 94 | Effect.gen(function* () { |
| 95 | return yield* ServiceNetwork("TestServiceNetwork", { |
| 96 | authType: "AWS_IAM", |
| 97 | tags: { Environment: "test" }, |
| 98 | }); |
| 99 | }), |
| 100 | ); |
| 101 | expect(updated.serviceNetworkId).toBe(network.serviceNetworkId); |
| 102 | expect(updated.authType).toBe("AWS_IAM"); |
| 103 | const live2 = yield* findServiceNetwork(network.serviceNetworkId); |
| 104 | expect(live2?.authType).toBe("AWS_IAM"); |
| 105 | |
| 106 | yield* stack.destroy(); |
| 107 | yield* assertServiceNetworkDeleted(network.serviceNetworkId); |
| 108 | }), |
| 109 | { |
| 110 | tags: ["provider:aws", "provider:aws:vpclattice", "live"], |
| 111 | timeout: 180_000, |
| 112 | }, |
| 113 | ); |
| 114 | |
| 115 | test.provider( |
| 116 | "create, update idle timeout, delete a service", |
| 117 | (stack) => |
| 118 | Effect.gen(function* () { |
| 119 | yield* stack.destroy(); |
| 120 | |
| 121 | const service = yield* stack.deploy( |
| 122 | Effect.gen(function* () { |
| 123 | return yield* Service("TestLatticeService", { |
| 124 | idleTimeout: "60 seconds", |
| 125 | }); |
| 126 | }), |
| 127 | ); |
| 128 | |
| 129 | expect(service.serviceId).toMatch(/^svc-/); |
| 130 | expect(service.serviceArn).toContain(":service/"); |
| 131 | |
| 132 | const live = yield* vpclattice |
| 133 | .getService({ serviceIdentifier: service.serviceId }) |
| 134 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined))); |
| 135 | expect(live?.idleTimeoutSeconds).toBe(60); |
| 136 | |
| 137 | const updated = yield* stack.deploy( |
| 138 | Effect.gen(function* () { |
| 139 | return yield* Service("TestLatticeService", { |
| 140 | idleTimeout: "120 seconds", |
| 141 | }); |
| 142 | }), |
| 143 | ); |
| 144 | expect(updated.serviceId).toBe(service.serviceId); |
| 145 | const live2 = yield* vpclattice |
| 146 | .getService({ serviceIdentifier: service.serviceId }) |
| 147 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined))); |
| 148 | expect(live2?.idleTimeoutSeconds).toBe(120); |
| 149 | |
| 150 | yield* stack.destroy(); |
| 151 | }), |
| 152 | { |
| 153 | tags: ["provider:aws", "provider:aws:vpclattice", "live"], |
| 154 | timeout: 180_000, |
| 155 | }, |
| 156 | ); |
| 157 | |
| 158 | test.provider( |
| 159 | "auth + resource policies deploy from PolicyDocument and re-deploy clean", |
| 160 | (stack) => |
| 161 | Effect.gen(function* () { |
| 162 | yield* stack.destroy(); |
| 163 | |
| 164 | // Deploy the network alone first — the policy documents below embed the |
| 165 | // resolved ARN/account as plain strings (Outputs are unresolved proxies |
| 166 | // during plan, so string ops on them are impossible inside the program). |
| 167 | const base = yield* stack.deploy( |
| 168 | Effect.gen(function* () { |
| 169 | return yield* ServiceNetwork("PolicyServiceNetwork", { |
| 170 | authType: "AWS_IAM", |
| 171 | }); |
| 172 | }), |
| 173 | ); |
| 174 | const account = base.serviceNetworkArn.split(":")[4]; |
| 175 | const networkArn = base.serviceNetworkArn; |
| 176 | |
| 177 | const program = Effect.gen(function* () { |
| 178 | const network = yield* ServiceNetwork("PolicyServiceNetwork", { |
| 179 | authType: "AWS_IAM", |
| 180 | }); |
| 181 | const authPolicy = yield* AuthPolicy("NetworkAuthPolicy", { |
| 182 | resourceIdentifier: network.serviceNetworkId, |
| 183 | policy: { |
| 184 | Version: "2012-10-17", |
| 185 | Statement: [ |
| 186 | { |
| 187 | Effect: "Allow", |
| 188 | Principal: { AWS: `arn:aws:iam::${account}:root` }, |
| 189 | Action: ["vpc-lattice-svcs:Invoke"], |
| 190 | Resource: "*", |
| 191 | }, |
| 192 | ], |
| 193 | }, |
| 194 | }); |
| 195 | const resourcePolicy = yield* ResourcePolicy("NetworkResourcePolicy", { |
| 196 | resourceArn: network.serviceNetworkArn, |
| 197 | policy: { |
| 198 | Version: "2012-10-17", |
| 199 | Statement: [ |
| 200 | { |
| 201 | Effect: "Allow", |
| 202 | Principal: { AWS: `arn:aws:iam::${account}:root` }, |
| 203 | Action: [ |
| 204 | "vpc-lattice:CreateServiceNetworkVpcAssociation", |
| 205 | "vpc-lattice:CreateServiceNetworkServiceAssociation", |
| 206 | "vpc-lattice:GetServiceNetwork", |
| 207 | ], |
| 208 | Resource: networkArn, |
| 209 | }, |
| 210 | ], |
| 211 | }, |
| 212 | }); |
| 213 | return { authPolicy, network, resourcePolicy }; |
| 214 | }); |
| 215 | |
| 216 | const first = yield* stack.deploy(program); |
| 217 | |
| 218 | expect(first.authPolicy.resourceIdentifier).toBe(first.network.serviceNetworkId); |
| 219 | expect(first.authPolicy.policy).toContain("vpc-lattice-svcs:Invoke"); |
| 220 | expect(first.resourcePolicy.resourceArn).toBe(first.network.serviceNetworkArn); |
| 221 | |
| 222 | // The live documents match the PolicyDocument we deployed (canonicalized). |
| 223 | const liveAuth = yield* vpclattice.getAuthPolicy({ |
| 224 | resourceIdentifier: first.network.serviceNetworkId, |
| 225 | }); |
| 226 | expect(normalizePolicyDocument(liveAuth.policy!)).toBe( |
| 227 | normalizePolicyDocument(first.authPolicy.policy), |
| 228 | ); |
| 229 | const liveResource = yield* vpclattice.getResourcePolicy({ |
| 230 | resourceArn: first.network.serviceNetworkArn, |
| 231 | }); |
| 232 | expect(normalizePolicyDocument(liveResource.policy!)).toBe( |
| 233 | normalizePolicyDocument(first.resourcePolicy.policy), |
| 234 | ); |
| 235 | |
| 236 | // Re-deploy the identical program — the normalized observed-vs-desired |
| 237 | // comparison must skip the puts, so the auth policy's lastUpdatedAt |
| 238 | // timestamp is unchanged (a put always bumps it). |
| 239 | const second = yield* stack.deploy(program); |
| 240 | expect(second.authPolicy.policy).toBe(first.authPolicy.policy); |
| 241 | expect(second.resourcePolicy.policy).toBe(first.resourcePolicy.policy); |
| 242 | const liveAuth2 = yield* vpclattice.getAuthPolicy({ |
| 243 | resourceIdentifier: first.network.serviceNetworkId, |
| 244 | }); |
| 245 | expect(liveAuth2.lastUpdatedAt?.toISOString()).toBe(liveAuth.lastUpdatedAt?.toISOString()); |
| 246 | |
| 247 | yield* stack.destroy(); |
| 248 | yield* assertServiceNetworkDeleted(first.network.serviceNetworkId); |
| 249 | }), |
| 250 | { |
| 251 | tags: ["provider:aws", "provider:aws:iam", "provider:aws:vpclattice", "live"], |
| 252 | timeout: 240_000, |
| 253 | }, |
| 254 | ); |
| 255 | |
| 256 | test.provider( |
| 257 | "associate a VPC with a service network", |
| 258 | (stack) => |
| 259 | Effect.gen(function* () { |
| 260 | yield* stack.destroy(); |
| 261 | |
| 262 | const { association, network } = yield* stack.deploy( |
| 263 | Effect.gen(function* () { |
| 264 | const vpc = yield* Vpc("LatticeVpc", { cidrBlock: "10.30.0.0/16" }); |
| 265 | const network = yield* ServiceNetwork("AssocServiceNetwork", {}); |
| 266 | const association = yield* ServiceNetworkVpcAssociation("VpcAssociation", { |
| 267 | serviceNetworkIdentifier: network.serviceNetworkId, |
| 268 | vpcIdentifier: vpc.vpcId, |
| 269 | }); |
| 270 | return { association, network }; |
| 271 | }), |
| 272 | ); |
| 273 | |
| 274 | expect(association.associationId).toMatch(/^snva-/); |
| 275 | expect(association.associationArn).toContain(":servicenetworkvpcassociation/"); |
| 276 | |
| 277 | const live = yield* vpclattice.getServiceNetworkVpcAssociation({ |
| 278 | serviceNetworkVpcAssociationIdentifier: association.associationId, |
| 279 | }); |
| 280 | expect(live.serviceNetworkId).toBe(network.serviceNetworkId); |
| 281 | expect(["ACTIVE", "CREATE_IN_PROGRESS"]).toContain(live.status); |
| 282 | |
| 283 | yield* stack.destroy(); |
| 284 | yield* assertAssociationDeleted(association.associationId); |
| 285 | yield* assertServiceNetworkDeleted(network.serviceNetworkId); |
| 286 | }), |
| 287 | { |
| 288 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:vpclattice", "live"], |
| 289 | timeout: 240_000, |
| 290 | }, |
| 291 | ); |
| 292 | |
| 293 | test.provider( |
| 294 | "force delete removes an unowned association but normal delete protects it", |
| 295 | (stack) => |
| 296 | Effect.gen(function* () { |
| 297 | yield* stack.destroy(); |
| 298 | |
| 299 | const network = yield* stack.deploy(ServiceNetwork("ForceDeleteServiceNetwork", {})); |
| 300 | const defaultVpc = yield* getDefaultVpc; |
| 301 | const association = yield* Effect.gen(function* () { |
| 302 | const listed = yield* vpclattice.listServiceNetworkVpcAssociations({ |
| 303 | serviceNetworkIdentifier: network.serviceNetworkId, |
| 304 | vpcIdentifier: defaultVpc.vpcId, |
| 305 | }); |
| 306 | const existing = listed.items.find((item) => item.status !== "DELETE_IN_PROGRESS"); |
| 307 | if (existing?.id) return existing; |
| 308 | |
| 309 | // A prior interrupted run or a simultaneous observation/create race |
| 310 | // may have already established this unique network/VPC association. |
| 311 | // Conflict is therefore an observe-again signal, not a test failure. |
| 312 | const created = yield* vpclattice |
| 313 | .createServiceNetworkVpcAssociation({ |
| 314 | // Idempotency tokens are scoped to the exact request parameters. |
| 315 | // Derive it from both physical identifiers so a replacement |
| 316 | // network after an interrupted run does not reuse a token whose |
| 317 | // prior request referenced a different network. |
| 318 | clientToken: `alchemy-${network.serviceNetworkId}-${defaultVpc.vpcId}`, |
| 319 | serviceNetworkIdentifier: network.serviceNetworkId, |
| 320 | vpcIdentifier: defaultVpc.vpcId, |
| 321 | }) |
| 322 | .pipe(Effect.catchTag("ConflictException", () => Effect.succeed(undefined))); |
| 323 | if (created?.id) return created; |
| 324 | |
| 325 | const raced = yield* vpclattice.listServiceNetworkVpcAssociations({ |
| 326 | serviceNetworkIdentifier: network.serviceNetworkId, |
| 327 | vpcIdentifier: defaultVpc.vpcId, |
| 328 | }); |
| 329 | const observed = raced.items.find((item) => item.status !== "DELETE_IN_PROGRESS"); |
| 330 | if (observed?.id) return observed; |
| 331 | return yield* Effect.fail( |
| 332 | new AssociationNotReady({ |
| 333 | serviceNetworkId: network.serviceNetworkId, |
| 334 | vpcId: defaultVpc.vpcId, |
| 335 | }), |
| 336 | ); |
| 337 | }).pipe( |
| 338 | Effect.retry({ |
| 339 | while: (error) => error._tag === "AssociationNotReady", |
| 340 | schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(20)]), |
| 341 | }), |
| 342 | ); |
| 343 | const associationId = association.id; |
| 344 | if (!associationId) { |
| 345 | return yield* Effect.die(new Error("VPC Lattice did not return an association id")); |
| 346 | } |
| 347 | |
| 348 | // Converge any association recovered from an interrupted run to the |
| 349 | // intended out-of-band, unowned fixture state. |
| 350 | if (association.arn) { |
| 351 | const listedTags = yield* vpclattice.listTagsForResource({ |
| 352 | resourceArn: association.arn, |
| 353 | }); |
| 354 | const tagKeys = Object.keys(listedTags.tags ?? {}); |
| 355 | if (tagKeys.length > 0) { |
| 356 | yield* vpclattice.untagResource({ |
| 357 | resourceArn: association.arn, |
| 358 | tagKeys, |
| 359 | }); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | const provider = yield* Provider.findProvider(ServiceNetwork); |
| 364 | const input = { |
| 365 | id: "ForceDeleteServiceNetwork", |
| 366 | fqn: "ForceDeleteServiceNetwork", |
| 367 | instanceId: "force-delete-test", |
| 368 | olds: {}, |
| 369 | output: network, |
| 370 | session: stubSession, |
| 371 | bindings: [], |
| 372 | }; |
| 373 | |
| 374 | // A normal stack destroy must not take ownership of an untagged, |
| 375 | // out-of-band association. The network delete therefore remains blocked. |
| 376 | const normalDelete = yield* Effect.result(provider.delete(input)); |
| 377 | expect(Result.isFailure(normalDelete)).toBe(true); |
| 378 | const stillAttached = yield* vpclattice.getServiceNetworkVpcAssociation({ |
| 379 | serviceNetworkVpcAssociationIdentifier: associationId, |
| 380 | }); |
| 381 | expect(stillAttached.id).toBe(associationId); |
| 382 | |
| 383 | // Nuke is explicitly operator-confirmed and passes force=true. It may |
| 384 | // remove all associations that prevent deletion of the listed network. |
| 385 | yield* provider.delete({ ...input, force: true }); |
| 386 | yield* assertAssociationDeleted(associationId); |
| 387 | yield* assertServiceNetworkDeleted(network.serviceNetworkId); |
| 388 | |
| 389 | // The stack still has stale state for the now-deleted network; provider |
| 390 | // delete is idempotent and must clean that state without another API error. |
| 391 | yield* stack.destroy(); |
| 392 | }), |
| 393 | { |
| 394 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:vpclattice", "live"], |
| 395 | timeout: 180_000, |
| 396 | }, |
| 397 | ); |