From bb5a37d19ecaa75ad502742fcd1086f5075e0fef Mon Sep 17 00:00:00 2001 From: Marvelous Ikponmwosa <46595957+marvikomo@users.noreply.github.com> Date: Sat, 20 Jun 2026 18:30:20 +0100 Subject: [PATCH] =?UTF-8?q?fix(apigatewayv2):=20cascade-delete=20child=20r?= =?UTF-8?q?esources=20and=20404=20references=20=E2=80=A6=20(#1300)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(apigatewayv2): cascade-delete child resources and 404 references on DeleteApi Closes #1218. DeleteApi removed only the Api row, leaving routes, integrations, stages, deployments, authorizers, models, route responses, and integration responses orphaned under their region::apiId::* keys. The list endpoints also silently returned empty lists for unknown apiIds instead of 404. Both diverge from real AWS (children deleted; subsequent reads return NotFoundException). - deleteApi now cascades through every api-scoped child store, mirroring the existing apigateway v1 deleteRestApi pattern (api row first, then inline stream-based deletes per store, then LOG.infov). - VpcLink is region-scoped (region::vpcLinkId — shared across APIs) and intentionally excluded from the cascade. - getRoutes / getStages / getIntegrations / getAuthorizers / getModels / getDeployments / getRouteResponses / getIntegrationResponses now call getApi(region, apiId) first so they 404 when the API doesn't exist, matching real AWS behavior. Verified end-to-end with boto3 against running Floci: the reproduction from #1218 went from 6 leaked child types on main to 0 with this branch. * refactor(apigatewayv2): extract deleteByPrefix helper, consolidate cascade tests --- .../apigatewayv2/ApiGatewayV2Service.java | 24 +++ .../ApiGatewayV2DeleteApiCascadeTest.java | 178 ++++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100644 src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2DeleteApiCascadeTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java index 925115821e..dd65b5e7ab 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java @@ -125,6 +125,22 @@ public class ApiGatewayV2Service { public void deleteApi(String region, String apiId) { getApi(region, apiId); apiStore.delete(apiKey(region, apiId)); + // Cascade-delete every child resource keyed under region::apiId::*. + // VpcLink is region-scoped (region::vpcLinkId, shared across APIs) and excluded. + String prefix = region + "::" + apiId + "::"; + deleteByPrefix(routeStore, prefix); + deleteByPrefix(integrationStore, prefix); + deleteByPrefix(authorizerStore, prefix); + deleteByPrefix(deploymentStore, prefix); + deleteByPrefix(stageStore, prefix); + deleteByPrefix(modelStore, prefix); + deleteByPrefix(routeResponseStore, prefix); + deleteByPrefix(integrationResponseStore, prefix); + LOG.infov("Deleted HTTP API: {0} in {1}", apiId, region); + } + + private static void deleteByPrefix(StorageBackend store, String prefix) { + store.keys().stream().filter(k -> k.startsWith(prefix)).forEach(store::delete); } public Api updateApi(String region, String apiId, Map request) { @@ -220,6 +236,7 @@ public class ApiGatewayV2Service { } public List getAuthorizers(String region, String apiId) { + getApi(region, apiId); String prefix = region + "::" + apiId + "::"; return authorizerStore.scan(k -> k.startsWith(prefix)); } @@ -296,6 +313,7 @@ public class ApiGatewayV2Service { } public List getRoutes(String region, String apiId) { + getApi(region, apiId); String prefix = region + "::" + apiId + "::"; return routeStore.scan(k -> k.startsWith(prefix)); } @@ -433,6 +451,7 @@ public class ApiGatewayV2Service { } public List getIntegrations(String region, String apiId) { + getApi(region, apiId); String prefix = region + "::" + apiId + "::"; return integrationStore.scan(k -> k.startsWith(prefix)); } @@ -516,6 +535,7 @@ public class ApiGatewayV2Service { } public List getStages(String region, String apiId) { + getApi(region, apiId); String prefix = region + "::" + apiId + "::"; return stageStore.scan(k -> k.startsWith(prefix)); } @@ -583,6 +603,7 @@ public class ApiGatewayV2Service { } public List getDeployments(String region, String apiId) { + getApi(region, apiId); String prefix = region + "::" + apiId + "::"; return deploymentStore.scan(k -> k.startsWith(prefix)); } @@ -634,6 +655,7 @@ public class ApiGatewayV2Service { } public List getRouteResponses(String region, String apiId, String routeId) { + getApi(region, apiId); String prefix = region + "::" + apiId + "::" + routeId + "::"; return routeResponseStore.scan(k -> k.startsWith(prefix)); } @@ -698,6 +720,7 @@ public class ApiGatewayV2Service { } public List getIntegrationResponses(String region, String apiId, String integrationId) { + getApi(region, apiId); String prefix = region + "::" + apiId + "::" + integrationId + "::"; return integrationResponseStore.scan(k -> k.startsWith(prefix)); } @@ -754,6 +777,7 @@ public class ApiGatewayV2Service { } public List getModels(String region, String apiId) { + getApi(region, apiId); String prefix = region + "::" + apiId + "::"; return modelStore.scan(k -> k.startsWith(prefix)); } diff --git a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2DeleteApiCascadeTest.java b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2DeleteApiCascadeTest.java new file mode 100644 index 0000000000..a739b1f118 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2DeleteApiCascadeTest.java @@ -0,0 +1,178 @@ +package io.github.hectorvent.floci.services.apigatewayv2; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.*; + +@QuarkusTest +class ApiGatewayV2DeleteApiCascadeTest { + + @Test + void deleteApiCascadesEveryChildResourceAnd404sOnSubsequentReferences() { + String apiId = null; + try { + // 1. setup: create the API and every flavor of child resource under it. + apiId = createApi("cascade-probe"); + String integrationId = createIntegration(apiId); + String routeId = createRoute(apiId, integrationId); + createStage(apiId); + createAuthorizer(apiId); + createDeployment(apiId); + createModel(apiId); + createRouteResponse(apiId, routeId); + createIntegrationResponse(apiId, integrationId); + + // 2. delete the API. + given().when().delete("/v2/apis/" + apiId).then().statusCode(204); + + // 3. every reference to the deleted apiId must 404. + List probes = List.of( + "", + "/routes", + "/integrations", + "/stages", + "/authorizers", + "/deployments", + "/models", + "/routes/" + routeId + "/routeresponses", + "/integrations/" + integrationId + "/integrationresponses" + ); + for (String suffix : probes) { + given().when().get("/v2/apis/" + apiId + suffix) + .then().statusCode(404); + } + } finally { + // best-effort cleanup in case any assertion above failed before delete-api ran. + if (apiId != null) { + given().when().delete("/v2/apis/" + apiId); + } + } + } + + @Test + void cascadeIsScopedToSingleApi() { + // Create two APIs with children under each; delete only api A; api B's children must remain. + String aId = null; + String bId = null; + try { + aId = createApi("cascade-iso-a"); + bId = createApi("cascade-iso-b"); + createIntegration(aId); + createIntegration(bId); + + given().when().delete("/v2/apis/" + aId).then().statusCode(204); + + // A is gone — every reference 404s. + given().when().get("/v2/apis/" + aId + "/integrations").then().statusCode(404); + + // B is untouched. + given().when().get("/v2/apis/" + bId + "/integrations") + .then().statusCode(200).body("items", hasSize(1)); + } finally { + if (bId != null) given().when().delete("/v2/apis/" + bId); + if (aId != null) given().when().delete("/v2/apis/" + aId); + } + } + + // ──────────────────────────── creation helpers ──────────────────────────── + + private static String createApi(String name) { + return given() + .contentType(ContentType.JSON) + .body("{\"name\":\"" + name + "\",\"protocolType\":\"HTTP\"}") + .when().post("/v2/apis") + .then().statusCode(201) + .extract().path("apiId"); + } + + private static String createIntegration(String apiId) { + return given() + .contentType(ContentType.JSON) + .body(""" + {"integrationType":"HTTP_PROXY","integrationUri":"https://example.com", + "integrationMethod":"GET","payloadFormatVersion":"1.0"} + """) + .when().post("/v2/apis/" + apiId + "/integrations") + .then().statusCode(201) + .extract().path("integrationId"); + } + + private static String createRoute(String apiId, String integrationId) { + return given() + .contentType(ContentType.JSON) + .body(""" + {"routeKey":"GET /hello","target":"integrations/%s"} + """.formatted(integrationId)) + .when().post("/v2/apis/" + apiId + "/routes") + .then().statusCode(201) + .extract().path("routeId"); + } + + private static void createStage(String apiId) { + given() + .contentType(ContentType.JSON) + .body(""" + {"stageName":"dev","autoDeploy":true} + """) + .when().post("/v2/apis/" + apiId + "/stages") + .then().statusCode(201); + } + + private static void createAuthorizer(String apiId) { + given() + .contentType(ContentType.JSON) + .body(""" + {"authorizerType":"JWT","name":"probe-jwt", + "identitySource":["$request.header.Authorization"], + "jwtConfiguration":{"issuer":"https://example.com","audience":["a"]}} + """) + .when().post("/v2/apis/" + apiId + "/authorizers") + .then().statusCode(201); + } + + private static void createDeployment(String apiId) { + given() + .contentType(ContentType.JSON) + .body(""" + {"description":"probe-dep","stageName":"dev"} + """) + .when().post("/v2/apis/" + apiId + "/deployments") + .then().statusCode(201); + } + + private static void createModel(String apiId) { + given() + .contentType(ContentType.JSON) + .body(""" + {"name":"ProbeModel","contentType":"application/json", + "schema":"{\\"type\\":\\"object\\"}"} + """) + .when().post("/v2/apis/" + apiId + "/models") + .then().statusCode(201); + } + + private static void createRouteResponse(String apiId, String routeId) { + given() + .contentType(ContentType.JSON) + .body(""" + {"routeResponseKey":"$default"} + """) + .when().post("/v2/apis/" + apiId + "/routes/" + routeId + "/routeresponses") + .then().statusCode(201); + } + + private static void createIntegrationResponse(String apiId, String integrationId) { + given() + .contentType(ContentType.JSON) + .body(""" + {"integrationResponseKey":"$default"} + """) + .when().post("/v2/apis/" + apiId + "/integrations/" + integrationId + "/integrationresponses") + .then().statusCode(201); + } +}