2014-07-22 22:26:48 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-01 16:13:41 +02:00
|
|
|
"math/rand"
|
2014-07-22 22:26:48 +02:00
|
|
|
"testing"
|
2015-04-01 16:13:41 +02:00
|
|
|
"time"
|
2014-07-22 22:26:48 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-02-23 22:22:52 +01:00
|
|
|
|
2015-06-03 20:36:57 +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"
|
2014-07-22 22:26:48 +02:00
|
|
|
)
|
|
|
|
|
2015-06-08 01:18:14 +02:00
|
|
|
func TestAccAWSDBInstance_basic(t *testing.T) {
|
2014-07-22 22:26:48 +02:00
|
|
|
var v rds.DBInstance
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSDBInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSDBInstanceConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v),
|
|
|
|
testAccCheckAWSDBInstanceAttributes(&v),
|
2014-07-23 04:54:10 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-07-31 21:18:33 +02:00
|
|
|
"aws_db_instance.bar", "allocated_storage", "10"),
|
2014-07-23 04:54:10 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_db_instance.bar", "engine", "mysql"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2015-01-10 22:19:24 +01:00
|
|
|
"aws_db_instance.bar", "engine_version", "5.6.21"),
|
2015-05-14 16:44:22 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_db_instance.bar", "license_model", "general-public-license"),
|
2014-07-23 04:54:10 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_db_instance.bar", "instance_class", "db.t1.micro"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_db_instance.bar", "name", "baz"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_db_instance.bar", "username", "foo"),
|
2014-10-22 23:22:30 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_db_instance.bar", "parameter_group_name", "default.mysql5.6"),
|
2014-07-22 22:26:48 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-13 23:36:30 +02:00
|
|
|
func TestAccAWSDBInstanceReplica(t *testing.T) {
|
2015-05-20 16:28:33 +02:00
|
|
|
var s, r rds.DBInstance
|
2015-05-13 23:36:30 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSDBInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2015-05-20 16:28:33 +02:00
|
|
|
Config: testAccReplicaInstanceConfig(rand.New(rand.NewSource(time.Now().UnixNano())).Int()),
|
2015-05-13 23:36:30 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-05-20 16:28:33 +02:00
|
|
|
testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &s),
|
2015-05-13 23:36:30 +02:00
|
|
|
testAccCheckAWSDBInstanceExists("aws_db_instance.replica", &r),
|
2015-05-20 16:28:33 +02:00
|
|
|
testAccCheckAWSDBInstanceReplicaAttributes(&s, &r),
|
2015-05-13 23:36:30 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-22 22:26:48 +02:00
|
|
|
func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error {
|
2015-04-13 22:45:55 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).rdsconn
|
2014-07-22 22:26:48 +02:00
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-22 22:26:48 +02:00
|
|
|
if rs.Type != "aws_db_instance" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the Group
|
2015-05-20 13:21:23 +02:00
|
|
|
var err error
|
2014-07-22 22:26:48 +02:00
|
|
|
resp, err := conn.DescribeDBInstances(
|
2015-04-13 22:45:55 +02:00
|
|
|
&rds.DescribeDBInstancesInput{
|
2015-02-23 22:22:52 +01:00
|
|
|
DBInstanceIdentifier: aws.String(rs.Primary.ID),
|
2014-07-22 22:26:48 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if len(resp.DBInstances) != 0 &&
|
2015-02-23 22:22:52 +01:00
|
|
|
*resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
|
2014-07-22 22:26:48 +02:00
|
|
|
return fmt.Errorf("DB Instance still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error
|
2015-05-20 13:21:23 +02:00
|
|
|
newerr, ok := err.(awserr.Error)
|
2014-07-22 22:26:48 +02:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 13:21:23 +02:00
|
|
|
if newerr.Code() != "InvalidDBInstance.NotFound" {
|
2014-07-22 22:26:48 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-23 04:54:10 +02:00
|
|
|
func testAccCheckAWSDBInstanceAttributes(v *rds.DBInstance) resource.TestCheckFunc {
|
2014-07-22 22:26:48 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
2015-02-23 22:22:52 +01:00
|
|
|
if *v.Engine != "mysql" {
|
2015-02-25 17:44:10 +01:00
|
|
|
return fmt.Errorf("bad engine: %#v", *v.Engine)
|
2014-07-23 04:54:10 +02:00
|
|
|
}
|
|
|
|
|
2015-02-23 22:22:52 +01:00
|
|
|
if *v.EngineVersion != "5.6.21" {
|
2015-02-25 17:44:10 +01:00
|
|
|
return fmt.Errorf("bad engine_version: %#v", *v.EngineVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *v.BackupRetentionPeriod != 0 {
|
|
|
|
return fmt.Errorf("bad backup_retention_period: %#v", *v.BackupRetentionPeriod)
|
2014-07-23 04:54:10 +02:00
|
|
|
}
|
2014-07-22 22:26:48 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 16:28:33 +02:00
|
|
|
func testAccCheckAWSDBInstanceReplicaAttributes(source, replica *rds.DBInstance) resource.TestCheckFunc {
|
2015-05-13 23:36:30 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
2015-05-20 16:28:33 +02:00
|
|
|
if replica.ReadReplicaSourceDBInstanceIdentifier != nil && *replica.ReadReplicaSourceDBInstanceIdentifier != *source.DBInstanceIdentifier {
|
|
|
|
return fmt.Errorf("bad source identifier for replica, expected: '%s', got: '%s'", *source.DBInstanceIdentifier, *replica.ReadReplicaSourceDBInstanceIdentifier)
|
2015-05-13 23:36:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-22 22:26:48 +02:00
|
|
|
func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-22 22:26:48 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-22 22:26:48 +02:00
|
|
|
return fmt.Errorf("No DB Instance ID is set")
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:45:55 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).rdsconn
|
2014-07-22 22:26:48 +02:00
|
|
|
|
2015-04-13 22:45:55 +02:00
|
|
|
opts := rds.DescribeDBInstancesInput{
|
2015-02-23 22:22:52 +01:00
|
|
|
DBInstanceIdentifier: aws.String(rs.Primary.ID),
|
2014-07-22 22:26:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := conn.DescribeDBInstances(&opts)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.DBInstances) != 1 ||
|
2015-02-23 22:22:52 +01:00
|
|
|
*resp.DBInstances[0].DBInstanceIdentifier != rs.Primary.ID {
|
2014-07-22 22:26:48 +02:00
|
|
|
return fmt.Errorf("DB Instance not found")
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:45:55 +02:00
|
|
|
*v = *resp.DBInstances[0]
|
2014-07-22 22:26:48 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 16:13:41 +02:00
|
|
|
// Database names cannot collide, and deletion takes so long, that making the
|
|
|
|
// name a bit random helps so able we can kill a test that's just waiting for a
|
|
|
|
// delete and not be blocked on kicking off another one.
|
|
|
|
var testAccAWSDBInstanceConfig = fmt.Sprintf(`
|
2014-07-22 22:26:48 +02:00
|
|
|
resource "aws_db_instance" "bar" {
|
2015-04-01 16:13:41 +02:00
|
|
|
identifier = "foobarbaz-test-terraform-%d"
|
2014-07-22 22:26:48 +02:00
|
|
|
|
|
|
|
allocated_storage = 10
|
2015-07-16 07:39:23 +02:00
|
|
|
engine = "MySQL"
|
2015-01-10 22:19:24 +01:00
|
|
|
engine_version = "5.6.21"
|
2014-07-22 22:26:48 +02:00
|
|
|
instance_class = "db.t1.micro"
|
|
|
|
name = "baz"
|
|
|
|
password = "barbarbarbar"
|
|
|
|
username = "foo"
|
|
|
|
|
2015-08-24 18:08:19 +02:00
|
|
|
|
|
|
|
# Maintenance Window is stored in lower case in the API, though not strictly
|
|
|
|
# documented. Terraform will downcase this to match (as opposed to throw a
|
|
|
|
# validation error).
|
|
|
|
maintenance_window = "Fri:09:00-Fri:09:30"
|
|
|
|
|
2015-02-25 16:41:05 +01:00
|
|
|
backup_retention_period = 0
|
|
|
|
|
2014-10-22 23:22:30 +02:00
|
|
|
parameter_group_name = "default.mysql5.6"
|
2015-04-01 16:13:41 +02:00
|
|
|
}`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
|
2015-05-13 23:36:30 +02:00
|
|
|
|
2015-05-20 16:28:33 +02:00
|
|
|
func testAccReplicaInstanceConfig(val int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_db_instance" "bar" {
|
|
|
|
identifier = "foobarbaz-test-terraform-%d"
|
|
|
|
|
|
|
|
allocated_storage = 5
|
|
|
|
engine = "mysql"
|
|
|
|
engine_version = "5.6.21"
|
|
|
|
instance_class = "db.t1.micro"
|
|
|
|
name = "baz"
|
|
|
|
password = "barbarbarbar"
|
|
|
|
username = "foo"
|
|
|
|
|
|
|
|
backup_retention_period = 1
|
|
|
|
|
|
|
|
parameter_group_name = "default.mysql5.6"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_db_instance" "replica" {
|
|
|
|
identifier = "tf-replica-db-%d"
|
|
|
|
backup_retention_period = 0
|
2015-05-21 16:49:46 +02:00
|
|
|
replicate_source_db = "${aws_db_instance.bar.identifier}"
|
2015-05-20 16:28:33 +02:00
|
|
|
allocated_storage = "${aws_db_instance.bar.allocated_storage}"
|
|
|
|
engine = "${aws_db_instance.bar.engine}"
|
|
|
|
engine_version = "${aws_db_instance.bar.engine_version}"
|
|
|
|
instance_class = "${aws_db_instance.bar.instance_class}"
|
|
|
|
password = "${aws_db_instance.bar.password}"
|
|
|
|
username = "${aws_db_instance.bar.username}"
|
|
|
|
tags {
|
|
|
|
Name = "tf-replica-db"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, val, val)
|
|
|
|
}
|