github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/aws/resource_aws_db_subnet_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 12 "github.com/aws/aws-sdk-go/aws" 13 "github.com/aws/aws-sdk-go/aws/awserr" 14 "github.com/aws/aws-sdk-go/service/rds" 15 ) 16 17 func TestAccAWSDBSubnetGroup_basic(t *testing.T) { 18 var v rds.DBSubnetGroup 19 20 testCheck := func(*terraform.State) error { 21 return nil 22 } 23 24 rName := fmt.Sprintf("tf-test-%d", acctest.RandInt()) 25 26 resource.Test(t, resource.TestCase{ 27 PreCheck: func() { testAccPreCheck(t) }, 28 Providers: testAccProviders, 29 CheckDestroy: testAccCheckDBSubnetGroupDestroy, 30 Steps: []resource.TestStep{ 31 resource.TestStep{ 32 Config: testAccDBSubnetGroupConfig(rName), 33 Check: resource.ComposeTestCheckFunc( 34 testAccCheckDBSubnetGroupExists( 35 "aws_db_subnet_group.foo", &v), 36 resource.TestCheckResourceAttr( 37 "aws_db_subnet_group.foo", "name", rName), 38 resource.TestCheckResourceAttr( 39 "aws_db_subnet_group.foo", "description", "Managed by Terraform"), 40 testCheck, 41 ), 42 }, 43 }, 44 }) 45 } 46 47 func TestAccAWSDBSubnetGroup_namePrefix(t *testing.T) { 48 var v rds.DBSubnetGroup 49 50 resource.Test(t, resource.TestCase{ 51 PreCheck: func() { testAccPreCheck(t) }, 52 Providers: testAccProviders, 53 CheckDestroy: testAccCheckDBSubnetGroupDestroy, 54 Steps: []resource.TestStep{ 55 resource.TestStep{ 56 Config: testAccDBSubnetGroupConfig_namePrefix, 57 Check: resource.ComposeTestCheckFunc( 58 testAccCheckDBSubnetGroupExists( 59 "aws_db_subnet_group.test", &v), 60 resource.TestMatchResourceAttr( 61 "aws_db_subnet_group.test", "name", regexp.MustCompile("^tf_test-")), 62 ), 63 }, 64 }, 65 }) 66 } 67 68 func TestAccAWSDBSubnetGroup_generatedName(t *testing.T) { 69 var v rds.DBSubnetGroup 70 71 resource.Test(t, resource.TestCase{ 72 PreCheck: func() { testAccPreCheck(t) }, 73 Providers: testAccProviders, 74 CheckDestroy: testAccCheckDBSubnetGroupDestroy, 75 Steps: []resource.TestStep{ 76 resource.TestStep{ 77 Config: testAccDBSubnetGroupConfig_generatedName, 78 Check: resource.ComposeTestCheckFunc( 79 testAccCheckDBSubnetGroupExists( 80 "aws_db_subnet_group.test", &v), 81 ), 82 }, 83 }, 84 }) 85 } 86 87 // Regression test for https://github.com/hashicorp/terraform/issues/2603 and 88 // https://github.com/hashicorp/terraform/issues/2664 89 func TestAccAWSDBSubnetGroup_withUndocumentedCharacters(t *testing.T) { 90 var v rds.DBSubnetGroup 91 92 testCheck := func(*terraform.State) error { 93 return nil 94 } 95 96 resource.Test(t, resource.TestCase{ 97 PreCheck: func() { testAccPreCheck(t) }, 98 Providers: testAccProviders, 99 CheckDestroy: testAccCheckDBSubnetGroupDestroy, 100 Steps: []resource.TestStep{ 101 resource.TestStep{ 102 Config: testAccDBSubnetGroupConfig_withUnderscoresAndPeriodsAndSpaces, 103 Check: resource.ComposeTestCheckFunc( 104 testAccCheckDBSubnetGroupExists( 105 "aws_db_subnet_group.underscores", &v), 106 testAccCheckDBSubnetGroupExists( 107 "aws_db_subnet_group.periods", &v), 108 testAccCheckDBSubnetGroupExists( 109 "aws_db_subnet_group.spaces", &v), 110 testCheck, 111 ), 112 }, 113 }, 114 }) 115 } 116 117 func TestAccAWSDBSubnetGroup_updateDescription(t *testing.T) { 118 var v rds.DBSubnetGroup 119 120 rName := fmt.Sprintf("tf-test-%d", acctest.RandInt()) 121 resource.Test(t, resource.TestCase{ 122 PreCheck: func() { testAccPreCheck(t) }, 123 Providers: testAccProviders, 124 CheckDestroy: testAccCheckDBSubnetGroupDestroy, 125 Steps: []resource.TestStep{ 126 resource.TestStep{ 127 Config: testAccDBSubnetGroupConfig(rName), 128 Check: resource.ComposeTestCheckFunc( 129 testAccCheckDBSubnetGroupExists( 130 "aws_db_subnet_group.foo", &v), 131 resource.TestCheckResourceAttr( 132 "aws_db_subnet_group.foo", "description", "Managed by Terraform"), 133 ), 134 }, 135 136 resource.TestStep{ 137 Config: testAccDBSubnetGroupConfig_updatedDescription(rName), 138 Check: resource.ComposeTestCheckFunc( 139 testAccCheckDBSubnetGroupExists( 140 "aws_db_subnet_group.foo", &v), 141 resource.TestCheckResourceAttr( 142 "aws_db_subnet_group.foo", "description", "foo description updated"), 143 ), 144 }, 145 }, 146 }) 147 } 148 149 func testAccCheckDBSubnetGroupDestroy(s *terraform.State) error { 150 conn := testAccProvider.Meta().(*AWSClient).rdsconn 151 152 for _, rs := range s.RootModule().Resources { 153 if rs.Type != "aws_db_subnet_group" { 154 continue 155 } 156 157 // Try to find the resource 158 resp, err := conn.DescribeDBSubnetGroups( 159 &rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)}) 160 if err == nil { 161 if len(resp.DBSubnetGroups) > 0 { 162 return fmt.Errorf("still exist.") 163 } 164 165 return nil 166 } 167 168 // Verify the error is what we want 169 rdserr, ok := err.(awserr.Error) 170 if !ok { 171 return err 172 } 173 if rdserr.Code() != "DBSubnetGroupNotFoundFault" { 174 return err 175 } 176 } 177 178 return nil 179 } 180 181 func testAccCheckDBSubnetGroupExists(n string, v *rds.DBSubnetGroup) resource.TestCheckFunc { 182 return func(s *terraform.State) error { 183 rs, ok := s.RootModule().Resources[n] 184 if !ok { 185 return fmt.Errorf("Not found: %s", n) 186 } 187 188 if rs.Primary.ID == "" { 189 return fmt.Errorf("No ID is set") 190 } 191 192 conn := testAccProvider.Meta().(*AWSClient).rdsconn 193 resp, err := conn.DescribeDBSubnetGroups( 194 &rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)}) 195 if err != nil { 196 return err 197 } 198 if len(resp.DBSubnetGroups) == 0 { 199 return fmt.Errorf("DbSubnetGroup not found") 200 } 201 202 *v = *resp.DBSubnetGroups[0] 203 204 return nil 205 } 206 } 207 208 func testAccDBSubnetGroupConfig(rName string) string { 209 return fmt.Sprintf(` 210 resource "aws_vpc" "foo" { 211 cidr_block = "10.1.0.0/16" 212 tags { 213 Name = "testAccDBSubnetGroupConfig" 214 } 215 } 216 217 resource "aws_subnet" "foo" { 218 cidr_block = "10.1.1.0/24" 219 availability_zone = "us-west-2a" 220 vpc_id = "${aws_vpc.foo.id}" 221 tags { 222 Name = "tf-dbsubnet-test-1" 223 } 224 } 225 226 resource "aws_subnet" "bar" { 227 cidr_block = "10.1.2.0/24" 228 availability_zone = "us-west-2b" 229 vpc_id = "${aws_vpc.foo.id}" 230 tags { 231 Name = "tf-dbsubnet-test-2" 232 } 233 } 234 235 resource "aws_db_subnet_group" "foo" { 236 name = "%s" 237 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 238 tags { 239 Name = "tf-dbsubnet-group-test" 240 } 241 }`, rName) 242 } 243 244 func testAccDBSubnetGroupConfig_updatedDescription(rName string) string { 245 return fmt.Sprintf(` 246 resource "aws_vpc" "foo" { 247 cidr_block = "10.1.0.0/16" 248 tags { 249 Name = "testAccDBSubnetGroupConfig_updatedDescription" 250 } 251 } 252 253 resource "aws_subnet" "foo" { 254 cidr_block = "10.1.1.0/24" 255 availability_zone = "us-west-2a" 256 vpc_id = "${aws_vpc.foo.id}" 257 tags { 258 Name = "tf-dbsubnet-test-1" 259 } 260 } 261 262 resource "aws_subnet" "bar" { 263 cidr_block = "10.1.2.0/24" 264 availability_zone = "us-west-2b" 265 vpc_id = "${aws_vpc.foo.id}" 266 tags { 267 Name = "tf-dbsubnet-test-2" 268 } 269 } 270 271 resource "aws_db_subnet_group" "foo" { 272 name = "%s" 273 description = "foo description updated" 274 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 275 tags { 276 Name = "tf-dbsubnet-group-test" 277 } 278 }`, rName) 279 } 280 281 const testAccDBSubnetGroupConfig_namePrefix = ` 282 resource "aws_vpc" "test" { 283 cidr_block = "10.1.0.0/16" 284 tags { 285 Name = "testAccDBSubnetGroupConfig_namePrefix" 286 } 287 } 288 289 resource "aws_subnet" "a" { 290 vpc_id = "${aws_vpc.test.id}" 291 cidr_block = "10.1.1.0/24" 292 availability_zone = "us-west-2a" 293 } 294 295 resource "aws_subnet" "b" { 296 vpc_id = "${aws_vpc.test.id}" 297 cidr_block = "10.1.2.0/24" 298 availability_zone = "us-west-2b" 299 } 300 301 resource "aws_db_subnet_group" "test" { 302 name_prefix = "tf_test-" 303 subnet_ids = ["${aws_subnet.a.id}", "${aws_subnet.b.id}"] 304 }` 305 306 const testAccDBSubnetGroupConfig_generatedName = ` 307 resource "aws_vpc" "test" { 308 cidr_block = "10.1.0.0/16" 309 tags { 310 Name = "testAccDBSubnetGroupConfig_generatedName" 311 } 312 } 313 314 resource "aws_subnet" "a" { 315 vpc_id = "${aws_vpc.test.id}" 316 cidr_block = "10.1.1.0/24" 317 availability_zone = "us-west-2a" 318 } 319 320 resource "aws_subnet" "b" { 321 vpc_id = "${aws_vpc.test.id}" 322 cidr_block = "10.1.2.0/24" 323 availability_zone = "us-west-2b" 324 } 325 326 resource "aws_db_subnet_group" "test" { 327 subnet_ids = ["${aws_subnet.a.id}", "${aws_subnet.b.id}"] 328 }` 329 330 const testAccDBSubnetGroupConfig_withUnderscoresAndPeriodsAndSpaces = ` 331 resource "aws_vpc" "main" { 332 cidr_block = "192.168.0.0/16" 333 tags { 334 Name = "testAccDBSubnetGroupConfig_withUnderscoresAndPeriodsAndSpaces" 335 } 336 } 337 338 resource "aws_subnet" "frontend" { 339 vpc_id = "${aws_vpc.main.id}" 340 availability_zone = "us-west-2b" 341 cidr_block = "192.168.1.0/24" 342 } 343 344 resource "aws_subnet" "backend" { 345 vpc_id = "${aws_vpc.main.id}" 346 availability_zone = "us-west-2c" 347 cidr_block = "192.168.2.0/24" 348 } 349 350 resource "aws_db_subnet_group" "underscores" { 351 name = "with_underscores" 352 description = "Our main group of subnets" 353 subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"] 354 } 355 356 resource "aws_db_subnet_group" "periods" { 357 name = "with.periods" 358 description = "Our main group of subnets" 359 subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"] 360 } 361 362 resource "aws_db_subnet_group" "spaces" { 363 name = "with spaces" 364 description = "Our main group of subnets" 365 subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"] 366 } 367 `