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 { Role } from "@alchemy.run/aws/IAM"; |
| 3 | import { Bucket } from "@alchemy.run/aws/S3"; |
| 4 | import { |
| 5 | Portfolio, |
| 6 | PortfolioProductAssociation, |
| 7 | PrincipalPortfolioAssociation, |
| 8 | Product, |
| 9 | } from "@alchemy.run/aws/ServiceCatalog"; |
| 10 | import { expect } from "@alchemy.run/test"; |
| 11 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 12 | import * as s3 from "@distilled.cloud/aws/s3"; |
| 13 | import * as servicecatalog from "@distilled.cloud/aws/service-catalog"; |
| 14 | import * as Provider from "alchemy/Provider"; |
| 15 | import * as Data from "effect/Data"; |
| 16 | import * as Effect from "effect/Effect"; |
| 17 | import * as Schedule from "effect/Schedule"; |
| 18 | |
| 19 | const { test } = Test.make({ providers: AWS.providers() }); |
| 20 | |
| 21 | const TEMPLATE = JSON.stringify({ |
| 22 | AWSTemplateFormatVersion: "2010-09-09", |
| 23 | Description: "alchemy Service Catalog association test template", |
| 24 | Resources: { |
| 25 | NoOp: { Type: "AWS::CloudFormation::WaitConditionHandle" }, |
| 26 | }, |
| 27 | }); |
| 28 | |
| 29 | class PortfolioStillExists extends Data.TaggedError("AssocPortfolioStillExists")<{ |
| 30 | portfolioId: string; |
| 31 | }> {} |
| 32 | |
| 33 | const assertPortfolioGone = (portfolioId: string) => |
| 34 | servicecatalog.describePortfolio({ Id: portfolioId }).pipe( |
| 35 | Effect.flatMap(() => Effect.fail(new PortfolioStillExists({ portfolioId }))), |
| 36 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 37 | Effect.retry({ |
| 38 | while: (e) => e._tag === "AssocPortfolioStillExists", |
| 39 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]), |
| 40 | }), |
| 41 | ); |
| 42 | |
| 43 | test.provider( |
| 44 | "associates a product and a principal with a portfolio", |
| 45 | (stack) => |
| 46 | Effect.gen(function* () { |
| 47 | yield* stack.destroy(); |
| 48 | |
| 49 | // Phase 1: a bucket to host the CloudFormation template. |
| 50 | const phase1 = yield* stack.deploy( |
| 51 | Effect.gen(function* () { |
| 52 | const bucket = yield* Bucket("AssocTemplateBucket", { |
| 53 | forceDestroy: true, |
| 54 | }); |
| 55 | return { bucketName: bucket.bucketName, region: bucket.region }; |
| 56 | }), |
| 57 | ); |
| 58 | yield* s3.putObject({ |
| 59 | Bucket: phase1.bucketName, |
| 60 | Key: "assoc-template.json", |
| 61 | Body: new TextEncoder().encode(TEMPLATE), |
| 62 | ContentType: "application/json", |
| 63 | }); |
| 64 | const templateUrl = `https://${phase1.bucketName}.s3.${phase1.region}.amazonaws.com/assoc-template.json`; |
| 65 | |
| 66 | // Phase 2: portfolio + product + principal + both associations. |
| 67 | const deployed = yield* stack.deploy( |
| 68 | Effect.gen(function* () { |
| 69 | yield* Bucket("AssocTemplateBucket", { forceDestroy: true }); |
| 70 | const portfolio = yield* Portfolio("AssocPortfolio", { |
| 71 | providerName: "alchemy-tests", |
| 72 | }); |
| 73 | const product = yield* Product("AssocProduct", { |
| 74 | owner: "alchemy-tests", |
| 75 | provisioningArtifact: { templateUrl }, |
| 76 | }); |
| 77 | const role = yield* Role("AssocPrincipalRole", { |
| 78 | assumeRolePolicyDocument: { |
| 79 | Version: "2012-10-17", |
| 80 | Statement: [ |
| 81 | { |
| 82 | Effect: "Allow", |
| 83 | Principal: { Service: "servicecatalog.amazonaws.com" }, |
| 84 | Action: ["sts:AssumeRole"], |
| 85 | }, |
| 86 | ], |
| 87 | }, |
| 88 | }); |
| 89 | yield* PortfolioProductAssociation("ProductInPortfolio", { |
| 90 | portfolioId: portfolio.portfolioId, |
| 91 | productId: product.productId, |
| 92 | }); |
| 93 | yield* PrincipalPortfolioAssociation("RoleAccess", { |
| 94 | portfolioId: portfolio.portfolioId, |
| 95 | principalArn: role.roleArn, |
| 96 | }); |
| 97 | return { |
| 98 | portfolioId: portfolio.portfolioId, |
| 99 | productId: product.productId, |
| 100 | roleArn: role.roleArn, |
| 101 | }; |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | // out-of-band verify both associations via distilled |
| 106 | const portfolios = yield* servicecatalog.listPortfoliosForProduct({ |
| 107 | ProductId: deployed.productId, |
| 108 | }); |
| 109 | expect((portfolios.PortfolioDetails ?? []).some((d) => d.Id === deployed.portfolioId)).toBe( |
| 110 | true, |
| 111 | ); |
| 112 | |
| 113 | const principals = yield* servicecatalog.listPrincipalsForPortfolio({ |
| 114 | PortfolioId: deployed.portfolioId, |
| 115 | }); |
| 116 | expect((principals.Principals ?? []).some((p) => p.PrincipalARN === deployed.roleArn)).toBe( |
| 117 | true, |
| 118 | ); |
| 119 | |
| 120 | // Account-wide provider enumeration drives `alchemy unsafe nuke`. |
| 121 | // Both association providers must discover their child resources so |
| 122 | // teardown can schedule them before the portfolio and product. |
| 123 | const productAssociationProvider = yield* Provider.findProvider(PortfolioProductAssociation); |
| 124 | const principalAssociationProvider = yield* Provider.findProvider( |
| 125 | PrincipalPortfolioAssociation, |
| 126 | ); |
| 127 | const [productAssociations, principalAssociations] = yield* Effect.all([ |
| 128 | productAssociationProvider.list(), |
| 129 | principalAssociationProvider.list(), |
| 130 | ]); |
| 131 | expect( |
| 132 | productAssociations.some( |
| 133 | (association) => |
| 134 | association.portfolioId === deployed.portfolioId && |
| 135 | association.productId === deployed.productId, |
| 136 | ), |
| 137 | ).toBe(true); |
| 138 | expect( |
| 139 | principalAssociations.some( |
| 140 | (association) => |
| 141 | association.portfolioId === deployed.portfolioId && |
| 142 | association.principalArn === deployed.roleArn && |
| 143 | association.principalType === "IAM", |
| 144 | ), |
| 145 | ).toBe(true); |
| 146 | |
| 147 | // idempotent re-deploy — associations already exist, reconcile no-ops |
| 148 | yield* stack.deploy( |
| 149 | Effect.gen(function* () { |
| 150 | yield* Bucket("AssocTemplateBucket", { forceDestroy: true }); |
| 151 | const portfolio = yield* Portfolio("AssocPortfolio", { |
| 152 | providerName: "alchemy-tests", |
| 153 | }); |
| 154 | const product = yield* Product("AssocProduct", { |
| 155 | owner: "alchemy-tests", |
| 156 | provisioningArtifact: { templateUrl }, |
| 157 | }); |
| 158 | const role = yield* Role("AssocPrincipalRole", { |
| 159 | assumeRolePolicyDocument: { |
| 160 | Version: "2012-10-17", |
| 161 | Statement: [ |
| 162 | { |
| 163 | Effect: "Allow", |
| 164 | Principal: { Service: "servicecatalog.amazonaws.com" }, |
| 165 | Action: ["sts:AssumeRole"], |
| 166 | }, |
| 167 | ], |
| 168 | }, |
| 169 | }); |
| 170 | yield* PortfolioProductAssociation("ProductInPortfolio", { |
| 171 | portfolioId: portfolio.portfolioId, |
| 172 | productId: product.productId, |
| 173 | }); |
| 174 | yield* PrincipalPortfolioAssociation("RoleAccess", { |
| 175 | portfolioId: portfolio.portfolioId, |
| 176 | principalArn: role.roleArn, |
| 177 | }); |
| 178 | }), |
| 179 | ); |
| 180 | |
| 181 | // teardown removes associations first, then the portfolio/product |
| 182 | yield* stack.destroy(); |
| 183 | yield* assertPortfolioGone(deployed.portfolioId); |
| 184 | }), |
| 185 | { |
| 186 | tags: [ |
| 187 | "provider:aws", |
| 188 | "provider:aws:iam", |
| 189 | "provider:aws:s3", |
| 190 | "provider:aws:servicecatalog", |
| 191 | "live", |
| 192 | ], |
| 193 | timeout: 240_000, |
| 194 | }, |
| 195 | ); |