| 1 | import * as AWS from "@/AWS"; |
| 2 | import { ServiceQuotaIncreaseRequest } from "@/AWS/ServiceQuotas"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as servicequotas from "@distilled.cloud/aws/service-quotas"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | |
| 8 | const { test } = Test.make({ providers: AWS.providers() }); |
| 9 | |
| 10 | // VPCs per region (default 5) and Subnets per VPC (default 200) — both |
| 11 | // always exist, so a desiredValue of 1 is always already satisfied and the |
| 12 | // reconciler never submits a real increase request. |
| 13 | const VPCS_PER_REGION = "L-F678F1CE"; |
| 14 | const SUBNETS_PER_VPC = "L-407747CB"; |
| 15 | |
| 16 | // Ungated typed-error probe: prove the distilled error union carries the |
| 17 | // not-found tag this provider's observe/read paths depend on. Runs in every |
| 18 | // CI pass at near-zero cost, unlike the gated live increase below. |
| 19 | test.provider( |
| 20 | "getServiceQuota with a bogus quota code fails with NoSuchResourceException", |
| 21 | () => |
| 22 | Effect.gen(function* () { |
| 23 | const error = yield* Effect.flip( |
| 24 | servicequotas.getServiceQuota({ |
| 25 | ServiceCode: "vpc", |
| 26 | QuotaCode: "L-00000000", |
| 27 | }), |
| 28 | ); |
| 29 | expect(error._tag).toBe("NoSuchResourceException"); |
| 30 | }), |
| 31 | { tags: ["provider:aws", "provider:aws:servicequotas", "live"] }, |
| 32 | ); |
| 33 | |
| 34 | // Full engine lifecycle on the SAFE path: a desired value at or below the |
| 35 | // applied quota is already satisfied, so no real increase request (and no |
| 36 | // support case) is ever opened. Covers create, no-op update, and |
| 37 | // replacement (quotaCode change). |
| 38 | test.provider( |
| 39 | "already-satisfied desired value deploys without submitting a request", |
| 40 | (stack) => |
| 41 | Effect.gen(function* () { |
| 42 | yield* stack.destroy(); |
| 43 | |
| 44 | const deployQuota = (quotaCode: string, desiredValue: number) => |
| 45 | stack.deploy( |
| 46 | Effect.gen(function* () { |
| 47 | const request = yield* ServiceQuotaIncreaseRequest("VpcQuota", { |
| 48 | serviceCode: "vpc", |
| 49 | quotaCode, |
| 50 | desiredValue, |
| 51 | }); |
| 52 | return { request }; |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | // 1. Create — satisfied, so no request is submitted. |
| 57 | const { request } = yield* deployQuota(VPCS_PER_REGION, 1); |
| 58 | expect(request.serviceCode).toBe("vpc"); |
| 59 | expect(request.quotaCode).toBe(VPCS_PER_REGION); |
| 60 | expect(request.desiredValue).toBe(1); |
| 61 | expect(request.requestId).toBeUndefined(); |
| 62 | expect(request.status).toBeUndefined(); |
| 63 | // The VPCs-per-region quota defaults to 5 — always >= 1. |
| 64 | expect(request.appliedValue).toBeGreaterThanOrEqual(1); |
| 65 | expect(request.quotaName).toBeDefined(); |
| 66 | |
| 67 | // 2. Update desiredValue (still satisfied) — default update path, no |
| 68 | // request submitted. |
| 69 | const second = yield* deployQuota(VPCS_PER_REGION, 2); |
| 70 | expect(second.request.desiredValue).toBe(2); |
| 71 | expect(second.request.requestId).toBeUndefined(); |
| 72 | |
| 73 | // 3. Replacement — quotaCode change replaces the resource. Still |
| 74 | // satisfied (Subnets per VPC defaults to 200), so still no request. |
| 75 | const third = yield* deployQuota(SUBNETS_PER_VPC, 1); |
| 76 | expect(third.request.quotaCode).toBe(SUBNETS_PER_VPC); |
| 77 | expect(third.request.requestId).toBeUndefined(); |
| 78 | expect(third.request.appliedValue).toBeGreaterThanOrEqual(1); |
| 79 | |
| 80 | yield* stack.destroy(); |
| 81 | }), |
| 82 | { |
| 83 | tags: ["provider:aws", "provider:aws:servicequotas", "live"], |
| 84 | timeout: 120_000, |
| 85 | }, |
| 86 | ); |
| 87 | |
| 88 | // Submitting a REAL quota increase request may open an AWS Support case and |
| 89 | // cannot be cancelled via the API — gated behind AWS_TEST_SERVICE_QUOTAS=1. |
| 90 | test.provider.skipIf(!process.env.AWS_TEST_SERVICE_QUOTAS)( |
| 91 | "submits a real quota increase request and adopts it on re-deploy", |
| 92 | (stack) => |
| 93 | Effect.gen(function* () { |
| 94 | yield* stack.destroy(); |
| 95 | |
| 96 | // Observe the current applied value out-of-band so the requested |
| 97 | // value is always strictly above it (AWS rejects <= applied). |
| 98 | const current = yield* servicequotas |
| 99 | .getServiceQuota({ |
| 100 | ServiceCode: "vpc", |
| 101 | QuotaCode: VPCS_PER_REGION, |
| 102 | }) |
| 103 | .pipe( |
| 104 | Effect.map((r) => r.Quota?.Value), |
| 105 | Effect.catchTag("NoSuchResourceException", () => |
| 106 | servicequotas |
| 107 | .getAWSDefaultServiceQuota({ |
| 108 | ServiceCode: "vpc", |
| 109 | QuotaCode: VPCS_PER_REGION, |
| 110 | }) |
| 111 | .pipe(Effect.map((r) => r.Quota?.Value)), |
| 112 | ), |
| 113 | ); |
| 114 | const desiredValue = Math.ceil(current ?? 5) + 1; |
| 115 | |
| 116 | const deployIncrease = stack.deploy( |
| 117 | Effect.gen(function* () { |
| 118 | const request = yield* ServiceQuotaIncreaseRequest("VpcIncrease", { |
| 119 | serviceCode: "vpc", |
| 120 | quotaCode: VPCS_PER_REGION, |
| 121 | desiredValue, |
| 122 | }); |
| 123 | return { request }; |
| 124 | }), |
| 125 | ); |
| 126 | |
| 127 | const { request } = yield* deployIncrease; |
| 128 | expect(request.requestId).toBeDefined(); |
| 129 | expect(["PENDING", "CASE_OPENED", "APPROVED"]).toContain(request.status); |
| 130 | expect(request.desiredValue).toBe(desiredValue); |
| 131 | |
| 132 | // Out-of-band verification via distilled. |
| 133 | const observed = yield* servicequotas.getRequestedServiceQuotaChange({ |
| 134 | RequestId: request.requestId!, |
| 135 | }); |
| 136 | expect(observed.RequestedQuota?.QuotaCode).toBe(VPCS_PER_REGION); |
| 137 | expect(observed.RequestedQuota?.DesiredValue).toBe(desiredValue); |
| 138 | |
| 139 | // Idempotency: a second deploy converges on the SAME open request |
| 140 | // instead of submitting a duplicate. |
| 141 | const second = yield* deployIncrease; |
| 142 | expect(second.request.requestId).toBe(request.requestId); |
| 143 | |
| 144 | // Destroy only forgets the request — Service Quotas has no cancel API. |
| 145 | yield* stack.destroy(); |
| 146 | }), |
| 147 | { |
| 148 | tags: ["provider:aws", "provider:aws:servicequotas", "live"], |
| 149 | timeout: 180_000, |
| 150 | }, |
| 151 | ); |