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

service/rds: Initial support for RDS Aurora Global Databases #6861

Merged
merged 3 commits into from
Dec 19, 2018
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
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ func Provider() terraform.ResourceProvider {
"aws_rds_cluster_endpoint": resourceAwsRDSClusterEndpoint(),
"aws_rds_cluster_instance": resourceAwsRDSClusterInstance(),
"aws_rds_cluster_parameter_group": resourceAwsRDSClusterParameterGroup(),
"aws_rds_global_cluster": resourceAwsRDSGlobalCluster(),
"aws_redshift_cluster": resourceAwsRedshiftCluster(),
"aws_redshift_security_group": resourceAwsRedshiftSecurityGroup(),
"aws_redshift_parameter_group": resourceAwsRedshiftParameterGroup(),
Expand Down
66 changes: 66 additions & 0 deletions aws/resource_aws_rds_cluster.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"errors"
"fmt"
"log"
"regexp"
Expand Down Expand Up @@ -106,6 +107,11 @@ func resourceAwsRDSCluster() *schema.Resource {
Computed: true,
},

"global_cluster_identifier": {
Type: schema.TypeString,
Optional: true,
},

"reader_endpoint": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -130,6 +136,7 @@ func resourceAwsRDSCluster() *schema.Resource {
ForceNew: true,
Default: "provisioned",
ValidateFunc: validation.StringInSlice([]string{
"global",
"parallelquery",
"provisioned",
"serverless",
Expand Down Expand Up @@ -750,6 +757,10 @@ func resourceAwsRDSClusterCreate(d *schema.ResourceData, meta interface{}) error
createOpts.EngineVersion = aws.String(attr.(string))
}

if attr, ok := d.GetOk("global_cluster_identifier"); ok {
createOpts.GlobalClusterIdentifier = aws.String(attr.(string))
}

if attr := d.Get("vpc_security_group_ids").(*schema.Set); attr.Len() > 0 {
createOpts.VpcSecurityGroupIds = expandStringList(attr.List())
}
Expand Down Expand Up @@ -975,6 +986,21 @@ func resourceAwsRDSClusterRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[WARN] Failed to save tags for RDS Cluster (%s): %s", aws.StringValue(dbc.DBClusterIdentifier), err)
}

// Fetch and save Global Cluster if engine mode global
d.Set("global_cluster_identifier", "")

if aws.StringValue(dbc.EngineMode) == "global" {
globalCluster, err := rdsDescribeGlobalClusterFromDbClusterARN(conn, aws.StringValue(dbc.DBClusterArn))

if err != nil {
return fmt.Errorf("error reading RDS Global Cluster information for DB Cluster (%s): %s", d.Id(), err)
}

if globalCluster != nil {
d.Set("global_cluster_identifier", globalCluster.GlobalClusterIdentifier)
}
}

return nil
}

Expand Down Expand Up @@ -1084,6 +1110,30 @@ func resourceAwsRDSClusterUpdate(d *schema.ResourceData, meta interface{}) error
}
}

if d.HasChange("global_cluster_identifier") {
oRaw, nRaw := d.GetChange("global_cluster_identifier")
o := oRaw.(string)
n := nRaw.(string)

if o == "" {
return errors.New("Existing RDS Clusters cannot be added to an existing RDS Global Cluster")
}

if n != "" {
return errors.New("Existing RDS Clusters cannot be migrated between existing RDS Global Clusters")
}

input := &rds.RemoveFromGlobalClusterInput{
DbClusterIdentifier: aws.String(d.Get("arn").(string)),
GlobalClusterIdentifier: aws.String(o),
}

log.Printf("[DEBUG] Removing RDS Cluster from RDS Global Cluster: %s", input)
if _, err := conn.RemoveFromGlobalCluster(input); err != nil {
return fmt.Errorf("error removing RDS Cluster (%s) from RDS Global Cluster: %s", d.Id(), err)
}
}

