Skip to content

Commit

Permalink
Merge pull request #32844 from hashicorp/b-aws_sfn_state_machine.publish
Browse files Browse the repository at this point in the history
r/aws_sfn_state_machine: Always set `publish` in resource Read
  • Loading branch information
ewbankkit authored Aug 3, 2023
2 parents 388225b + d68c88c commit c00070c
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 68 deletions.
3 changes: 3 additions & 0 deletions .changelog/32844.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_sfn_state_machine: Fix `Provider produced inconsistent final plan` errors for `publish`
```
1 change: 1 addition & 0 deletions internal/service/sfn/generate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/listpages/main.go -ListOps=ListStateMachineVersions
//go:generate go run ../../generate/tags/main.go -ListTags -ServiceTagsSlice -UpdateTags
//go:generate go run ../../generate/servicepackage/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.
Expand Down
28 changes: 28 additions & 0 deletions internal/service/sfn/list_pages_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 19 additions & 5 deletions internal/service/sfn/state_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ func resourceStateMachineCreate(ctx context.Context, d *schema.ResourceData, met

arn := aws.StringValue(outputRaw.(*sfn.CreateStateMachineOutput).StateMachineArn)
d.SetId(arn)
d.Set("state_machine_version_arn", outputRaw.(*sfn.CreateStateMachineOutput).StateMachineVersionArn)

return resourceStateMachineRead(ctx, d, meta)
}
Expand Down Expand Up @@ -228,7 +227,6 @@ func resourceStateMachineRead(ctx context.Context, d *schema.ResourceData, meta
}
d.Set("definition", output.Definition)
d.Set("description", output.Description)

if output.LoggingConfiguration != nil {
if err := d.Set("logging_configuration", []interface{}{flattenLoggingConfiguration(output.LoggingConfiguration)}); err != nil {
return diag.Errorf("setting logging_configuration: %s", err)
Expand All @@ -238,6 +236,7 @@ func resourceStateMachineRead(ctx context.Context, d *schema.ResourceData, meta
}
d.Set("name", output.Name)
d.Set("name_prefix", create.NamePrefixFromName(aws.StringValue(output.Name)))
d.Set("publish", d.Get("publish").(bool))
d.Set("role_arn", output.RoleArn)
d.Set("revision_id", output.RevisionId)
d.Set("status", output.Status)
Expand All @@ -250,6 +249,23 @@ func resourceStateMachineRead(ctx context.Context, d *schema.ResourceData, meta
}
d.Set("type", output.Type)

input := &sfn.ListStateMachineVersionsInput{
StateMachineArn: aws.String(d.Id()),
}
listVersionsOutput, err := conn.ListStateMachineVersionsWithContext(ctx, input)

if err != nil {
return diag.Errorf("listing Step Functions State Machine (%s) Versions: %s", d.Id(), err)
}

// The results are sorted in descending order of the version creation time.
// https://docs.aws.amazon.com/step-functions/latest/apireference/API_ListStateMachineVersions.html
if len(listVersionsOutput.StateMachineVersions) > 0 {
d.Set("state_machine_version_arn", listVersionsOutput.StateMachineVersions[0].StateMachineVersionArn)
} else {
d.Set("state_machine_version_arn", nil)
}

return nil
}

Expand Down Expand Up @@ -281,7 +297,7 @@ func resourceStateMachineUpdate(ctx context.Context, d *schema.ResourceData, met
}
}

out, err := conn.UpdateStateMachineWithContext(ctx, input)
_, err := conn.UpdateStateMachineWithContext(ctx, input)

if err != nil {
return diag.Errorf("updating Step Functions State Machine (%s): %s", d.Id(), err)
Expand Down Expand Up @@ -310,8 +326,6 @@ func resourceStateMachineUpdate(ctx context.Context, d *schema.ResourceData, met
if err != nil {
return diag.Errorf("waiting for Step Functions State Machine (%s) update: %s", d.Id(), err)
}

d.Set("state_machine_version_arn", out.StateMachineVersionArn)
}

return resourceStateMachineRead(ctx, d, meta)
Expand Down
90 changes: 57 additions & 33 deletions internal/service/sfn/state_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"regexp"
"strings"
"testing"

"github.com/aws/aws-sdk-go/service/sfn"
Expand Down Expand Up @@ -35,7 +36,7 @@ func TestAccSFNStateMachine_createUpdate(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccStateMachineConfig_basic(rName, 5),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckExists(ctx, resourceName, &sm),
acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "states", fmt.Sprintf("stateMachine:%s", rName)),
resource.TestCheckResourceAttr(resourceName, "status", sfn.StateMachineStatusActive),
Expand All @@ -50,21 +51,22 @@ func TestAccSFNStateMachine_createUpdate(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "logging_configuration.0.level", "OFF"),
resource.TestCheckResourceAttr(resourceName, "logging_configuration.0.log_destination", ""),
resource.TestCheckResourceAttr(resourceName, "publish", "false"),
resource.TestCheckResourceAttr(resourceName, "revision_id", ""),
resource.TestCheckResourceAttr(resourceName, "state_machine_version_arn", ""),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttr(resourceName, "tracing_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "tracing_configuration.0.enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "type", "STANDARD"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"publish", "state_machine_version_arn"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccStateMachineConfig_basic(rName, 10),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckExists(ctx, resourceName, &sm),
acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "states", fmt.Sprintf("stateMachine:%s", rName)),
resource.TestCheckResourceAttr(resourceName, "status", sfn.StateMachineStatusActive),
Expand All @@ -77,6 +79,7 @@ func TestAccSFNStateMachine_createUpdate(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "logging_configuration.0.level", "OFF"),
resource.TestCheckResourceAttr(resourceName, "logging_configuration.0.log_destination", ""),
resource.TestCheckResourceAttr(resourceName, "publish", "false"),
resource.TestCheckResourceAttr(resourceName, "state_machine_version_arn", ""),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttr(resourceName, "tracing_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "tracing_configuration.0.enabled", "false"),
Expand All @@ -101,26 +104,41 @@ func TestAccSFNStateMachine_expressUpdate(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccStateMachineConfig_typed(rName, "EXPRESS", 5),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckExists(ctx, resourceName, &sm),
resource.TestCheckResourceAttr(resourceName, "status", sfn.StateMachineStatusActive),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttrSet(resourceName, "creation_date"),
resource.TestCheckResourceAttrSet(resourceName, "definition"),
resource.TestMatchResourceAttr(resourceName, "definition", regexp.MustCompile(`.*\"MaxAttempts\": 5.*`)),
resource.TestCheckResourceAttr(resourceName, "revision_id", ""),
resource.TestCheckResourceAttrSet(resourceName, "role_arn"),
resource.TestCheckResourceAttrWith(resourceName, "state_machine_version_arn", func(value string) error {
if !strings.HasSuffix(value, ":1") {
return fmt.Errorf("incorrect version number: %s", value)
}

return nil
}),
resource.TestCheckResourceAttr(resourceName, "type", "EXPRESS"),
),
},
{
Config: testAccStateMachineConfig_typed(rName, "EXPRESS", 10),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckExists(ctx, resourceName, &sm),
resource.TestCheckResourceAttr(resourceName, "status", sfn.StateMachineStatusActive),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttrSet(resourceName, "creation_date"),
resource.TestMatchResourceAttr(resourceName, "definition", regexp.MustCompile(`.*\"MaxAttempts\": 10.*`)),
resource.TestCheckResourceAttrSet(resourceName, "role_arn"),
resource.TestCheckResourceAttrWith(resourceName, "state_machine_version_arn", func(value string) error {
if !strings.HasSuffix(value, ":2") {
return fmt.Errorf("incorrect version number: %s", value)
}

return nil
}),
resource.TestCheckResourceAttr(resourceName, "type", "EXPRESS"),
),
},
Expand All @@ -142,34 +160,43 @@ func TestAccSFNStateMachine_standardUpdate(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccStateMachineConfig_typed(rName, "STANDARD", 5),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckExists(ctx, resourceName, &sm),
resource.TestCheckResourceAttr(resourceName, "status", sfn.StateMachineStatusActive),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttrSet(resourceName, "creation_date"),
resource.TestCheckResourceAttrSet(resourceName, "definition"),
resource.TestMatchResourceAttr(resourceName, "definition", regexp.MustCompile(`.*\"MaxAttempts\": 5.*`)),
resource.TestCheckResourceAttr(resourceName, "publish", "true"),
//resource.TestCheckResourceAttrSet(resourceName, "version_description"),
//resource.TestCheckResourceAttrSet(resourceName, "revision_id"),
//resource.TestCheckResourceAttrSet(resourceName, "state_machine_version_arn"),
resource.TestCheckResourceAttr(resourceName, "revision_id", ""),
resource.TestCheckResourceAttrSet(resourceName, "role_arn"),
resource.TestCheckResourceAttrWith(resourceName, "state_machine_version_arn", func(value string) error {
if !strings.HasSuffix(value, ":1") {
return fmt.Errorf("incorrect version number: %s", value)
}

return nil
}),
resource.TestCheckResourceAttr(resourceName, "type", "STANDARD"),
),
},
{
Config: testAccStateMachineConfig_typed(rName, "STANDARD", 10),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckExists(ctx, resourceName, &sm),
resource.TestCheckResourceAttr(resourceName, "status", sfn.StateMachineStatusActive),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttrSet(resourceName, "creation_date"),
resource.TestMatchResourceAttr(resourceName, "definition", regexp.MustCompile(`.*\"MaxAttempts\": 10.*`)),
resource.TestCheckResourceAttr(resourceName, "publish", "true"),
//resource.TestCheckResourceAttrSet(resourceName, "version_description"),
//resource.TestCheckResourceAttrSet(resourceName, "revision_id"),
//resource.TestCheckResourceAttrSet(resourceName, "state_machine_version_arn"),
resource.TestCheckResourceAttrSet(resourceName, "role_arn"),
resource.TestCheckResourceAttrWith(resourceName, "state_machine_version_arn", func(value string) error {
if !strings.HasSuffix(value, ":2") {
return fmt.Errorf("incorrect version number: %s", value)
}

return nil
}),
resource.TestCheckResourceAttr(resourceName, "type", "STANDARD"),
),
},
Expand Down Expand Up @@ -198,10 +225,9 @@ func TestAccSFNStateMachine_nameGenerated(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"publish", "state_machine_version_arn"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand All @@ -228,10 +254,9 @@ func TestAccSFNStateMachine_namePrefix(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"publish", "state_machine_version_arn"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand All @@ -254,13 +279,14 @@ func TestAccSFNStateMachine_publish(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckExists(ctx, resourceName, &sm),
resource.TestCheckResourceAttr(resourceName, "publish", "true"),
resource.TestCheckResourceAttrSet(resourceName, "state_machine_version_arn"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"publish", "state_machine_version_arn"},
ImportStateVerifyIgnore: []string{"publish"},
},
},
})
Expand All @@ -287,10 +313,9 @@ func TestAccSFNStateMachine_tags(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"publish", "state_machine_version_arn"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccStateMachineConfig_tags2(rName, "key1", "value1updated", "key2", "value2"),
Expand Down Expand Up @@ -334,10 +359,9 @@ func TestAccSFNStateMachine_tracing(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"publish", "state_machine_version_arn"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccStateMachineConfig_tracingEnable(rName),
Expand Down
Loading

0 comments on commit c00070c

Please sign in to comment.