mirror of
https://github.com/floci-io/floci.git
synced 2026-12-03 02:07:58 -05:00
refactor(cloudformation): move the AutoScaling types into a per-service provisioner (#3582)
Moves AWS::AutoScaling::AutoScalingGroup and AWS::AutoScaling::LaunchConfiguration out of the monolith switch into AutoScalingGroupCfnProvisioner. Monolith 4,988 to 4,702 lines, legacy types 31 to 29. The monolith side of the diff is pure deletion: nothing in it was edited, only removed. autoScalingService was exclusive to these two types, so the field and its constructor parameter leave with them. Seven private helpers leave too, having no caller left behind: sameNameExistingResource, deleteRenamedResource, parseIntProp, resolveAsgTags, resolveAsgTagPropagation, resolveMixedInstancesPolicy and parseOptionalInt. The first three were shared with RDS until batch 7 took RDS out, which is why they could not move before now. Reading the name and the prior entity goes through ProvisionContext rather than a copied helper: stablePhysicalName for the create-only name, reusesPriorEntity for the update-versus-replace decision, and resolveOptional and resolveStringList for the properties. That is behaviour-preserving because the engine sets the resource's physical id and the context's priorPhysicalId from the same value on the same call. Two gaps close with the move, each proved by a test that fails without it. AWS::AutoScaling::AutoScalingGroup declares exactly one readOnlyProperty in the registry schema, AutoScalingGroupARN, but the monolith recorded the ARN only under the key "Arn". Fn::GetAtt for the documented name therefore resolved to the literal string "LogicalId.AutoScalingGroupARN" and the stack still reported CREATE_COMPLETE. Both keys are now set: "Arn" stays because templates and an existing integration test read it. AWS::AutoScaling::LaunchConfiguration declares no readOnlyProperties, so it adds no attribute surface. #3494 added DesiredCapacityType, CapacityRebalance, MaxInstanceLifetime and DefaultInstanceWarmup to AutoScalingService but left the CloudFormation path passing AsgOptionalFields.none(), noting in its own description that the wiring belonged in a per-service provisioner rather than in the monolith. All four are registry schema properties, so a template setting them was silently dropped. They are read now, and absent stays null rather than false or zero, so an update overwrites only what the template set, matching applyToExistingGroup and the Query handler. Delete keeps the monolith's behaviour and does not wrap the calls in CfnDeletes.safeDelete. AutoScalingService raises ValidationError at 28 sites and only one of them means "not found", so tolerating that code would be the catch-all AGENTS.md rules out, swallowing a real delete failure and reporting DELETE_COMPLETE over it. Tests: 8 unit tests against a mocked service, plus the existing AutoScaling integration test extended to assert the schema attribute and the four fields end to end. Reverting each change makes the matching assertions fail, checked separately because RestAssured stops at the first failed expectation and one would otherwise mask the other.
This commit is contained in:
1 parent
d8e7382d5b
commit
0f7df0fd19
6 files changed
+643
-292
No files matched your search
+2
-1
@@ -29,6 +29,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.ApiGatewa
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.ApiGatewayApiKeyCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.ApiGatewayUsagePlanCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.ApiGatewayDomainCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.AutoScalingGroupCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.AutoScalingLifecycleHookCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.AutoScalingScalingPolicyCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.BackupVaultCfnProvisioner;
|
||||
@@ -284,6 +285,7 @@ final class CfnProvisionerFixture {
|
||||
discovered.add(new ApiGatewayDomainCfnProvisioner(apiGatewayService));
|
||||
}
|
||||
if (autoScalingService != null) {
|
||||
discovered.add(new AutoScalingGroupCfnProvisioner(autoScalingService));
|
||||
discovered.add(new AutoScalingLifecycleHookCfnProvisioner(autoScalingService));
|
||||
discovered.add(new AutoScalingScalingPolicyCfnProvisioner(autoScalingService));
|
||||
}
|
||||
@@ -673,7 +675,6 @@ final class CfnProvisionerFixture {
|
||||
logsService,
|
||||
kinesisService,
|
||||
cloudWatchMetricsService,
|
||||
autoScalingService,
|
||||
firehoseService,
|
||||
cloudFrontService,
|
||||
resourceRegistry,
|
||||
|
||||
+20
-3
@@ -48,6 +48,10 @@ class CloudFormationAutoScalingIntegrationTest {
|
||||
"MaxSize": 3,
|
||||
"DesiredCapacity": 2,
|
||||
"AvailabilityZones": ["us-east-1a"],
|
||||
"DesiredCapacityType": "units",
|
||||
"CapacityRebalance": true,
|
||||
"MaxInstanceLifetime": 86400,
|
||||
"DefaultInstanceWarmup": 300,
|
||||
"Tags": [
|
||||
{"Key": "cluster", "Value": "demo", "PropagateAtLaunch": true},
|
||||
{"Key": "control-plane", "Value": "only", "PropagateAtLaunch": false}
|
||||
@@ -56,7 +60,8 @@ class CloudFormationAutoScalingIntegrationTest {
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"AsgArn": {"Value": {"Fn::GetAtt": ["Asg", "Arn"]}}
|
||||
"AsgArn": {"Value": {"Fn::GetAtt": ["Asg", "Arn"]}},
|
||||
"AsgSchemaArn": {"Value": {"Fn::GetAtt": ["Asg", "AutoScalingGroupARN"]}}
|
||||
}
|
||||
}
|
||||
""".formatted(lcName, asgName);
|
||||
@@ -83,7 +88,12 @@ class CloudFormationAutoScalingIntegrationTest {
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"))
|
||||
.body(containsString(":autoScalingGroup:" + asgName));
|
||||
.body(containsString(":autoScalingGroup:" + asgName))
|
||||
// AutoScalingGroupARN is the name the registry schema defines, so it is the one real
|
||||
// CloudFormation resolves. An unset attribute does not fail the stack: Fn::GetAtt falls
|
||||
// back to the literal "Asg.AutoScalingGroupARN", which is what this rules out.
|
||||
.body(containsString("<OutputKey>AsgSchemaArn</OutputKey>"))
|
||||
.body(not(containsString("Asg.AutoScalingGroupARN")));
|
||||
|
||||
// The group really exists in Auto Scaling and references the launch configuration.
|
||||
given()
|
||||
@@ -102,7 +112,14 @@ class CloudFormationAutoScalingIntegrationTest {
|
||||
.body(containsString("<PropagateAtLaunch>true</PropagateAtLaunch>"))
|
||||
.body(containsString("<Key>control-plane</Key>"))
|
||||
.body(containsString("<Value>only</Value>"))
|
||||
.body(containsString("<PropagateAtLaunch>false</PropagateAtLaunch>"));
|
||||
.body(containsString("<PropagateAtLaunch>false</PropagateAtLaunch>"))
|
||||
// The CloudFormation path used to pass AsgOptionalFields.none(), so a template setting
|
||||
// these four had them dropped without any error. #3494 added them to the service and
|
||||
// left the wiring to this provisioner.
|
||||
.body(containsString("<DesiredCapacityType>units</DesiredCapacityType>"))
|
||||
.body(containsString("<CapacityRebalance>true</CapacityRebalance>"))
|
||||
.body(containsString("<MaxInstanceLifetime>86400</MaxInstanceLifetime>"))
|
||||
.body(containsString("<DefaultInstanceWarmup>300</DefaultInstanceWarmup>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+253
@@ -0,0 +1,253 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
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.LaunchConfiguration;
|
||||
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.ArgumentMatchers.anyMap;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* The Auto Scaling group and launch configuration CFN provisioner in isolation, against a mocked
|
||||
* {@link AutoScalingService}.
|
||||
*/
|
||||
class AutoScalingGroupCfnProvisionerTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String GROUP = "AWS::AutoScaling::AutoScalingGroup";
|
||||
private static final String LAUNCH_CONFIGURATION = "AWS::AutoScaling::LaunchConfiguration";
|
||||
private static final String ASG_ARN =
|
||||
"arn:aws:autoscaling:us-east-1:000000000000:autoScalingGroup:uuid:autoScalingGroupName/my-asg";
|
||||
|
||||
private final AutoScalingService autoScaling = mock(AutoScalingService.class);
|
||||
private final AutoScalingGroupCfnProvisioner provisioner =
|
||||
new AutoScalingGroupCfnProvisioner(autoScaling);
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
private ProvisionContext ctx() {
|
||||
return ctx(null);
|
||||
}
|
||||
|
||||
private ProvisionContext ctx(String priorPhysicalId) {
|
||||
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
|
||||
when(engine.resolve(any())).thenAnswer(inv -> {
|
||||
JsonNode node = inv.getArgument(0);
|
||||
return node == null || node.isMissingNode() || node.isNull() ? null : node.asText();
|
||||
});
|
||||
when(engine.resolveNode(any())).thenAnswer(inv -> inv.getArgument(0));
|
||||
when(engine.resolveStringList(any())).thenAnswer(inv -> {
|
||||
JsonNode node = inv.getArgument(0);
|
||||
List<String> values = new ArrayList<>();
|
||||
if (node != null && node.isArray()) {
|
||||
node.forEach(element -> values.add(element.asText()));
|
||||
}
|
||||
return values;
|
||||
});
|
||||
return new ProvisionContext(engine, REGION, "000000000000", "my-stack", priorPhysicalId);
|
||||
}
|
||||
|
||||
private static StackResource resource(String type, String logicalId, String priorPhysicalId) {
|
||||
StackResource r = new StackResource();
|
||||
r.setLogicalId(logicalId);
|
||||
r.setResourceType(type);
|
||||
r.setPhysicalId(priorPhysicalId);
|
||||
r.setAttributes(new HashMap<>());
|
||||
return r;
|
||||
}
|
||||
|
||||
private static AutoScalingGroup group(String name) {
|
||||
AutoScalingGroup asg = new AutoScalingGroup();
|
||||
asg.setAutoScalingGroupName(name);
|
||||
asg.setAutoScalingGroupArn(ASG_ARN);
|
||||
return asg;
|
||||
}
|
||||
|
||||
@Test
|
||||
void groupCreateRecordsTheSchemaArnAttribute() {
|
||||
// AutoScalingGroupARN is the schema's only readOnlyProperty, so it is the name real
|
||||
// CloudFormation resolves. The monolith only ever wrote "Arn", which meant
|
||||
// Fn::GetAtt [Asg, AutoScalingGroupARN] fell through to the literal "Asg.AutoScalingGroupARN".
|
||||
when(autoScaling.createAutoScalingGroup(eq(REGION), eq("my-asg"), any(), any(), any(), any(),
|
||||
any(), anyInt(), anyInt(), anyInt(), anyInt(), anyList(), anyList(), anyList(), anyList(),
|
||||
any(), anyInt(), anyList(), anyMap(), anyMap(), any()))
|
||||
.thenReturn(group("my-asg"));
|
||||
StackResource r = resource(GROUP, "Asg", null);
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("AutoScalingGroupName", "my-asg")
|
||||
.put("MinSize", "1")
|
||||
.put("MaxSize", "3");
|
||||
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
assertEquals("my-asg", r.getPhysicalId());
|
||||
assertEquals(ASG_ARN, r.getAttributes().get("AutoScalingGroupARN"));
|
||||
// Kept beside it: templates and the existing integration test read this name.
|
||||
assertEquals(ASG_ARN, r.getAttributes().get("Arn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void groupCreateCarriesTheOptionalFieldsTheTemplateSet() {
|
||||
// #3494 added these four to the service but left the CloudFormation path passing
|
||||
// AsgOptionalFields.none(), so a template setting them was silently dropped.
|
||||
when(autoScaling.createAutoScalingGroup(eq(REGION), eq("my-asg"), any(), any(), any(), any(),
|
||||
any(), anyInt(), anyInt(), anyInt(), anyInt(), anyList(), anyList(), anyList(), anyList(),
|
||||
any(), anyInt(), anyList(), anyMap(), anyMap(), any()))
|
||||
.thenReturn(group("my-asg"));
|
||||
StackResource r = resource(GROUP, "Asg", null);
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("AutoScalingGroupName", "my-asg")
|
||||
.put("MinSize", "1")
|
||||
.put("MaxSize", "3")
|
||||
.put("DesiredCapacityType", "vcpu")
|
||||
.put("CapacityRebalance", "true")
|
||||
.put("MaxInstanceLifetime", "86400")
|
||||
.put("DefaultInstanceWarmup", "300");
|
||||
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
ArgumentCaptor<AsgOptionalFields> captor = ArgumentCaptor.forClass(AsgOptionalFields.class);
|
||||
verify(autoScaling).createAutoScalingGroup(eq(REGION), eq("my-asg"), any(), any(), any(), any(),
|
||||
any(), anyInt(), anyInt(), anyInt(), anyInt(), anyList(), anyList(), anyList(), anyList(),
|
||||
any(), anyInt(), anyList(), anyMap(), anyMap(), captor.capture());
|
||||
AsgOptionalFields optional = captor.getValue();
|
||||
assertEquals("vcpu", optional.desiredCapacityType());
|
||||
assertEquals(Boolean.TRUE, optional.capacityRebalance());
|
||||
assertEquals(86400, optional.maxInstanceLifetime());
|
||||
assertEquals(300, optional.defaultInstanceWarmup());
|
||||
}
|
||||
|
||||
@Test
|
||||
void groupOptionalFieldsStayNullWhenTheTemplateOmitsThem() {
|
||||
// Absent must stay null, not false or zero: applyToExistingGroup overwrites only the
|
||||
// members that are set, matching what UpdateAutoScalingGroup does on the Query path.
|
||||
when(autoScaling.createAutoScalingGroup(eq(REGION), eq("my-asg"), any(), any(), any(), any(),
|
||||
any(), anyInt(), anyInt(), anyInt(), anyInt(), anyList(), anyList(), anyList(), anyList(),
|
||||
any(), anyInt(), anyList(), anyMap(), anyMap(), any()))
|
||||
.thenReturn(group("my-asg"));
|
||||
StackResource r = resource(GROUP, "Asg", null);
|
||||
ObjectNode props = mapper.createObjectNode().put("AutoScalingGroupName", "my-asg");
|
||||
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
ArgumentCaptor<AsgOptionalFields> captor = ArgumentCaptor.forClass(AsgOptionalFields.class);
|
||||
verify(autoScaling).createAutoScalingGroup(eq(REGION), eq("my-asg"), any(), any(), any(), any(),
|
||||
any(), anyInt(), anyInt(), anyInt(), anyInt(), anyList(), anyList(), anyList(), anyList(),
|
||||
any(), anyInt(), anyList(), anyMap(), anyMap(), captor.capture());
|
||||
AsgOptionalFields optional = captor.getValue();
|
||||
assertNull(optional.desiredCapacityType());
|
||||
assertNull(optional.capacityRebalance());
|
||||
assertNull(optional.maxInstanceLifetime());
|
||||
assertNull(optional.defaultInstanceWarmup());
|
||||
}
|
||||
|
||||
@Test
|
||||
void groupUpdateReconcilesTheSameNameInsteadOfCreatingItAgain() {
|
||||
// provision() re-runs on every UpdateStack. createAutoScalingGroup throws AlreadyExists, so
|
||||
// a same-named group already on file has to go through updateAutoScalingGroup.
|
||||
when(autoScaling.describeAutoScalingGroups(REGION, List.of("my-asg")))
|
||||
.thenReturn(List.of(group("my-asg")));
|
||||
StackResource r = resource(GROUP, "Asg", "my-asg");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("AutoScalingGroupName", "my-asg")
|
||||
.put("MinSize", "2")
|
||||
.put("MaxSize", "6")
|
||||
.put("DesiredCapacityType", "units");
|
||||
|
||||
provisioner.provision(r, props, ctx("my-asg"));
|
||||
|
||||
ArgumentCaptor<AsgOptionalFields> captor = ArgumentCaptor.forClass(AsgOptionalFields.class);
|
||||
verify(autoScaling).updateAutoScalingGroup(eq(REGION), eq("my-asg"), any(), any(), any(), any(),
|
||||
any(), eq(2), eq(6), anyInt(), anyInt(), anyList(), anyList(), any(), anyInt(), anyList(),
|
||||
captor.capture());
|
||||
assertEquals("units", captor.getValue().desiredCapacityType());
|
||||
verify(autoScaling, never()).createAutoScalingGroup(anyString(), anyString(), any(), any(), any(),
|
||||
any(), any(), anyInt(), anyInt(), anyInt(), anyInt(), anyList(), anyList(), anyList(),
|
||||
anyList(), any(), anyInt(), anyList(), anyMap(), anyMap(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchConfigurationCreateSetsTheNameAsPhysicalId() {
|
||||
LaunchConfiguration lc = new LaunchConfiguration();
|
||||
lc.setLaunchConfigurationName("my-lc");
|
||||
lc.setLaunchConfigurationArn("arn:aws:autoscaling:us-east-1:000000000000:launchConfiguration:my-lc");
|
||||
when(autoScaling.createLaunchConfiguration(eq(REGION), eq("my-lc"), any(), eq("ami-1"),
|
||||
eq("t3.micro"), any(), anyList(), any(), any(), any()))
|
||||
.thenReturn(lc);
|
||||
StackResource r = resource(LAUNCH_CONFIGURATION, "Lc", null);
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("LaunchConfigurationName", "my-lc")
|
||||
.put("ImageId", "ami-1")
|
||||
.put("InstanceType", "t3.micro");
|
||||
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
assertEquals("my-lc", r.getPhysicalId());
|
||||
assertEquals("arn:aws:autoscaling:us-east-1:000000000000:launchConfiguration:my-lc",
|
||||
r.getAttributes().get("Arn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchConfigurationUpdateLeavesTheExistingOneAlone() {
|
||||
// Launch configurations have no update API on real AWS, so a same-named one already on file
|
||||
// must not be re-created.
|
||||
LaunchConfiguration lc = new LaunchConfiguration();
|
||||
lc.setLaunchConfigurationName("my-lc");
|
||||
lc.setLaunchConfigurationArn("arn:aws:autoscaling:us-east-1:000000000000:launchConfiguration:my-lc");
|
||||
when(autoScaling.describeLaunchConfigurations(REGION, List.of("my-lc"))).thenReturn(List.of(lc));
|
||||
StackResource r = resource(LAUNCH_CONFIGURATION, "Lc", "my-lc");
|
||||
ObjectNode props = mapper.createObjectNode()
|
||||
.put("LaunchConfigurationName", "my-lc")
|
||||
.put("ImageId", "ami-2");
|
||||
|
||||
provisioner.provision(r, props, ctx("my-lc"));
|
||||
|
||||
verify(autoScaling, never()).createLaunchConfiguration(anyString(), anyString(), any(), any(),
|
||||
any(), any(), anyList(), any(), any(), any());
|
||||
assertEquals("my-lc", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteRoutesEachTypeToItsOwnCall() {
|
||||
provisioner.delete(LAUNCH_CONFIGURATION, "my-lc", REGION);
|
||||
verify(autoScaling).deleteLaunchConfiguration(REGION, "my-lc");
|
||||
|
||||
provisioner.delete(GROUP, "my-asg", REGION);
|
||||
verify(autoScaling).deleteAutoScalingGroup(REGION, "my-asg", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnsupportedTypeIsRejectedRatherThanSilentlyIgnored() {
|
||||
StackResource r = resource("AWS::AutoScaling::ScalingPolicy", "Policy", null);
|
||||
ProvisionContext ctx = ctx();
|
||||
ObjectNode props = mapper.createObjectNode();
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> provisioner.provision(r, props, ctx));
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> provisioner.delete("AWS::AutoScaling::ScalingPolicy", "p", REGION));
|
||||
verify(autoScaling, never()).deleteAutoScalingGroup(anyString(), anyString(), anyBoolean());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user