fix(cloudformation): tolerate an already-gone REST API on delete (#4047)

ApiGatewayRestApiCfnProvisioner.delete called apiGatewayService.deleteRestApi directly, so a DeleteStack failed if the REST API had already been removed out of band. Route it through CfnDeletes.safeDelete tolerating NotFoundException, matching every other provisioner's delete path. deleteRestApi resolves the id first and raises NotFoundException when the API is gone, and the AWS model declares that error for DeleteRestApi. A real failure (any other code) still propagates so the stack reports DELETE_FAILED.

Refs #3885
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-20 23:22:19 -04:00
1 parent bf1152e144
commit 3976f2bd05
2 files changed
+25 -1

No files matched your search

@@ -73,7 +73,10 @@ public class ApiGatewayRestApiCfnProvisioner implements CfnResourceProvisioner {
// deployments, stages and authorizers, so the child types own no separate delete, the same
// as the legacy switch (their delete fell through to a no-op).
if (REST_API.equals(resourceType)) {
apiGatewayService.deleteRestApi(region, physicalId);
// Tolerate an API already removed out of band so DeleteStack does not fail on it;
// deleteRestApi resolves the id first and raises NotFoundException when it is gone.
CfnDeletes.safeDelete("REST API", physicalId,
() -> apiGatewayService.deleteRestApi(region, physicalId), "NotFoundException");
}
}
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.apigateway.ApiGatewayService;
import io.github.hectorvent.floci.services.apigateway.model.ApiGatewayResource;
import io.github.hectorvent.floci.services.apigateway.model.Authorizer;
@@ -16,11 +17,14 @@ import java.util.HashMap;
import java.util.List;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@@ -135,6 +139,23 @@ class ApiGatewayRestApiCfnProvisionerTest {
verify(api, never()).deleteRestApi("us-east-1", "api-1-res-1-GET");
}
@Test
void deleteToleratesARestApiAlreadyGone() {
doThrow(new AwsException("NotFoundException", "Invalid API id specified", 404))
.when(api).deleteRestApi("us-east-1", "api-1");
assertDoesNotThrow(() -> provisioner.delete("AWS::ApiGateway::RestApi", "api-1", "us-east-1"));
}
@Test
void deletePropagatesAnUnexpectedRestApiError() {
doThrow(new AwsException("TooManyRequestsException", "rate exceeded", 429))
.when(api).deleteRestApi("us-east-1", "api-1");
assertThrows(AwsException.class,
() -> provisioner.delete("AWS::ApiGateway::RestApi", "api-1", "us-east-1"));
}
private ProvisionContext ctx() {
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
when(engine.resolve(any())).thenAnswer(inv -> {