github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_rds_cluster_instance_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 14 "github.com/aws/aws-sdk-go/aws" 15 "github.com/aws/aws-sdk-go/aws/awserr" 16 "github.com/aws/aws-sdk-go/service/rds" 17 ) 18 19 func TestAccAWSRDSClusterInstance_basic(t *testing.T) { 20 var v rds.DBInstance 21 22 resource.Test(t, resource.TestCase{ 23 PreCheck: func() { testAccPreCheck(t) }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckAWSClusterDestroy, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: testAccAWSClusterInstanceConfig(acctest.RandInt()), 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v), 31 testAccCheckAWSDBClusterInstanceAttributes(&v), 32 ), 33 }, 34 }, 35 }) 36 } 37 38 func TestAccAWSRDSClusterInstance_kmsKey(t *testing.T) { 39 var v rds.DBInstance 40 keyRegex := regexp.MustCompile("^arn:aws:kms:") 41 42 resource.Test(t, resource.TestCase{ 43 PreCheck: func() { testAccPreCheck(t) }, 44 Providers: testAccProviders, 45 CheckDestroy: testAccCheckAWSClusterDestroy, 46 Steps: []resource.TestStep{ 47 resource.TestStep{ 48 Config: testAccAWSClusterInstanceConfigKmsKey(acctest.RandInt()), 49 Check: resource.ComposeTestCheckFunc( 50 testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v), 51 resource.TestMatchResourceAttr( 52 "aws_rds_cluster_instance.cluster_instances", "kms_key_id", keyRegex), 53 ), 54 }, 55 }, 56 }) 57 } 58 59 // https://github.com/hashicorp/terraform/issues/5350 60 func TestAccAWSRDSClusterInstance_disappears(t *testing.T) { 61 var v rds.DBInstance 62 63 resource.Test(t, resource.TestCase{ 64 PreCheck: func() { testAccPreCheck(t) }, 65 Providers: testAccProviders, 66 CheckDestroy: testAccCheckAWSClusterDestroy, 67 Steps: []resource.TestStep{ 68 resource.TestStep{ 69 Config: testAccAWSClusterInstanceConfig(acctest.RandInt()), 70 Check: resource.ComposeTestCheckFunc( 71 testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v), 72 testAccAWSClusterInstanceDisappears(&v), 73 ), 74 // A non-empty plan is what we want. A crash is what we don't want. :) 75 ExpectNonEmptyPlan: true, 76 }, 77 }, 78 }) 79 } 80 81 func testAccCheckAWSClusterInstanceDestroy(s *terraform.State) error { 82 for _, rs := range s.RootModule().Resources { 83 if rs.Type != "aws_rds_cluster" { 84 continue 85 } 86 87 // Try to find the Group 88 conn := testAccProvider.Meta().(*AWSClient).rdsconn 89 var err error 90 resp, err := conn.DescribeDBInstances( 91 &rds.DescribeDBInstancesInput{ 92 DBInstanceIdentifier: aws.String(rs.Primary.ID), 93 }) 94 95 if err == nil { 96 if len(resp.DBInstances) != 0 && 97 *resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID { 98 return fmt.Errorf("DB Cluster Instance %s still exists", rs.Primary.ID) 99 } 100 } 101 102 // Return nil if the Cluster Instance is already destroyed 103 if awsErr, ok := err.(awserr.Error); ok { 104 if awsErr.Code() == "DBInstanceNotFound" { 105 return nil 106 } 107 } 108 109 return err 110 111 } 112 113 return nil 114 } 115 116 func testAccCheckAWSDBClusterInstanceAttributes(v *rds.DBInstance) resource.TestCheckFunc { 117 return func(s *terraform.State) error { 118 119 if *v.Engine != "aurora" { 120 return fmt.Errorf("bad engine, expected \"aurora\": %#v", *v.Engine) 121 } 122 123 if !strings.HasPrefix(*v.DBClusterIdentifier, "tf-aurora-cluster") { 124 return fmt.Errorf("Bad Cluster Identifier prefix:\nexpected: %s\ngot: %s", "tf-aurora-cluster", *v.DBClusterIdentifier) 125 } 126 127 return nil 128 } 129 } 130 131 func testAccAWSClusterInstanceDisappears(v *rds.DBInstance) resource.TestCheckFunc { 132 return func(s *terraform.State) error { 133 conn := testAccProvider.Meta().(*AWSClient).rdsconn 134 opts := &rds.DeleteDBInstanceInput{ 135 DBInstanceIdentifier: v.DBInstanceIdentifier, 136 } 137 if _, err := conn.DeleteDBInstance(opts); err != nil { 138 return err 139 } 140 return resource.Retry(40*time.Minute, func() *resource.RetryError { 141 opts := &rds.DescribeDBInstancesInput{ 142 DBInstanceIdentifier: v.DBInstanceIdentifier, 143 } 144 _, err := conn.DescribeDBInstances(opts) 145 if err != nil { 146 dbinstanceerr, ok := err.(awserr.Error) 147 if ok && dbinstanceerr.Code() == "DBInstanceNotFound" { 148 return nil 149 } 150 return resource.NonRetryableError( 151 fmt.Errorf("Error retrieving DB Instances: %s", err)) 152 } 153 return resource.RetryableError(fmt.Errorf( 154 "Waiting for instance to be deleted: %v", v.DBInstanceIdentifier)) 155 }) 156 } 157 } 158 159 func testAccCheckAWSClusterInstanceExists(n string, v *rds.DBInstance) resource.TestCheckFunc { 160 return func(s *terraform.State) error { 161 rs, ok := s.RootModule().Resources[n] 162 if !ok { 163 return fmt.Errorf("Not found: %s", n) 164 } 165 166 if rs.Primary.ID == "" { 167 return fmt.Errorf("No DB Instance ID is set") 168 } 169 170 conn := testAccProvider.Meta().(*AWSClient).rdsconn 171 resp, err := conn.DescribeDBInstances(&rds.DescribeDBInstancesInput{ 172 DBInstanceIdentifier: aws.String(rs.Primary.ID), 173 }) 174 175 if err != nil { 176 return err 177 } 178 179 for _, d := range resp.DBInstances { 180 if *d.DBInstanceIdentifier == rs.Primary.ID { 181 *v = *d 182 return nil 183 } 184 } 185 186 return fmt.Errorf("DB Cluster (%s) not found", rs.Primary.ID) 187 } 188 } 189 190 func TestAccAWSRDSClusterInstance_withInstanceEnhancedMonitor(t *testing.T) { 191 var v rds.DBInstance 192 193 resource.Test(t, resource.TestCase{ 194 PreCheck: func() { testAccPreCheck(t) }, 195 Providers: testAccProviders, 196 CheckDestroy: testAccCheckAWSClusterDestroy, 197 Steps: []resource.TestStep{ 198 resource.TestStep{ 199 Config: testAccAWSClusterInstanceEnhancedMonitor(acctest.RandInt()), 200 Check: resource.ComposeTestCheckFunc( 201 testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v), 202 testAccCheckAWSDBClusterInstanceAttributes(&v), 203 ), 204 }, 205 }, 206 }) 207 } 208 209 // Add some random to the name, to avoid collision 210 func testAccAWSClusterInstanceConfig(n int) string { 211 return fmt.Sprintf(` 212 resource "aws_rds_cluster" "default" { 213 cluster_identifier = "tf-aurora-cluster-test-%d" 214 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 215 database_name = "mydb" 216 master_username = "foo" 217 master_password = "mustbeeightcharaters" 218 } 219 220 resource "aws_rds_cluster_instance" "cluster_instances" { 221 identifier = "tf-cluster-instance-%d" 222 cluster_identifier = "${aws_rds_cluster.default.id}" 223 instance_class = "db.r3.large" 224 db_parameter_group_name = "${aws_db_parameter_group.bar.name}" 225 promotion_tier = "3" 226 } 227 228 resource "aws_db_parameter_group" "bar" { 229 name = "tfcluster-test-group-%d" 230 family = "aurora5.6" 231 232 parameter { 233 name = "back_log" 234 value = "32767" 235 apply_method = "pending-reboot" 236 } 237 238 tags { 239 foo = "bar" 240 } 241 } 242 `, n, n, n) 243 } 244 245 func testAccAWSClusterInstanceConfigKmsKey(n int) string { 246 return fmt.Sprintf(` 247 248 resource "aws_kms_key" "foo" { 249 description = "Terraform acc test %d" 250 policy = <<POLICY 251 { 252 "Version": "2012-10-17", 253 "Id": "kms-tf-1", 254 "Statement": [ 255 { 256 "Sid": "Enable IAM User Permissions", 257 "Effect": "Allow", 258 "Principal": { 259 "AWS": "*" 260 }, 261 "Action": "kms:*", 262 "Resource": "*" 263 } 264 ] 265 } 266 POLICY 267 } 268 269 resource "aws_rds_cluster" "default" { 270 cluster_identifier = "tf-aurora-cluster-test-%d" 271 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 272 database_name = "mydb" 273 master_username = "foo" 274 master_password = "mustbeeightcharaters" 275 storage_encrypted = true 276 kms_key_id = "${aws_kms_key.foo.arn}" 277 } 278 279 resource "aws_rds_cluster_instance" "cluster_instances" { 280 identifier = "tf-cluster-instance-%d" 281 cluster_identifier = "${aws_rds_cluster.default.id}" 282 instance_class = "db.r3.large" 283 db_parameter_group_name = "${aws_db_parameter_group.bar.name}" 284 storage_encrypted = true 285 kms_key_id = "${aws_kms_key.foo.arn}" 286 } 287 288 resource "aws_db_parameter_group" "bar" { 289 name = "tfcluster-test-group-%d" 290 family = "aurora5.6" 291 292 parameter { 293 name = "back_log" 294 value = "32767" 295 apply_method = "pending-reboot" 296 } 297 298 tags { 299 foo = "bar" 300 } 301 } 302 `, n, n, n, n) 303 } 304 305 func testAccAWSClusterInstanceEnhancedMonitor(n int) string { 306 return fmt.Sprintf(` 307 resource "aws_rds_cluster" "default" { 308 cluster_identifier = "tf-aurora-cluster-test-%d" 309 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 310 database_name = "mydb" 311 master_username = "foo" 312 master_password = "mustbeeightcharaters" 313 } 314 315 resource "aws_rds_cluster_instance" "cluster_instances" { 316 identifier = "tf-cluster-instance-%d" 317 cluster_identifier = "${aws_rds_cluster.default.id}" 318 instance_class = "db.r3.large" 319 db_parameter_group_name = "${aws_db_parameter_group.bar.name}" 320 monitoring_interval = "60" 321 monitoring_role_arn = "${aws_iam_role.tf_enhanced_monitor_role.arn}" 322 } 323 324 resource "aws_iam_role" "tf_enhanced_monitor_role" { 325 name = "tf_enhanced_monitor_role-%d" 326 assume_role_policy = <<EOF 327 { 328 "Version": "2012-10-17", 329 "Statement": [ 330 { 331 "Action": "sts:AssumeRole", 332 "Principal": { 333 "Service": "monitoring.rds.amazonaws.com" 334 }, 335 "Effect": "Allow", 336 "Sid": "" 337 } 338 ] 339 } 340 EOF 341 } 342 343 resource "aws_iam_policy_attachment" "rds_m_attach" { 344 name = "AmazonRDSEnhancedMonitoringRole" 345 roles = ["${aws_iam_role.tf_enhanced_monitor_role.name}"] 346 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole" 347 } 348 349 resource "aws_db_parameter_group" "bar" { 350 name = "tfcluster-test-group-%d" 351 family = "aurora5.6" 352 353 parameter { 354 name = "back_log" 355 value = "32767" 356 apply_method = "pending-reboot" 357 } 358 359 tags { 360 foo = "bar" 361 } 362 } 363 `, n, n, n, n) 364 }