mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(autoscaling): round-trip InstanceMaintenancePolicy, AvailabilityZoneDistribution, CapacityReservationSpecification, ServiceLinkedRoleARN, WeightedCapacity and EnabledMetrics (#5513)
* fix(autoscaling): round-trip InstanceMaintenancePolicy, AvailabilityZoneDistribution, CapacityReservationSpecification, ServiceLinkedRoleARN, WeightedCapacity and EnabledMetrics DescribeAutoScalingGroups dropped several members of the AutoScalingGroup shape that CreateAutoScalingGroup and UpdateAutoScalingGroup accept, so a client that set them read them back as absent and saw a permanent diff. - AsgOptionalFields gains ServiceLinkedRoleARN, InstanceMaintenancePolicy, AvailabilityZoneDistribution and CapacityReservationSpecification with its CapacityReservationTarget. The four-argument constructor stays, so existing call sites are unchanged. - MixedInstancesPolicy launch template overrides carry WeightedCapacity. - EnableMetricsCollection and DisableMetricsCollection are implemented, and DescribeAutoScalingGroups reports EnabledMetrics. - Validation follows the botocore model. MinHealthyPercentage 0 to 100 and MaxHealthyPercentage 100 to 200, both required, at most 100 apart, and -1 on both clears the policy. CapacityDistributionStrategy and CapacityReservationPreference enums. No target with preference none. CapacityReservationIds 1 to 255 characters matching [A-Za-z0-9-_/] and resource group ARNs 1 to 1600. ServiceLinkedRoleARN 1 to 1600 characters. WeightedCapacity 1 to 999 in at most 32 characters, set on every override or none. Granularity required and 1Minute. Known metric names only. - The AWS::AutoScaling::AutoScalingGroup provisioner passes the new properties and MetricsCollection through, and its update rollback snapshot restores them. It requires CapacityReservationPreference and each entry's Granularity as the resource schema does, and rejects an unknown metric before creating the group. - The Java SDK compatibility suite gains a round-trip case that sets every new member, reads it back through DescribeAutoScalingGroups, then clears the maintenance policy and the enabled metrics. * fix(autoscaling): route the metrics collection actions and close the review gaps EnableMetricsCollection and DisableMetricsCollection now route to Auto Scaling when a request carries no credential scope. Both hold the group lock, so concurrent calls keep each other's changes and a call racing a delete cannot write the group back. A failed stack update now clears the service-linked role, AZ distribution and capacity reservation when the group had none before. A blank ServiceLinkedRoleARN or WeightedCapacity in a template reaches the service's validation instead of reading as unset. The provisioner reuses the service's granularity rule and message.
This commit is contained in:
1 parent
d21f5f6a05
commit
991bf3ab9e
17 files changed
+1635
-19
No files matched your search
@@ -162,7 +162,8 @@ public class AwsQueryController {
|
||||
"DescribeScalingActivities",
|
||||
"DescribeAutoScalingNotificationTypes", "DescribeTerminationPolicyTypes",
|
||||
"DescribeAdjustmentTypes", "DescribeAccountLimits",
|
||||
"DescribeLifecycleHookTypes", "DescribeMetricCollectionTypes"
|
||||
"DescribeLifecycleHookTypes", "DescribeMetricCollectionTypes",
|
||||
"EnableMetricsCollection", "DisableMetricsCollection"
|
||||
);
|
||||
|
||||
private static final Set<String> ELB_V2_ACTIONS = Set.of(
|
||||
|
||||
+134
-1
@@ -55,6 +55,8 @@ public class AutoScalingQueryHandler {
|
||||
case "DescribeInstanceRefreshes" -> handleDescribeInstanceRefreshes(p, region);
|
||||
case "CreateOrUpdateTags" -> handleCreateOrUpdateTags(p, region);
|
||||
case "DeleteTags" -> handleDeleteTags(p, region);
|
||||
case "EnableMetricsCollection" -> handleEnableMetricsCollection(p, region);
|
||||
case "DisableMetricsCollection" -> handleDisableMetricsCollection(p, region);
|
||||
// Instances
|
||||
case "DescribeAutoScalingInstances" -> handleDescribeAutoScalingInstances(p, region);
|
||||
case "SetInstanceProtection" -> handleSetInstanceProtection(p, region);
|
||||
@@ -287,6 +289,13 @@ public class AutoScalingQueryHandler {
|
||||
if (asg.getDefaultInstanceWarmup() != null) {
|
||||
xml.elem("DefaultInstanceWarmup", String.valueOf(asg.getDefaultInstanceWarmup()));
|
||||
}
|
||||
if (asg.getServiceLinkedRoleArn() != null) {
|
||||
xml.elem("ServiceLinkedRoleARN", asg.getServiceLinkedRoleArn());
|
||||
}
|
||||
appendInstanceMaintenancePolicyXml(xml, asg.getInstanceMaintenancePolicy());
|
||||
appendAvailabilityZoneDistributionXml(xml, asg.getAvailabilityZoneDistribution());
|
||||
appendCapacityReservationSpecificationXml(xml, asg.getCapacityReservationSpecification());
|
||||
appendEnabledMetricsXml(xml, asg.getEnabledMetrics());
|
||||
if (asg.getLaunchConfigurationName() != null) {
|
||||
xml.elem("LaunchConfigurationName", asg.getLaunchConfigurationName());
|
||||
}
|
||||
@@ -482,6 +491,23 @@ public class AutoScalingQueryHandler {
|
||||
.end("CreateOrUpdateTagsResponse").build());
|
||||
}
|
||||
|
||||
private Response handleEnableMetricsCollection(MultivaluedMap<String, String> p, String region) {
|
||||
service.enableMetricsCollection(region, p.getFirst("AutoScalingGroupName"),
|
||||
memberList(p, "Metrics"), p.getFirst("Granularity"));
|
||||
return ok(new XmlBuilder()
|
||||
.start("EnableMetricsCollectionResponse", NS)
|
||||
.raw(AwsQueryResponse.responseMetadata())
|
||||
.end("EnableMetricsCollectionResponse").build());
|
||||
}
|
||||
|
||||
private Response handleDisableMetricsCollection(MultivaluedMap<String, String> p, String region) {
|
||||
service.disableMetricsCollection(region, p.getFirst("AutoScalingGroupName"), memberList(p, "Metrics"));
|
||||
return ok(new XmlBuilder()
|
||||
.start("DisableMetricsCollectionResponse", NS)
|
||||
.raw(AwsQueryResponse.responseMetadata())
|
||||
.end("DisableMetricsCollectionResponse").build());
|
||||
}
|
||||
|
||||
private Response handleDeleteTags(MultivaluedMap<String, String> p, String region) {
|
||||
for (TagRequest tag : parseTagRequests(p)) {
|
||||
service.deleteTags(region, tag.resourceId(), tag.resourceType(), List.of(tag.key()));
|
||||
@@ -565,6 +591,74 @@ public class AutoScalingQueryHandler {
|
||||
xml.end("LaunchTemplate");
|
||||
}
|
||||
|
||||
private static void appendInstanceMaintenancePolicyXml(XmlBuilder xml, InstanceMaintenancePolicy policy) {
|
||||
if (policy == null) {
|
||||
return;
|
||||
}
|
||||
xml.start("InstanceMaintenancePolicy");
|
||||
if (policy.getMinHealthyPercentage() != null) {
|
||||
xml.elem("MinHealthyPercentage", String.valueOf(policy.getMinHealthyPercentage()));
|
||||
}
|
||||
if (policy.getMaxHealthyPercentage() != null) {
|
||||
xml.elem("MaxHealthyPercentage", String.valueOf(policy.getMaxHealthyPercentage()));
|
||||
}
|
||||
xml.end("InstanceMaintenancePolicy");
|
||||
}
|
||||
|
||||
private static void appendAvailabilityZoneDistributionXml(XmlBuilder xml,
|
||||
AvailabilityZoneDistribution distribution) {
|
||||
if (distribution == null || distribution.getCapacityDistributionStrategy() == null) {
|
||||
return;
|
||||
}
|
||||
xml.start("AvailabilityZoneDistribution")
|
||||
.elem("CapacityDistributionStrategy", distribution.getCapacityDistributionStrategy())
|
||||
.end("AvailabilityZoneDistribution");
|
||||
}
|
||||
|
||||
private static void appendCapacityReservationSpecificationXml(XmlBuilder xml,
|
||||
CapacityReservationSpecification specification) {
|
||||
if (specification == null) {
|
||||
return;
|
||||
}
|
||||
xml.start("CapacityReservationSpecification");
|
||||
if (specification.getCapacityReservationPreference() != null) {
|
||||
xml.elem("CapacityReservationPreference", specification.getCapacityReservationPreference());
|
||||
}
|
||||
if (specification.hasTarget()) {
|
||||
xml.start("CapacityReservationTarget");
|
||||
if (!specification.getCapacityReservationIds().isEmpty()) {
|
||||
xml.start("CapacityReservationIds");
|
||||
for (String id : specification.getCapacityReservationIds()) {
|
||||
xml.elem("member", id);
|
||||
}
|
||||
xml.end("CapacityReservationIds");
|
||||
}
|
||||
if (!specification.getCapacityReservationResourceGroupArns().isEmpty()) {
|
||||
xml.start("CapacityReservationResourceGroupArns");
|
||||
for (String arn : specification.getCapacityReservationResourceGroupArns()) {
|
||||
xml.elem("member", arn);
|
||||
}
|
||||
xml.end("CapacityReservationResourceGroupArns");
|
||||
}
|
||||
xml.end("CapacityReservationTarget");
|
||||
}
|
||||
xml.end("CapacityReservationSpecification");
|
||||
}
|
||||
|
||||
private static void appendEnabledMetricsXml(XmlBuilder xml, List<String> enabledMetrics) {
|
||||
if (enabledMetrics == null || enabledMetrics.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
xml.start("EnabledMetrics");
|
||||
for (String metric : enabledMetrics) {
|
||||
xml.start("member")
|
||||
.elem("Metric", metric)
|
||||
.elem("Granularity", AutoScalingService.METRICS_GRANULARITY)
|
||||
.end("member");
|
||||
}
|
||||
xml.end("EnabledMetrics");
|
||||
}
|
||||
|
||||
private static void appendMixedInstancesPolicyXml(XmlBuilder xml, MixedInstancesPolicy policy) {
|
||||
if (policy == null) {
|
||||
return;
|
||||
@@ -581,6 +675,9 @@ public class AutoScalingQueryHandler {
|
||||
if (override.getInstanceType() != null) {
|
||||
xml.elem("InstanceType", override.getInstanceType());
|
||||
}
|
||||
if (override.getWeightedCapacity() != null) {
|
||||
xml.elem("WeightedCapacity", override.getWeightedCapacity());
|
||||
}
|
||||
appendInstanceRequirementsXml(xml, override.getInstanceRequirements());
|
||||
xml.end("member");
|
||||
}
|
||||
@@ -1386,6 +1483,7 @@ public class AutoScalingQueryHandler {
|
||||
MixedInstancesPolicy.LaunchTemplateOverride override =
|
||||
new MixedInstancesPolicy.LaunchTemplateOverride();
|
||||
override.setInstanceType(p.getFirst(prefix + ".InstanceType"));
|
||||
override.setWeightedCapacity(p.getFirst(prefix + ".WeightedCapacity"));
|
||||
override.setInstanceRequirements(parseInstanceRequirements(p, prefix + ".InstanceRequirements"));
|
||||
result.add(override);
|
||||
}
|
||||
@@ -1455,7 +1553,42 @@ public class AutoScalingQueryHandler {
|
||||
p.getFirst("DesiredCapacityType"),
|
||||
nullableBoolParam(p, "CapacityRebalance"),
|
||||
parseOptionalInt(p.getFirst("MaxInstanceLifetime"), "MaxInstanceLifetime"),
|
||||
parseOptionalInt(p.getFirst("DefaultInstanceWarmup"), "DefaultInstanceWarmup"));
|
||||
parseOptionalInt(p.getFirst("DefaultInstanceWarmup"), "DefaultInstanceWarmup"),
|
||||
p.getFirst("ServiceLinkedRoleARN"),
|
||||
parseInstanceMaintenancePolicy(p),
|
||||
parseAvailabilityZoneDistribution(p),
|
||||
parseCapacityReservationSpecification(p));
|
||||
}
|
||||
|
||||
private InstanceMaintenancePolicy parseInstanceMaintenancePolicy(MultivaluedMap<String, String> p) {
|
||||
if (!hasAnyPrefix(p, "InstanceMaintenancePolicy.")) {
|
||||
return null;
|
||||
}
|
||||
return new InstanceMaintenancePolicy(
|
||||
parseOptionalInt(p.getFirst("InstanceMaintenancePolicy.MinHealthyPercentage"),
|
||||
"InstanceMaintenancePolicy.MinHealthyPercentage"),
|
||||
parseOptionalInt(p.getFirst("InstanceMaintenancePolicy.MaxHealthyPercentage"),
|
||||
"InstanceMaintenancePolicy.MaxHealthyPercentage"));
|
||||
}
|
||||
|
||||
private AvailabilityZoneDistribution parseAvailabilityZoneDistribution(MultivaluedMap<String, String> p) {
|
||||
String strategy = p.getFirst("AvailabilityZoneDistribution.CapacityDistributionStrategy");
|
||||
return strategy != null ? new AvailabilityZoneDistribution(strategy) : null;
|
||||
}
|
||||
|
||||
private CapacityReservationSpecification parseCapacityReservationSpecification(
|
||||
MultivaluedMap<String, String> p) {
|
||||
String prefix = "CapacityReservationSpecification.";
|
||||
if (!hasAnyPrefix(p, prefix)) {
|
||||
return null;
|
||||
}
|
||||
CapacityReservationSpecification specification = new CapacityReservationSpecification();
|
||||
specification.setCapacityReservationPreference(p.getFirst(prefix + "CapacityReservationPreference"));
|
||||
specification.setCapacityReservationIds(
|
||||
memberList(p, prefix + "CapacityReservationTarget.CapacityReservationIds"));
|
||||
specification.setCapacityReservationResourceGroupArns(
|
||||
memberList(p, prefix + "CapacityReservationTarget.CapacityReservationResourceGroupArns"));
|
||||
return specification;
|
||||
}
|
||||
|
||||
private ParsedTags parseTags(MultivaluedMap<String, String> p) {
|
||||
|
||||
+207
@@ -49,6 +49,55 @@ public class AutoScalingService {
|
||||
"Amazon EC2 Auto Scaling supports DesiredCapacityType for attribute-based instance type "
|
||||
+ "selection only. Specify InstanceRequirements on a MixedInstancesPolicy launch "
|
||||
+ "template override, or use the default DesiredCapacityType of units.";
|
||||
static final String INSTANCE_MAINTENANCE_POLICY_BOTH_REQUIRED_MESSAGE =
|
||||
"Both MinHealthyPercentage and MaxHealthyPercentage must be specified in InstanceMaintenancePolicy.";
|
||||
static final String INVALID_MIN_HEALTHY_PERCENTAGE_MESSAGE =
|
||||
"MinHealthyPercentage must be between 0 and 100, or -1 to clear a previously set value.";
|
||||
static final String INVALID_MAX_HEALTHY_PERCENTAGE_MESSAGE =
|
||||
"MaxHealthyPercentage must be between 100 and 200, or -1 to clear a previously set value.";
|
||||
static final String INSTANCE_MAINTENANCE_POLICY_PARTIAL_CLEAR_MESSAGE =
|
||||
"To clear an InstanceMaintenancePolicy, specify -1 for both MinHealthyPercentage and MaxHealthyPercentage.";
|
||||
static final String INSTANCE_MAINTENANCE_POLICY_RANGE_MESSAGE =
|
||||
"The difference between MaxHealthyPercentage and MinHealthyPercentage cannot be greater than 100.";
|
||||
static final Set<String> CAPACITY_DISTRIBUTION_STRATEGIES =
|
||||
Set.of("balanced-only", "balanced-best-effort", "reservations-then-balanced");
|
||||
static final String INVALID_CAPACITY_DISTRIBUTION_STRATEGY_MESSAGE =
|
||||
"The specified value for CapacityDistributionStrategy is not valid. Valid values are: "
|
||||
+ "balanced-only, balanced-best-effort, reservations-then-balanced.";
|
||||
static final Set<String> CAPACITY_RESERVATION_PREFERENCES =
|
||||
Set.of("capacity-reservations-only", "capacity-reservations-first", "none", "default");
|
||||
static final String INVALID_CAPACITY_RESERVATION_PREFERENCE_MESSAGE =
|
||||
"The specified value for CapacityReservationPreference is not valid. Valid values are: "
|
||||
+ "capacity-reservations-only, capacity-reservations-first, none, default.";
|
||||
static final String CAPACITY_RESERVATION_TARGET_WITH_NONE_MESSAGE =
|
||||
"You cannot specify a CapacityReservationTarget when CapacityReservationPreference is none.";
|
||||
static final int MAX_SERVICE_LINKED_ROLE_ARN_LENGTH = 1600;
|
||||
static final int MAX_RESOURCE_NAME_LENGTH = 1600;
|
||||
private static final Pattern CAPACITY_RESERVATION_ID_PATTERN = Pattern.compile("^[A-Za-z0-9\\-_/]+$");
|
||||
static final int MAX_WEIGHTED_CAPACITY_LENGTH = 32;
|
||||
static final String INVALID_SERVICE_LINKED_ROLE_ARN_MESSAGE =
|
||||
"ServiceLinkedRoleARN must be between 1 and 1600 characters long.";
|
||||
static final int MIN_WEIGHTED_CAPACITY = 1;
|
||||
static final int MAX_WEIGHTED_CAPACITY = 999;
|
||||
static final String INVALID_WEIGHTED_CAPACITY_MESSAGE =
|
||||
"WeightedCapacity must be an integer in the range of 1-999.";
|
||||
static final String PARTIAL_WEIGHTED_CAPACITY_MESSAGE =
|
||||
"If you specify a value for WeightedCapacity for one instance type, you must specify a value "
|
||||
+ "for WeightedCapacity for all of them.";
|
||||
public static final List<String> GROUP_METRICS = List.of(
|
||||
"GroupMinSize", "GroupMaxSize", "GroupDesiredCapacity", "GroupInServiceInstances",
|
||||
"GroupPendingInstances", "GroupStandbyInstances", "GroupTerminatingInstances",
|
||||
"GroupTotalInstances", "GroupInServiceCapacity", "GroupPendingCapacity",
|
||||
"GroupStandbyCapacity", "GroupTerminatingCapacity", "GroupTotalCapacity",
|
||||
"WarmPoolDesiredCapacity", "WarmPoolWarmedCapacity", "WarmPoolPendingCapacity",
|
||||
"WarmPoolTerminatingCapacity", "WarmPoolTotalCapacity",
|
||||
"GroupAndWarmPoolDesiredCapacity", "GroupAndWarmPoolTotalCapacity");
|
||||
public static final String METRICS_GRANULARITY = "1Minute";
|
||||
static final String MISSING_GRANULARITY_MESSAGE =
|
||||
"1 validation error detected: Value null at 'granularity' failed to satisfy constraint: "
|
||||
+ "Member must not be null";
|
||||
public static final String INVALID_GRANULARITY_MESSAGE =
|
||||
"Granularity must be 1Minute. 1Minute is the only valid value.";
|
||||
static final String ACTIVE_INSTANCE_REFRESH_DESIRED_CONFIGURATION_MESSAGE =
|
||||
"An active instance refresh with a desired configuration exists. All configuration options derived from the desired configuration are not available for update while the instance refresh is active.";
|
||||
|
||||
@@ -332,6 +381,58 @@ public class AutoScalingService {
|
||||
groups.put(asgKey(region, name), asg);
|
||||
}
|
||||
|
||||
/**
|
||||
* EnableMetricsCollection: {@code Granularity} is required and 1Minute is its only valid value.
|
||||
* Naming no metrics enables all of them.
|
||||
*/
|
||||
public void enableMetricsCollection(String region, String name, List<String> metrics, String granularity) {
|
||||
if (granularity == null) {
|
||||
throw new AwsException("ValidationError", MISSING_GRANULARITY_MESSAGE, 400);
|
||||
}
|
||||
if (!METRICS_GRANULARITY.equals(granularity)) {
|
||||
throw new AwsException("ValidationError", INVALID_GRANULARITY_MESSAGE, 400);
|
||||
}
|
||||
validateMetricNames(metrics);
|
||||
String key = asgKey(region, name);
|
||||
synchronized (lockFor(key)) {
|
||||
AutoScalingGroup asg = requireGroup(region, name);
|
||||
List<String> requested = metrics == null || metrics.isEmpty() ? GROUP_METRICS : metrics;
|
||||
LinkedHashSet<String> enabled = new LinkedHashSet<>(asg.getEnabledMetrics());
|
||||
enabled.addAll(requested);
|
||||
asg.setEnabledMetrics(new ArrayList<>(enabled));
|
||||
groups.put(key, asg);
|
||||
}
|
||||
}
|
||||
|
||||
/** DisableMetricsCollection: naming no metrics disables all of them. */
|
||||
public void disableMetricsCollection(String region, String name, List<String> metrics) {
|
||||
validateMetricNames(metrics);
|
||||
String key = asgKey(region, name);
|
||||
synchronized (lockFor(key)) {
|
||||
AutoScalingGroup asg = requireGroup(region, name);
|
||||
if (metrics == null || metrics.isEmpty()) {
|
||||
asg.setEnabledMetrics(new ArrayList<>());
|
||||
} else {
|
||||
List<String> remaining = new ArrayList<>(asg.getEnabledMetrics());
|
||||
remaining.removeAll(metrics);
|
||||
asg.setEnabledMetrics(remaining);
|
||||
}
|
||||
groups.put(key, asg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void validateMetricNames(List<String> metrics) {
|
||||
if (metrics == null) {
|
||||
return;
|
||||
}
|
||||
for (String metric : metrics) {
|
||||
if (!GROUP_METRICS.contains(metric)) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Metric " + metric + " is not a valid Auto Scaling group metric.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteAutoScalingGroup(String region, String name, boolean forceDelete) {
|
||||
String key = asgKey(region, name);
|
||||
synchronized (lockFor(key)) {
|
||||
@@ -1109,6 +1210,7 @@ public class AutoScalingService {
|
||||
}
|
||||
|
||||
private static void validateLaunchTemplateOverrides(MixedInstancesPolicy mixedInstancesPolicy) {
|
||||
validateWeightedCapacities(overridesOf(mixedInstancesPolicy));
|
||||
for (MixedInstancesPolicy.LaunchTemplateOverride override : overridesOf(mixedInstancesPolicy)) {
|
||||
MixedInstancesPolicy.InstanceRequirements requirements = override.getInstanceRequirements();
|
||||
if (requirements == null || requirements.isEmpty()) {
|
||||
@@ -1125,6 +1227,36 @@ public class AutoScalingService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Botocore types {@code WeightedCapacity} as a string of 1-32 characters, while its prose
|
||||
* requires a value in the range 1-999 and requires every override to carry one once any does.
|
||||
*/
|
||||
private static void validateWeightedCapacities(List<MixedInstancesPolicy.LaunchTemplateOverride> overrides) {
|
||||
int weighted = 0;
|
||||
for (MixedInstancesPolicy.LaunchTemplateOverride override : overrides) {
|
||||
String weightedCapacity = override.getWeightedCapacity();
|
||||
if (weightedCapacity == null) {
|
||||
continue;
|
||||
}
|
||||
weighted++;
|
||||
if (weightedCapacity.isEmpty() || weightedCapacity.length() > MAX_WEIGHTED_CAPACITY_LENGTH) {
|
||||
throw new AwsException("ValidationError", INVALID_WEIGHTED_CAPACITY_MESSAGE, 400);
|
||||
}
|
||||
int value;
|
||||
try {
|
||||
value = Integer.parseInt(weightedCapacity.trim());
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("ValidationError", INVALID_WEIGHTED_CAPACITY_MESSAGE, 400);
|
||||
}
|
||||
if (value < MIN_WEIGHTED_CAPACITY || value > MAX_WEIGHTED_CAPACITY) {
|
||||
throw new AwsException("ValidationError", INVALID_WEIGHTED_CAPACITY_MESSAGE, 400);
|
||||
}
|
||||
}
|
||||
if (weighted > 0 && weighted < overrides.size()) {
|
||||
throw new AwsException("ValidationError", PARTIAL_WEIGHTED_CAPACITY_MESSAGE, 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Botocore documents {@code DesiredCapacityType} as supported "for attribute-based instance type
|
||||
* selection only", and it types the member as a plain string rather than an enum, so the rule
|
||||
@@ -1196,6 +1328,81 @@ public class AutoScalingService {
|
||||
&& defaultInstanceWarmup < AsgOptionalFields.DEFAULT_INSTANCE_WARMUP_REMOVAL_SENTINEL) {
|
||||
throw new AwsException("ValidationError", INVALID_DEFAULT_INSTANCE_WARMUP_MESSAGE, 400);
|
||||
}
|
||||
String serviceLinkedRoleArn = optionalFields.serviceLinkedRoleArn();
|
||||
if (serviceLinkedRoleArn != null
|
||||
&& (serviceLinkedRoleArn.isEmpty() || serviceLinkedRoleArn.length() > MAX_SERVICE_LINKED_ROLE_ARN_LENGTH)) {
|
||||
throw new AwsException("ValidationError", INVALID_SERVICE_LINKED_ROLE_ARN_MESSAGE, 400);
|
||||
}
|
||||
validateInstanceMaintenancePolicy(optionalFields.instanceMaintenancePolicy());
|
||||
AvailabilityZoneDistribution azDistribution = optionalFields.availabilityZoneDistribution();
|
||||
if (azDistribution != null && azDistribution.getCapacityDistributionStrategy() != null
|
||||
&& !CAPACITY_DISTRIBUTION_STRATEGIES.contains(azDistribution.getCapacityDistributionStrategy())) {
|
||||
throw new AwsException("ValidationError", INVALID_CAPACITY_DISTRIBUTION_STRATEGY_MESSAGE, 400);
|
||||
}
|
||||
validateCapacityReservationSpecification(optionalFields.capacityReservationSpecification());
|
||||
}
|
||||
|
||||
/**
|
||||
* Botocore bounds MinHealthyPercentage to [-1, 100] and MaxHealthyPercentage to [-1, 200], and
|
||||
* its prose narrows the maximum to 100-200, requires both members, caps their difference at 100,
|
||||
* and reserves -1 for clearing a previously set policy.
|
||||
*/
|
||||
private static void validateInstanceMaintenancePolicy(InstanceMaintenancePolicy policy) {
|
||||
if (policy == null) {
|
||||
return;
|
||||
}
|
||||
Integer min = policy.getMinHealthyPercentage();
|
||||
Integer max = policy.getMaxHealthyPercentage();
|
||||
if (min == null || max == null) {
|
||||
throw new AwsException("ValidationError", INSTANCE_MAINTENANCE_POLICY_BOTH_REQUIRED_MESSAGE, 400);
|
||||
}
|
||||
if (policy.isRemoval()) {
|
||||
return;
|
||||
}
|
||||
if (min == InstanceMaintenancePolicy.REMOVAL_SENTINEL || max == InstanceMaintenancePolicy.REMOVAL_SENTINEL) {
|
||||
throw new AwsException("ValidationError", INSTANCE_MAINTENANCE_POLICY_PARTIAL_CLEAR_MESSAGE, 400);
|
||||
}
|
||||
if (min < 0 || min > 100) {
|
||||
throw new AwsException("ValidationError", INVALID_MIN_HEALTHY_PERCENTAGE_MESSAGE, 400);
|
||||
}
|
||||
if (max < 100 || max > 200) {
|
||||
throw new AwsException("ValidationError", INVALID_MAX_HEALTHY_PERCENTAGE_MESSAGE, 400);
|
||||
}
|
||||
if (max - min > 100) {
|
||||
throw new AwsException("ValidationError", INSTANCE_MAINTENANCE_POLICY_RANGE_MESSAGE, 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateCapacityReservationSpecification(CapacityReservationSpecification specification) {
|
||||
if (specification == null) {
|
||||
return;
|
||||
}
|
||||
String preference = specification.getCapacityReservationPreference();
|
||||
if (preference != null && !CAPACITY_RESERVATION_PREFERENCES.contains(preference)) {
|
||||
throw new AwsException("ValidationError", INVALID_CAPACITY_RESERVATION_PREFERENCE_MESSAGE, 400);
|
||||
}
|
||||
if ("none".equals(preference) && specification.hasTarget()) {
|
||||
throw new AwsException("ValidationError", CAPACITY_RESERVATION_TARGET_WITH_NONE_MESSAGE, 400);
|
||||
}
|
||||
for (String id : specification.getCapacityReservationIds()) {
|
||||
if (id == null || id.isEmpty() || id.length() > 255
|
||||
|| !CAPACITY_RESERVATION_ID_PATTERN.matcher(id).matches()) {
|
||||
throw new AwsException("ValidationError",
|
||||
"1 validation error detected: Value '" + id
|
||||
+ "' at 'capacityReservationSpecification.capacityReservationTarget"
|
||||
+ ".capacityReservationIds' failed to satisfy constraint: Member must have length "
|
||||
+ "between 1 and 255 and satisfy regular expression pattern: [A-Za-z0-9\\-_\\/]+",
|
||||
400);
|
||||
}
|
||||
}
|
||||
for (String arn : specification.getCapacityReservationResourceGroupArns()) {
|
||||
if (arn == null || arn.isEmpty() || arn.length() > MAX_RESOURCE_NAME_LENGTH) {
|
||||
throw new AwsException("ValidationError",
|
||||
"1 validation error detected: Value at 'capacityReservationSpecification"
|
||||
+ ".capacityReservationTarget.capacityReservationResourceGroupArns' failed to "
|
||||
+ "satisfy constraint: Member must have length between 1 and 1600", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasUsableLaunchTemplate(MixedInstancesPolicy mixedInstancesPolicy) {
|
||||
|
||||
+43
-6
@@ -1,28 +1,42 @@
|
||||
package io.github.hectorvent.floci.services.autoscaling.model;
|
||||
|
||||
/**
|
||||
* The optional scalar members that {@code CreateAutoScalingGroup} and
|
||||
* {@code UpdateAutoScalingGroup} share and that {@code DescribeAutoScalingGroups} echoes back.
|
||||
* The optional members that {@code CreateAutoScalingGroup} and {@code UpdateAutoScalingGroup}
|
||||
* share and that {@code DescribeAutoScalingGroups} echoes back.
|
||||
*
|
||||
* <p>Carried as one object rather than four more positional parameters on
|
||||
* <p>Carried as one object rather than more positional parameters on
|
||||
* {@code AutoScalingService.createAutoScalingGroup}, whose argument list is already long.
|
||||
*
|
||||
* <p>A {@code null} component means the request did not set the member. On update that means
|
||||
* "leave the stored value alone", which is UpdateAutoScalingGroup's partial-update behaviour for
|
||||
* every other member.
|
||||
*
|
||||
* <p>{@code DefaultInstanceWarmup} carries a sentinel: botocore documents {@code -1} as the value
|
||||
* that removes a previously set warmup, so it clears the stored value instead of storing -1.
|
||||
* <p>Two members carry a removal sentinel. Botocore documents {@code -1} as the value that removes
|
||||
* a previously set {@code DefaultInstanceWarmup}, and {@code -1} on the percentages of
|
||||
* {@code InstanceMaintenancePolicy} as the value that clears that policy. Both clear the stored
|
||||
* value instead of storing -1.
|
||||
*/
|
||||
public record AsgOptionalFields(String desiredCapacityType,
|
||||
Boolean capacityRebalance,
|
||||
Integer maxInstanceLifetime,
|
||||
Integer defaultInstanceWarmup) {
|
||||
Integer defaultInstanceWarmup,
|
||||
String serviceLinkedRoleArn,
|
||||
InstanceMaintenancePolicy instanceMaintenancePolicy,
|
||||
AvailabilityZoneDistribution availabilityZoneDistribution,
|
||||
CapacityReservationSpecification capacityReservationSpecification) {
|
||||
|
||||
public static final int DEFAULT_INSTANCE_WARMUP_REMOVAL_SENTINEL = -1;
|
||||
|
||||
private static final AsgOptionalFields NONE = new AsgOptionalFields(null, null, null, null);
|
||||
|
||||
public AsgOptionalFields(String desiredCapacityType,
|
||||
Boolean capacityRebalance,
|
||||
Integer maxInstanceLifetime,
|
||||
Integer defaultInstanceWarmup) {
|
||||
this(desiredCapacityType, capacityRebalance, maxInstanceLifetime, defaultInstanceWarmup,
|
||||
null, null, null, null);
|
||||
}
|
||||
|
||||
public static AsgOptionalFields none() {
|
||||
return NONE;
|
||||
}
|
||||
@@ -33,6 +47,10 @@ public record AsgOptionalFields(String desiredCapacityType,
|
||||
asg.setCapacityRebalance(capacityRebalance);
|
||||
asg.setMaxInstanceLifetime(maxInstanceLifetime);
|
||||
asg.setDefaultInstanceWarmup(resolvedDefaultInstanceWarmup());
|
||||
asg.setServiceLinkedRoleArn(serviceLinkedRoleArn);
|
||||
asg.setInstanceMaintenancePolicy(resolvedInstanceMaintenancePolicy());
|
||||
asg.setAvailabilityZoneDistribution(availabilityZoneDistribution);
|
||||
asg.setCapacityReservationSpecification(capacityReservationSpecification);
|
||||
}
|
||||
|
||||
/** Applies onto an existing group, overwriting only the members this request set. */
|
||||
@@ -49,6 +67,18 @@ public record AsgOptionalFields(String desiredCapacityType,
|
||||
if (defaultInstanceWarmup != null) {
|
||||
asg.setDefaultInstanceWarmup(resolvedDefaultInstanceWarmup());
|
||||
}
|
||||
if (serviceLinkedRoleArn != null) {
|
||||
asg.setServiceLinkedRoleArn(serviceLinkedRoleArn);
|
||||
}
|
||||
if (instanceMaintenancePolicy != null) {
|
||||
asg.setInstanceMaintenancePolicy(resolvedInstanceMaintenancePolicy());
|
||||
}
|
||||
if (availabilityZoneDistribution != null) {
|
||||
asg.setAvailabilityZoneDistribution(availabilityZoneDistribution);
|
||||
}
|
||||
if (capacityReservationSpecification != null) {
|
||||
asg.setCapacityReservationSpecification(capacityReservationSpecification);
|
||||
}
|
||||
}
|
||||
|
||||
private Integer resolvedDefaultInstanceWarmup() {
|
||||
@@ -58,4 +88,11 @@ public record AsgOptionalFields(String desiredCapacityType,
|
||||
}
|
||||
return defaultInstanceWarmup;
|
||||
}
|
||||
|
||||
private InstanceMaintenancePolicy resolvedInstanceMaintenancePolicy() {
|
||||
if (instanceMaintenancePolicy != null && instanceMaintenancePolicy.isRemoval()) {
|
||||
return null;
|
||||
}
|
||||
return instanceMaintenancePolicy;
|
||||
}
|
||||
}
|
||||
+22
@@ -50,6 +50,13 @@ public class AutoScalingGroup {
|
||||
private Boolean capacityRebalance;
|
||||
private Integer maxInstanceLifetime;
|
||||
private Integer defaultInstanceWarmup;
|
||||
private String serviceLinkedRoleArn;
|
||||
private InstanceMaintenancePolicy instanceMaintenancePolicy;
|
||||
private AvailabilityZoneDistribution availabilityZoneDistribution;
|
||||
private CapacityReservationSpecification capacityReservationSpecification;
|
||||
// Set only by EnableMetricsCollection/DisableMetricsCollection. 1Minute is the only granularity
|
||||
// the model allows, so the metric names are all that needs storing.
|
||||
private List<String> enabledMetrics = new ArrayList<>();
|
||||
|
||||
public AutoScalingGroup() {}
|
||||
|
||||
@@ -142,4 +149,19 @@ public class AutoScalingGroup {
|
||||
|
||||
public Integer getDefaultInstanceWarmup() { return defaultInstanceWarmup; }
|
||||
public void setDefaultInstanceWarmup(Integer v) { this.defaultInstanceWarmup = v; }
|
||||
|
||||
public String getServiceLinkedRoleArn() { return serviceLinkedRoleArn; }
|
||||
public void setServiceLinkedRoleArn(String v) { this.serviceLinkedRoleArn = v; }
|
||||
|
||||
public InstanceMaintenancePolicy getInstanceMaintenancePolicy() { return instanceMaintenancePolicy; }
|
||||
public void setInstanceMaintenancePolicy(InstanceMaintenancePolicy v) { this.instanceMaintenancePolicy = v; }
|
||||
|
||||
public AvailabilityZoneDistribution getAvailabilityZoneDistribution() { return availabilityZoneDistribution; }
|
||||
public void setAvailabilityZoneDistribution(AvailabilityZoneDistribution v) { this.availabilityZoneDistribution = v; }
|
||||
|
||||
public CapacityReservationSpecification getCapacityReservationSpecification() { return capacityReservationSpecification; }
|
||||
public void setCapacityReservationSpecification(CapacityReservationSpecification v) { this.capacityReservationSpecification = v; }
|
||||
|
||||
public List<String> getEnabledMetrics() { return enabledMetrics; }
|
||||
public void setEnabledMetrics(List<String> v) { this.enabledMetrics = v != null ? new ArrayList<>(v) : new ArrayList<>(); }
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package io.github.hectorvent.floci.services.autoscaling.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
/**
|
||||
* The {@code AvailabilityZoneDistribution} member of CreateAutoScalingGroup, UpdateAutoScalingGroup
|
||||
* and the {@code AutoScalingGroup} shape.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class AvailabilityZoneDistribution {
|
||||
|
||||
private String capacityDistributionStrategy;
|
||||
|
||||
public AvailabilityZoneDistribution() {}
|
||||
|
||||
public AvailabilityZoneDistribution(String capacityDistributionStrategy) {
|
||||
this.capacityDistributionStrategy = capacityDistributionStrategy;
|
||||
}
|
||||
|
||||
public String getCapacityDistributionStrategy() { return capacityDistributionStrategy; }
|
||||
public void setCapacityDistributionStrategy(String v) { this.capacityDistributionStrategy = v; }
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package io.github.hectorvent.floci.services.autoscaling.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The {@code CapacityReservationSpecification} member of CreateAutoScalingGroup,
|
||||
* UpdateAutoScalingGroup and the {@code AutoScalingGroup} shape, with its
|
||||
* {@code CapacityReservationTarget} flattened into the two lists that target carries.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class CapacityReservationSpecification {
|
||||
|
||||
private String capacityReservationPreference;
|
||||
private List<String> capacityReservationIds = new ArrayList<>();
|
||||
private List<String> capacityReservationResourceGroupArns = new ArrayList<>();
|
||||
|
||||
public CapacityReservationSpecification() {}
|
||||
|
||||
public String getCapacityReservationPreference() { return capacityReservationPreference; }
|
||||
public void setCapacityReservationPreference(String v) { this.capacityReservationPreference = v; }
|
||||
|
||||
public List<String> getCapacityReservationIds() { return capacityReservationIds; }
|
||||
public void setCapacityReservationIds(List<String> v) {
|
||||
this.capacityReservationIds = v != null ? new ArrayList<>(v) : new ArrayList<>();
|
||||
}
|
||||
|
||||
public List<String> getCapacityReservationResourceGroupArns() { return capacityReservationResourceGroupArns; }
|
||||
public void setCapacityReservationResourceGroupArns(List<String> v) {
|
||||
this.capacityReservationResourceGroupArns = v != null ? new ArrayList<>(v) : new ArrayList<>();
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public boolean hasTarget() {
|
||||
return !capacityReservationIds.isEmpty() || !capacityReservationResourceGroupArns.isEmpty();
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package io.github.hectorvent.floci.services.autoscaling.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
/**
|
||||
* The {@code InstanceMaintenancePolicy} member of CreateAutoScalingGroup, UpdateAutoScalingGroup
|
||||
* and the {@code AutoScalingGroup} shape. Botocore documents {@code -1} on either percentage as the
|
||||
* value that clears a previously set policy.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class InstanceMaintenancePolicy {
|
||||
|
||||
public static final int REMOVAL_SENTINEL = -1;
|
||||
|
||||
private Integer minHealthyPercentage;
|
||||
private Integer maxHealthyPercentage;
|
||||
|
||||
public InstanceMaintenancePolicy() {}
|
||||
|
||||
public InstanceMaintenancePolicy(Integer minHealthyPercentage, Integer maxHealthyPercentage) {
|
||||
this.minHealthyPercentage = minHealthyPercentage;
|
||||
this.maxHealthyPercentage = maxHealthyPercentage;
|
||||
}
|
||||
|
||||
public Integer getMinHealthyPercentage() { return minHealthyPercentage; }
|
||||
public void setMinHealthyPercentage(Integer v) { this.minHealthyPercentage = v; }
|
||||
|
||||
public Integer getMaxHealthyPercentage() { return maxHealthyPercentage; }
|
||||
public void setMaxHealthyPercentage(Integer v) { this.maxHealthyPercentage = v; }
|
||||
|
||||
@JsonIgnore
|
||||
public boolean isRemoval() {
|
||||
return Integer.valueOf(REMOVAL_SENTINEL).equals(minHealthyPercentage)
|
||||
&& Integer.valueOf(REMOVAL_SENTINEL).equals(maxHealthyPercentage);
|
||||
}
|
||||
}
|
||||
+4
@@ -58,6 +58,7 @@ public class MixedInstancesPolicy {
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class LaunchTemplateOverride {
|
||||
private String instanceType;
|
||||
private String weightedCapacity;
|
||||
private InstanceRequirements instanceRequirements;
|
||||
|
||||
public LaunchTemplateOverride() {}
|
||||
@@ -65,6 +66,9 @@ public class MixedInstancesPolicy {
|
||||
public String getInstanceType() { return instanceType; }
|
||||
public void setInstanceType(String v) { this.instanceType = v; }
|
||||
|
||||
public String getWeightedCapacity() { return weightedCapacity; }
|
||||
public void setWeightedCapacity(String v) { this.weightedCapacity = v; }
|
||||
|
||||
public InstanceRequirements getInstanceRequirements() { return instanceRequirements; }
|
||||
public void setInstanceRequirements(InstanceRequirements v) { this.instanceRequirements = v; }
|
||||
}
|
||||
|
||||
+163
-8
@@ -7,6 +7,9 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.autoscaling.AutoScalingService;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AsgOptionalFields;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AutoScalingGroup;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AvailabilityZoneDistribution;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.CapacityReservationSpecification;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.InstanceMaintenancePolicy;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.LaunchConfiguration;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.MixedInstancesPolicy;
|
||||
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
|
||||
@@ -15,6 +18,7 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -84,13 +88,23 @@ public class AutoScalingGroupCfnProvisioner implements CfnResourceProvisioner {
|
||||
AsgSnapshot snapshot = MAPPER.readValue(rawSnapshot, AsgSnapshot.class);
|
||||
// UpdateAutoScalingGroup treats null as "leave unchanged". Restore an unset
|
||||
// snapshot member with its API default instead; -1 removes DefaultInstanceWarmup.
|
||||
// InstanceMaintenancePolicy clears with -1 on both percentages. The service-linked
|
||||
// role, AZ distribution and capacity reservation members have no clearing value
|
||||
// on UpdateAutoScalingGroup, so an unset snapshot member is cleared afterwards.
|
||||
AsgOptionalFields optionalFields = new AsgOptionalFields(
|
||||
snapshot.desiredCapacityType() != null ? snapshot.desiredCapacityType() : "units",
|
||||
snapshot.capacityRebalance() != null ? snapshot.capacityRebalance() : false,
|
||||
snapshot.maxInstanceLifetime() != null ? snapshot.maxInstanceLifetime() : 0,
|
||||
snapshot.defaultInstanceWarmup() != null
|
||||
? snapshot.defaultInstanceWarmup()
|
||||
: AsgOptionalFields.DEFAULT_INSTANCE_WARMUP_REMOVAL_SENTINEL
|
||||
: AsgOptionalFields.DEFAULT_INSTANCE_WARMUP_REMOVAL_SENTINEL,
|
||||
snapshot.serviceLinkedRoleArn(),
|
||||
snapshot.instanceMaintenancePolicy() != null
|
||||
? snapshot.instanceMaintenancePolicy()
|
||||
: new InstanceMaintenancePolicy(InstanceMaintenancePolicy.REMOVAL_SENTINEL,
|
||||
InstanceMaintenancePolicy.REMOVAL_SENTINEL),
|
||||
snapshot.availabilityZoneDistribution(),
|
||||
snapshot.capacityReservationSpecification()
|
||||
);
|
||||
autoScalingService.updateAutoScalingGroup(
|
||||
snapshot.region(),
|
||||
@@ -111,6 +125,10 @@ public class AutoScalingGroupCfnProvisioner implements CfnResourceProvisioner {
|
||||
snapshot.terminationPolicies(),
|
||||
optionalFields
|
||||
);
|
||||
clearUnsetSnapshotMembers(snapshot);
|
||||
if (snapshot.enabledMetrics() != null) {
|
||||
reconcileMetricsCollection(snapshot.region(), snapshot.name(), snapshot.enabledMetrics());
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LOG.errorv("Could not restore Auto Scaling group update snapshot for {0}: {1}",
|
||||
@@ -147,7 +165,12 @@ public class AutoScalingGroupCfnProvisioner implements CfnResourceProvisioner {
|
||||
String desiredCapacityType,
|
||||
Boolean capacityRebalance,
|
||||
Integer maxInstanceLifetime,
|
||||
Integer defaultInstanceWarmup
|
||||
Integer defaultInstanceWarmup,
|
||||
String serviceLinkedRoleArn,
|
||||
InstanceMaintenancePolicy instanceMaintenancePolicy,
|
||||
AvailabilityZoneDistribution availabilityZoneDistribution,
|
||||
CapacityReservationSpecification capacityReservationSpecification,
|
||||
List<String> enabledMetrics
|
||||
) {}
|
||||
|
||||
private static IllegalStateException unsupported(String resourceType) {
|
||||
@@ -224,6 +247,7 @@ public class AutoScalingGroupCfnProvisioner implements CfnResourceProvisioner {
|
||||
int healthCheckGracePeriod = parseIntProp(props, "HealthCheckGracePeriod", ctx, 0);
|
||||
List<String> terminationPolicies = ctx.resolveStringList(props, "TerminationPolicies");
|
||||
AsgOptionalFields optionalFields = resolveOptionalFields(props, ctx);
|
||||
List<String> metricsCollection = resolveMetricsCollection(props, ctx);
|
||||
|
||||
// provision() re-runs on every UpdateStack, so a same-named group already on file must be
|
||||
// reconciled via UpdateAutoScalingGroup instead of re-created (createAutoScalingGroup throws
|
||||
@@ -252,7 +276,12 @@ public class AutoScalingGroupCfnProvisioner implements CfnResourceProvisioner {
|
||||
existing.getDesiredCapacityType(),
|
||||
existing.getCapacityRebalance(),
|
||||
existing.getMaxInstanceLifetime(),
|
||||
existing.getDefaultInstanceWarmup()
|
||||
existing.getDefaultInstanceWarmup(),
|
||||
existing.getServiceLinkedRoleArn(),
|
||||
existing.getInstanceMaintenancePolicy(),
|
||||
existing.getAvailabilityZoneDistribution(),
|
||||
existing.getCapacityReservationSpecification(),
|
||||
existing.getEnabledMetrics() != null ? List.copyOf(existing.getEnabledMetrics()) : List.of()
|
||||
);
|
||||
try {
|
||||
r.getAttributes().put(CfnRollback.ASG_UPDATE_SNAPSHOT_ATTR, MAPPER.writeValueAsString(snapshot));
|
||||
@@ -267,6 +296,9 @@ public class AutoScalingGroupCfnProvisioner implements CfnResourceProvisioner {
|
||||
mixedInstancesPolicy, minSize, maxSize, desiredCapacity, cooldown,
|
||||
availabilityZones, subnetIds, healthCheckType, healthCheckGracePeriod, terminationPolicies,
|
||||
optionalFields);
|
||||
if (metricsCollection != null) {
|
||||
reconcileMetricsCollection(region, name, metricsCollection);
|
||||
}
|
||||
asg = requireAutoScalingGroup(region, name);
|
||||
} else {
|
||||
asg = autoScalingService.createAutoScalingGroup(region, name,
|
||||
@@ -280,6 +312,10 @@ public class AutoScalingGroupCfnProvisioner implements CfnResourceProvisioner {
|
||||
resolveAsgTags(props, engine),
|
||||
resolveAsgTagPropagation(props, engine),
|
||||
optionalFields);
|
||||
if (metricsCollection != null && !metricsCollection.isEmpty()) {
|
||||
autoScalingService.enableMetricsCollection(region, name, metricsCollection,
|
||||
AutoScalingService.METRICS_GRANULARITY);
|
||||
}
|
||||
deleteRenamedResource(ctx, name, n -> autoScalingService.deleteAutoScalingGroup(region, n, true),
|
||||
"Auto Scaling group");
|
||||
}
|
||||
@@ -292,6 +328,25 @@ public class AutoScalingGroupCfnProvisioner implements CfnResourceProvisioner {
|
||||
r.getAttributes().put("Arn", asg.getAutoScalingGroupArn());
|
||||
}
|
||||
|
||||
private void clearUnsetSnapshotMembers(AsgSnapshot snapshot) {
|
||||
if (snapshot.serviceLinkedRoleArn() != null
|
||||
&& snapshot.availabilityZoneDistribution() != null
|
||||
&& snapshot.capacityReservationSpecification() != null) {
|
||||
return;
|
||||
}
|
||||
AutoScalingGroup group = requireAutoScalingGroup(snapshot.region(), snapshot.name());
|
||||
if (snapshot.serviceLinkedRoleArn() == null) {
|
||||
group.setServiceLinkedRoleArn(null);
|
||||
}
|
||||
if (snapshot.availabilityZoneDistribution() == null) {
|
||||
group.setAvailabilityZoneDistribution(null);
|
||||
}
|
||||
if (snapshot.capacityReservationSpecification() == null) {
|
||||
group.setCapacityReservationSpecification(null);
|
||||
}
|
||||
autoScalingService.saveAutoScalingGroupIfPresent(group);
|
||||
}
|
||||
|
||||
private AutoScalingGroup requireAutoScalingGroup(String region, String name) {
|
||||
List<AutoScalingGroup> found = autoScalingService.describeAutoScalingGroups(region, List.of(name));
|
||||
if (found.isEmpty()) {
|
||||
@@ -301,10 +356,10 @@ public class AutoScalingGroupCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
/**
|
||||
* The four group properties the schema defines that the CloudFormation path used to drop,
|
||||
* passing {@code AsgOptionalFields.none()} instead. Absent stays null so an update overwrites
|
||||
* only what the template set, which is what {@code AsgOptionalFields.applyToExistingGroup} and
|
||||
* the Query handler's {@code UpdateAutoScalingGroup} both do.
|
||||
* The optional group properties the schema defines that Create/UpdateAutoScalingGroup also
|
||||
* carry. Absent stays null so an update overwrites only what the template set, which is what
|
||||
* {@code AsgOptionalFields.applyToExistingGroup} and the Query handler's
|
||||
* {@code UpdateAutoScalingGroup} both do.
|
||||
*/
|
||||
private AsgOptionalFields resolveOptionalFields(JsonNode props, ProvisionContext ctx) {
|
||||
String capacityRebalance = ctx.resolveOptional(props, "CapacityRebalance");
|
||||
@@ -314,7 +369,104 @@ public class AutoScalingGroupCfnProvisioner implements CfnResourceProvisioner {
|
||||
? null
|
||||
: Boolean.parseBoolean(capacityRebalance),
|
||||
parseOptionalInt("MaxInstanceLifetime", ctx.resolveOptional(props, "MaxInstanceLifetime")),
|
||||
parseOptionalInt("DefaultInstanceWarmup", ctx.resolveOptional(props, "DefaultInstanceWarmup")));
|
||||
parseOptionalInt("DefaultInstanceWarmup", ctx.resolveOptional(props, "DefaultInstanceWarmup")),
|
||||
ctx.resolveOptional(props, "ServiceLinkedRoleARN"),
|
||||
resolveInstanceMaintenancePolicy(props, ctx),
|
||||
resolveAvailabilityZoneDistribution(props, ctx),
|
||||
resolveCapacityReservationSpecification(props, ctx));
|
||||
}
|
||||
|
||||
private InstanceMaintenancePolicy resolveInstanceMaintenancePolicy(JsonNode props, ProvisionContext ctx) {
|
||||
JsonNode node = objectProperty(props, "InstanceMaintenancePolicy", ctx);
|
||||
if (node == null) {
|
||||
return null;
|
||||
}
|
||||
return new InstanceMaintenancePolicy(
|
||||
parseOptionalInt("MinHealthyPercentage", ctx.resolveOptional(node, "MinHealthyPercentage")),
|
||||
parseOptionalInt("MaxHealthyPercentage", ctx.resolveOptional(node, "MaxHealthyPercentage")));
|
||||
}
|
||||
|
||||
private AvailabilityZoneDistribution resolveAvailabilityZoneDistribution(JsonNode props, ProvisionContext ctx) {
|
||||
JsonNode node = objectProperty(props, "AvailabilityZoneDistribution", ctx);
|
||||
if (node == null) {
|
||||
return null;
|
||||
}
|
||||
String strategy = blankToNull(ctx.resolveOptional(node, "CapacityDistributionStrategy"));
|
||||
return strategy != null ? new AvailabilityZoneDistribution(strategy) : null;
|
||||
}
|
||||
|
||||
private CapacityReservationSpecification resolveCapacityReservationSpecification(JsonNode props,
|
||||
ProvisionContext ctx) {
|
||||
JsonNode node = objectProperty(props, "CapacityReservationSpecification", ctx);
|
||||
if (node == null) {
|
||||
return null;
|
||||
}
|
||||
String preference = blankToNull(ctx.resolveOptional(node, "CapacityReservationPreference"));
|
||||
if (preference == null) {
|
||||
throw new AwsException("ValidationError",
|
||||
"CapacityReservationSpecification requires CapacityReservationPreference.", 400);
|
||||
}
|
||||
CapacityReservationSpecification specification = new CapacityReservationSpecification();
|
||||
specification.setCapacityReservationPreference(preference);
|
||||
JsonNode target = objectProperty(node, "CapacityReservationTarget", ctx);
|
||||
if (target != null) {
|
||||
specification.setCapacityReservationIds(ctx.resolveStringList(target, "CapacityReservationIds"));
|
||||
specification.setCapacityReservationResourceGroupArns(
|
||||
ctx.resolveStringList(target, "CapacityReservationResourceGroupArns"));
|
||||
}
|
||||
return specification;
|
||||
}
|
||||
|
||||
/**
|
||||
* The metric names that {@code MetricsCollection} enables, or {@code null} when the template
|
||||
* omits the property. The schema allows several entries. Their metric lists are merged, and an
|
||||
* entry naming no metrics stands for all of them, as it does on EnableMetricsCollection. The
|
||||
* schema requires {@code Granularity} on every entry.
|
||||
*/
|
||||
private List<String> resolveMetricsCollection(JsonNode props, ProvisionContext ctx) {
|
||||
JsonNode node = props != null ? ctx.engine().resolveNode(props.get("MetricsCollection")) : null;
|
||||
if (node == null || !node.isArray()) {
|
||||
return null;
|
||||
}
|
||||
List<String> metrics = new ArrayList<>();
|
||||
for (JsonNode entry : node) {
|
||||
String granularity = blankToNull(ctx.resolveOptional(entry, "Granularity"));
|
||||
if (granularity == null) {
|
||||
throw new AwsException("ValidationError", "MetricsCollection requires Granularity.", 400);
|
||||
}
|
||||
if (!AutoScalingService.METRICS_GRANULARITY.equals(granularity)) {
|
||||
throw new AwsException("ValidationError", AutoScalingService.INVALID_GRANULARITY_MESSAGE, 400);
|
||||
}
|
||||
List<String> named = ctx.resolveStringList(entry, "Metrics");
|
||||
AutoScalingService.validateMetricNames(named);
|
||||
for (String metric : named.isEmpty() ? AutoScalingService.GROUP_METRICS : named) {
|
||||
if (!metrics.contains(metric)) {
|
||||
metrics.add(metric);
|
||||
}
|
||||
}
|
||||
}
|
||||
return metrics;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the group's enabled metrics exactly {@code desired}, so an empty list disables them all.
|
||||
* A template that drops {@code MetricsCollection} altogether never reaches here and leaves
|
||||
* collection untouched.
|
||||
*/
|
||||
private void reconcileMetricsCollection(String region, String name, List<String> desired) {
|
||||
autoScalingService.disableMetricsCollection(region, name, List.of());
|
||||
if (!desired.isEmpty()) {
|
||||
autoScalingService.enableMetricsCollection(region, name, desired,
|
||||
AutoScalingService.METRICS_GRANULARITY);
|
||||
}
|
||||
}
|
||||
|
||||
private JsonNode objectProperty(JsonNode props, String name, ProvisionContext ctx) {
|
||||
if (props == null || !props.has(name)) {
|
||||
return null;
|
||||
}
|
||||
JsonNode node = ctx.engine().resolveNode(props.get(name));
|
||||
return node != null && node.isObject() ? node : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -348,6 +500,9 @@ public class AutoScalingGroupCfnProvisioner implements CfnResourceProvisioner {
|
||||
MixedInstancesPolicy.LaunchTemplateOverride override =
|
||||
new MixedInstancesPolicy.LaunchTemplateOverride();
|
||||
override.setInstanceType(instanceType);
|
||||
override.setWeightedCapacity(overrideNode.hasNonNull("WeightedCapacity")
|
||||
? engine.resolve(overrideNode.get("WeightedCapacity"))
|
||||
: null);
|
||||
launchTemplate.getOverrides().add(override);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user