Skip to content

Commit

Permalink
service/servicediscovery: AWS Cloud Map resource tagging (#13750)
Browse files Browse the repository at this point in the history
* r/aws_service_discovery_http_namespace: Add support for resource tagging.

* r/aws_service_discovery_private_dns_namespace: Add support for resource tagging.

* r/aws_service_discovery_public_dns_namespace: Add support for resource tagging.

* r/aws_service_discovery_service: Add support for resource tagging.
  • Loading branch information
ewbankkit committed Jun 15, 2020
1 parent 1baf958 commit 2183d75
Show file tree
Hide file tree
Showing 12 changed files with 659 additions and 97 deletions.
31 changes: 30 additions & 1 deletion aws/resource_aws_service_discovery_http_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"github.com/aws/aws-sdk-go/service/servicediscovery"
"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"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/servicediscovery/waiter"
)

func resourceAwsServiceDiscoveryHttpNamespace() *schema.Resource {
return &schema.Resource{
Create: resourceAwsServiceDiscoveryHttpNamespaceCreate,
Read: resourceAwsServiceDiscoveryHttpNamespaceRead,
Update: resourceAwsServiceDiscoveryHttpNamespaceUpdate,
Delete: resourceAwsServiceDiscoveryHttpNamespaceDelete,

Importer: &schema.ResourceImporter{
Expand All @@ -32,6 +34,7 @@ func resourceAwsServiceDiscoveryHttpNamespace() *schema.Resource {
Optional: true,
ForceNew: true,
},
"tags": tagsSchema(),
"arn": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -47,6 +50,7 @@ func resourceAwsServiceDiscoveryHttpNamespaceCreate(d *schema.ResourceData, meta

input := &servicediscovery.CreateHttpNamespaceInput{
Name: aws.String(name),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().ServicediscoveryTags(),
CreatorRequestId: aws.String(resource.UniqueId()),
}

Expand Down Expand Up @@ -87,6 +91,7 @@ func resourceAwsServiceDiscoveryHttpNamespaceCreate(d *schema.ResourceData, meta

func resourceAwsServiceDiscoveryHttpNamespaceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sdconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

input := &servicediscovery.GetNamespaceInput{
Id: aws.String(d.Id()),
Expand All @@ -101,13 +106,37 @@ func resourceAwsServiceDiscoveryHttpNamespaceRead(d *schema.ResourceData, meta i
return fmt.Errorf("error reading Service Discovery HTTP Namespace (%s): %s", d.Id(), err)
}

arn := aws.StringValue(resp.Namespace.Arn)
d.Set("name", resp.Namespace.Name)
d.Set("description", resp.Namespace.Description)
d.Set("arn", resp.Namespace.Arn)
d.Set("arn", arn)

tags, err := keyvaluetags.ServicediscoveryListTags(conn, arn)

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

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

return nil
}

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

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if err := keyvaluetags.ServicediscoveryUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating Service Discovery HTTP Namespace (%s) tags: %s", d.Id(), err)
}
}

return resourceAwsServiceDiscoveryHttpNamespaceRead(d, meta)
}

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

Expand Down
98 changes: 95 additions & 3 deletions aws/resource_aws_service_discovery_http_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -105,9 +106,11 @@ func TestAccAWSServiceDiscoveryHttpNamespace_basic(t *testing.T) {
Config: testAccServiceDiscoveryHttpNamespaceConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsServiceDiscoveryHttpNamespaceExists(resourceName),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "servicediscovery", regexp.MustCompile(`namespace/.+`)),
resource.TestCheckResourceAttrSet(resourceName, "arn"),
resource.TestCheckResourceAttr(resourceName, "description", ""),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
Expand All @@ -119,6 +122,27 @@ func TestAccAWSServiceDiscoveryHttpNamespace_basic(t *testing.T) {
})
}

