2015-08-04 23:24:55 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2015-10-07 18:27:24 +02:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
2015-08-04 23:24:55 +02:00
|
|
|
|
2016-03-31 23:25:35 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-10-07 18:27:24 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-08-04 23:24:55 +02:00
|
|
|
|
2015-10-07 18:27:24 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/rds"
|
2015-08-04 23:24:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAWSRDSCluster_basic(t *testing.T) {
|
2015-10-07 18:27:24 +02:00
|
|
|
var v rds.DBCluster
|
|
|
|
|
2015-11-05 11:25:01 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSClusterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-03-31 23:25:35 +02:00
|
|
|
Config: testAccAWSClusterConfig(acctest.RandInt()),
|
2015-11-05 11:25:01 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSClusterExists("aws_rds_cluster.default", &v),
|
2016-03-08 23:28:32 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_rds_cluster.default", "storage_encrypted", "false"),
|
2016-02-23 11:52:07 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-09 23:45:29 +02:00
|
|
|
"aws_rds_cluster.default", "db_cluster_parameter_group_name", "default.aurora5.6"),
|
2015-11-05 11:25:01 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-08 23:28:44 +01:00
|
|
|
func TestAccAWSRDSCluster_encrypted(t *testing.T) {
|
|
|
|
var v rds.DBCluster
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSClusterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-03-31 23:25:35 +02:00
|
|
|
Config: testAccAWSClusterConfig_encrypted(acctest.RandInt()),
|
2016-03-08 23:28:44 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSClusterExists("aws_rds_cluster.default", &v),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_rds_cluster.default", "storage_encrypted", "true"),
|
2016-02-23 11:52:07 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-09 23:45:29 +02:00
|
|
|
"aws_rds_cluster.default", "db_cluster_parameter_group_name", "default.aurora5.6"),
|
2016-03-08 23:28:44 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-05 11:25:01 +01:00
|
|
|
func TestAccAWSRDSCluster_backupsUpdate(t *testing.T) {
|
2015-11-04 22:06:41 +01:00
|
|
|
var v rds.DBCluster
|
|
|
|
|
2016-03-31 23:25:35 +02:00
|
|
|
ri := acctest.RandInt()
|
2015-11-04 22:06:41 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSClusterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-03-31 23:25:35 +02:00
|
|
|
Config: testAccAWSClusterConfig_backups(ri),
|
2015-11-04 22:06:41 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSClusterExists("aws_rds_cluster.default", &v),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_rds_cluster.default", "preferred_backup_window", "07:00-09:00"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_rds_cluster.default", "backup_retention_period", "5"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_rds_cluster.default", "preferred_maintenance_window", "tue:04:00-tue:04:30"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
2016-03-31 23:25:35 +02:00
|
|
|
Config: testAccAWSClusterConfig_backupsUpdate(ri),
|
2015-11-04 22:06:41 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSClusterExists("aws_rds_cluster.default", &v),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_rds_cluster.default", "preferred_backup_window", "03:00-09:00"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_rds_cluster.default", "backup_retention_period", "10"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_rds_cluster.default", "preferred_maintenance_window", "wed:01:00-wed:01:30"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-04 23:24:55 +02:00
|
|
|
func testAccCheckAWSClusterDestroy(s *terraform.State) error {
|
2015-10-07 18:27:24 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_rds_cluster" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the Group
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).rdsconn
|
|
|
|
var err error
|
|
|
|
resp, err := conn.DescribeDBClusters(
|
|
|
|
&rds.DescribeDBClustersInput{
|
|
|
|
DBClusterIdentifier: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if len(resp.DBClusters) != 0 &&
|
|
|
|
*resp.DBClusters[0].DBClusterIdentifier == rs.Primary.ID {
|
|
|
|
return fmt.Errorf("DB Cluster %s still exists", rs.Primary.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return nil if the cluster is already destroyed
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
2016-01-26 17:35:21 +01:00
|
|
|
if awsErr.Code() == "DBClusterNotFoundFault" {
|
2015-10-07 18:27:24 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-08-04 23:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSClusterExists(n string, v *rds.DBCluster) resource.TestCheckFunc {
|
2015-10-07 18:27:24 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No DB Instance ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).rdsconn
|
|
|
|
resp, err := conn.DescribeDBClusters(&rds.DescribeDBClustersInput{
|
|
|
|
DBClusterIdentifier: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range resp.DBClusters {
|
|
|
|
if *c.DBClusterIdentifier == rs.Primary.ID {
|
|
|
|
*v = *c
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("DB Cluster (%s) not found", rs.Primary.ID)
|
|
|
|
}
|
2015-08-04 23:24:55 +02:00
|
|
|
}
|
|
|
|
|
2016-03-31 23:25:35 +02:00
|
|
|
func testAccAWSClusterConfig(n int) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-11-05 11:25:01 +01:00
|
|
|
resource "aws_rds_cluster" "default" {
|
|
|
|
cluster_identifier = "tf-aurora-cluster-%d"
|
|
|
|
availability_zones = ["us-west-2a","us-west-2b","us-west-2c"]
|
|
|
|
database_name = "mydb"
|
|
|
|
master_username = "foo"
|
|
|
|
master_password = "mustbeeightcharaters"
|
2016-06-09 23:45:29 +02:00
|
|
|
db_cluster_parameter_group_name = "default.aurora5.6"
|
2016-03-31 23:25:35 +02:00
|
|
|
}`, n)
|
|
|
|
}
|
2015-11-05 11:25:01 +01:00
|
|
|
|
2016-03-31 23:25:35 +02:00
|
|
|
func testAccAWSClusterConfig_encrypted(n int) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-03-08 23:28:44 +01:00
|
|
|
resource "aws_rds_cluster" "default" {
|
|
|
|
cluster_identifier = "tf-aurora-cluster-%d"
|
|
|
|
availability_zones = ["us-west-2a","us-west-2b","us-west-2c"]
|
|
|
|
database_name = "mydb"
|
|
|
|
master_username = "foo"
|
|
|
|
master_password = "mustbeeightcharaters"
|
|
|
|
storage_encrypted = true
|
2016-03-31 23:25:35 +02:00
|
|
|
}`, n)
|
|
|
|
}
|
2016-03-08 23:28:44 +01:00
|
|
|
|
2016-03-31 23:25:35 +02:00
|
|
|
func testAccAWSClusterConfig_backups(n int) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-08-04 23:24:55 +02:00
|
|
|
resource "aws_rds_cluster" "default" {
|
|
|
|
cluster_identifier = "tf-aurora-cluster-%d"
|
|
|
|
availability_zones = ["us-west-2a","us-west-2b","us-west-2c"]
|
|
|
|
database_name = "mydb"
|
|
|
|
master_username = "foo"
|
|
|
|
master_password = "mustbeeightcharaters"
|
2015-11-04 20:40:22 +01:00
|
|
|
backup_retention_period = 5
|
|
|
|
preferred_backup_window = "07:00-09:00"
|
|
|
|
preferred_maintenance_window = "tue:04:00-tue:04:30"
|
2016-03-31 23:25:35 +02:00
|
|
|
}`, n)
|
|
|
|
}
|
2015-11-04 22:06:41 +01:00
|
|
|
|
2016-03-31 23:25:35 +02:00
|
|
|
func testAccAWSClusterConfig_backupsUpdate(n int) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-11-04 22:06:41 +01:00
|
|
|
resource "aws_rds_cluster" "default" {
|
|
|
|
cluster_identifier = "tf-aurora-cluster-%d"
|
|
|
|
availability_zones = ["us-west-2a","us-west-2b","us-west-2c"]
|
|
|
|
database_name = "mydb"
|
|
|
|
master_username = "foo"
|
|
|
|
master_password = "mustbeeightcharaters"
|
|
|
|
backup_retention_period = 10
|
|
|
|
preferred_backup_window = "03:00-09:00"
|
|
|
|
preferred_maintenance_window = "wed:01:00-wed:01:30"
|
|
|
|
apply_immediately = true
|
2016-03-31 23:25:35 +02:00
|
|
|
}`, n)
|
|
|
|
}
|