diff --git a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandler.java index 783acaa5d6..7dace3e60c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandler.java @@ -638,7 +638,7 @@ public class AutoScalingQueryHandler { private Response handleSetInstanceProtection(MultivaluedMap p, String region) { String groupName = p.getFirst("AutoScalingGroupName"); - List instanceIds = memberList(p, "InstanceIds"); + List instanceIds = indexedMembers(p, "InstanceIds"); boolean protectedFromScaleIn = requiredBoolParam(p, "ProtectedFromScaleIn"); service.setInstanceProtection(region, groupName, instanceIds, protectedFromScaleIn); return ok(new XmlBuilder() @@ -893,7 +893,7 @@ public class AutoScalingQueryHandler { } private Response handleAttachInstances(MultivaluedMap p, String region) { - service.attachInstances(region, p.getFirst("AutoScalingGroupName"), memberList(p, "InstanceIds")); + service.attachInstances(region, p.getFirst("AutoScalingGroupName"), indexedMembers(p, "InstanceIds")); return ok(new XmlBuilder() .start("AttachInstancesResponse", NS) .raw(AwsQueryResponse.responseMetadata()) @@ -902,7 +902,7 @@ public class AutoScalingQueryHandler { private Response handleDetachInstances(MultivaluedMap p, String region) { service.detachInstances(region, p.getFirst("AutoScalingGroupName"), - memberList(p, "InstanceIds"), + indexedMembers(p, "InstanceIds"), "true".equalsIgnoreCase(p.getFirst("ShouldDecrementDesiredCapacity"))); return ok(new XmlBuilder() .start("DetachInstancesResponse", NS) diff --git a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingService.java b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingService.java index 9bd7692dc5..4bab351347 100644 --- a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingService.java +++ b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingService.java @@ -1016,7 +1016,7 @@ public class AutoScalingService implements PassedRoleLookup { releaseRefreshOwnership(asg, detached); asg.getInstances().removeIf(i -> instanceIds.contains(i.getInstanceId())); if (decrementDesiredCapacity) { - int newDesired = Math.max(asg.getMinSize(), asg.getDesiredCapacity() - instanceIds.size()); + int newDesired = Math.max(asg.getMinSize(), asg.getDesiredCapacity() - detached.size()); asg.setDesiredCapacity(newDesired); } groups.put(key, asg); diff --git a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandlerTest.java index 872be9fde6..b8778eb351 100644 --- a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandlerTest.java @@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.services.autoscaling.model.AsgInstance; import io.github.hectorvent.floci.services.autoscaling.model.AsgOptionalFields; import io.github.hectorvent.floci.services.autoscaling.model.AutoScalingGroup; +import io.github.hectorvent.floci.services.ec2.Ec2Service; import jakarta.ws.rs.core.MultivaluedHashMap; import jakarta.ws.rs.core.Response; import org.junit.jupiter.api.Test; @@ -13,7 +14,6 @@ import org.junit.jupiter.params.provider.ValueSource; import org.w3c.dom.Element; import org.w3c.dom.NodeList; -import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -29,37 +29,109 @@ class AutoScalingQueryHandlerTest { private static final String REGION = "us-east-1"; @ParameterizedTest - @ValueSource(strings = {"EnterStandby", "ExitStandby"}) - void standbyMembersRetainNumericOrderAndRepeatedParameterValues(String action) { + @ValueSource(booleans = {false, true}) + void repeatedDetachMembersCountEachDetachedInstanceOnce(boolean decrementDesiredCapacity) { + AutoScalingService service = new AutoScalingService(); + service.regionResolver = new RegionResolver(REGION, "000000000000"); + service.createAutoScalingGroup(REGION, "repeated-detach", null, "lt", null, "1", null, + 0, 3, 3, 300, List.of("us-east-1a"), List.of(), List.of(), List.of(), + "EC2", 0, List.of("Default"), Map.of(), Map.of(), AsgOptionalFields.none()); + service.ec2Service = mock(Ec2Service.class); + AutoScalingGroup group = service.describeAutoScalingGroups(REGION, List.of("repeated-detach")).getFirst(); + for (String id : List.of("i-detached", "i-retained-one", "i-retained-two")) { + AsgInstance instance = new AsgInstance(); + instance.setInstanceId(id); + instance.setLifecycleState("InService"); + group.getInstances().add(instance); + } + MultivaluedHashMap params = new MultivaluedHashMap<>(); + params.add("AutoScalingGroupName", "repeated-detach"); + params.add("ShouldDecrementDesiredCapacity", Boolean.toString(decrementDesiredCapacity)); + params.add("InstanceIds.member.1", "i-detached"); + params.add("InstanceIds.member.1", "i-detached"); + params.add("InstanceIds.member.3", "i-detached"); + + Response response = new AutoScalingQueryHandler(service).handle("DetachInstances", params, REGION); + + assertEquals(200, response.getStatus()); + assertEquals(decrementDesiredCapacity ? 2 : 3, group.getDesiredCapacity()); + assertEquals(List.of("i-retained-one", "i-retained-two"), + group.getInstances().stream().map(AsgInstance::getInstanceId).toList()); + verify(service.ec2Service).deleteSystemTag( + REGION, List.of("i-detached"), "aws:autoscaling:groupName", "repeated-detach"); + } + + @ParameterizedTest + @ValueSource(strings = {"AttachInstances", "DetachInstances", "SetInstanceProtection", "EnterStandby", "ExitStandby"}) + void instanceMutationsRetainSparseOrderedAndRepeatedMembers(String action) { AutoScalingService service = mock(AutoScalingService.class); AutoScalingQueryHandler handler = new AutoScalingQueryHandler(service); MultivaluedHashMap params = new MultivaluedHashMap<>(); - params.add("AutoScalingGroupName", "indexed-standby"); + params.add("AutoScalingGroupName", "indexed-instances"); params.add("ShouldDecrementDesiredCapacity", "true"); - for (int index = 12; index >= 1; index--) { - params.add("InstanceIds.member." + index, "i-" + index); - } + params.add("ProtectedFromScaleIn", "true"); + params.add("InstanceIds.member.999999999999999999999999", "i-large"); + params.add("InstanceIds.member.10", "i-ten"); + params.add("InstanceIds.member.2", "i-two"); params.add("InstanceIds.member.2", "i-repeated"); - List expected = new ArrayList<>(); - for (int index = 1; index <= 12; index++) { - expected.add("i-" + index); - if (index == 2) { - expected.add("i-repeated"); - } - } - when(service.enterStandby(eq(REGION), eq("indexed-standby"), anyList(), eq(true))) - .thenReturn(List.of()); - when(service.exitStandby(eq(REGION), eq("indexed-standby"), anyList())).thenReturn(List.of()); + params.add("InstanceIds.member.01", "ignored"); + params.add("InstanceIds.member.3.Other", "ignored"); + List expected = List.of("i-two", "i-repeated", "i-ten", "i-large"); assertEquals(200, handler.handle(action, params, REGION).getStatus()); - if ("EnterStandby".equals(action)) { - verify(service).enterStandby(REGION, "indexed-standby", expected, true); + if ("AttachInstances".equals(action)) { + verify(service).attachInstances(REGION, "indexed-instances", expected); + } else if ("DetachInstances".equals(action)) { + verify(service).detachInstances(REGION, "indexed-instances", expected, true); + } else if ("SetInstanceProtection".equals(action)) { + verify(service).setInstanceProtection(REGION, "indexed-instances", expected, true); + } else if ("EnterStandby".equals(action)) { + verify(service).enterStandby(REGION, "indexed-instances", expected, true); } else { - verify(service).exitStandby(REGION, "indexed-standby", expected); + verify(service).exitStandby(REGION, "indexed-instances", expected); } } + @ParameterizedTest + @ValueSource(strings = {"AttachInstances", "DetachInstances", "SetInstanceProtection"}) + void instanceMutationsValidateMembersAfterGapBeforeChangingGroup(String action) { + AutoScalingService service = new AutoScalingService(); + service.regionResolver = new RegionResolver(REGION, "000000000000"); + service.createAutoScalingGroup(REGION, "indexed-validation", null, "lt", null, "1", null, + 0, 3, 1, 300, List.of("us-east-1a"), List.of(), List.of(), List.of(), + "EC2", 0, List.of("Default"), Map.of(), Map.of(), AsgOptionalFields.none()); + AutoScalingGroup group = service.describeAutoScalingGroups(REGION, List.of("indexed-validation")).getFirst(); + service.ec2Service = mock(Ec2Service.class); + AsgInstance original = new AsgInstance(); + original.setInstanceId("i-original"); + original.setLifecycleState("InService"); + group.getInstances().add(original); + AsgInstance moving = new AsgInstance(); + moving.setInstanceId("i-moving"); + moving.setLifecycleState("EnteringStandby"); + group.getInstances().add(moving); + MultivaluedHashMap params = new MultivaluedHashMap<>(); + params.add("AutoScalingGroupName", "indexed-validation"); + params.add("ProtectedFromScaleIn", "true"); + params.add("ShouldDecrementDesiredCapacity", "true"); + params.add("InstanceIds.member.1", "AttachInstances".equals(action) ? "i-new" : "i-original"); + params.add("InstanceIds.member.3", "SetInstanceProtection".equals(action) ? "i-missing" : "i-moving"); + AutoScalingQueryHandler handler = new AutoScalingQueryHandler(service); + + Response response = handler.handle(action, params, REGION); + + assertEquals(400, response.getStatus()); + assertTrue(response.getEntity().toString().contains("ValidationError")); + assertEquals(List.of("i-original", "i-moving"), + group.getInstances().stream().map(AsgInstance::getInstanceId).toList()); + assertEquals(1, group.getDesiredCapacity()); + assertEquals("InService", original.getLifecycleState()); + assertEquals("EnteringStandby", moving.getLifecycleState()); + assertFalse(original.isProtectedFromScaleIn()); + verifyNoInteractions(service.ec2Service); + } + @ParameterizedTest @CsvSource({ "CapacityReservationIds,cr-0123456789abcdef0", diff --git a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingServiceTest.java index 3bdc016c49..de5eb2bad7 100644 --- a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingServiceTest.java @@ -228,15 +228,20 @@ class AutoScalingServiceTest { assertEquals(decrementDesiredCapacity ? 1 : 2, group.getDesiredCapacity()); } - @Test - void detachingAnInstanceTheGroupDoesNotHoldChangesNoTags() { + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void detachingAnInstanceTheGroupDoesNotHoldChangesNoTags(boolean decrementDesiredCapacity) { service.ec2Service = mock(Ec2Service.class); service.attachInstances(REGION, "test-asg", List.of("i-member")); + int desiredCapacity = service.describeAutoScalingGroups(REGION, List.of("test-asg")).getFirst() + .getDesiredCapacity(); clearInvocations(service.ec2Service); - service.detachInstances(REGION, "test-asg", List.of("i-stranger"), false); + service.detachInstances(REGION, "test-asg", List.of("i-stranger"), decrementDesiredCapacity); verify(service.ec2Service, never()).deleteSystemTag(anyString(), anyList(), anyString(), anyString()); + assertEquals(desiredCapacity, service.describeAutoScalingGroups(REGION, List.of("test-asg")).getFirst() + .getDesiredCapacity()); assertEquals(List.of("i-member"), service.describeAutoScalingGroups(REGION, List.of("test-asg")) .getFirst().getInstances().stream().map(AsgInstance::getInstanceId).toList()); }