Skip to content

Commit

Permalink
Merge pull request #12249 from grahamjenson/enhancement/sfn-express-s…
Browse files Browse the repository at this point in the history
…tate-machine

Enhancement: Step Functions for Express Workflows
  • Loading branch information
bill-rich authored Feb 4, 2021
2 parents 11cd8e2 + 012aa79 commit 746f48f
Show file tree
Hide file tree
Showing 3 changed files with 552 additions and 7 deletions.
107 changes: 100 additions & 7 deletions aws/resource_aws_sfn_state_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,35 @@ func resourceAwsSfnStateMachine() *schema.Resource {
ValidateFunc: validation.StringLenBetween(0, 1024*1024), // 1048576
},

"logging_configuration": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"log_destination": {
Type: schema.TypeString,
Optional: true,
},
"include_execution_data": {
Type: schema.TypeBool,
Optional: true,
},
"level": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
sfn.LogLevelAll,
sfn.LogLevelError,
sfn.LogLevelFatal,
sfn.LogLevelOff,
}, false),
},
},
},
},

"name": {
Type: schema.TypeString,
Required: true,
Expand All @@ -53,24 +82,36 @@ func resourceAwsSfnStateMachine() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsSchema(),
"arn": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: sfn.StateMachineTypeStandard,
ValidateFunc: validation.StringInSlice([]string{
sfn.StateMachineTypeStandard,
sfn.StateMachineTypeExpress,
}, false),
},
},
}
}

func resourceAwsSfnStateMachineCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sfnconn
log.Print("[DEBUG] Creating Step Function State Machine")

params := &sfn.CreateStateMachineInput{
Definition: aws.String(d.Get("definition").(string)),
Name: aws.String(d.Get("name").(string)),
RoleArn: aws.String(d.Get("role_arn").(string)),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().SfnTags(),
Definition: aws.String(d.Get("definition").(string)),
LoggingConfiguration: expandAwsSfnLoggingConfiguration(d.Get("logging_configuration").([]interface{})),
Name: aws.String(d.Get("name").(string)),
RoleArn: aws.String(d.Get("role_arn").(string)),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().SfnTags(),
Type: aws.String(d.Get("type").(string)),
}

var stateMachine *sfn.CreateStateMachineOutput
Expand Down Expand Up @@ -114,7 +155,6 @@ func resourceAwsSfnStateMachineRead(d *schema.ResourceData, meta interface{}) er
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

log.Printf("[DEBUG] Reading Step Function State Machine: %s", d.Id())

sm, err := conn.DescribeStateMachine(&sfn.DescribeStateMachineInput{
StateMachineArn: aws.String(d.Id()),
})
Expand All @@ -132,8 +172,15 @@ func resourceAwsSfnStateMachineRead(d *schema.ResourceData, meta interface{}) er
d.Set("definition", sm.Definition)
d.Set("name", sm.Name)
d.Set("role_arn", sm.RoleArn)
d.Set("type", sm.Type)
d.Set("status", sm.Status)

loggingConfiguration := flattenAwsSfnLoggingConfiguration(sm.LoggingConfiguration)

if err := d.Set("logging_configuration", loggingConfiguration); err != nil {
log.Printf("[DEBUG] Error setting logging_configuration %s", err)
}

if err := d.Set("creation_date", sm.CreationDate.Format(time.RFC3339)); err != nil {
log.Printf("[DEBUG] Error setting creation_date: %s", err)
}
Expand All @@ -154,13 +201,17 @@ func resourceAwsSfnStateMachineRead(d *schema.ResourceData, meta interface{}) er
func resourceAwsSfnStateMachineUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sfnconn

if d.HasChanges("definition", "role_arn") {
if d.HasChanges("definition", "role_arn", "logging_configuration") {
params := &sfn.UpdateStateMachineInput{
StateMachineArn: aws.String(d.Id()),
Definition: aws.String(d.Get("definition").(string)),
RoleArn: aws.String(d.Get("role_arn").(string)),
}

if d.HasChange("logging_configuration") {
params.LoggingConfiguration = expandAwsSfnLoggingConfiguration(d.Get("logging_configuration").([]interface{}))
}

_, err := conn.UpdateStateMachine(params)

log.Printf("[DEBUG] Updating Step Function State Machine: %#v", params)
Expand Down Expand Up @@ -206,3 +257,45 @@ func resourceAwsSfnStateMachineDelete(d *schema.ResourceData, meta interface{})

return nil
}

func expandAwsSfnLoggingConfiguration(l []interface{}) *sfn.LoggingConfiguration {

if len(l) == 0 || l[0] == nil {
return nil
}

m := l[0].(map[string]interface{})

loggingConfiguration := &sfn.LoggingConfiguration{
Destinations: []*sfn.LogDestination{
{
CloudWatchLogsLogGroup: &sfn.CloudWatchLogsLogGroup{
LogGroupArn: aws.String(m["log_destination"].(string)),
},
},
},
IncludeExecutionData: aws.Bool(m["include_execution_data"].(bool)),
Level: aws.String(m["level"].(string)),
}

return loggingConfiguration
}

func flattenAwsSfnLoggingConfiguration(loggingConfiguration *sfn.LoggingConfiguration) []interface{} {

if loggingConfiguration == nil {
return []interface{}{}
}

m := map[string]interface{}{
"log_destination": "",
"include_execution_data": aws.BoolValue(loggingConfiguration.IncludeExecutionData),
"level": aws.StringValue(loggingConfiguration.Level),
}

if len(loggingConfiguration.Destinations) > 0 {
m["log_destination"] = aws.StringValue(loggingConfiguration.Destinations[0].CloudWatchLogsLogGroup.LogGroupArn)
}

return []interface{}{m}
}
Loading

0 comments on commit 746f48f

Please sign in to comment.