Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(autoscaling): validate MixedInstancesPolicy launch template (#1439)

Hector Venturadbad78991d7b
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 de67d5bc87..4062e90f2d 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
@@ -128,7 +128,8 @@ public class AutoScalingService {
if (launchConfigName == null && launchTemplateId == null && launchTemplateName == null
&& mixedInstancesPolicy == null) {
throw new AwsException("ValidationError",
- "Either LaunchConfigurationName or LaunchTemplate must be specified.", 400);
+ "Valid requests must contain either LaunchTemplate, LaunchConfigurationName, "
+ + "InstanceId or MixedInstancesPolicy parameter.", 400);
}
AutoScalingGroup asg = new AutoScalingGroup();
@@ -642,6 +643,29 @@ public class AutoScalingService {
throw new AwsException("ValidationError",
"LaunchConfigurationName, LaunchTemplate, and MixedInstancesPolicy are mutually exclusive.", 400);
}
+ if (mixedInstancesPolicy != null && !hasUsableLaunchTemplate(mixedInstancesPolicy)) {
+ throw new AwsException("ValidationError",
+ "A MixedInstancesPolicy must specify a LaunchTemplate with a LaunchTemplateId "
+ + "or LaunchTemplateName.", 400);
+ }
+ }
+
+ private static boolean hasUsableLaunchTemplate(MixedInstancesPolicy mixedInstancesPolicy) {
+ MixedInstancesPolicy.LaunchTemplate launchTemplate = mixedInstancesPolicy.getLaunchTemplate();
+ if (launchTemplate == null) {
+ return false;
+ }
+ MixedInstancesPolicy.LaunchTemplateSpecification specification =
+ launchTemplate.getLaunchTemplateSpecification();
+ if (specification == null) {
+ return false;
+ }
+ return notBlank(specification.getLaunchTemplateId())
+ || notBlank(specification.getLaunchTemplateName());
+ }
+
+ private static boolean notBlank(String value) {
+ return value != null && !value.isBlank();
}
private static String instanceRefreshKey(String region, String asgName, String refreshId) {
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 8843f3461e..33bae9b837 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
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
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.InstanceRefresh;
+import io.github.hectorvent.floci.services.autoscaling.model.MixedInstancesPolicy;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -317,6 +318,79 @@ class AutoScalingServiceTest {
assertTrue(service.describeAutoScalingGroups(REGION, List.of("test-asg")).isEmpty());
}
+ @Test
+ void createAutoScalingGroupRejectsMixedInstancesPolicyWithoutLaunchTemplate() {
+ MixedInstancesPolicy policy = new MixedInstancesPolicy();
+ MixedInstancesPolicy.InstancesDistribution distribution =
+ new MixedInstancesPolicy.InstancesDistribution();
+ distribution.setOnDemandBaseCapacity(1);
+ policy.setInstancesDistribution(distribution);
+
+ AwsException error = assertThrows(AwsException.class,
+ () -> createWithMixedInstancesPolicy("mixed-no-lt", policy));
+
+ assertEquals("ValidationError", error.getErrorCode());
+ assertEquals(400, error.getHttpStatus());
+ }
+
+ @Test
+ void createAutoScalingGroupRejectsMixedInstancesPolicyWithBlankLaunchTemplateIdentifiers() {
+ MixedInstancesPolicy policy = new MixedInstancesPolicy();
+ MixedInstancesPolicy.LaunchTemplate launchTemplate = new MixedInstancesPolicy.LaunchTemplate();
+ MixedInstancesPolicy.LaunchTemplateSpecification specification =
+ new MixedInstancesPolicy.LaunchTemplateSpecification();
+ specification.setLaunchTemplateId("");
+ specification.setLaunchTemplateName(" ");
+ launchTemplate.setLaunchTemplateSpecification(specification);
+ policy.setLaunchTemplate(launchTemplate);
+
+ AwsException error = assertThrows(AwsException.class,
+ () -> createWithMixedInstancesPolicy("mixed-blank-lt", policy));
+
+ assertEquals("ValidationError", error.getErrorCode());
+ assertEquals(400, error.getHttpStatus());
+ }
+
+ @Test
+ void createAutoScalingGroupAcceptsMixedInstancesPolicyWithLaunchTemplate() {
+ MixedInstancesPolicy policy = new MixedInstancesPolicy();
+ MixedInstancesPolicy.LaunchTemplate launchTemplate = new MixedInstancesPolicy.LaunchTemplate();
+ MixedInstancesPolicy.LaunchTemplateSpecification specification =
+ new MixedInstancesPolicy.LaunchTemplateSpecification();
+ specification.setLaunchTemplateId("lt-mixed");
+ launchTemplate.setLaunchTemplateSpecification(specification);
+ policy.setLaunchTemplate(launchTemplate);
+
+ createWithMixedInstancesPolicy("mixed-with-lt", policy);
+
+ var group = service.describeAutoScalingGroups(REGION, List.of("mixed-with-lt")).getFirst();
+ assertEquals("lt-mixed",
+ group.getMixedInstancesPolicy().getLaunchTemplate()
+ .getLaunchTemplateSpecification().getLaunchTemplateId());
+ }
+
+ private void createWithMixedInstancesPolicy(String name, MixedInstancesPolicy policy) {
+ service.createAutoScalingGroup(REGION,
+ name,
+ null,
+ null,
+ null,
+ null,
+ policy,
+ 0,
+ 3,
+ 1,
+ 300,
+ List.of("us-east-1a"),
+ List.of(),
+ List.of(),
+ List.of(),
+ "EC2",
+ 0,
+ List.of("Default"),
+ java.util.Map.of());
+ }
+
private static final class AutoScalingGroupFixture {
private static void addInstance(AutoScalingService service, String region, String name, String instanceId,
String lifecycleState, String launchTemplateId, String launchTemplateVersion) {