mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cloudformation): tolerate an already-gone EC2 instance or security group on stack delete (#3842)
Ec2InstanceCfnProvisioner.delete and Ec2SecurityGroupCfnProvisioner.delete called terminateInstances / deleteSecurityGroup directly, so a group or instance removed out of band turned DeleteStack into DELETE_FAILED. Wrap both in CfnDeletes.safeDelete with the specific not-found code (InvalidInstanceID.NotFound, InvalidGroup.NotFound), per AGENTS.md step 5, so already-gone is tolerated while a real error such as DependencyViolation still propagates.
This commit is contained in:
1 parent
938eca1fe9
commit
6e034837ad
4 files changed
+52
-2
No files matched your search
+4
-1
@@ -137,6 +137,9 @@ public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
ec2Service.terminateInstances(region, List.of(physicalId));
|
||||
// Tolerate an instance already terminated out of band, so DeleteStack does not fail on it.
|
||||
CfnDeletes.safeDelete("EC2 instance", physicalId,
|
||||
() -> ec2Service.terminateInstances(region, List.of(physicalId)),
|
||||
"InvalidInstanceID.NotFound");
|
||||
}
|
||||
}
|
||||
+4
-1
@@ -106,7 +106,10 @@ public class Ec2SecurityGroupCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
ec2Service.deleteSecurityGroup(region, physicalId);
|
||||
// Tolerate a group already deleted out of band, so DeleteStack does not fail on it.
|
||||
CfnDeletes.safeDelete("security group", physicalId,
|
||||
() -> ec2Service.deleteSecurityGroup(region, physicalId),
|
||||
"InvalidGroup.NotFound");
|
||||
}
|
||||
|
||||
/** The VPC a security group would land in for this template value: the default when omitted. */
|
||||
|
||||
+22
@@ -24,6 +24,10 @@ import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
|
||||
/**
|
||||
* {@code AWS::EC2::Instance} in isolation: the attributes Ref and Fn::GetAtt read once the launch
|
||||
@@ -102,6 +106,24 @@ class Ec2InstanceCfnProvisionerTest {
|
||||
verify(ec2).terminateInstances("us-east-1", List.of("i-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteToleratesAnInstanceAlreadyGone() {
|
||||
doThrow(new AwsException("InvalidInstanceID.NotFound", "gone", 400))
|
||||
.when(ec2).terminateInstances("us-east-1", List.of("i-1"));
|
||||
|
||||
assertDoesNotThrow(() -> provisioner.delete(TYPE, "i-1", "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletePropagatesAnUnexpectedError() {
|
||||
doThrow(new AwsException("DependencyViolation", "still in use", 400))
|
||||
.when(ec2).terminateInstances("us-east-1", List.of("i-1"));
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> provisioner.delete(TYPE, "i-1", "us-east-1"));
|
||||
assertEquals("DependencyViolation", failure.getErrorCode());
|
||||
}
|
||||
|
||||
private void stubLaunch(Instance instance) {
|
||||
Reservation reservation = new Reservation();
|
||||
reservation.getInstances().add(instance);
|
||||
|
||||
+22
@@ -27,6 +27,10 @@ import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
|
||||
/**
|
||||
* {@code AWS::EC2::SecurityGroup} in isolation: the attributes Ref and Fn::GetAtt read, the
|
||||
@@ -117,6 +121,24 @@ class Ec2SecurityGroupCfnProvisionerTest {
|
||||
verify(ec2).deleteSecurityGroup("us-east-1", "sg-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteToleratesAGroupAlreadyGone() {
|
||||
doThrow(new AwsException("InvalidGroup.NotFound", "gone", 400))
|
||||
.when(ec2).deleteSecurityGroup("us-east-1", "sg-1");
|
||||
|
||||
assertDoesNotThrow(() -> provisioner.delete(TYPE, "sg-1", "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletePropagatesAnUnexpectedError() {
|
||||
doThrow(new AwsException("DependencyViolation", "in use", 400))
|
||||
.when(ec2).deleteSecurityGroup("us-east-1", "sg-1");
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> provisioner.delete(TYPE, "sg-1", "us-east-1"));
|
||||
assertEquals("DependencyViolation", failure.getErrorCode());
|
||||
}
|
||||
|
||||
private ProvisionContext ctx(String priorPhysicalId) {
|
||||
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
|
||||
when(engine.resolve(any())).thenAnswer(inv -> {
|
||||
|
||||
Reference in new issue
Block a user