2015-08-04 23:24:55 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2015-10-07 18:27:24 +02:00
|
|
|
"fmt"
|
2016-07-21 00:52:34 +02:00
|
|
|
"regexp"
|
2015-10-07 18:27:24 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-03-18 17:12:31 +01:00
|
|
|
"time"
|
2015-10-07 18:27:24 +02:00
|
|
|
|
2016-02-12 18:43:07 +01: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"
|
|
|
|
|
|
|
|
"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 TestAccAWSRDSClusterInstance_basic(t *testing.T) {
|
2015-10-07 18:27:24 +02:00
|
|
|
var v rds.DBInstance
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSClusterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-11-22 12:33:01 +01:00
|
|
|
{
|
2016-03-31 23:25:35 +02:00
|
|
|
Config: testAccAWSClusterInstanceConfig(acctest.RandInt()),
|
2015-10-07 18:27:24 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v),
|
|
|
|
testAccCheckAWSDBClusterInstanceAttributes(&v),
|
2016-11-22 12:33:01 +01:00
|
|
|
resource.TestCheckResourceAttr("aws_rds_cluster_instance.cluster_instances", "auto_minor_version_upgrade", "true"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: testAccAWSClusterInstanceConfigModified(acctest.RandInt()),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v),
|
|
|
|
testAccCheckAWSDBClusterInstanceAttributes(&v),
|
|
|
|
resource.TestCheckResourceAttr("aws_rds_cluster_instance.cluster_instances", "auto_minor_version_upgrade", "false"),
|
2015-10-07 18:27:24 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-08-04 23:24:55 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 00:52:34 +02:00
|
|
|
func TestAccAWSRDSClusterInstance_kmsKey(t *testing.T) {
|
|
|
|
var v rds.DBInstance
|
|
|
|
keyRegex := regexp.MustCompile("^arn:aws:kms:")
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSClusterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-11-22 12:33:01 +01:00
|
|
|
{
|
2016-07-21 00:52:34 +02:00
|
|
|
Config: testAccAWSClusterInstanceConfigKmsKey(acctest.RandInt()),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v),
|
|
|
|
resource.TestMatchResourceAttr(
|
|
|
|
"aws_rds_cluster_instance.cluster_instances", "kms_key_id", keyRegex),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-18 17:12:31 +01:00
|
|
|
// https://github.com/hashicorp/terraform/issues/5350
|
|
|
|
func TestAccAWSRDSClusterInstance_disappears(t *testing.T) {
|
|
|
|
var v rds.DBInstance
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSClusterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-11-22 12:33:01 +01:00
|
|
|
{
|
2016-03-31 23:25:35 +02:00
|
|
|
Config: testAccAWSClusterInstanceConfig(acctest.RandInt()),
|
2016-03-18 17:12:31 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v),
|
|
|
|
testAccAWSClusterInstanceDisappears(&v),
|
|
|
|
),
|
|
|
|
// A non-empty plan is what we want. A crash is what we don't want. :)
|
|
|
|
ExpectNonEmptyPlan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-04 23:24:55 +02:00
|
|
|
func testAccCheckAWSDBClusterInstanceAttributes(v *rds.DBInstance) resource.TestCheckFunc {
|
2015-10-07 18:27:24 +02:00
|
|
|
return func(s *terraform.State) error {
|
2015-08-04 23:24:55 +02:00
|
|
|
|
2015-10-07 18:27:24 +02:00
|
|
|
if *v.Engine != "aurora" {
|
|
|
|
return fmt.Errorf("bad engine, expected \"aurora\": %#v", *v.Engine)
|
|
|
|
}
|
2015-08-04 23:24:55 +02:00
|
|
|
|
2015-10-07 18:27:24 +02:00
|
|
|
if !strings.HasPrefix(*v.DBClusterIdentifier, "tf-aurora-cluster") {
|
|
|
|
return fmt.Errorf("Bad Cluster Identifier prefix:\nexpected: %s\ngot: %s", "tf-aurora-cluster", *v.DBClusterIdentifier)
|
|
|
|
}
|
2015-08-04 23:24:55 +02:00
|
|
|
|
2015-10-07 18:27:24 +02:00
|
|
|
return nil
|
|
|
|
}
|
2015-08-04 23:24:55 +02:00
|
|
|
}
|
|
|
|
|
2016-03-18 17:12:31 +01:00
|
|
|
func testAccAWSClusterInstanceDisappears(v *rds.DBInstance) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).rdsconn
|
|
|
|
opts := &rds.DeleteDBInstanceInput{
|
|
|
|
DBInstanceIdentifier: v.DBInstanceIdentifier,
|
|
|
|
}
|
|
|
|
if _, err := conn.DeleteDBInstance(opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return resource.Retry(40*time.Minute, func() *resource.RetryError {
|
|
|
|
opts := &rds.DescribeDBInstancesInput{
|
|
|
|
DBInstanceIdentifier: v.DBInstanceIdentifier,
|
|
|
|
}
|
|
|
|
_, err := conn.DescribeDBInstances(opts)
|
|
|
|
if err != nil {
|
|
|
|
dbinstanceerr, ok := err.(awserr.Error)
|
|
|
|
if ok && dbinstanceerr.Code() == "DBInstanceNotFound" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return resource.NonRetryableError(
|
|
|
|
fmt.Errorf("Error retrieving DB Instances: %s", err))
|
|
|
|
}
|
|
|
|
return resource.RetryableError(fmt.Errorf(
|
|
|
|
"Waiting for instance to be deleted: %v", v.DBInstanceIdentifier))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 23:24:55 +02:00
|
|
|
func testAccCheckAWSClusterInstanceExists(n string, v *rds.DBInstance) 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.DescribeDBInstances(&rds.DescribeDBInstancesInput{
|
|
|
|
DBInstanceIdentifier: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range resp.DBInstances {
|
|
|
|
if *d.DBInstanceIdentifier == rs.Primary.ID {
|
|
|
|
*v = *d
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("DB Cluster (%s) not found", rs.Primary.ID)
|
|
|
|
}
|
2015-08-04 23:24:55 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 05:43:02 +02:00
|
|
|
func TestAccAWSRDSClusterInstance_withInstanceEnhancedMonitor(t *testing.T) {
|
2016-08-08 08:43:47 +02:00
|
|
|
var v rds.DBInstance
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSClusterDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-11-22 12:33:01 +01:00
|
|
|
{
|
2016-08-08 08:43:47 +02:00
|
|
|
Config: testAccAWSClusterInstanceEnhancedMonitor(acctest.RandInt()),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v),
|
|
|
|
testAccCheckAWSDBClusterInstanceAttributes(&v),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-04 23:24:55 +02:00
|
|
|
// Add some random to the name, to avoid collision
|
2016-03-31 23:25:35 +02:00
|
|
|
func testAccAWSClusterInstanceConfig(n int) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-08-04 23:24:55 +02:00
|
|
|
resource "aws_rds_cluster" "default" {
|
|
|
|
cluster_identifier = "tf-aurora-cluster-test-%d"
|
2016-02-12 18:43:07 +01:00
|
|
|
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
|
|
|
|
database_name = "mydb"
|
|
|
|
master_username = "foo"
|
|
|
|
master_password = "mustbeeightcharaters"
|
2017-02-13 20:29:31 +01:00
|
|
|
skip_final_snapshot = true
|
2015-08-04 23:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_rds_cluster_instance" "cluster_instances" {
|
2016-06-03 20:46:01 +02:00
|
|
|
identifier = "tf-cluster-instance-%d"
|
|
|
|
cluster_identifier = "${aws_rds_cluster.default.id}"
|
|
|
|
instance_class = "db.r3.large"
|
|
|
|
db_parameter_group_name = "${aws_db_parameter_group.bar.name}"
|
2016-08-11 23:51:25 +02:00
|
|
|
promotion_tier = "3"
|
2015-08-04 23:24:55 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 20:46:01 +02:00
|
|
|
resource "aws_db_parameter_group" "bar" {
|
2016-06-14 17:42:36 +02:00
|
|
|
name = "tfcluster-test-group-%d"
|
2016-06-03 20:46:01 +02:00
|
|
|
family = "aurora5.6"
|
|
|
|
|
|
|
|
parameter {
|
|
|
|
name = "back_log"
|
|
|
|
value = "32767"
|
|
|
|
apply_method = "pending-reboot"
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 17:42:36 +02:00
|
|
|
`, n, n, n)
|
2016-11-22 12:33:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func testAccAWSClusterInstanceConfigModified(n int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_rds_cluster" "default" {
|
|
|
|
cluster_identifier = "tf-aurora-cluster-test-%d"
|
|
|
|
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
|
|
|
|
database_name = "mydb"
|
|
|
|
master_username = "foo"
|
|
|
|
master_password = "mustbeeightcharaters"
|
2017-02-13 20:29:31 +01:00
|
|
|
skip_final_snapshot = true
|
2016-11-22 12:33:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_rds_cluster_instance" "cluster_instances" {
|
|
|
|
identifier = "tf-cluster-instance-%d"
|
|
|
|
cluster_identifier = "${aws_rds_cluster.default.id}"
|
|
|
|
instance_class = "db.r3.large"
|
|
|
|
db_parameter_group_name = "${aws_db_parameter_group.bar.name}"
|
|
|
|
auto_minor_version_upgrade = false
|
|
|
|
promotion_tier = "3"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_db_parameter_group" "bar" {
|
|
|
|
name = "tfcluster-test-group-%d"
|
|
|
|
family = "aurora5.6"
|
|
|
|
|
|
|
|
parameter {
|
|
|
|
name = "back_log"
|
|
|
|
value = "32767"
|
|
|
|
apply_method = "pending-reboot"
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, n, n, n)
|
2016-03-31 23:25:35 +02:00
|
|
|
}
|
2016-07-21 00:52:34 +02:00
|
|
|
|
|
|
|
func testAccAWSClusterInstanceConfigKmsKey(n int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
|
|
|
|
resource "aws_kms_key" "foo" {
|
|
|
|
description = "Terraform acc test %d"
|
|
|
|
policy = <<POLICY
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Id": "kms-tf-1",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid": "Enable IAM User Permissions",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": {
|
|
|
|
"AWS": "*"
|
|
|
|
},
|
|
|
|
"Action": "kms:*",
|
|
|
|
"Resource": "*"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
POLICY
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_rds_cluster" "default" {
|
|
|
|
cluster_identifier = "tf-aurora-cluster-test-%d"
|
|
|
|
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
|
|
|
|
database_name = "mydb"
|
|
|
|
master_username = "foo"
|
|
|
|
master_password = "mustbeeightcharaters"
|
|
|
|
storage_encrypted = true
|
|
|
|
kms_key_id = "${aws_kms_key.foo.arn}"
|
2017-02-13 20:29:31 +01:00
|
|
|
skip_final_snapshot = true
|
2016-07-21 00:52:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_rds_cluster_instance" "cluster_instances" {
|
|
|
|
identifier = "tf-cluster-instance-%d"
|
|
|
|
cluster_identifier = "${aws_rds_cluster.default.id}"
|
|
|
|
instance_class = "db.r3.large"
|
|
|
|
db_parameter_group_name = "${aws_db_parameter_group.bar.name}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_db_parameter_group" "bar" {
|
|
|
|
name = "tfcluster-test-group-%d"
|
|
|
|
family = "aurora5.6"
|
|
|
|
|
|
|
|
parameter {
|
|
|
|
name = "back_log"
|
|
|
|
value = "32767"
|
|
|
|
apply_method = "pending-reboot"
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, n, n, n, n)
|
|
|
|
}
|
2016-08-08 08:43:47 +02:00
|
|
|
|
|
|
|
func testAccAWSClusterInstanceEnhancedMonitor(n int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_rds_cluster" "default" {
|
|
|
|
cluster_identifier = "tf-aurora-cluster-test-%d"
|
|
|
|
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
|
|
|
|
database_name = "mydb"
|
|
|
|
master_username = "foo"
|
|
|
|
master_password = "mustbeeightcharaters"
|
2017-02-13 20:29:31 +01:00
|
|
|
skip_final_snapshot = true
|
2016-08-08 08:43:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_rds_cluster_instance" "cluster_instances" {
|
|
|
|
identifier = "tf-cluster-instance-%d"
|
|
|
|
cluster_identifier = "${aws_rds_cluster.default.id}"
|
|
|
|
instance_class = "db.r3.large"
|
|
|
|
db_parameter_group_name = "${aws_db_parameter_group.bar.name}"
|
2016-08-08 10:15:22 +02:00
|
|
|
monitoring_interval = "60"
|
|
|
|
monitoring_role_arn = "${aws_iam_role.tf_enhanced_monitor_role.arn}"
|
2016-08-08 08:43:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_role" "tf_enhanced_monitor_role" {
|
|
|
|
name = "tf_enhanced_monitor_role-%d"
|
|
|
|
assume_role_policy = <<EOF
|
2016-08-08 10:15:22 +02:00
|
|
|
{
|
2016-08-08 08:43:47 +02:00
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Action": "sts:AssumeRole",
|
|
|
|
"Principal": {
|
|
|
|
"Service": "monitoring.rds.amazonaws.com"
|
|
|
|
},
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Sid": ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_policy_attachment" "rds_m_attach" {
|
|
|
|
name = "AmazonRDSEnhancedMonitoringRole"
|
2016-08-08 10:15:22 +02:00
|
|
|
roles = ["${aws_iam_role.tf_enhanced_monitor_role.name}"]
|
2016-08-08 08:43:47 +02:00
|
|
|
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_db_parameter_group" "bar" {
|
|
|
|
name = "tfcluster-test-group-%d"
|
|
|
|
family = "aurora5.6"
|
|
|
|
|
|
|
|
parameter {
|
|
|
|
name = "back_log"
|
|
|
|
value = "32767"
|
|
|
|
apply_method = "pending-reboot"
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, n, n, n, n)
|
|
|
|
}
|