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

RDS Cluster Endpoint tagging support #11074

Merged
merged 1 commit into from
Jan 17, 2020
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
24 changes: 23 additions & 1 deletion aws/resource_aws_rds_cluster_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

const (
Expand Down Expand Up @@ -70,6 +71,7 @@ func resourceAwsRDSClusterEndpoint() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
Expand All @@ -85,6 +87,7 @@ func resourceAwsRDSClusterEndpointCreate(d *schema.ResourceData, meta interface{
DBClusterIdentifier: aws.String(clusterId),
DBClusterEndpointIdentifier: aws.String(endpointId),
EndpointType: aws.String(endpointType),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().RdsTags(),
}

if v := d.Get("static_members"); v != nil {
Expand Down Expand Up @@ -140,9 +143,10 @@ func resourceAwsRDSClusterEndpointRead(d *schema.ResourceData, meta interface{})
return nil
}

arn := clusterEp.DBClusterEndpointArn
d.Set("cluster_endpoint_identifier", clusterEp.DBClusterEndpointIdentifier)
d.Set("cluster_identifier", clusterEp.DBClusterIdentifier)
d.Set("arn", clusterEp.DBClusterEndpointArn)
d.Set("arn", arn)
d.Set("endpoint", clusterEp.Endpoint)
d.Set("custom_endpoint_type", clusterEp.CustomEndpointType)

Expand All @@ -154,6 +158,16 @@ func resourceAwsRDSClusterEndpointRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("error setting static_members: %s", err)
}

tags, err := keyvaluetags.RdsListTags(conn, *arn)

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

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

return nil
}

Expand All @@ -163,6 +177,14 @@ func resourceAwsRDSClusterEndpointUpdate(d *schema.ResourceData, meta interface{
DBClusterEndpointIdentifier: aws.String(d.Id()),
}

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

if err := keyvaluetags.RdsUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating RDS Cluster Endpoint (%s) tags: %s", d.Get("arn").(string), err)
}
}

if v, ok := d.GetOk("custom_endpoint_type"); ok {
input.EndpointType = aws.String(v.(string))
}
Expand Down
97 changes: 90 additions & 7 deletions aws/resource_aws_rds_cluster_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func TestAccAWSRDSClusterEndpoint_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(readerResourceName, "endpoint"),
testAccMatchResourceAttrRegionalARN(defaultResourceName, "arn", "rds", regexp.MustCompile(`cluster-endpoint:.+`)),
resource.TestCheckResourceAttrSet(defaultResourceName, "endpoint"),
resource.TestCheckResourceAttr(defaultResourceName, "tags.%", "0"),
resource.TestCheckResourceAttr(readerResourceName, "tags.%", "0"),
),
},
{
Expand All @@ -55,6 +57,50 @@ func TestAccAWSRDSClusterEndpoint_basic(t *testing.T) {
})
}

func TestAccAWSRDSClusterEndpoint_tags(t *testing.T) {
rInt := acctest.RandInt()
var customReaderEndpoint rds.DBClusterEndpoint
resourceName := "aws_rds_cluster_endpoint.reader"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSClusterEndpointDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSClusterEndpointConfigTags1(rInt, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRDSClusterEndpointExists(resourceName, &customReaderEndpoint),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSClusterEndpointConfigTags2(rInt, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRDSClusterEndpointExists(resourceName, &customReaderEndpoint),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccAWSClusterEndpointConfigTags1(rInt, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRDSClusterEndpointExists(resourceName, &customReaderEndpoint),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
})
}

func testAccCheckAWSRDSClusterEndpointAttributes(v *rds.DBClusterEndpoint) resource.TestCheckFunc {
return func(s *terraform.State) error {

Expand Down Expand Up @@ -160,10 +206,10 @@ func testAccCheckAWSRDSClusterEndpointExistsWithProvider(resourceName string, en
}
}

func testAccAWSClusterEndpointConfig(n int) string {
func testAccAWSClusterEndpointConfigBase(n int) string {
return fmt.Sprintf(`
resource "aws_rds_cluster" "default" {
cluster_identifier = "tf-aurora-cluster-%d"
cluster_identifier = "tf-aurora-cluster-%[1]d"
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
database_name = "mydb"
master_username = "foo"
Expand All @@ -175,31 +221,68 @@ resource "aws_rds_cluster" "default" {
resource "aws_rds_cluster_instance" "test1" {
apply_immediately = true
cluster_identifier = "${aws_rds_cluster.default.id}"
identifier = "tf-aurora-cluster-instance-test1-%d"
identifier = "tf-aurora-cluster-instance-test1-%[1]d"
instance_class = "db.t2.small"
}

resource "aws_rds_cluster_instance" "test2" {
apply_immediately = true
cluster_identifier = "${aws_rds_cluster.default.id}"
identifier = "tf-aurora-cluster-instance-test2-%d"
identifier = "tf-aurora-cluster-instance-test2-%[1]d"
instance_class = "db.t2.small"
}
`, n)
}

func testAccAWSClusterEndpointConfig(n int) string {
return testAccAWSClusterEndpointConfigBase(n) + fmt.Sprintf(`
resource "aws_rds_cluster_endpoint" "reader" {
cluster_identifier = "${aws_rds_cluster.default.id}"
cluster_endpoint_identifier = "reader-%d"
cluster_endpoint_identifier = "reader-%[1]d"
custom_endpoint_type = "READER"

static_members = ["${aws_rds_cluster_instance.test2.id}"]
}

resource "aws_rds_cluster_endpoint" "default" {
cluster_identifier = "${aws_rds_cluster.default.id}"
cluster_endpoint_identifier = "default-%d"
cluster_endpoint_identifier = "default-%[1]d"
custom_endpoint_type = "ANY"

excluded_members = ["${aws_rds_cluster_instance.test2.id}"]
}
`, n, n, n, n, n)
`, n)
}

func testAccAWSClusterEndpointConfigTags1(n int, tagKey1, tagValue1 string) string {
return testAccAWSClusterEndpointConfigBase(n) + fmt.Sprintf(`
resource "aws_rds_cluster_endpoint" "reader" {
cluster_identifier = "${aws_rds_cluster.default.id}"
cluster_endpoint_identifier = "reader-%[1]d"
custom_endpoint_type = "READER"

static_members = ["${aws_rds_cluster_instance.test2.id}"]

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

func testAccAWSClusterEndpointConfigTags2(n int, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return testAccAWSClusterEndpointConfigBase(n) + fmt.Sprintf(`
resource "aws_rds_cluster_endpoint" "reader" {
cluster_identifier = "${aws_rds_cluster.default.id}"
cluster_endpoint_identifier = "reader-%[1]d"
custom_endpoint_type = "READER"

static_members = ["${aws_rds_cluster_instance.test2.id}"]

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