-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Data Source: aws_sfn_activity (#11080)
Output from acceptance testing: ``` --- PASS: TestAccAWSStepFunctionsActivityDataSource (13.22s) ```
- Loading branch information
Showing
5 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |