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