Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(autoscaling): round-trip the whole target tracking configuration (#4369)

lex00a33c4e0b002b
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 910fe69573..4ab0ad8dbc 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
@@ -961,10 +961,17 @@ public class AutoScalingQueryHandler {
return ok(xml.build());
}
+ private static final String TTC = "TargetTrackingConfiguration.";
+ private static final String CUSTOM = TTC + "CustomizedMetricSpecification.";
+
private static ScalingPolicy.TargetTrackingConfiguration parseTargetTrackingConfiguration(MultivaluedMap<String, String> p) {
- String predefinedMetricType = p.getFirst("TargetTrackingConfiguration.PredefinedMetricSpecification.PredefinedMetricType");
- Double targetValue = nullableDoubleParam(p, "TargetTrackingConfiguration.TargetValue");
- if (predefinedMetricType == null && targetValue == null) {
+ String predefinedMetricType = p.getFirst(TTC + "PredefinedMetricSpecification.PredefinedMetricType");
+ String customMetricName = p.getFirst(CUSTOM + "MetricName");
+ String firstMetricQueryId = p.getFirst(CUSTOM + "Metrics.member.1.Id");
+ Double targetValue = nullableDoubleParam(p, TTC + "TargetValue");
+ String disableScaleIn = p.getFirst(TTC + "DisableScaleIn");
+ if (predefinedMetricType == null && customMetricName == null && firstMetricQueryId == null
+ && targetValue == null && disableScaleIn == null) {
return null;
}
ScalingPolicy.TargetTrackingConfiguration configuration = new ScalingPolicy.TargetTrackingConfiguration();
@@ -972,12 +979,107 @@ public class AutoScalingQueryHandler {
ScalingPolicy.PredefinedMetricSpecification specification =
new ScalingPolicy.PredefinedMetricSpecification();
specification.setPredefinedMetricType(predefinedMetricType);
+ specification.setResourceLabel(
+ p.getFirst(TTC + "PredefinedMetricSpecification.ResourceLabel"));
configuration.setPredefinedMetricSpecification(specification);
}
+ if (customMetricName != null || firstMetricQueryId != null) {
+ configuration.setCustomizedMetricSpecification(parseCustomizedMetricSpecification(p));
+ }
configuration.setTargetValue(targetValue);
+ configuration.setDisableScaleIn(
+ parseOptionalBoolean(disableScaleIn, "DisableScaleIn"));
return configuration;
}
+ private static ScalingPolicy.CustomizedMetricSpecification parseCustomizedMetricSpecification(
+ MultivaluedMap<String, String> p) {
+ ScalingPolicy.CustomizedMetricSpecification spec = new ScalingPolicy.CustomizedMetricSpecification();
+ spec.setMetricName(p.getFirst(CUSTOM + "MetricName"));
+ spec.setNamespace(p.getFirst(CUSTOM + "Namespace"));
+ spec.setStatistic(p.getFirst(CUSTOM + "Statistic"));
+ spec.setUnit(p.getFirst(CUSTOM + "Unit"));
+ spec.setPeriod(parseMetricPeriod(p.getFirst(CUSTOM + "Period"), "Period"));
+ // Numbered members are read from 1 up to the first gap, the same reading the other Query
+ // handlers apply. The model requires both Name and Value on a dimension.
+ List<ScalingPolicy.MetricDimension> dimensions = new ArrayList<>();
+ for (int i = 1; p.getFirst(CUSTOM + "Dimensions.member." + i + ".Name") != null; i++) {
+ ScalingPolicy.MetricDimension dimension = new ScalingPolicy.MetricDimension();
+ dimension.setName(p.getFirst(CUSTOM + "Dimensions.member." + i + ".Name"));
+ dimension.setValue(p.getFirst(CUSTOM + "Dimensions.member." + i + ".Value"));
+ dimensions.add(dimension);
+ }
+ spec.setDimensions(dimensions);
+ spec.setMetrics(parseMetricDataQueries(p));
+ return spec;
+ }
+
+ /**
+ * Integer.valueOf on a client-supplied value threw straight out of the handler, so a
+ * non-numeric Period was reported as InternalFailure. A bad request member is a parameter
+ * error.
+ */
+ private static Integer parseMetricPeriod(String value, String member) {
+ if (value == null || value.isBlank()) {
+ return null;
+ }
+ int parsed;
+ try {
+ parsed = Integer.parseInt(value.trim());
+ } catch (NumberFormatException e) {
+ throw new AwsException("ValidationError",
+ member + " must be an integer.", 400);
+ }
+ if (parsed <= 0) {
+ throw new AwsException("ValidationError",
+ member + " must be greater than zero.", 400);
+ }
+ return parsed;
+ }
+
+ /** CustomizedMetricSpecification.Metrics, the metric data query form. */
+ private static List<ScalingPolicy.TargetTrackingMetricDataQuery> parseMetricDataQueries(
+ MultivaluedMap<String, String> p) {
+ List<ScalingPolicy.TargetTrackingMetricDataQuery> queries = new ArrayList<>();
+ for (int i = 1; p.getFirst(CUSTOM + "Metrics.member." + i + ".Id") != null; i++) {
+ String base = CUSTOM + "Metrics.member." + i + ".";
+ ScalingPolicy.TargetTrackingMetricDataQuery query =
+ new ScalingPolicy.TargetTrackingMetricDataQuery();
+ query.setId(p.getFirst(base + "Id"));
+ query.setExpression(p.getFirst(base + "Expression"));
+ query.setLabel(p.getFirst(base + "Label"));
+ query.setPeriod(parseMetricPeriod(p.getFirst(base + "Period"),
+ "Metrics.member." + i + ".Period"));
+ query.setReturnData(parseOptionalBoolean(
+ p.getFirst(base + "ReturnData"), "ReturnData"));
+ String stat = p.getFirst(base + "MetricStat.Stat");
+ if (stat != null) {
+ ScalingPolicy.TargetTrackingMetricStat metricStat =
+ new ScalingPolicy.TargetTrackingMetricStat();
+ metricStat.setStat(stat);
+ metricStat.setUnit(p.getFirst(base + "MetricStat.Unit"));
+ metricStat.setPeriod(parseMetricPeriod(
+ p.getFirst(base + "MetricStat.Period"), "MetricStat.Period"));
+ ScalingPolicy.Metric metric = new ScalingPolicy.Metric();
+ metric.setNamespace(p.getFirst(base + "MetricStat.Metric.Namespace"));
+ metric.setMetricName(p.getFirst(base + "MetricStat.Metric.MetricName"));
+ List<ScalingPolicy.MetricDimension> metricDimensions = new ArrayList<>();
+ String dimensionBase = base + "MetricStat.Metric.Dimensions.member.";
+ for (int d = 1; p.getFirst(dimensionBase + d + ".Name") != null; d++) {
+ ScalingPolicy.MetricDimension dimension = new ScalingPolicy.MetricDimension();
+ dimension.setName(p.getFirst(dimensionBase + d + ".Name"));
+ dimension.setValue(p.getFirst(dimensionBase + d + ".Value"));
+ metricDimensions.add(dimension);
+ }
+ metric.setDimensions(metricDimensions);
+ metricStat.setMetric(metric);
+ query.setMetricStat(metricStat);
+ }
+ queries.add(query);
+ }
+ return queries;
+ }
+
private static void appendTargetTrackingConfigurationXml(
XmlBuilder xml, ScalingPolicy.TargetTrackingConfiguration configuration) {
if (configuration == null) {
@@ -991,14 +1093,92 @@ public class AutoScalingQueryHandler {
if (predefinedMetric.getPredefinedMetricType() != null) {
xml.elem("PredefinedMetricType", predefinedMetric.getPredefinedMetricType());
}
+ if (predefinedMetric.getResourceLabel() != null) {
+ xml.elem("ResourceLabel", predefinedMetric.getResourceLabel());
+ }
xml.end("PredefinedMetricSpecification");
}
+ appendCustomizedMetricSpecificationXml(xml, configuration.getCustomizedMetricSpecification());
if (configuration.getTargetValue() != null) {
xml.elem("TargetValue", String.valueOf(configuration.getTargetValue()));
}
+ if (configuration.getDisableScaleIn() != null) {
+ xml.elem("DisableScaleIn", String.valueOf(configuration.getDisableScaleIn()));
+ }
xml.end("TargetTrackingConfiguration");
}
+ private static void appendCustomizedMetricSpecificationXml(
+ XmlBuilder xml, ScalingPolicy.CustomizedMetricSpecification spec) {
+ if (spec == null) {
+ return;
+ }
+ xml.start("CustomizedMetricSpecification");
+ if (!spec.getDimensions().isEmpty()) {
+ xml.start("Dimensions");
+ for (ScalingPolicy.MetricDimension dimension : spec.getDimensions()) {
+ xml.start("member")
+ .elem("Name", dimension.getName())
+ .elem("Value", dimension.getValue())
+ .end("member");
+ }
+ xml.end("Dimensions");
+ }
+ if (spec.getMetricName() != null) { xml.elem("MetricName", spec.getMetricName()); }
+ if (spec.getNamespace() != null) { xml.elem("Namespace", spec.getNamespace()); }
+ if (spec.getStatistic() != null) { xml.elem("Statistic", spec.getStatistic()); }
+ if (spec.getUnit() != null) { xml.elem("Unit", spec.getUnit()); }
+ if (spec.getPeriod() != null) { xml.elem("Period", String.valueOf(spec.getPeriod())); }
+ appendMetricDataQueriesXml(xml, spec.getMetrics());
+ xml.end("CustomizedMetricSpecification");
+ }
+
+ private static void appendMetricDataQueriesXml(
+ XmlBuilder xml, List<ScalingPolicy.TargetTrackingMetricDataQuery> queries) {
+ if (queries == null || queries.isEmpty()) {
+ return;
+ }
+ xml.start("Metrics");
+ for (ScalingPolicy.TargetTrackingMetricDataQuery query : queries) {
+ xml.start("member").elem("Id", query.getId());
+ if (query.getExpression() != null) { xml.elem("Expression", query.getExpression()); }
+ if (query.getLabel() != null) { xml.elem("Label", query.getLabel()); }
+ if (query.getPeriod() != null) {
+ xml.elem("Period", String.valueOf(query.getPeriod()));
+ }
+ if (query.getReturnData() != null) {
+ xml.elem("ReturnData", String.valueOf(query.getReturnData()));
+ }
+ ScalingPolicy.TargetTrackingMetricStat stat = query.getMetricStat();
+ if (stat != null) {
+ xml.start("MetricStat");
+ ScalingPolicy.Metric metric = stat.getMetric();
+ if (metric != null) {
+ xml.start("Metric")
+ .elem("Namespace", metric.getNamespace())
+ .elem("MetricName", metric.getMetricName());
+ if (!metric.getDimensions().isEmpty()) {
+ xml.start("Dimensions");
+ for (ScalingPolicy.MetricDimension dimension : metric.getDimensions()) {
+ xml.start("member")
+ .elem("Name", dimension.getName())
+ .elem("Value", dimension.getValue())
+ .end("member");
+ }
+ xml.end("Dimensions");
+ }
+ xml.end("Metric");
+ }
+ xml.elem("Stat", stat.getStat());
+ if (stat.getUnit() != null) { xml.elem("Unit", stat.getUnit()); }
+ if (stat.getPeriod() != null) { xml.elem("Period", String.valueOf(stat.getPeriod())); }
+ xml.end("MetricStat");
+ }
+ xml.end("member");
+ }
+ xml.end("Metrics");
+ }
+
// ── Activities ────────────────────────────────────────────────────────────
private Response handleDescribeScalingActivities(MultivaluedMap<String, String> p, String region) {
@@ -1633,7 +1813,7 @@ public class AutoScalingQueryHandler {
}
}
- private Boolean parseOptionalBoolean(String value, String name) {
+ private static Boolean parseOptionalBoolean(String value, String name) {
if (value == null || value.isBlank()) {
return null;
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/autoscaling/model/ScalingPolicy.java b/src/main/java/io/github/hectorvent/floci/services/autoscaling/model/ScalingPolicy.java
index 8571f13f53..0c244747ce 100644
--- a/src/main/java/io/github/hectorvent/floci/services/autoscaling/model/ScalingPolicy.java
+++ b/src/main/java/io/github/hectorvent/floci/services/autoscaling/model/ScalingPolicy.java
@@ -3,6 +3,9 @@ package io.github.hectorvent.floci.services.autoscaling.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
+import java.util.ArrayList;
+import java.util.List;
+
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public class ScalingPolicy {
@@ -58,25 +61,148 @@ public class ScalingPolicy {
@JsonIgnoreProperties(ignoreUnknown = true)
public static class TargetTrackingConfiguration {
private PredefinedMetricSpecification predefinedMetricSpecification;
+ private CustomizedMetricSpecification customizedMetricSpecification;
private Double targetValue;
+ private Boolean disableScaleIn;
public TargetTrackingConfiguration() {}
public PredefinedMetricSpecification getPredefinedMetricSpecification() { return predefinedMetricSpecification; }
public void setPredefinedMetricSpecification(PredefinedMetricSpecification v) { this.predefinedMetricSpecification = v; }
+ public CustomizedMetricSpecification getCustomizedMetricSpecification() { return customizedMetricSpecification; }
+ public void setCustomizedMetricSpecification(CustomizedMetricSpecification v) { this.customizedMetricSpecification = v; }
+
public Double getTargetValue() { return targetValue; }
public void setTargetValue(Double v) { this.targetValue = v; }
+
+ public Boolean getDisableScaleIn() { return disableScaleIn; }
+ public void setDisableScaleIn(Boolean v) { this.disableScaleIn = v; }
+ }
+
+ /** CustomizedMetricSpecification, the alternative to naming a predefined metric. */
+ @RegisterForReflection
+ public static class CustomizedMetricSpecification {
+ private String metricName;
+ private String namespace;
+ private String statistic;
+ private String unit;
+ private Integer period;
+ private List<MetricDimension> dimensions = new ArrayList<>();
+ private List<TargetTrackingMetricDataQuery> metrics = new ArrayList<>();
+
+ public CustomizedMetricSpecification() {}
+
+ public String getMetricName() { return metricName; }
+ public void setMetricName(String v) { this.metricName = v; }
+ public String getNamespace() { return namespace; }
+ public void setNamespace(String v) { this.namespace = v; }
+ public String getStatistic() { return statistic; }
+ public void setStatistic(String v) { this.statistic = v; }
+ public String getUnit() { return unit; }
+ public void setUnit(String v) { this.unit = v; }
+ public Integer getPeriod() { return period; }
+ public void setPeriod(Integer v) { this.period = v; }
+ public List<MetricDimension> getDimensions() { return dimensions; }
+ public void setDimensions(List<MetricDimension> v) { this.dimensions = v; }
+ public List<TargetTrackingMetricDataQuery> getMetrics() { return metrics; }
+ public void setMetrics(List<TargetTrackingMetricDataQuery> v) { this.metrics = v; }
+ }
+
+ /**
+ * One entry of CustomizedMetricSpecification.Metrics, the metric data query form. It carries
+ * either an Expression for metric math or a MetricStat naming a raw metric, never both. The
+ * model requires Id.
+ */
+ @RegisterForReflection
+ public static class TargetTrackingMetricDataQuery {
+ private String id;
+ private String expression;
+ private String label;
+ private Integer period;
+ private Boolean returnData;
+ private TargetTrackingMetricStat metricStat;
+
+ public TargetTrackingMetricDataQuery() {}
+
+ public String getId() { return id; }
+ public void setId(String v) { this.id = v; }
+ public String getExpression() { return expression; }
+ public void setExpression(String v) { this.expression = v; }
+ public String getLabel() { return label; }
+ public void setLabel(String v) { this.label = v; }
+ /** The query's own Period, which the model keeps separate from MetricStat.Period. */
+ public Integer getPeriod() { return period; }
+ public void setPeriod(Integer v) { this.period = v; }
+ public Boolean getReturnData() { return returnData; }
+ public void setReturnData(Boolean v) { this.returnData = v; }
+ public TargetTrackingMetricStat getMetricStat() { return metricStat; }
+ public void setMetricStat(TargetTrackingMetricStat v) { this.metricStat = v; }
+ }
+
+ /** MetricStat, which the model requires to carry both a Metric and a Stat. */
+ @RegisterForReflection
+ public static class TargetTrackingMetricStat {
+ private String stat;
+ private String unit;
+ private Integer period;
+ private Metric metric;
+
+ public TargetTrackingMetricStat() {}
+
+ public String getStat() { return stat; }
+ public void setStat(String v) { this.stat = v; }
+ public String getUnit() { return unit; }
+ public void setUnit(String v) { this.unit = v; }
+ public Integer getPeriod() { return period; }
+ public void setPeriod(Integer v) { this.period = v; }
+ public Metric getMetric() { return metric; }
+ public void setMetric(Metric v) { this.metric = v; }
+ }
+
+ /** The raw metric a MetricStat names. The model requires Namespace and MetricName. */
+ @RegisterForReflection
+ public static class Metric {
+ private String namespace;
+ private String metricName;
+ private List<MetricDimension> dimensions = new ArrayList<>();
+
+ public Metric() {}
+
+ public String getNamespace() { return namespace; }
+ public void setNamespace(String v) { this.namespace = v; }
+ public String getMetricName() { return metricName; }
+ public void setMetricName(String v) { this.metricName = v; }
+ public List<MetricDimension> getDimensions() { return dimensions; }
+ public void setDimensions(List<MetricDimension> v) { this.dimensions = v; }
+ }
+
+ /** A dimension of a customized metric. The model requires both members. */
+ @RegisterForReflection
+ public static class MetricDimension {
+ private String name;
+ private String value;
+
+ public MetricDimension() {}
+
+ public String getName() { return name; }
+ public void setName(String v) { this.name = v; }
+ public String getValue() { return value; }
+ public void setValue(String v) { this.value = v; }
}
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public static class PredefinedMetricSpecification {
private String predefinedMetricType;
+ private String resourceLabel;
public PredefinedMetricSpecification() {}
public String getPredefinedMetricType() { return predefinedMetricType; }
public void setPredefinedMetricType(String v) { this.predefinedMetricType = v; }
+
+ public String getResourceLabel() { return resourceLabel; }
+ public void setResourceLabel(String v) { this.resourceLabel = v; }
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingTargetTrackingRoundTripIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingTargetTrackingRoundTripIntegrationTest.java
new file mode 100644
index 0000000000..1a3c0faa52
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingTargetTrackingRoundTripIntegrationTest.java
@@ -0,0 +1,239 @@
+package io.github.hectorvent.floci.services.autoscaling;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.response.ValidatableResponse;
+import io.restassured.specification.RequestSpecification;
+import org.junit.jupiter.api.Test;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.not;
+
+/**
+ * A target tracking policy round-trips every member the model gives it.
+ *
+ * <p>DescribePolicies carried only PredefinedMetricSpecification.PredefinedMetricType and
+ * TargetValue, so CustomizedMetricSpecification, DisableScaleIn and ResourceLabel were accepted at
+ * put and then absent from the read. A client comparing its configuration against the live policy
+ * sees a difference that never resolves, however many times it re-applies.
+ */
+@QuarkusTest
+class AutoScalingTargetTrackingRoundTripIntegrationTest {
+
+ private static final String AUTH =
+ "AWS4-HMAC-SHA256 Credential=test/20260615/us-east-1/autoscaling/aws4_request";
+
+ private static ValidatableResponse asg(String action, String... formParams) {
+ RequestSpecification request = given()
+ .header("Authorization", AUTH)
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", action)
+ .formParam("Version", "2011-01-01");
+ for (int i = 0; i < formParams.length; i += 2) {
+ request = request.formParam(formParams[i], formParams[i + 1]);
+ }
+ return request.when().post("/").then();
+ }
+
+ private static String group() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String launchConfiguration = "tt-lc-" + suffix;
+ asg("CreateLaunchConfiguration", "LaunchConfigurationName", launchConfiguration,
+ "ImageId", "ami-12345678", "InstanceType", "t3.micro").statusCode(200);
+ String name = "tt-" + suffix;
+ asg("CreateAutoScalingGroup", "AutoScalingGroupName", name,
+ "LaunchConfigurationName", launchConfiguration,
+ "MinSize", "0", "MaxSize", "2",
+ "AvailabilityZones.member.1", "us-east-1a").statusCode(200);
+ return name;
+ }
+
+ @Test
+ void aCustomizedMetricSpecificationSurvivesTheRoundTrip() {
+ String name = group();
+ asg("PutScalingPolicy",
+ "AutoScalingGroupName", name,
+ "PolicyName", "custom",
+ "PolicyType", "TargetTrackingScaling",
+ "TargetTrackingConfiguration.TargetValue", "42.5",
+ "TargetTrackingConfiguration.DisableScaleIn", "true",
+ "TargetTrackingConfiguration.CustomizedMetricSpecification.MetricName", "queue_depth",
+ "TargetTrackingConfiguration.CustomizedMetricSpecification.Namespace", "app/metrics",
+ "TargetTrackingConfiguration.CustomizedMetricSpecification.Statistic", "Average",
+ "TargetTrackingConfiguration.CustomizedMetricSpecification.Unit", "Count",
+ "TargetTrackingConfiguration.CustomizedMetricSpecification.Period", "60",
+ "TargetTrackingConfiguration.CustomizedMetricSpecification.Dimensions.member.1.Name", "queue",
+ "TargetTrackingConfiguration.CustomizedMetricSpecification.Dimensions.member.1.Value", "orders",
+ "TargetTrackingConfiguration.CustomizedMetricSpecification.Dimensions.member.2.Name", "shard",
+ "TargetTrackingConfiguration.CustomizedMetricSpecification.Dimensions.member.2.Value", "b")
+ .statusCode(200);
+
+ asg("DescribePolicies", "AutoScalingGroupName", name)
+ .statusCode(200)
+ .body(containsString("<MetricName>queue_depth</MetricName>"))
+ .body(containsString("<Namespace>app/metrics</Namespace>"))
+ .body(containsString("<Statistic>Average</Statistic>"))
+ .body(containsString("<Unit>Count</Unit>"))
+ .body(containsString("<Period>60</Period>"))
+ .body(containsString("<Name>queue</Name>"))
+ .body(containsString("<Value>orders</Value>"))
+ .body(containsString("<Name>shard</Name>"))
+ .body(containsString("<Value>b</Value>"))
+ .body(containsString("<DisableScaleIn>true</DisableScaleIn>"))
+ .body(containsString("<TargetValue>42.5</TargetValue>"));
+ }
+
+ /**
+ * A customized metric can be defined by Metrics queries instead of MetricName, which is how
+ * metric math reaches a target tracking policy. Those were accepted and never read back.
+ *
+ * <p>Each query carries its own Period, which the model keeps separate from the Period on its
+ * MetricStat.
+ */
+ @Test
+ void aMetricsQuerySurvivesTheRoundTrip() {
+ String name = group();
+ String q = "TargetTrackingConfiguration.CustomizedMetricSpecification.Metrics.member.";
+ asg("PutScalingPolicy",
+ "AutoScalingGroupName", name,
+ "PolicyName", "mathy",
+ "PolicyType", "TargetTrackingScaling",
+ "TargetTrackingConfiguration.TargetValue", "10",
+ q + "1.Id", "m1",
+ q + "1.Period", "10",
+ q + "1.ReturnData", "false",
+ q + "1.MetricStat.Stat", "Sum",
+ q + "1.MetricStat.Unit", "Count",
+ q + "1.MetricStat.Period", "60",
+ q + "1.MetricStat.Metric.Namespace", "AWS/SQS",
+ q + "1.MetricStat.Metric.MetricName", "ApproximateNumberOfMessagesVisible",
+ q + "1.MetricStat.Metric.Dimensions.member.1.Name", "QueueName",
+ q + "1.MetricStat.Metric.Dimensions.member.1.Value", "orders",
+ q + "2.Id", "e1",
+ q + "2.Expression", "m1 / 4",
+ q + "2.Label", "backlog per instance",
+ q + "2.Period", "30",
+ q + "2.ReturnData", "true")
+ .statusCode(200);
+
+ asg("DescribePolicies", "AutoScalingGroupName", name)
+ .statusCode(200)
+ .body(containsString("<Id>m1</Id>"))
+ .body(containsString("<Stat>Sum</Stat>"))
+ .body(containsString("<Namespace>AWS/SQS</Namespace>"))
+ .body(containsString("<MetricName>ApproximateNumberOfMessagesVisible</MetricName>"))
+ .body(containsString("<Name>QueueName</Name>"))
+ .body(containsString("<Id>e1</Id>"))
+ .body(containsString("<Expression>m1 / 4</Expression>"))
+ .body(containsString("<Label>backlog per instance</Label>"))
+ .body(containsString("<ReturnData>true</ReturnData>"))
+ // The query's own Period and its MetricStat.Period are separate members, so all three
+ // values have to survive rather than one overwriting another.
+ .body(containsString("<Period>10</Period>"))
+ .body(containsString("<Period>30</Period>"))
+ .body(containsString("<Period>60</Period>"));
+ }
+
+ /** A bad Period is the client's error, and used to come back as InternalFailure. */
+ @Test
+ void aNonNumericPeriodIsAParameterError() {
+ String name = group();
+ asg("PutScalingPolicy",
+ "AutoScalingGroupName", name,
+ "PolicyName", "badperiod",
+ "PolicyType", "TargetTrackingScaling",
+ "TargetTrackingConfiguration.TargetValue", "10",
+ "TargetTrackingConfiguration.CustomizedMetricSpecification.MetricName", "m",
+ "TargetTrackingConfiguration.CustomizedMetricSpecification.Period", "not-a-number")
+ .statusCode(400)
+ .body(containsString("ValidationError"));
+
+ asg("PutScalingPolicy",
+ "AutoScalingGroupName", name,
+ "PolicyName", "zeroperiod",
+ "PolicyType", "TargetTrackingScaling",
+ "TargetTrackingConfiguration.TargetValue", "10",
+ "TargetTrackingConfiguration.CustomizedMetricSpecification.MetricName", "m",
+ "TargetTrackingConfiguration.CustomizedMetricSpecification.Period", "0")
+ .statusCode(400);
+ }
+
+ /**
+ * Boolean.parseBoolean turns anything that is not "true" into false, so an unintended value was
+ * accepted and read back as a setting the caller never asked for. This handler already has a
+ * strict helper for exactly that.
+ */
+ @Test
+ void anUnparseableBooleanIsAParameterError() {
+ String name = group();
+ asg("PutScalingPolicy",
+ "AutoScalingGroupName", name, "PolicyName", "badbool",
+ "PolicyType", "TargetTrackingScaling",
+ "TargetTrackingConfiguration.TargetValue", "10",
+ "TargetTrackingConfiguration.PredefinedMetricSpecification.PredefinedMetricType",
+ "ASGAverageCPUUtilization",
+ "TargetTrackingConfiguration.DisableScaleIn", "maybe")
+ .statusCode(400)
+ .body(containsString("ValidationError"));
+
+ String q = "TargetTrackingConfiguration.CustomizedMetricSpecification.Metrics.member.";
+ asg("PutScalingPolicy",
+ "AutoScalingGroupName", name, "PolicyName", "badreturn",
+ "PolicyType", "TargetTrackingScaling",
+ "TargetTrackingConfiguration.TargetValue", "10",
+ q + "1.Id", "m1", q + "1.Expression", "1", q + "1.ReturnData", "maybe")
+ .statusCode(400)
+ .body(containsString("ValidationError"));
+
+ // The documented values still work, in either case.
+ asg("PutScalingPolicy",
+ "AutoScalingGroupName", name, "PolicyName", "goodbool",
+ "PolicyType", "TargetTrackingScaling",
+ "TargetTrackingConfiguration.TargetValue", "10",
+ "TargetTrackingConfiguration.PredefinedMetricSpecification.PredefinedMetricType",
+ "ASGAverageCPUUtilization",
+ "TargetTrackingConfiguration.DisableScaleIn", "TRUE")
+ .statusCode(200);
+ }
+
+ @Test
+ void aPredefinedSpecificationKeepsItsResourceLabel() {
+ String name = group();
+ asg("PutScalingPolicy",
+ "AutoScalingGroupName", name,
+ "PolicyName", "predefined",
+ "PolicyType", "TargetTrackingScaling",
+ "TargetTrackingConfiguration.TargetValue", "70",
+ "TargetTrackingConfiguration.PredefinedMetricSpecification.PredefinedMetricType",
+ "ALBRequestCountPerTarget",
+ "TargetTrackingConfiguration.PredefinedMetricSpecification.ResourceLabel",
+ "app/my-lb/abc/targetgroup/my-tg/def")
+ .statusCode(200);
+
+ asg("DescribePolicies", "AutoScalingGroupName", name)
+ .statusCode(200)
+ .body(containsString("<PredefinedMetricType>ALBRequestCountPerTarget</PredefinedMetricType>"))
+ .body(containsString("<ResourceLabel>app/my-lb/abc/targetgroup/my-tg/def</ResourceLabel>"));
+ }
+
+ /** A policy naming neither specification must not grow an empty one on read. */
+ @Test
+ void aPolicyWithNoCustomMetricReportsNone() {
+ String name = group();
+ asg("PutScalingPolicy",
+ "AutoScalingGroupName", name,
+ "PolicyName", "plain",
+ "PolicyType", "TargetTrackingScaling",
+ "TargetTrackingConfiguration.TargetValue", "50",
+ "TargetTrackingConfiguration.PredefinedMetricSpecification.PredefinedMetricType",
+ "ASGAverageCPUUtilization")
+ .statusCode(200);
+
+ asg("DescribePolicies", "AutoScalingGroupName", name)
+ .statusCode(200)
+ .body(not(containsString("<CustomizedMetricSpecification>")))
+ .body(not(containsString("<Metrics>")))
+ .body(not(containsString("<DisableScaleIn>")))
+ .body(not(containsString("<ResourceLabel>")));
+ }
+}