Skip to content

Commit

Permalink
Merge pull request #27574 from hashicorp/td-aws_sfn-without-timeout-c…
Browse files Browse the repository at this point in the history
…rud-handlers

r/aws_sfn_state_machine: Add `name_prefix` argument
  • Loading branch information
ewbankkit authored Nov 1, 2022
2 parents 7157a5a + 532b45f commit c127533
Show file tree
Hide file tree
Showing 20 changed files with 653 additions and 551 deletions.
3 changes: 3 additions & 0 deletions .changelog/27574.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_sfn_state_machine: Add `name_prefix` argument
```
13 changes: 2 additions & 11 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1780,18 +1780,9 @@ func ConfigLatestAmazonLinux2HVMEBSARM64AMI() string {
}

func ConfigLambdaBase(policyName, roleName, sgName string) string {
return fmt.Sprintf(`
return ConfigCompose(ConfigAvailableAZsNoOptIn(), fmt.Sprintf(`
data "aws_partition" "current" {}
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_iam_role_policy" "iam_policy_for_lambda" {
name = "%s"
role = aws_iam_role.iam_for_lambda.id
Expand Down Expand Up @@ -1912,7 +1903,7 @@ resource "aws_security_group" "sg_for_lambda" {
cidr_blocks = ["0.0.0.0/0"]
}
}
`, policyName, roleName, sgName)
`, policyName, roleName, sgName))
}

func ConfigVPCWithSubnets(rName string, subnetCount int) string {
Expand Down
138 changes: 81 additions & 57 deletions internal/service/sfn/activity.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
package sfn

import (
"fmt"
"context"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/sfn"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func ResourceActivity() *schema.Resource {
return &schema.Resource{
Create: resourceActivityCreate,
Read: resourceActivityRead,
Update: resourceActivityUpdate,
Delete: resourceActivityDelete,
CreateWithoutTimeout: resourceActivityCreate,
ReadWithoutTimeout: resourceActivityRead,
UpdateWithoutTimeout: resourceActivityUpdate,
DeleteWithoutTimeout: resourceActivityDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(0, 80),
},

"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
},
Expand All @@ -45,97 +48,118 @@ func ResourceActivity() *schema.Resource {
}
}

func resourceActivityCreate(d *schema.ResourceData, meta interface{}) error {
func resourceActivityCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).SFNConn
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))
log.Print("[DEBUG] Creating Step Function Activity")

params := &sfn.CreateActivityInput{
Name: aws.String(d.Get("name").(string)),
name := d.Get("name").(string)
input := &sfn.CreateActivityInput{
Name: aws.String(name),
Tags: Tags(tags.IgnoreAWS()),
}

activity, err := conn.CreateActivity(params)
output, err := conn.CreateActivityWithContext(ctx, input)

if err != nil {
return fmt.Errorf("Error creating Step Function Activity: %s", err)
return diag.Errorf("creating Step Functions Activity (%s): %s", name, err)
}

d.SetId(aws.StringValue(activity.ActivityArn))
d.SetId(aws.StringValue(output.ActivityArn))

return resourceActivityRead(d, meta)
return resourceActivityRead(ctx, d, meta)
}

func resourceActivityUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).SFNConn

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")
if err := UpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}

return resourceActivityRead(d, meta)
}

func resourceActivityRead(d *schema.ResourceData, meta interface{}) error {
func resourceActivityRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).SFNConn
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

log.Printf("[DEBUG] Reading Step Function Activity: %s", d.Id())
output, err := FindActivityByARN(ctx, conn, d.Id())

sm, err := conn.DescribeActivity(&sfn.DescribeActivityInput{
ActivityArn: aws.String(d.Id()),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "ActivityDoesNotExist" {
d.SetId("")
return nil
}
return err
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Step Functions Activity (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

d.Set("name", sm.Name)

if err := d.Set("creation_date", sm.CreationDate.Format(time.RFC3339)); err != nil {
log.Printf("[DEBUG] Error setting creation_date: %s", err)
if err != nil {
return diag.Errorf("reading Step Functions Activity (%s): %s", d.Id(), err)
}

tags, err := ListTags(conn, d.Id())
d.Set("creation_date", output.CreationDate.Format(time.RFC3339))
d.Set("name", output.Name)

tags, err := ListTagsWithContext(ctx, conn, d.Id())

if err != nil {
return fmt.Errorf("error listing tags for SFN Activity (%s): %s", d.Id(), err)
return diag.Errorf("listing tags for Step Functions Activity (%s): %s", d.Id(), err)
}

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
return diag.Errorf("setting tags: %s", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return fmt.Errorf("error setting tags_all: %w", err)
return diag.Errorf("setting tags_all: %s", err)
}

return nil
}

func resourceActivityDelete(d *schema.ResourceData, meta interface{}) error {
func resourceActivityUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).SFNConn
log.Printf("[DEBUG] Deleting Step Functions Activity: %s", d.Id())

input := &sfn.DeleteActivityInput{
ActivityArn: aws.String(d.Id()),
if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := UpdateTagsWithContext(ctx, conn, d.Id(), o, n); err != nil {
return diag.Errorf("updating Step Functions Activity (%s) tags: %s", d.Id(), err)
}
}

_, err := conn.DeleteActivity(input)
return resourceActivityRead(ctx, d, meta)
}

func resourceActivityDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).SFNConn

log.Printf("[DEBUG] Deleting Step Functions Activity: %s", d.Id())
_, err := conn.DeleteActivityWithContext(ctx, &sfn.DeleteActivityInput{
ActivityArn: aws.String(d.Id()),
})

if err != nil {
return fmt.Errorf("Error deleting SFN Activity: %s", err)
return diag.Errorf("deleting Step Functions Activity (%s): %s", d.Id(), err)
}

return nil
}

func FindActivityByARN(ctx context.Context, conn *sfn.SFN, arn string) (*sfn.DescribeActivityOutput, error) {
input := &sfn.DescribeActivityInput{
ActivityArn: aws.String(arn),
}

output, err := conn.DescribeActivityWithContext(ctx, input)

if tfawserr.ErrCodeEquals(err, sfn.ErrCodeActivityDoesNotExist) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil || output.CreationDate == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output, nil
}
Loading

0 comments on commit c127533

Please sign in to comment.