github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/awserr" 12 "github.com/aws/aws-sdk-go/service/rds" 13 ) 14 15 func TestAccAWSDBSubnetGroup_basic(t *testing.T) { 16 var v rds.DBSubnetGroup 17 18 testCheck := func(*terraform.State) error { 19 return nil 20 } 21 22 resource.Test(t, resource.TestCase{ 23 PreCheck: func() { testAccPreCheck(t) }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckDBSubnetGroupDestroy, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: testAccDBSubnetGroupConfig, 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckDBSubnetGroupExists( 31 "aws_db_subnet_group.foo", &v), 32 testCheck, 33 ), 34 }, 35 }, 36 }) 37 } 38 39 // Regression test for https://github.com/hashicorp/terraform/issues/2603 and 40 // https://github.com/hashicorp/terraform/issues/2664 41 func TestAccAWSDBSubnetGroup_withUndocumentedCharacters(t *testing.T) { 42 var v rds.DBSubnetGroup 43 44 testCheck := func(*terraform.State) error { 45 return nil 46 } 47 48 resource.Test(t, resource.TestCase{ 49 PreCheck: func() { testAccPreCheck(t) }, 50 Providers: testAccProviders, 51 CheckDestroy: testAccCheckDBSubnetGroupDestroy, 52 Steps: []resource.TestStep{ 53 resource.TestStep{ 54 Config: testAccDBSubnetGroupConfig_withUnderscoresAndPeriodsAndSpaces, 55 Check: resource.ComposeTestCheckFunc( 56 testAccCheckDBSubnetGroupExists( 57 "aws_db_subnet_group.underscores", &v), 58 testAccCheckDBSubnetGroupExists( 59 "aws_db_subnet_group.periods", &v), 60 testAccCheckDBSubnetGroupExists( 61 "aws_db_subnet_group.spaces", &v), 62 testCheck, 63 ), 64 }, 65 }, 66 }) 67 } 68 69 func testAccCheckDBSubnetGroupDestroy(s *terraform.State) error { 70 conn := testAccProvider.Meta().(*AWSClient).rdsconn 71 72 for _, rs := range s.RootModule().Resources { 73 if rs.Type != "aws_db_subnet_group" { 74 continue 75 } 76 77 // Try to find the resource 78 resp, err := conn.DescribeDBSubnetGroups( 79 &rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)}) 80 if err == nil { 81 if len(resp.DBSubnetGroups) > 0 { 82 return fmt.Errorf("still exist.") 83 } 84 85 return nil 86 } 87 88 // Verify the error is what we want 89 rdserr, ok := err.(awserr.Error) 90 if !ok { 91 return err 92 } 93 if rdserr.Code() != "DBSubnetGroupNotFoundFault" { 94 return err 95 } 96 } 97 98 return nil 99 } 100 101 func testAccCheckDBSubnetGroupExists(n string, v *rds.DBSubnetGroup) resource.TestCheckFunc { 102 return func(s *terraform.State) error { 103 rs, ok := s.RootModule().Resources[n] 104 if !ok { 105 return fmt.Errorf("Not found: %s", n) 106 } 107 108 if rs.Primary.ID == "" { 109 return fmt.Errorf("No ID is set") 110 } 111 112 conn := testAccProvider.Meta().(*AWSClient).rdsconn 113 resp, err := conn.DescribeDBSubnetGroups( 114 &rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)}) 115 if err != nil { 116 return err 117 } 118 if len(resp.DBSubnetGroups) == 0 { 119 return fmt.Errorf("DbSubnetGroup not found") 120 } 121 122 *v = *resp.DBSubnetGroups[0] 123 124 return nil 125 } 126 } 127 128 const testAccDBSubnetGroupConfig = ` 129 resource "aws_vpc" "foo" { 130 cidr_block = "10.1.0.0/16" 131 } 132 133 resource "aws_subnet" "foo" { 134 cidr_block = "10.1.1.0/24" 135 availability_zone = "us-west-2a" 136 vpc_id = "${aws_vpc.foo.id}" 137 tags { 138 Name = "tf-dbsubnet-test-1" 139 } 140 } 141 142 resource "aws_subnet" "bar" { 143 cidr_block = "10.1.2.0/24" 144 availability_zone = "us-west-2b" 145 vpc_id = "${aws_vpc.foo.id}" 146 tags { 147 Name = "tf-dbsubnet-test-2" 148 } 149 } 150 151 resource "aws_db_subnet_group" "foo" { 152 name = "FOO" 153 description = "foo description" 154 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 155 tags { 156 Name = "tf-dbsubnet-group-test" 157 } 158 } 159 ` 160 161 const testAccDBSubnetGroupConfig_withUnderscoresAndPeriodsAndSpaces = ` 162 resource "aws_vpc" "main" { 163 cidr_block = "192.168.0.0/16" 164 } 165 166 resource "aws_subnet" "frontend" { 167 vpc_id = "${aws_vpc.main.id}" 168 availability_zone = "us-west-2b" 169 cidr_block = "192.168.1.0/24" 170 } 171 172 resource "aws_subnet" "backend" { 173 vpc_id = "${aws_vpc.main.id}" 174 availability_zone = "us-west-2c" 175 cidr_block = "192.168.2.0/24" 176 } 177 178 resource "aws_db_subnet_group" "underscores" { 179 name = "with_underscores" 180 description = "Our main group of subnets" 181 subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"] 182 } 183 184 resource "aws_db_subnet_group" "periods" { 185 name = "with.periods" 186 description = "Our main group of subnets" 187 subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"] 188 } 189 190 resource "aws_db_subnet_group" "spaces" { 191 name = "with spaces" 192 description = "Our main group of subnets" 193 subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"] 194 } 195 `