if d.HasChange("iam_roles") {
oraw, nraw := d.GetChange("iam_roles")
if oraw == nil {
Expand Down Expand Up @@ -1128,6 +1178,22 @@ func resourceAwsRDSClusterDelete(d *schema.ResourceData, meta interface{}) error
conn := meta.(*AWSClient).rdsconn
log.Printf("[DEBUG] Destroying RDS Cluster (%s)", d.Id())

// Automatically remove from global cluster to bypass this error on deletion:
// InvalidDBClusterStateFault: This cluster is a part of a global cluster, please remove it from globalcluster first
if d.Get("global_cluster_identifier").(string) != "" {
input := &rds.RemoveFromGlobalClusterInput{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this same chunk of code is used in update, I'd suggest having it in it's own function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling is slightly different. 😄 I'm not sure if the indirection is worthwhile to fill in a two value struct, print a log line, and call the API, but we can certainly discuss this out of band! 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good with me!

DbClusterIdentifier: aws.String(d.Get("arn").(string)),
GlobalClusterIdentifier: aws.String(d.Get("global_cluster_identifier").(string)),
}

log.Printf("[DEBUG] Removing RDS Cluster from RDS Global Cluster: %s", input)
_, err := conn.RemoveFromGlobalCluster(input)

if err != nil && !isAWSErr(err, rds.ErrCodeGlobalClusterNotFoundFault, "") {
return fmt.Errorf("error removing RDS Cluster (%s) from RDS Global Cluster: %s", d.Id(), err)
}
}

deleteOpts := rds.DeleteDBClusterInput{
DBClusterIdentifier: aws.String(d.Id()),
}
Expand Down
190 changes: 190 additions & 0 deletions aws/resource_aws_rds_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestAccAWSRDSCluster_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "cluster_resource_id"),
resource.TestCheckResourceAttr(resourceName, "engine", "aurora"),
resource.TestCheckResourceAttrSet(resourceName, "engine_version"),
resource.TestCheckResourceAttr(resourceName, "global_cluster_identifier", ""),
resource.TestCheckResourceAttrSet(resourceName, "hosted_zone_id"),
resource.TestCheckResourceAttr(resourceName,
"enabled_cloudwatch_logs_exports.0", "audit"),
Expand Down Expand Up @@ -529,6 +530,40 @@ func TestAccAWSRDSCluster_EngineMode(t *testing.T) {
})
}

func TestAccAWSRDSCluster_EngineMode_Global(t *testing.T) {
var dbCluster1 rds.DBCluster

rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_rds_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSRDSClusterConfig_EngineMode(rName, "global"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterExists(resourceName, &dbCluster1),
resource.TestCheckResourceAttr(resourceName, "engine_mode", "global"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"apply_immediately",
"cluster_identifier_prefix",
"master_password",
"skip_final_snapshot",
"snapshot_identifier",
},
},
},
})
}

func TestAccAWSRDSCluster_EngineMode_ParallelQuery(t *testing.T) {
var dbCluster1 rds.DBCluster

Expand Down Expand Up @@ -619,6 +654,125 @@ func TestAccAWSRDSCluster_EngineVersionWithPrimaryInstance(t *testing.T) {
})
}

func TestAccAWSRDSCluster_GlobalClusterIdentifier(t *testing.T) {
var dbCluster1 rds.DBCluster

rName := acctest.RandomWithPrefix("tf-acc-test")
globalClusterResourceName := "aws_rds_global_cluster.test"
resourceName := "aws_rds_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSRDSClusterConfig_GlobalClusterIdentifier(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterExists(resourceName, &dbCluster1),
resource.TestCheckResourceAttrPair(resourceName, "global_cluster_identifier", globalClusterResourceName, "id"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"apply_immediately",
"cluster_identifier_prefix",
"master_password",
"skip_final_snapshot",
"snapshot_identifier",
},
},
},
})
}

