github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 } 285 286 resource "aws_db_parameter_group" "bar" { 287 name = "tfcluster-test-group-%d" 288 family = "aurora5.6" 289 290 parameter { 291 name = "back_log" 292 value = "32767" 293 apply_method = "pending-reboot" 294 } 295 296 tags { 297 foo = "bar" 298 } 299 } 300 `, n, n, n, n) 301 } 302 303 func testAccAWSClusterInstanceEnhancedMonitor(n int) string { 304 return fmt.Sprintf(` 305 resource "aws_rds_cluster" "default" { 306 cluster_identifier = "tf-aurora-cluster-test-%d" 307 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 308 database_name = "mydb" 309 master_username = "foo" 310 master_password = "mustbeeightcharaters" 311 } 312 313 resource "aws_rds_cluster_instance" "cluster_instances" { 314 identifier = "tf-cluster-instance-%d" 315 cluster_identifier = "${aws_rds_cluster.default.id}" 316 instance_class = "db.r3.large" 317 db_parameter_group_name = "${aws_db_parameter_group.bar.name}" 318 monitoring_interval = "60" 319 monitoring_role_arn = "${aws_iam_role.tf_enhanced_monitor_role.arn}" 320 } 321 322 resource "aws_iam_role" "tf_enhanced_monitor_role" { 323 name = "tf_enhanced_monitor_role-%d" 324 assume_role_policy = <<EOF 325 { 326 "Version": "2012-10-17", 327 "Statement": [ 328 { 329 "Action": "sts:AssumeRole", 330 "Principal": { 331 "Service": "monitoring.rds.amazonaws.com" 332 }, 333 "Effect": "Allow", 334 "Sid": "" 335 } 336 ] 337 } 338 EOF 339 } 340 341 resource "aws_iam_policy_attachment" "rds_m_attach" { 342 name = "AmazonRDSEnhancedMonitoringRole" 343 roles = ["${aws_iam_role.tf_enhanced_monitor_role.name}"] 344 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole" 345 } 346 347 resource "aws_db_parameter_group" "bar" { 348 name = "tfcluster-test-group-%d" 349 family = "aurora5.6" 350 351 parameter { 352 name = "back_log" 353 value = "32767" 354 apply_method = "pending-reboot" 355 } 356 357 tags { 358 foo = "bar" 359 } 360 } 361 `, n, n, n, n) 362 }