github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_db_option_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/aws/awserr" 10 "github.com/aws/aws-sdk-go/service/rds" 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSDBOptionGroup_basic(t *testing.T) { 17 var v rds.OptionGroup 18 rName := fmt.Sprintf("option-group-test-terraform-%s", acctest.RandString(5)) 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckAWSDBOptionGroupDestroy, 24 Steps: []resource.TestStep{ 25 resource.TestStep{ 26 Config: testAccAWSDBOptionGroupBasicConfig(rName), 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckAWSDBOptionGroupExists("aws_db_option_group.bar", &v), 29 testAccCheckAWSDBOptionGroupAttributes(&v), 30 resource.TestCheckResourceAttr( 31 "aws_db_option_group.bar", "name", rName), 32 ), 33 }, 34 }, 35 }) 36 } 37 38 func TestAccAWSDBOptionGroup_namePrefix(t *testing.T) { 39 var v rds.OptionGroup 40 41 resource.Test(t, resource.TestCase{ 42 PreCheck: func() { testAccPreCheck(t) }, 43 Providers: testAccProviders, 44 CheckDestroy: testAccCheckAWSDBOptionGroupDestroy, 45 Steps: []resource.TestStep{ 46 resource.TestStep{ 47 Config: testAccAWSDBOptionGroup_namePrefix, 48 Check: resource.ComposeTestCheckFunc( 49 testAccCheckAWSDBOptionGroupExists("aws_db_option_group.test", &v), 50 testAccCheckAWSDBOptionGroupAttributes(&v), 51 resource.TestMatchResourceAttr( 52 "aws_db_option_group.test", "name", regexp.MustCompile("^tf-test-")), 53 ), 54 }, 55 }, 56 }) 57 } 58 59 func TestAccAWSDBOptionGroup_generatedName(t *testing.T) { 60 var v rds.OptionGroup 61 62 resource.Test(t, resource.TestCase{ 63 PreCheck: func() { testAccPreCheck(t) }, 64 Providers: testAccProviders, 65 CheckDestroy: testAccCheckAWSDBOptionGroupDestroy, 66 Steps: []resource.TestStep{ 67 resource.TestStep{ 68 Config: testAccAWSDBOptionGroup_generatedName, 69 Check: resource.ComposeTestCheckFunc( 70 testAccCheckAWSDBOptionGroupExists("aws_db_option_group.test", &v), 71 testAccCheckAWSDBOptionGroupAttributes(&v), 72 ), 73 }, 74 }, 75 }) 76 } 77 78 func TestAccAWSDBOptionGroup_defaultDescription(t *testing.T) { 79 var v rds.OptionGroup 80 81 resource.Test(t, resource.TestCase{ 82 PreCheck: func() { testAccPreCheck(t) }, 83 Providers: testAccProviders, 84 CheckDestroy: testAccCheckAWSDBOptionGroupDestroy, 85 Steps: []resource.TestStep{ 86 resource.TestStep{ 87 Config: testAccAWSDBOptionGroup_defaultDescription(acctest.RandInt()), 88 Check: resource.ComposeTestCheckFunc( 89 testAccCheckAWSDBOptionGroupExists("aws_db_option_group.test", &v), 90 resource.TestCheckResourceAttr( 91 "aws_db_option_group.test", "option_group_description", "Managed by Terraform"), 92 ), 93 }, 94 }, 95 }) 96 } 97 98 func TestAccAWSDBOptionGroup_basicDestroyWithInstance(t *testing.T) { 99 rName := fmt.Sprintf("option-group-test-terraform-%s", acctest.RandString(5)) 100 101 resource.Test(t, resource.TestCase{ 102 PreCheck: func() { testAccPreCheck(t) }, 103 Providers: testAccProviders, 104 CheckDestroy: testAccCheckAWSDBOptionGroupDestroy, 105 Steps: []resource.TestStep{ 106 resource.TestStep{ 107 Config: testAccAWSDBOptionGroupBasicDestroyConfig(rName), 108 }, 109 }, 110 }) 111 } 112 113 func TestAccAWSDBOptionGroup_OptionSettings(t *testing.T) { 114 var v rds.OptionGroup 115 rName := fmt.Sprintf("option-group-test-terraform-%s", acctest.RandString(5)) 116 117 resource.Test(t, resource.TestCase{ 118 PreCheck: func() { testAccPreCheck(t) }, 119 Providers: testAccProviders, 120 CheckDestroy: testAccCheckAWSDBOptionGroupDestroy, 121 Steps: []resource.TestStep{ 122 resource.TestStep{ 123 Config: testAccAWSDBOptionGroupOptionSettings(rName), 124 Check: resource.ComposeTestCheckFunc( 125 testAccCheckAWSDBOptionGroupExists("aws_db_option_group.bar", &v), 126 resource.TestCheckResourceAttr( 127 "aws_db_option_group.bar", "name", rName), 128 resource.TestCheckResourceAttr( 129 "aws_db_option_group.bar", "option.#", "1"), 130 resource.TestCheckResourceAttr( 131 "aws_db_option_group.bar", "option.961211605.option_settings.129825347.value", "UTC"), 132 ), 133 }, 134 resource.TestStep{ 135 Config: testAccAWSDBOptionGroupOptionSettings_update(rName), 136 Check: resource.ComposeTestCheckFunc( 137 testAccCheckAWSDBOptionGroupExists("aws_db_option_group.bar", &v), 138 resource.TestCheckResourceAttr( 139 "aws_db_option_group.bar", "name", rName), 140 resource.TestCheckResourceAttr( 141 "aws_db_option_group.bar", "option.#", "1"), 142 resource.TestCheckResourceAttr( 143 "aws_db_option_group.bar", "option.2422743510.option_settings.1350509764.value", "US/Pacific"), 144 ), 145 }, 146 }, 147 }) 148 } 149 150 func TestAccAWSDBOptionGroup_sqlServerOptionsUpdate(t *testing.T) { 151 var v rds.OptionGroup 152 rName := fmt.Sprintf("option-group-test-terraform-%s", acctest.RandString(5)) 153 154 resource.Test(t, resource.TestCase{ 155 PreCheck: func() { testAccPreCheck(t) }, 156 Providers: testAccProviders, 157 CheckDestroy: testAccCheckAWSDBOptionGroupDestroy, 158 Steps: []resource.TestStep{ 159 resource.TestStep{ 160 Config: testAccAWSDBOptionGroupSqlServerEEOptions(rName), 161 Check: resource.ComposeTestCheckFunc( 162 testAccCheckAWSDBOptionGroupExists("aws_db_option_group.bar", &v), 163 resource.TestCheckResourceAttr( 164 "aws_db_option_group.bar", "name", rName), 165 ), 166 }, 167 168 resource.TestStep{ 169 Config: testAccAWSDBOptionGroupSqlServerEEOptions_update(rName), 170 Check: resource.ComposeTestCheckFunc( 171 testAccCheckAWSDBOptionGroupExists("aws_db_option_group.bar", &v), 172 resource.TestCheckResourceAttr( 173 "aws_db_option_group.bar", "name", rName), 174 resource.TestCheckResourceAttr( 175 "aws_db_option_group.bar", "option.#", "1"), 176 ), 177 }, 178 }, 179 }) 180 } 181 182 func TestAccAWSDBOptionGroup_multipleOptions(t *testing.T) { 183 var v rds.OptionGroup 184 rName := fmt.Sprintf("option-group-test-terraform-%s", acctest.RandString(5)) 185 186 resource.Test(t, resource.TestCase{ 187 PreCheck: func() { testAccPreCheck(t) }, 188 Providers: testAccProviders, 189 CheckDestroy: testAccCheckAWSDBOptionGroupDestroy, 190 Steps: []resource.TestStep{ 191 resource.TestStep{ 192 Config: testAccAWSDBOptionGroupMultipleOptions(rName), 193 Check: resource.ComposeTestCheckFunc( 194 testAccCheckAWSDBOptionGroupExists("aws_db_option_group.bar", &v), 195 resource.TestCheckResourceAttr( 196 "aws_db_option_group.bar", "name", rName), 197 resource.TestCheckResourceAttr( 198 "aws_db_option_group.bar", "option.#", "2"), 199 ), 200 }, 201 }, 202 }) 203 } 204 205 func testAccCheckAWSDBOptionGroupAttributes(v *rds.OptionGroup) resource.TestCheckFunc { 206 return func(s *terraform.State) error { 207 208 if *v.EngineName != "mysql" { 209 return fmt.Errorf("bad engine_name: %#v", *v.EngineName) 210 } 211 212 if *v.MajorEngineVersion != "5.6" { 213 return fmt.Errorf("bad major_engine_version: %#v", *v.MajorEngineVersion) 214 } 215 216 if *v.OptionGroupDescription != "Test option group for terraform" { 217 return fmt.Errorf("bad option_group_description: %#v", *v.OptionGroupDescription) 218 } 219 220 return nil 221 } 222 } 223 224 func testAccCheckAWSDBOptionGroupExists(n string, v *rds.OptionGroup) resource.TestCheckFunc { 225 return func(s *terraform.State) error { 226 rs, ok := s.RootModule().Resources[n] 227 if !ok { 228 return fmt.Errorf("Not found: %s", n) 229 } 230 231 if rs.Primary.ID == "" { 232 return fmt.Errorf("No DB Option Group Name is set") 233 } 234 235 conn := testAccProvider.Meta().(*AWSClient).rdsconn 236 237 opts := rds.DescribeOptionGroupsInput{ 238 OptionGroupName: aws.String(rs.Primary.ID), 239 } 240 241 resp, err := conn.DescribeOptionGroups(&opts) 242 243 if err != nil { 244 return err 245 } 246 247 if len(resp.OptionGroupsList) != 1 || 248 *resp.OptionGroupsList[0].OptionGroupName != rs.Primary.ID { 249 return fmt.Errorf("DB Option Group not found") 250 } 251 252 *v = *resp.OptionGroupsList[0] 253 254 return nil 255 } 256 } 257 258 func testAccCheckAWSDBOptionGroupDestroy(s *terraform.State) error { 259 conn := testAccProvider.Meta().(*AWSClient).rdsconn 260 261 for _, rs := range s.RootModule().Resources { 262 if rs.Type != "aws_db_option_group" { 263 continue 264 } 265 266 resp, err := conn.DescribeOptionGroups( 267 &rds.DescribeOptionGroupsInput{ 268 OptionGroupName: aws.String(rs.Primary.ID), 269 }) 270 271 if err == nil { 272 if len(resp.OptionGroupsList) != 0 && 273 *resp.OptionGroupsList[0].OptionGroupName == rs.Primary.ID { 274 return fmt.Errorf("DB Option Group still exists") 275 } 276 } 277 278 // Verify the error 279 newerr, ok := err.(awserr.Error) 280 if !ok { 281 return err 282 } 283 if newerr.Code() != "OptionGroupNotFoundFault" { 284 return err 285 } 286 } 287 288 return nil 289 } 290 291 func testAccAWSDBOptionGroupBasicConfig(r string) string { 292 return fmt.Sprintf(` 293 resource "aws_db_option_group" "bar" { 294 name = "%s" 295 option_group_description = "Test option group for terraform" 296 engine_name = "mysql" 297 major_engine_version = "5.6" 298 } 299 `, r) 300 } 301 302 func testAccAWSDBOptionGroupBasicDestroyConfig(r string) string { 303 return fmt.Sprintf(` 304 resource "aws_db_instance" "bar" { 305 allocated_storage = 10 306 engine = "MySQL" 307 engine_version = "5.6.21" 308 instance_class = "db.t2.micro" 309 name = "baz" 310 password = "barbarbarbar" 311 username = "foo" 312 313 314 # Maintenance Window is stored in lower case in the API, though not strictly 315 # documented. Terraform will downcase this to match (as opposed to throw a 316 # validation error). 317 maintenance_window = "Fri:09:00-Fri:09:30" 318 319 backup_retention_period = 0 320 skip_final_snapshot = true 321 322 option_group_name = "${aws_db_option_group.bar.name}" 323 } 324 325 resource "aws_db_option_group" "bar" { 326 name = "%s" 327 option_group_description = "Test option group for terraform" 328 engine_name = "mysql" 329 major_engine_version = "5.6" 330 } 331 `, r) 332 } 333 334 func testAccAWSDBOptionGroupOptionSettings(r string) string { 335 return fmt.Sprintf(` 336 resource "aws_db_option_group" "bar" { 337 name = "%s" 338 option_group_description = "Test option group for terraform" 339 engine_name = "oracle-ee" 340 major_engine_version = "11.2" 341 342 option { 343 option_name = "Timezone" 344 option_settings { 345 name = "TIME_ZONE" 346 value = "UTC" 347 } 348 } 349 } 350 `, r) 351 } 352 353 func testAccAWSDBOptionGroupOptionSettings_update(r string) string { 354 return fmt.Sprintf(` 355 resource "aws_db_option_group" "bar" { 356 name = "%s" 357 option_group_description = "Test option group for terraform" 358 engine_name = "oracle-ee" 359 major_engine_version = "11.2" 360 361 option { 362 option_name = "Timezone" 363 option_settings { 364 name = "TIME_ZONE" 365 value = "US/Pacific" 366 } 367 } 368 } 369 `, r) 370 } 371 372 func testAccAWSDBOptionGroupSqlServerEEOptions(r string) string { 373 return fmt.Sprintf(` 374 resource "aws_db_option_group" "bar" { 375 name = "%s" 376 option_group_description = "Test option group for terraform" 377 engine_name = "sqlserver-ee" 378 major_engine_version = "11.00" 379 } 380 `, r) 381 } 382 383 func testAccAWSDBOptionGroupSqlServerEEOptions_update(r string) string { 384 return fmt.Sprintf(` 385 resource "aws_db_option_group" "bar" { 386 name = "%s" 387 option_group_description = "Test option group for terraform" 388 engine_name = "sqlserver-ee" 389 major_engine_version = "11.00" 390 391 option { 392 option_name = "Mirroring" 393 } 394 } 395 `, r) 396 } 397 398 func testAccAWSDBOptionGroupMultipleOptions(r string) string { 399 return fmt.Sprintf(` 400 resource "aws_db_option_group" "bar" { 401 name = "%s" 402 option_group_description = "Test option group for terraform" 403 engine_name = "oracle-se" 404 major_engine_version = "11.2" 405 406 option { 407 option_name = "STATSPACK" 408 } 409 410 option { 411 option_name = "XMLDB" 412 } 413 } 414 `, r) 415 } 416 417 const testAccAWSDBOptionGroup_namePrefix = ` 418 resource "aws_db_option_group" "test" { 419 name_prefix = "tf-test-" 420 option_group_description = "Test option group for terraform" 421 engine_name = "mysql" 422 major_engine_version = "5.6" 423 } 424 ` 425 426 const testAccAWSDBOptionGroup_generatedName = ` 427 resource "aws_db_option_group" "test" { 428 option_group_description = "Test option group for terraform" 429 engine_name = "mysql" 430 major_engine_version = "5.6" 431 } 432 ` 433 434 func testAccAWSDBOptionGroup_defaultDescription(n int) string { 435 return fmt.Sprintf(` 436 resource "aws_db_option_group" "test" { 437 name = "tf-test-%d" 438 engine_name = "mysql" 439 major_engine_version = "5.6" 440 } 441 `, n) 442 }