func TestAccAWSRDSCluster_GlobalClusterIdentifier_Add(t *testing.T) {
var dbCluster1 rds.DBCluster

rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_rds_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSRDSClusterConfig_EngineMode(rName, "global"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterExists(resourceName, &dbCluster1),
resource.TestCheckResourceAttr(resourceName, "global_cluster_identifier", ""),
),
},
{
Config: testAccAWSRDSClusterConfig_GlobalClusterIdentifier(rName),
ExpectError: regexp.MustCompile(`Existing RDS Clusters cannot be added to an existing RDS Global Cluster`),
},
},
})
}

func TestAccAWSRDSCluster_GlobalClusterIdentifier_Remove(t *testing.T) {
var dbCluster1 rds.DBCluster

rName := acctest.RandomWithPrefix("tf-acc-test")
globalClusterResourceName := "aws_rds_global_cluster.test"
resourceName := "aws_rds_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSRDSClusterConfig_GlobalClusterIdentifier(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterExists(resourceName, &dbCluster1),
resource.TestCheckResourceAttrPair(resourceName, "global_cluster_identifier", globalClusterResourceName, "id"),
),
},
{
Config: testAccAWSRDSClusterConfig_EngineMode(rName, "global"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterExists(resourceName, &dbCluster1),
resource.TestCheckResourceAttr(resourceName, "global_cluster_identifier", ""),
),
},
},
})
}

func TestAccAWSRDSCluster_GlobalClusterIdentifier_Update(t *testing.T) {
var dbCluster1 rds.DBCluster

rName := acctest.RandomWithPrefix("tf-acc-test")
globalClusterResourceName1 := "aws_rds_global_cluster.test.0"
globalClusterResourceName2 := "aws_rds_global_cluster.test.1"
resourceName := "aws_rds_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSRDSClusterConfig_GlobalClusterIdentifier_Update(rName, globalClusterResourceName1),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterExists(resourceName, &dbCluster1),
resource.TestCheckResourceAttrPair(resourceName, "global_cluster_identifier", globalClusterResourceName1, "id"),
),
},
{
Config: testAccAWSRDSClusterConfig_GlobalClusterIdentifier_Update(rName, globalClusterResourceName2),
ExpectError: regexp.MustCompile(`Existing RDS Clusters cannot be migrated between existing RDS Global Clusters`),
},
},
})
}

func TestAccAWSRDSCluster_Port(t *testing.T) {
var dbCluster1, dbCluster2 rds.DBCluster
rInt := acctest.RandInt()
Expand Down Expand Up @@ -2118,6 +2272,42 @@ resource "aws_rds_cluster" "test" {
`, rName, engineMode)
}

func testAccAWSRDSClusterConfig_GlobalClusterIdentifier(rName string) string {
return fmt.Sprintf(`
resource "aws_rds_global_cluster" "test" {
global_cluster_identifier = %q
}

resource "aws_rds_cluster" "test" {
cluster_identifier = %q
global_cluster_identifier = "${aws_rds_global_cluster.test.id}"
engine_mode = "global"
master_password = "barbarbarbar"
master_username = "foo"
skip_final_snapshot = true
}
`, rName, rName)
}

func testAccAWSRDSClusterConfig_GlobalClusterIdentifier_Update(rName, globalClusterIdentifierResourceName string) string {
return fmt.Sprintf(`
resource "aws_rds_global_cluster" "test" {
count = 2

global_cluster_identifier = "%s-${count.index}"
}

resource "aws_rds_cluster" "test" {
cluster_identifier = %q
global_cluster_identifier = "${%s.id}"
engine_mode = "global"
master_password = "barbarbarbar"
master_username = "foo"
skip_final_snapshot = true
}
`, rName, rName, globalClusterIdentifierResourceName)
}

func testAccAWSRDSClusterConfig_ScalingConfiguration(rName string, autoPause bool, maxCapacity, minCapacity, secondsUntilAutoPause int) string {
return fmt.Sprintf(`
resource "aws_rds_cluster" "test" {
Expand Down
Loading