mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cloudformation): publish EC2 Instance State via Fn::GetAtt (#4203)
The registry schema lists State as a read-only property of AWS::EC2::Instance, but it is a nested object with Code and Name, and Fn::GetAtt exposes only the dotted members: cfn-lint's GetAtt derivation for this type yields State.Code and State.Name and no bare State, the same shape as an RDS Endpoint. Floci never published either, so a template reading them got the literal "Server.State.Name" back. Ec2InstanceCfnProvisioner now publishes State.Code (the code as a string, the schema's type) and State.Name, removing both when the instance carries no state, and the create path re-reads the instance once the launch settles, as CloudFormation's own stabilization does. Without the re-read a mock-mode instance would publish "pending", since it only reaches "running" on the next DescribeInstances. The State gap row now records the dotted form, like the RDS Endpoint rows. Tests: Ec2InstanceCfnProvisionerTest pins both keys, the settled state and the no-state case; CloudFormationEc2InstanceIntegrationTest asserts both GetAtt forms against the state DescribeInstances reports.
This commit is contained in:
1 parent
fcc89c0e8d
commit
024c2cbe27
4 files changed
+75
-6
No files matched your search
+14
-1
@@ -147,7 +147,10 @@ public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner {
|
||||
r.getAttributes().put(CfnRollback.ROLLBACK_OWNED_ATTR, "true");
|
||||
ec2Service.awaitContainerLaunch(instance);
|
||||
r.getAttributes().remove(CfnRollback.ROLLBACK_OWNED_ATTR);
|
||||
publishInstanceAttributes(r, instance);
|
||||
// Re-read once the launch settles, as CloudFormation's own stabilization does, so the
|
||||
// published State is the settled instance's rather than the pending one RunInstances returned.
|
||||
Instance launched = findInstance(region, instance.getInstanceId());
|
||||
publishInstanceAttributes(r, launched != null ? launched : instance);
|
||||
storeDeclaredCreateOnly(r, privateIpAddress, availabilityZone);
|
||||
// A createOnly change that landed a new instance id replaced the prior one: record it so
|
||||
// the stack cleans the displaced instance up after the update commits.
|
||||
@@ -306,6 +309,16 @@ public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner {
|
||||
} else {
|
||||
r.getAttributes().remove("VpcId");
|
||||
}
|
||||
// The schema's State is a nested object, so Fn::GetAtt exposes its members in dotted form
|
||||
// (State.Code, State.Name), the same shape as an RDS Endpoint; there is no bare State.
|
||||
InstanceState state = instance.getState();
|
||||
if (state != null && state.getName() != null) {
|
||||
r.getAttributes().put("State.Code", String.valueOf(state.getCode()));
|
||||
r.getAttributes().put("State.Name", state.getName());
|
||||
} else {
|
||||
r.getAttributes().remove("State.Code");
|
||||
r.getAttributes().remove("State.Name");
|
||||
}
|
||||
}
|
||||
|
||||
/** Reconciles the instance tags to the template when the instance is kept in place. */
|
||||
|
||||
+23
-3
@@ -8,6 +8,7 @@ import java.util.regex.Pattern;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@@ -39,7 +40,9 @@ class CloudFormationEc2InstanceIntegrationTest {
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"InstanceId": {"Value": {"Ref": "Server"}}
|
||||
"InstanceId": {"Value": {"Ref": "Server"}},
|
||||
"StateName": {"Value": {"Fn::GetAtt": ["Server", "State.Name"]}},
|
||||
"StateCode": {"Value": {"Fn::GetAtt": "Server.State.Code"}}
|
||||
}
|
||||
}
|
||||
""";
|
||||
@@ -73,7 +76,7 @@ class CloudFormationEc2InstanceIntegrationTest {
|
||||
String instanceId = m.group(1);
|
||||
|
||||
// The instance really exists in EC2.
|
||||
given()
|
||||
String describeInstances = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", EC2_AUTH)
|
||||
.formParam("Action", "DescribeInstances")
|
||||
@@ -82,7 +85,24 @@ class CloudFormationEc2InstanceIntegrationTest {
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString(instanceId));
|
||||
.body(containsString(instanceId))
|
||||
.extract().asString();
|
||||
|
||||
// The schema's State is a nested object, so Fn::GetAtt exposes State.Code and State.Name:
|
||||
// the state the launch settled to, as DescribeInstances reports it, in both GetAtt forms.
|
||||
Matcher state = Pattern.compile("<instanceState>\\s*<code>(\\d+)</code>\\s*<name>([a-z-]+)</name>")
|
||||
.matcher(describeInstances);
|
||||
assertTrue(state.find(), "expected an instance state in DescribeInstances");
|
||||
assertEquals("running", state.group(2));
|
||||
assertEquals(state.group(2), output(describeStacks, "StateName"));
|
||||
assertEquals(state.group(1), output(describeStacks, "StateCode"));
|
||||
}
|
||||
|
||||
private static String output(String describeStacks, String key) {
|
||||
Matcher m = Pattern.compile("<OutputKey>" + key + "</OutputKey>\\s*<OutputValue>([^<]*)</OutputValue>")
|
||||
.matcher(describeStacks);
|
||||
assertTrue(m.find(), "expected output " + key + " in " + describeStacks);
|
||||
return m.group(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+37
-1
@@ -63,6 +63,7 @@ class Ec2InstanceCfnProvisionerTest {
|
||||
instance.setPublicDnsName("ec2-54-0-0-5.compute-1.amazonaws.com");
|
||||
instance.setPlacement(new Placement("us-east-1a"));
|
||||
instance.setVpcId("vpc-1");
|
||||
instance.setState(InstanceState.running());
|
||||
stubLaunch(instance);
|
||||
StackResource r = resource("Server");
|
||||
|
||||
@@ -70,13 +71,48 @@ class Ec2InstanceCfnProvisionerTest {
|
||||
|
||||
assertEquals("i-1", r.getPhysicalId());
|
||||
assertEquals(Set.of("InstanceId", "PrivateIp", "PublicIp", "PrivateDnsName", "PublicDnsName",
|
||||
"AvailabilityZone", "VpcId"), r.getAttributes().keySet());
|
||||
"AvailabilityZone", "VpcId", "State.Code", "State.Name"), r.getAttributes().keySet());
|
||||
assertEquals("10.0.0.5", r.getAttributes().get("PrivateIp"));
|
||||
assertEquals("us-east-1a", r.getAttributes().get("AvailabilityZone"));
|
||||
assertEquals("vpc-1", r.getAttributes().get("VpcId"));
|
||||
assertEquals("16", r.getAttributes().get("State.Code"));
|
||||
assertEquals("running", r.getAttributes().get("State.Name"));
|
||||
verify(ec2).awaitContainerLaunch(instance);
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchPublishesTheStateTheLaunchSettledTo() throws Exception {
|
||||
Instance pending = new Instance();
|
||||
pending.setInstanceId("i-1");
|
||||
pending.setState(InstanceState.pending());
|
||||
stubLaunch(pending);
|
||||
Instance running = new Instance();
|
||||
running.setInstanceId("i-1");
|
||||
running.setState(InstanceState.running());
|
||||
when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
|
||||
.thenReturn(List.of(reservationOf(running)));
|
||||
StackResource r = resource("Server");
|
||||
|
||||
provisioner.provision(r, props("{\"ImageId\": \"ami-1\"}"), ctx());
|
||||
|
||||
assertEquals("16", r.getAttributes().get("State.Code"));
|
||||
assertEquals("running", r.getAttributes().get("State.Name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInstanceWithoutAStateExposesNoStateAttributes() throws Exception {
|
||||
Instance instance = new Instance();
|
||||
instance.setInstanceId("i-stateless");
|
||||
stubLaunch(instance);
|
||||
StackResource r = resource("Server");
|
||||
r.getAttributes().put("State.Code", "80");
|
||||
r.getAttributes().put("State.Name", "stopped");
|
||||
|
||||
provisioner.provision(r, props("{\"ImageId\": \"ami-1\"}"), ctx());
|
||||
|
||||
assertEquals(Set.of("InstanceId"), r.getAttributes().keySet());
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchTemplateFillsWhatThePropertiesLeaveUnset() throws Exception {
|
||||
LaunchTemplateData data = new LaunchTemplateData();
|
||||
|
||||
@@ -2,7 +2,7 @@ AWS::AppSync::GraphQLApi GraphQLEndpointArn the private-endpoint ARN of the Grap
|
||||
AWS::CodeBuild::Project Id schema gives no description; AWS's Id may differ from the project name Floci uses
|
||||
AWS::CodePipeline::CustomActionType Id schema gives no description; the custom action has no separate id in Floci
|
||||
AWS::CodePipeline::Webhook Id schema gives no description; Floci keys webhooks by name
|
||||
AWS::EC2::Instance State not published yet: Floci tracks the instance state, setting it is a separate change to what Fn::GetAtt returns
|
||||
AWS::EC2::Instance State the dotted State.Code and State.Name are set instead, the form AWS's GetAtt exposes for the nested State object
|
||||
AWS::EC2::NatGateway AutoProvisionZones availability-mode NAT gateways are not emulated
|
||||
AWS::EC2::NatGateway AutoScalingIps availability-mode NAT gateways are not emulated
|
||||
AWS::EC2::NatGateway EniId the NAT gateway's network interface is not created
|
||||
|
||||
|
Reference in new issue
Block a user