Skip to content

Commit

Permalink
New Data Source: aws_sfn_activity (#11080)
Browse files Browse the repository at this point in the history
Output from acceptance testing:

```
--- PASS: TestAccAWSStepFunctionsActivityDataSource (13.22s)
```
  • Loading branch information
slizco authored Feb 26, 2020
1 parent 5d7731e commit e3039f4
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 0 deletions.
108 changes: 108 additions & 0 deletions aws/data_source_aws_sfn_activity.go
Original file line number Diff line number Diff line change
@@ -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
}
60 changes: 60 additions & 0 deletions aws/data_source_aws_sfn_activity_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2986,6 +2986,9 @@
<li>
<a href="#">Data Sources</a>
<ul class="nav nav-auto-expand">
<li>
<a href="/docs/providers/aws/d/sfn_activity.html">aws_sfn_activity</a>
</li>
<li>
<a href="/docs/providers/aws/d/sfn_state_machine.html">aws_sfn_state_machine</a>
</li>
Expand Down
33 changes: 33 additions & 0 deletions website/docs/d/sfn_activity.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
subcategory: "Step Function (SFN)"
layout: "aws"
page_title: "AWS: aws_sfn_activity"
description: |-
Use this data source to get information about a Step Functions Activity.
---

# Data Source: aws_sfn_activity

Provides a Step Functions Activity data source

## Example Usage

```hcl
data "aws_sfn_activity" "sfn_activity" {
name = "my-activity"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Optional) The name that identifies the activity.
* `arn` - (Optional) The Amazon Resource Name (ARN) that identifies the activity.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The Amazon Resource Name (ARN) that identifies the activity.
* `creation_date` - The date the activity was created.

0 comments on commit e3039f4

Please sign in to comment.