diff --git a/aws/data_source_aws_sfn_activity.go b/aws/data_source_aws_sfn_activity.go new file mode 100644 index 00000000000..e7c47928f43 --- /dev/null +++ b/aws/data_source_aws_sfn_activity.go @@ -0,0 +1,108 @@ +package aws + +import ( + "fmt" + "log" + "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/sfn" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func dataSourceAwsSfnActivity() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsSfnActivityRead, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ExactlyOneOf: []string{ + "arn", + "name", + }, + }, + "arn": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ExactlyOneOf: []string{ + "arn", + "name", + }, + }, + "creation_date": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceAwsSfnActivityRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*AWSClient) + conn := client.sfnconn + log.Print("[DEBUG] Reading Step Function Activity") + + if nm, ok := d.GetOk("name"); ok { + name := nm.(string) + var acts []*sfn.ActivityListItem + + err := conn.ListActivitiesPages(&sfn.ListActivitiesInput{}, func(page *sfn.ListActivitiesOutput, b bool) bool { + for _, a := range page.Activities { + if name == aws.StringValue(a.Name) { + acts = append(acts, a) + } + } + return true + }) + + if err != nil { + return fmt.Errorf("Error listing activities: %s", err) + } + + if len(acts) == 0 { + return fmt.Errorf("No activity found with name %s in this region", name) + } + + if len(acts) > 1 { + return fmt.Errorf("Found more than 1 activity with name %s in this region", name) + } + + act := acts[0] + + d.SetId(*act.ActivityArn) + d.Set("name", act.Name) + d.Set("arn", act.ActivityArn) + if err := d.Set("creation_date", act.CreationDate.Format(time.RFC3339)); err != nil { + log.Printf("[DEBUG] Error setting creation_date: %s", err) + } + } + + if rnm, ok := d.GetOk("arn"); ok { + arn := rnm.(string) + params := &sfn.DescribeActivityInput{ + ActivityArn: aws.String(arn), + } + + act, err := conn.DescribeActivity(params) + if err != nil { + return fmt.Errorf("Error describing activities: %s", err) + } + + if act == nil { + return fmt.Errorf("No activity found with arn %s in this region", arn) + } + + d.SetId(*act.ActivityArn) + d.Set("name", act.Name) + d.Set("arn", act.ActivityArn) + if err := d.Set("creation_date", act.CreationDate.Format(time.RFC3339)); err != nil { + log.Printf("[DEBUG] Error setting creation_date: %s", err) + } + } + + return nil +} diff --git a/aws/data_source_aws_sfn_activity_test.go b/aws/data_source_aws_sfn_activity_test.go new file mode 100644 index 00000000000..5c7bf2174b2 --- /dev/null +++ b/aws/data_source_aws_sfn_activity_test.go @@ -0,0 +1,60 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccAWSStepFunctionsActivityDataSource(t *testing.T) { + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_sfn_activity.test" + dataName := "data.aws_sfn_activity.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckAWSStepFunctionsActivityDataSourceConfig_ActivityArn(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair(resourceName, "id", dataName, "id"), + resource.TestCheckResourceAttrPair(resourceName, "creation_date", dataName, "creation_date"), + resource.TestCheckResourceAttrPair(resourceName, "name", dataName, "name"), + ), + }, + { + Config: testAccCheckAWSStepFunctionsActivityDataSourceConfig_ActivityName(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair(resourceName, "id", dataName, "id"), + resource.TestCheckResourceAttrPair(resourceName, "creation_date", dataName, "creation_date"), + resource.TestCheckResourceAttrPair(resourceName, "name", dataName, "name"), + ), + }, + }, + }) +} + +func testAccCheckAWSStepFunctionsActivityDataSourceConfig_ActivityArn(rName string) string { + return fmt.Sprintf(` +resource aws_sfn_activity "test" { + name = "%s" +} +data aws_sfn_activity "test" { + arn = "${aws_sfn_activity.test.id}" +} +`, rName) +} + +func testAccCheckAWSStepFunctionsActivityDataSourceConfig_ActivityName(rName string) string { + return fmt.Sprintf(` +resource aws_sfn_activity "test" { + name = "%s" +} +data aws_sfn_activity "test" { + name = "${aws_sfn_activity.test.name}" +} +`, rName) +} diff --git a/aws/provider.go b/aws/provider.go index fd7c3570330..25892a84de7 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -281,6 +281,7 @@ func Provider() terraform.ResourceProvider { "aws_secretsmanager_secret_version": dataSourceAwsSecretsManagerSecretVersion(), "aws_servicequotas_service": dataSourceAwsServiceQuotasService(), "aws_servicequotas_service_quota": dataSourceAwsServiceQuotasServiceQuota(), + "aws_sfn_activity": dataSourceAwsSfnActivity(), "aws_sfn_state_machine": dataSourceAwsSfnStateMachine(), "aws_sns_topic": dataSourceAwsSnsTopic(), "aws_sqs_queue": dataSourceAwsSqsQueue(), diff --git a/website/aws.erb b/website/aws.erb index 4b09c1836d9..449853fd0be 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -2986,6 +2986,9 @@
  • Data Sources