Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Hector Ventura3976f2bd0520
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisioner.java
index e231578f98..e1d974b4ac 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisioner.java
@@ -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");
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisionerTest.java
index ade5b731c8..6b9171a798 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisionerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisionerTest.java
@@ -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 -> {