Skip to content

armgit / floci

Public Imported from GitHub
0

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

feat(cloudformation): provision AWS::StepFunctions::StateMachine (#1152)

Luke Walkera89121c6258e
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java
index 7e49d2175e..1d7150559e 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java
@@ -44,6 +44,8 @@ import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
import io.github.hectorvent.floci.services.sns.SnsService;
import io.github.hectorvent.floci.services.sqs.SqsService;
import io.github.hectorvent.floci.services.ssm.SsmService;
+import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService;
+import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine;
import io.github.hectorvent.floci.services.apigateway.ApiGatewayService;
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
import io.github.hectorvent.floci.services.apigatewayv2.model.*;
@@ -98,6 +100,7 @@ public class CloudFormationResourceProvisioner {
private final CognitoService cognitoService;
private final EcsService ecsService;
private final ElbV2Service elbV2Service;
+ private final StepFunctionsService stepFunctionsService;
@Inject
public CloudFormationResourceProvisioner(S3Service s3Service, SqsService sqsService,
@@ -112,7 +115,8 @@ public class CloudFormationResourceProvisioner {
PipesService pipesService,
CognitoService cognitoService,
EcsService ecsService,
- ElbV2Service elbV2Service) {
+ ElbV2Service elbV2Service,
+ StepFunctionsService stepFunctionsService) {
this.s3Service = s3Service;
this.sqsService = sqsService;
this.snsService = snsService;
@@ -130,6 +134,7 @@ public class CloudFormationResourceProvisioner {
this.cognitoService = cognitoService;
this.ecsService = ecsService;
this.elbV2Service = elbV2Service;
+ this.stepFunctionsService = stepFunctionsService;
}
/**
@@ -197,6 +202,8 @@ public class CloudFormationResourceProvisioner {
case "AWS::ApiGatewayV2::Stage" -> provisionApiGatewayV2Stage(resource, properties, engine, region);
case "AWS::ApiGatewayV2::Deployment" -> provisionApiGatewayV2Deployment(resource, properties, engine, region);
case "AWS::Pipes::Pipe" -> provisionPipe(resource, properties, engine, region, stackName);
+ case "AWS::StepFunctions::StateMachine" ->
+ provisionStepFunctionsStateMachine(resource, properties, engine, region, stackName);
case "AWS::Lambda::EventSourceMapping" ->
provisionLambdaEventSourceMapping(resource, properties, engine, region);
case "AWS::Cognito::UserPool" ->
@@ -253,6 +260,7 @@ public class CloudFormationResourceProvisioner {
case "AWS::ECR::Repository" ->
ecrService.deleteRepository(physicalId, null, true, region);
case "AWS::Pipes::Pipe" -> pipesService.deletePipe(physicalId, region);
+ case "AWS::StepFunctions::StateMachine" -> stepFunctionsService.deleteStateMachine(physicalId);
case "AWS::Lambda::EventSourceMapping" -> lambdaService.deleteEventSourceMapping(physicalId);
case "AWS::Cognito::UserPool" -> cognitoService.deleteUserPool(physicalId);
case "AWS::Cognito::UserPoolClient" -> cognitoService.deleteUserPoolClient(physicalId);
@@ -1358,6 +1366,56 @@ public class CloudFormationResourceProvisioner {
r.getAttributes().put("Arn", pipe.getArn());
}
+ private void provisionStepFunctionsStateMachine(StackResource r, JsonNode props,
+ CloudFormationTemplateEngine engine,
+ String region, String stackName) {
+ String name = resolveOptional(props, "StateMachineName", engine);
+ if (name == null || name.isBlank()) {
+ name = generatePhysicalName(stackName, r.getLogicalId(), 80, false);
+ }
+
+ String roleArn = resolveOptional(props, "RoleArn", engine);
+ String type = resolveOptional(props, "StateMachineType", engine);
+ Map<String, String> tags = parseCfnTags(props != null ? props.get("Tags") : null, engine);
+
+ String definition = resolveStateMachineDefinition(props, engine);
+
+ StateMachine sm = stepFunctionsService.createStateMachine(name, definition, roleArn, type, region, tags);
+
+ r.setPhysicalId(sm.getStateMachineArn());
+ r.getAttributes().put("Arn", sm.getStateMachineArn());
+ r.getAttributes().put("Name", sm.getName());
+ }
+
+ private String resolveStateMachineDefinition(JsonNode props, CloudFormationTemplateEngine engine) {
+ if (props == null) {
+ return null;
+ }
+
+ String definition = resolveOptional(props, "DefinitionString", engine);
+ if (definition == null && props.has("Definition") && !props.get("Definition").isNull()) {
+ definition = engine.resolveNode(props.get("Definition")).toString();
+ }
+ if (definition == null) {
+ return null;
+ }
+
+ JsonNode subsNode = props.get("DefinitionSubstitutions");
+ if (subsNode == null || subsNode.isNull()) {
+ return definition;
+ }
+
+ JsonNode resolvedSubs = engine.resolveNode(subsNode);
+ Iterator<Map.Entry<String, JsonNode>> entries = resolvedSubs.fields();
+ while (entries.hasNext()) {
+ Map.Entry<String, JsonNode> entry = entries.next();
+ String placeholder = "${" + entry.getKey() + "}";
+ String value = entry.getValue().isTextual() ? entry.getValue().asText() : entry.getValue().toString();
+ definition = definition.replace(placeholder, value);
+ }
+ return definition;
+ }
+
// ── Helpers ───────────────────────────────────────────────────────────────
private void provisionCdkMetadata(StackResource r) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
index b39f663642..07d8ad9cca 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
@@ -5635,6 +5635,108 @@ class CloudFormationIntegrationTest {
.statusCode(200);
}
+ private static final String SFN_CONTENT_TYPE = "application/x-amz-json-1.0";
+
+ @Test
+ void createStack_stepFunctionsStateMachineResolvesDefinitionSubstitutionsAndIsDeletable() {
+ String template = """
+ {
+ "Resources": {
+ "MyRole": {
+ "Type": "AWS::IAM::Role",
+ "Properties": {
+ "RoleName": "cfn-sfn-role",
+ "AssumeRolePolicyDocument": {
+ "Version": "2012-10-17",
+ "Statement": [{
+ "Effect": "Allow",
+ "Principal": { "Service": "states.amazonaws.com" },
+ "Action": "sts:AssumeRole"
+ }]
+ }
+ }
+ },
+ "MyStateMachine": {
+ "Type": "AWS::StepFunctions::StateMachine",
+ "Properties": {
+ "StateMachineName": "cfn-sfn-pipeline",
+ "RoleArn": { "Fn::GetAtt": ["MyRole", "Arn"] },
+ "DefinitionString": "{\\"StartAt\\":\\"Done\\",\\"States\\":{\\"Done\\":{\\"Type\\":\\"Pass\\",\\"Result\\":\\"${Marker}\\",\\"End\\":true}}}",
+ "DefinitionSubstitutions": {
+ "Marker": "substituted-value"
+ },
+ "Tags": [
+ { "Key": "env", "Value": "test" }
+ ]
+ }
+ }
+ },
+ "Outputs": {
+ "StateMachineArn": { "Value": { "Ref": "MyStateMachine" } },
+ "StateMachineName": { "Value": { "Fn::GetAtt": ["MyStateMachine", "Name"] } }
+ }
+ }
+ """;
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", "sfn-cfn-stack")
+ .formParam("TemplateBody", template)
+ .formParam("Capabilities.member.1", "CAPABILITY_NAMED_IAM")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ String expectedArn = "arn:aws:states:us-east-1:000000000000:stateMachine:cfn-sfn-pipeline";
+
+ given()
+ .header("X-Amz-Target", "AWSStepFunctions.DescribeStateMachine")
+ .contentType(SFN_CONTENT_TYPE)
+ .body("{\"stateMachineArn\":\"" + expectedArn + "\"}")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("name", equalTo("cfn-sfn-pipeline"))
+ .body("roleArn", containsString("cfn-sfn-role"))
+ .body("definition", containsString("substituted-value"))
+ .body("definition", not(containsString("${Marker}")));
+
+ String describeXml = given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", "sfn-cfn-stack")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract().asString();
+ assertThat(describeXml, containsString("<OutputKey>StateMachineArn</OutputKey>"));
+ assertThat(describeXml, containsString("<OutputValue>" + expectedArn + "</OutputValue>"));
+ assertThat(describeXml, containsString("<OutputKey>StateMachineName</OutputKey>"));
+ assertThat(describeXml, containsString("<OutputValue>cfn-sfn-pipeline</OutputValue>"));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DeleteStack")
+ .formParam("StackName", "sfn-cfn-stack")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .header("X-Amz-Target", "AWSStepFunctions.DescribeStateMachine")
+ .contentType(SFN_CONTENT_TYPE)
+ .body("{\"stateMachineArn\":\"" + expectedArn + "\"}")
+ .when()
+ .post("/")
+ .then()
+ .body("__type", containsString("StateMachineDoesNotExist"));
+ }
+
// ── Issue #924: roll back failed stack creates (criterion #9) ────────────
@Test