terraform/builtin/providers/aws/resource_aws_db_instance_te...

164 lines
3.9 KiB
Go
Raw Normal View History

2014-07-22 22:26:48 +02:00
package aws
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/rds"
)
func TestAccAWSDBInstance(t *testing.T) {
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),
resource.TestCheckResourceAttr(
2014-07-23 04:54:10 +02:00
"aws_db_instance.bar", "instance_identifier", "foobarbaz-test-terraform"),
resource.TestCheckResourceAttr(
"aws_db_instance.bar", "allocated_storage", "foobarbaz-test-terraform"),
resource.TestCheckResourceAttr(
"aws_db_instance.bar", "engine", "mysql"),
resource.TestCheckResourceAttr(
"aws_db_instance.bar", "engine_version", "5.6.17"),
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", "password", "barbarbarbar"),
resource.TestCheckResourceAttr(
"aws_db_instance.bar", "username", "foo"),
resource.TestCheckResourceAttr(
"aws_db_instance.bar", "skip_final_snapshot", "true"),
resource.TestCheckResourceAttr(
"aws_db_instance.bar", "security_group_names.0", "secfoobarbaz-test-terraform"),
2014-07-22 22:26:48 +02:00
),
},
},
})
}
func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error {
conn := testAccProvider.rdsconn
for _, rs := range s.Resources {
if rs.Type != "aws_db_instance" {
continue
}
// Try to find the Group
resp, err := conn.DescribeDBInstances(
&rds.DescribeDBInstances{
DBInstanceIdentifier: rs.ID,
})
if err == nil {
if len(resp.DBInstances) != 0 &&
resp.DBInstances[0].DBInstanceIdentifier == rs.ID {
return fmt.Errorf("DB Instance still exists")
}
}
// Verify the error
newerr, ok := err.(*rds.Error)
if !ok {
return err
}
if newerr.Code != "InvalidDBInstance.NotFound" {
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 {
2014-07-23 04:54:10 +02:00
if len(v.DBSecurityGroupNames) == 0 {
return fmt.Errorf("no sec names: %#v", v.DBSecurityGroupNames)
}
if v.Engine != "mysql" {
return fmt.Errorf("bad engine: %#v", v.Engine)
}
2014-07-23 05:13:49 +02:00
if v.EngineVersion != "5.6.17" {
return fmt.Errorf("bad engine_version: %#v", v.EngineVersion)
2014-07-23 04:54:10 +02:00
}
2014-07-22 22:26:48 +02:00
return nil
}
}
func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.ID == "" {
return fmt.Errorf("No DB Instance ID is set")
}
conn := testAccProvider.rdsconn
opts := rds.DescribeDBInstances{
DBInstanceIdentifier: rs.ID,
}
resp, err := conn.DescribeDBInstances(&opts)
if err != nil {
return err
}
if len(resp.DBInstances) != 1 ||
resp.DBInstances[0].DBInstanceIdentifier != rs.ID {
return fmt.Errorf("DB Instance not found")
}
*v = resp.DBInstances[0]
return nil
}
}
const testAccAWSDBInstanceConfig = `
2014-07-23 04:22:52 +02:00
resource "aws_db_security_group" "bar" {
2014-07-23 04:54:10 +02:00
name = "secfoobarbaz-test-terraform"
2014-07-23 04:22:52 +02:00
description = "just cuz"
ingress {
cidr = "10.0.0.1/24"
}
}
2014-07-22 22:26:48 +02:00
resource "aws_db_instance" "bar" {
2014-07-23 04:54:10 +02:00
identifier = "foobarbaz-test-terraform"
2014-07-22 22:26:48 +02:00
allocated_storage = 10
engine = "mysql"
2014-07-23 04:54:10 +02:00
engine_version = "5.6.17"
2014-07-22 22:26:48 +02:00
instance_class = "db.t1.micro"
name = "baz"
password = "barbarbarbar"
username = "foo"
skip_final_snapshot = true
2014-07-23 04:22:52 +02:00
security_group_names = [${aws_db_security_group.bar.name}]
2014-07-22 22:26:48 +02:00
}
`