func TestAccAWSServiceDiscoveryHttpNamespace_disappears(t *testing.T) {
resourceName := "aws_service_discovery_http_namespace.test"
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSServiceDiscovery(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsServiceDiscoveryHttpNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccServiceDiscoveryHttpNamespaceConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsServiceDiscoveryHttpNamespaceExists(resourceName),
testAccCheckResourceDisappears(testAccProvider, resourceAwsServiceDiscoveryHttpNamespace(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSServiceDiscoveryHttpNamespace_Description(t *testing.T) {
resourceName := "aws_service_discovery_http_namespace.test"
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
Expand All @@ -144,6 +168,49 @@ func TestAccAWSServiceDiscoveryHttpNamespace_Description(t *testing.T) {
})
}

func TestAccAWSServiceDiscoveryHttpNamespace_Tags(t *testing.T) {
resourceName := "aws_service_discovery_http_namespace.test"
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSServiceDiscovery(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsServiceDiscoveryHttpNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccServiceDiscoveryHttpNamespaceConfigTags1(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsServiceDiscoveryHttpNamespaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccServiceDiscoveryHttpNamespaceConfigTags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsServiceDiscoveryHttpNamespaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccServiceDiscoveryHttpNamespaceConfigTags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsServiceDiscoveryHttpNamespaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
})
}

func testAccCheckAwsServiceDiscoveryHttpNamespaceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).sdconn

Expand Down Expand Up @@ -188,16 +255,41 @@ func testAccCheckAwsServiceDiscoveryHttpNamespaceExists(name string) resource.Te
func testAccServiceDiscoveryHttpNamespaceConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_service_discovery_http_namespace" "test" {
name = %q
name = %[1]q
}
`, rName)
}

func testAccServiceDiscoveryHttpNamespaceConfigDescription(rName, description string) string {
return fmt.Sprintf(`
resource "aws_service_discovery_http_namespace" "test" {
description = %q
name = %q
description = %[1]q
name = %[2]q
}
`, description, rName)
}

func testAccServiceDiscoveryHttpNamespaceConfigTags1(rName, tagKey1, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_service_discovery_http_namespace" "test" {
name = %[1]q
tags = {
%[2]q = %[3]q
}
}
`, rName, tagKey1, tagValue1)
}

func testAccServiceDiscoveryHttpNamespaceConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_service_discovery_http_namespace" "test" {
name = %[1]q
tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}
32 changes: 31 additions & 1 deletion aws/resource_aws_service_discovery_private_dns_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"github.com/aws/aws-sdk-go/service/servicediscovery"
"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"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/servicediscovery/waiter"
)

func resourceAwsServiceDiscoveryPrivateDnsNamespace() *schema.Resource {
return &schema.Resource{
Create: resourceAwsServiceDiscoveryPrivateDnsNamespaceCreate,
Read: resourceAwsServiceDiscoveryPrivateDnsNamespaceRead,
Update: resourceAwsServiceDiscoveryPrivateDnsNamespaceUpdate,
Delete: resourceAwsServiceDiscoveryPrivateDnsNamespaceDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
Expand Down Expand Up @@ -44,6 +46,7 @@ func resourceAwsServiceDiscoveryPrivateDnsNamespace() *schema.Resource {
Required: true,
ForceNew: true,
},
"tags": tagsSchema(),
"arn": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -70,6 +73,7 @@ func resourceAwsServiceDiscoveryPrivateDnsNamespaceCreate(d *schema.ResourceData

input := &servicediscovery.CreatePrivateDnsNamespaceInput{
Name: aws.String(name),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().ServicediscoveryTags(),
Vpc: aws.String(d.Get("vpc").(string)),
CreatorRequestId: aws.String(requestId),
}
Expand Down Expand Up @@ -111,6 +115,7 @@ func resourceAwsServiceDiscoveryPrivateDnsNamespaceCreate(d *schema.ResourceData

func resourceAwsServiceDiscoveryPrivateDnsNamespaceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sdconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

input := &servicediscovery.GetNamespaceInput{
Id: aws.String(d.Id()),
Expand All @@ -125,15 +130,40 @@ func resourceAwsServiceDiscoveryPrivateDnsNamespaceRead(d *schema.ResourceData,
return err
}

arn := aws.StringValue(resp.Namespace.Arn)
d.Set("description", resp.Namespace.Description)
d.Set("arn", resp.Namespace.Arn)
d.Set("arn", arn)
d.Set("name", resp.Namespace.Name)
if resp.Namespace.Properties != nil {
d.Set("hosted_zone", resp.Namespace.Properties.DnsProperties.HostedZoneId)
}

tags, err := keyvaluetags.ServicediscoveryListTags(conn, arn)

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

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

return nil
}

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

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if err := keyvaluetags.ServicediscoveryUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating Service Discovery Private DNS Namespace (%s) tags: %s", d.Id(), err)
}
}

return resourceAwsServiceDiscoveryHttpNamespaceRead(d, meta)
}

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

Expand Down
Loading

0 comments on commit 2183d75

Please sign in to comment.