Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/swf_domain - add tagging support #10763

Merged
merged 9 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions aws/resource_aws_swf_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"github.com/aws/aws-sdk-go/service/swf"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsSwfDomain() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSwfDomainCreate,
Read: resourceAwsSwfDomainRead,
Update: resourceAwsSwfDomainUpdate,
Delete: resourceAwsSwfDomainDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand Down Expand Up @@ -52,6 +54,11 @@ func resourceAwsSwfDomain() *schema.Resource {
return
},
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
Expand All @@ -72,6 +79,7 @@ func resourceAwsSwfDomainCreate(d *schema.ResourceData, meta interface{}) error
input := &swf.RegisterDomainInput{
Name: aws.String(name),
WorkflowExecutionRetentionPeriodInDays: aws.String(d.Get("workflow_execution_retention_period_in_days").(string)),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().SwfTags(),
}

if v, ok := d.GetOk("description"); ok {
Expand Down Expand Up @@ -111,13 +119,39 @@ func resourceAwsSwfDomainRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

arn := *resp.DomainInfo.Arn
tags, err := keyvaluetags.SwfListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for SWF Domain (%s): %s", arn, err)
}

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

d.Set("arn", resp.DomainInfo.Arn)
d.Set("name", resp.DomainInfo.Name)
d.Set("description", resp.DomainInfo.Description)
d.Set("workflow_execution_retention_period_in_days", resp.Configuration.WorkflowExecutionRetentionPeriodInDays)

return nil
}

func resourceAwsSwfDomainUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).swfconn

if d.HasChange("tags") {
o, n := d.GetChange("tags")

if err := keyvaluetags.SwfUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating SWF Domain (%s) tags: %s", d.Id(), err)
}
}

return resourceAwsSwfDomainRead(d, meta)
}

func resourceAwsSwfDomainDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).swfconn

Expand Down
82 changes: 78 additions & 4 deletions aws/resource_aws_swf_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestAccAWSSwfDomain_basic(t *testing.T) {
Config: testAccAWSSwfDomainConfig_Name(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAwsSwfDomainExists(resourceName),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "swf", regexp.MustCompile(`/domain/.+`)),
resource.TestCheckResourceAttr(resourceName, "description", ""),
resource.TestCheckResourceAttr(resourceName, "name", rName),
),
Expand All @@ -51,6 +52,52 @@ func TestAccAWSSwfDomain_basic(t *testing.T) {
})
}

func TestAccAWSSwfDomain_tags(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_swf_domain.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckSwfDomainTestingEnabled(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsSwfDomainDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSwfDomainConfigTags1(rName, "key1", "value1"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAwsSwfDomainExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSSwfDomainConfigTags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAwsSwfDomainExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccAWSSwfDomainConfigTags1(rName, "key2", "value2"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAwsSwfDomainExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
})
}

func TestAccAWSSwfDomain_NamePrefix(t *testing.T) {
resourceName := "aws_swf_domain.test"

Expand Down Expand Up @@ -151,8 +198,8 @@ func testAccCheckAwsSwfDomainDestroy(s *terraform.State) error {
return err
}

if *resp.DomainInfo.Status != "DEPRECATED" {
return fmt.Errorf(`SWF Domain %s status is %s instead of "DEPRECATED". Failing!`, name, *resp.DomainInfo.Status)
if *resp.DomainInfo.Status != swf.RegistrationStatusDeprecated {
return fmt.Errorf(`SWF Domain %s status is %s instead of %s. Failing!`, name, *resp.DomainInfo.Status, swf.RegistrationStatusDeprecated)
}
}

Expand Down Expand Up @@ -182,8 +229,8 @@ func testAccCheckAwsSwfDomainExists(n string) resource.TestCheckFunc {
return fmt.Errorf("SWF Domain %s not found in AWS", name)
}

if *resp.DomainInfo.Status != "REGISTERED" {
return fmt.Errorf(`SWF Domain %s status is %s instead of "REGISTERED". Failing!`, name, *resp.DomainInfo.Status)
if *resp.DomainInfo.Status != swf.RegistrationStatusRegistered {
return fmt.Errorf(`SWF Domain %s status is %s instead of %s. Failing!`, name, *resp.DomainInfo.Status, swf.RegistrationStatusRegistered)
}
return nil
}
Expand Down Expand Up @@ -214,6 +261,33 @@ resource "aws_swf_domain" "test" {
`, rName)
}

func testAccAWSSwfDomainConfigTags1(rName, tagKey1, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_swf_domain" "test" {
name = %[1]q
workflow_execution_retention_period_in_days = 1

tags = {
%[2]q = %[3]q
}
}
`, rName, tagKey1, tagValue1)
}

func testAccAWSSwfDomainConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_swf_domain" "test" {
name = %[1]q
workflow_execution_retention_period_in_days = 1

tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}

const testAccAWSSwfDomainConfig_NamePrefix = `
resource "aws_swf_domain" "test" {
name_prefix = "tf-acc-test"
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/swf_domain.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ The following arguments are supported:
* `name_prefix` - (Optional, Forces new resource) Creates a unique name beginning with the specified prefix. Conflicts with `name`.
* `description` - (Optional, Forces new resource) The domain description.
* `workflow_execution_retention_period_in_days` - (Required, Forces new resource) Length of time that SWF will continue to retain information about the workflow execution after the workflow execution is complete, must be between 0 and 90 days.
* `tags` - (Optional) Key-value mapping of resource tags

## Attributes Reference

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

* `id` - The name of the domain.
* `arn` - Amazon Resource Name (ARN)

## Import

Expand Down