github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/builtin/providers/aws/resource_aws_db_subnet_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/mitchellh/goamz/rds" 10 ) 11 12 func TestAccAWSDbSubnetGroup(t *testing.T) { 13 var v rds.DBSubnetGroup 14 15 testCheck := func(*terraform.State) error { 16 return nil 17 } 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckDbSubnetGroupDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: testAccDbSubnetGroupConfig, 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckDbSubnetGroupExists( 28 "aws_db_subnet_group.foo", &v), 29 testCheck, 30 ), 31 }, 32 }, 33 }) 34 } 35 36 func testAccCheckDbSubnetGroupDestroy(s *terraform.State) error { 37 conn := testAccProvider.rdsconn 38 39 for _, rs := range s.RootModule().Resources { 40 if rs.Type != "aws_db_subnet_group" { 41 continue 42 } 43 44 // Try to find the resource 45 resp, err := conn.DescribeDBSubnetGroups(&rds.DescribeDBSubnetGroups{rs.Primary.ID}) 46 if err == nil { 47 if len(resp.DBSubnetGroups) > 0 { 48 return fmt.Errorf("still exist.") 49 } 50 51 return nil 52 } 53 54 // Verify the error is what we want 55 rdserr, ok := err.(*rds.Error) 56 if !ok { 57 return err 58 } 59 if rdserr.Code != "DBSubnetGroupNotFoundFault" { 60 return err 61 } 62 } 63 64 return nil 65 } 66 67 func testAccCheckDbSubnetGroupExists(n string, v *rds.DBSubnetGroup) resource.TestCheckFunc { 68 return func(s *terraform.State) error { 69 rs, ok := s.RootModule().Resources[n] 70 if !ok { 71 return fmt.Errorf("Not found: %s", n) 72 } 73 74 if rs.Primary.ID == "" { 75 return fmt.Errorf("No ID is set") 76 } 77 78 conn := testAccProvider.rdsconn 79 resp, err := conn.DescribeDBSubnetGroups(&rds.DescribeDBSubnetGroups{rs.Primary.ID}) 80 if err != nil { 81 return err 82 } 83 if len(resp.DBSubnetGroups) == 0 { 84 return fmt.Errorf("DbSubnetGroup not found") 85 } 86 87 *v = resp.DBSubnetGroups[0] 88 89 return nil 90 } 91 } 92 93 const testAccDbSubnetGroupConfig = ` 94 resource "aws_vpc" "foo" { 95 cidr_block = "10.1.0.0/16" 96 } 97 98 resource "aws_subnet" "foo" { 99 cidr_block = "10.1.1.0/24" 100 vpc_id = "${aws_vpc.foo.id}" 101 } 102 103 resource "aws_subnet" "bar" { 104 cidr_block = "10.1.2.0/24" 105 vpc_id = "${aws_vpc.foo.id}" 106 } 107 108 resource "aws_db_subnet_group" "foo" { 109 name = "foo" 110 description = "foo description" 111 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 112 } 113 `