github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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  					resource.TestCheckResourceAttrSet("aws_rds_cluster_instance.cluster_instances", "preferred_maintenance_window"),
    34  					resource.TestCheckResourceAttrSet("aws_rds_cluster_instance.cluster_instances", "preferred_backup_window"),
    35  				),
    36  			},
    37  			{
    38  				Config: testAccAWSClusterInstanceConfigModified(acctest.RandInt()),
    39  				Check: resource.ComposeTestCheckFunc(
    40  					testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v),
    41  					testAccCheckAWSDBClusterInstanceAttributes(&v),
    42  					resource.TestCheckResourceAttr("aws_rds_cluster_instance.cluster_instances", "auto_minor_version_upgrade", "false"),
    43  				),
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func TestAccAWSRDSClusterInstance_namePrefix(t *testing.T) {
    50  	var v rds.DBInstance
    51  
    52  	resource.Test(t, resource.TestCase{
    53  		PreCheck:     func() { testAccPreCheck(t) },
    54  		Providers:    testAccProviders,
    55  		CheckDestroy: testAccCheckAWSClusterDestroy,
    56  		Steps: []resource.TestStep{
    57  			{
    58  				Config: testAccAWSClusterInstanceConfig_namePrefix(acctest.RandInt()),
    59  				Check: resource.ComposeTestCheckFunc(
    60  					testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.test", &v),
    61  					testAccCheckAWSDBClusterInstanceAttributes(&v),
    62  					resource.TestMatchResourceAttr(
    63  						"aws_rds_cluster_instance.test", "identifier", regexp.MustCompile("^tf-cluster-instance-")),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func TestAccAWSRDSClusterInstance_generatedName(t *testing.T) {
    71  	var v rds.DBInstance
    72  
    73  	resource.Test(t, resource.TestCase{
    74  		PreCheck:     func() { testAccPreCheck(t) },
    75  		Providers:    testAccProviders,
    76  		CheckDestroy: testAccCheckAWSClusterDestroy,
    77  		Steps: []resource.TestStep{
    78  			{
    79  				Config: testAccAWSClusterInstanceConfig_generatedName(acctest.RandInt()),
    80  				Check: resource.ComposeTestCheckFunc(
    81  					testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.test", &v),
    82  					testAccCheckAWSDBClusterInstanceAttributes(&v),
    83  					resource.TestMatchResourceAttr(
    84  						"aws_rds_cluster_instance.test", "identifier", regexp.MustCompile("^tf-")),
    85  				),
    86  			},
    87  		},
    88  	})
    89  }
    90  
    91  func TestAccAWSRDSClusterInstance_kmsKey(t *testing.T) {
    92  	var v rds.DBInstance
    93  	keyRegex := regexp.MustCompile("^arn:aws:kms:")
    94  
    95  	resource.Test(t, resource.TestCase{
    96  		PreCheck:     func() { testAccPreCheck(t) },
    97  		Providers:    testAccProviders,
    98  		CheckDestroy: testAccCheckAWSClusterDestroy,
    99  		Steps: []resource.TestStep{
   100  			{
   101  				Config: testAccAWSClusterInstanceConfigKmsKey(acctest.RandInt()),
   102  				Check: resource.ComposeTestCheckFunc(
   103  					testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v),
   104  					resource.TestMatchResourceAttr(
   105  						"aws_rds_cluster_instance.cluster_instances", "kms_key_id", keyRegex),
   106  				),
   107  			},
   108  		},
   109  	})
   110  }
   111  
   112  // https://github.com/hashicorp/terraform/issues/5350
   113  func TestAccAWSRDSClusterInstance_disappears(t *testing.T) {
   114  	var v rds.DBInstance
   115  
   116  	resource.Test(t, resource.TestCase{
   117  		PreCheck:     func() { testAccPreCheck(t) },
   118  		Providers:    testAccProviders,
   119  		CheckDestroy: testAccCheckAWSClusterDestroy,
   120  		Steps: []resource.TestStep{
   121  			{
   122  				Config: testAccAWSClusterInstanceConfig(acctest.RandInt()),
   123  				Check: resource.ComposeTestCheckFunc(
   124  					testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v),
   125  					testAccAWSClusterInstanceDisappears(&v),
   126  				),
   127  				// A non-empty plan is what we want. A crash is what we don't want. :)
   128  				ExpectNonEmptyPlan: true,
   129  			},
   130  		},
   131  	})
   132  }
   133  
   134  func testAccCheckAWSDBClusterInstanceAttributes(v *rds.DBInstance) resource.TestCheckFunc {
   135  	return func(s *terraform.State) error {
   136  
   137  		if *v.Engine != "aurora" {
   138  			return fmt.Errorf("bad engine, expected \"aurora\": %#v", *v.Engine)
   139  		}
   140  
   141  		if !strings.HasPrefix(*v.DBClusterIdentifier, "tf-aurora-cluster") {
   142  			return fmt.Errorf("Bad Cluster Identifier prefix:\nexpected: %s\ngot: %s", "tf-aurora-cluster", *v.DBClusterIdentifier)
   143  		}
   144  
   145  		return nil
   146  	}
   147  }
   148  
   149  func testAccAWSClusterInstanceDisappears(v *rds.DBInstance) resource.TestCheckFunc {
   150  	return func(s *terraform.State) error {
   151  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   152  		opts := &rds.DeleteDBInstanceInput{
   153  			DBInstanceIdentifier: v.DBInstanceIdentifier,
   154  		}
   155  		if _, err := conn.DeleteDBInstance(opts); err != nil {
   156  			return err
   157  		}
   158  		return resource.Retry(40*time.Minute, func() *resource.RetryError {
   159  			opts := &rds.DescribeDBInstancesInput{
   160  				DBInstanceIdentifier: v.DBInstanceIdentifier,
   161  			}
   162  			_, err := conn.DescribeDBInstances(opts)
   163  			if err != nil {
   164  				dbinstanceerr, ok := err.(awserr.Error)
   165  				if ok && dbinstanceerr.Code() == "DBInstanceNotFound" {
   166  					return nil
   167  				}
   168  				return resource.NonRetryableError(
   169  					fmt.Errorf("Error retrieving DB Instances: %s", err))
   170  			}
   171  			return resource.RetryableError(fmt.Errorf(
   172  				"Waiting for instance to be deleted: %v", v.DBInstanceIdentifier))
   173  		})
   174  	}
   175  }
   176  
   177  func testAccCheckAWSClusterInstanceExists(n string, v *rds.DBInstance) resource.TestCheckFunc {
   178  	return func(s *terraform.State) error {
   179  		rs, ok := s.RootModule().Resources[n]
   180  		if !ok {
   181  			return fmt.Errorf("Not found: %s", n)
   182  		}
   183  
   184  		if rs.Primary.ID == "" {
   185  			return fmt.Errorf("No DB Instance ID is set")
   186  		}
   187  
   188  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   189  		resp, err := conn.DescribeDBInstances(&rds.DescribeDBInstancesInput{
   190  			DBInstanceIdentifier: aws.String(rs.Primary.ID),
   191  		})
   192  
   193  		if err != nil {
   194  			return err
   195  		}
   196  
   197  		for _, d := range resp.DBInstances {
   198  			if *d.DBInstanceIdentifier == rs.Primary.ID {
   199  				*v = *d
   200  				return nil
   201  			}
   202  		}
   203  
   204  		return fmt.Errorf("DB Cluster (%s) not found", rs.Primary.ID)
   205  	}
   206  }
   207  
   208  func TestAccAWSRDSClusterInstance_withInstanceEnhancedMonitor(t *testing.T) {
   209  	var v rds.DBInstance
   210  
   211  	resource.Test(t, resource.TestCase{
   212  		PreCheck:     func() { testAccPreCheck(t) },
   213  		Providers:    testAccProviders,
   214  		CheckDestroy: testAccCheckAWSClusterDestroy,
   215  		Steps: []resource.TestStep{
   216  			{
   217  				Config: testAccAWSClusterInstanceEnhancedMonitor(acctest.RandInt()),
   218  				Check: resource.ComposeTestCheckFunc(
   219  					testAccCheckAWSClusterInstanceExists("aws_rds_cluster_instance.cluster_instances", &v),
   220  					testAccCheckAWSDBClusterInstanceAttributes(&v),
   221  				),
   222  			},
   223  		},
   224  	})
   225  }
   226  
   227  // Add some random to the name, to avoid collision
   228  func testAccAWSClusterInstanceConfig(n int) string {
   229  	return fmt.Sprintf(`
   230  resource "aws_rds_cluster" "default" {
   231    cluster_identifier = "tf-aurora-cluster-test-%d"
   232    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   233    database_name      = "mydb"
   234    master_username    = "foo"
   235    master_password    = "mustbeeightcharaters"
   236    skip_final_snapshot = true
   237  }
   238  
   239  resource "aws_rds_cluster_instance" "cluster_instances" {
   240    identifier              = "tf-cluster-instance-%d"
   241    cluster_identifier      = "${aws_rds_cluster.default.id}"
   242    instance_class          = "db.r3.large"
   243    db_parameter_group_name = "${aws_db_parameter_group.bar.name}"
   244    promotion_tier          = "3"
   245  }
   246  
   247  resource "aws_db_parameter_group" "bar" {
   248    name   = "tfcluster-test-group-%d"
   249    family = "aurora5.6"
   250  
   251    parameter {
   252      name         = "back_log"
   253      value        = "32767"
   254      apply_method = "pending-reboot"
   255    }
   256  
   257    tags {
   258      foo = "bar"
   259    }
   260  }
   261  `, n, n, n)
   262  }
   263  
   264  func testAccAWSClusterInstanceConfigModified(n int) string {
   265  	return fmt.Sprintf(`
   266  resource "aws_rds_cluster" "default" {
   267    cluster_identifier = "tf-aurora-cluster-test-%d"
   268    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   269    database_name      = "mydb"
   270    master_username    = "foo"
   271    master_password    = "mustbeeightcharaters"
   272    skip_final_snapshot = true
   273  }
   274  
   275  resource "aws_rds_cluster_instance" "cluster_instances" {
   276    identifier                 = "tf-cluster-instance-%d"
   277    cluster_identifier         = "${aws_rds_cluster.default.id}"
   278    instance_class             = "db.r3.large"
   279    db_parameter_group_name    = "${aws_db_parameter_group.bar.name}"
   280    auto_minor_version_upgrade = false
   281    promotion_tier             = "3"
   282  }
   283  
   284  resource "aws_db_parameter_group" "bar" {
   285    name   = "tfcluster-test-group-%d"
   286    family = "aurora5.6"
   287  
   288    parameter {
   289      name         = "back_log"
   290      value        = "32767"
   291      apply_method = "pending-reboot"
   292    }
   293  
   294    tags {
   295      foo = "bar"
   296    }
   297  }
   298  `, n, n, n)
   299  }
   300  
   301  func testAccAWSClusterInstanceConfig_namePrefix(n int) string {
   302  	return fmt.Sprintf(`
   303  resource "aws_rds_cluster_instance" "test" {
   304    identifier_prefix = "tf-cluster-instance-"
   305    cluster_identifier = "${aws_rds_cluster.test.id}"
   306    instance_class = "db.r3.large"
   307  }
   308  
   309  resource "aws_rds_cluster" "test" {
   310    cluster_identifier = "tf-aurora-cluster-%d"
   311    master_username = "root"
   312    master_password = "password"
   313    db_subnet_group_name = "${aws_db_subnet_group.test.name}"
   314    skip_final_snapshot = true
   315  }
   316  
   317  resource "aws_vpc" "test" {
   318    cidr_block = "10.0.0.0/16"
   319  }
   320  
   321  resource "aws_subnet" "a" {
   322    vpc_id = "${aws_vpc.test.id}"
   323    cidr_block = "10.0.0.0/24"
   324    availability_zone = "us-west-2a"
   325  }
   326  
   327  resource "aws_subnet" "b" {
   328    vpc_id = "${aws_vpc.test.id}"
   329    cidr_block = "10.0.1.0/24"
   330    availability_zone = "us-west-2b"
   331  }
   332  
   333  resource "aws_db_subnet_group" "test" {
   334    name = "tf-test-%d"
   335    subnet_ids = ["${aws_subnet.a.id}", "${aws_subnet.b.id}"]
   336  }
   337  `, n, n)
   338  }
   339  
   340  func testAccAWSClusterInstanceConfig_generatedName(n int) string {
   341  	return fmt.Sprintf(`
   342  resource "aws_rds_cluster_instance" "test" {
   343    cluster_identifier = "${aws_rds_cluster.test.id}"
   344    instance_class = "db.r3.large"
   345  }
   346  
   347  resource "aws_rds_cluster" "test" {
   348    cluster_identifier = "tf-aurora-cluster-%d"
   349    master_username = "root"
   350    master_password = "password"
   351    db_subnet_group_name = "${aws_db_subnet_group.test.name}"
   352    skip_final_snapshot = true
   353  }
   354  
   355  resource "aws_vpc" "test" {
   356    cidr_block = "10.0.0.0/16"
   357  }
   358  
   359  resource "aws_subnet" "a" {
   360    vpc_id = "${aws_vpc.test.id}"
   361    cidr_block = "10.0.0.0/24"
   362    availability_zone = "us-west-2a"
   363  }
   364  
   365  resource "aws_subnet" "b" {
   366    vpc_id = "${aws_vpc.test.id}"
   367    cidr_block = "10.0.1.0/24"
   368    availability_zone = "us-west-2b"
   369  }
   370  
   371  resource "aws_db_subnet_group" "test" {
   372    name = "tf-test-%d"
   373    subnet_ids = ["${aws_subnet.a.id}", "${aws_subnet.b.id}"]
   374  }
   375  `, n, n)
   376  }
   377  
   378  func testAccAWSClusterInstanceConfigKmsKey(n int) string {
   379  	return fmt.Sprintf(`
   380  
   381  resource "aws_kms_key" "foo" {
   382      description = "Terraform acc test %d"
   383      policy = <<POLICY
   384  {
   385    "Version": "2012-10-17",
   386    "Id": "kms-tf-1",
   387    "Statement": [
   388      {
   389        "Sid": "Enable IAM User Permissions",
   390        "Effect": "Allow",
   391        "Principal": {
   392          "AWS": "*"
   393        },
   394        "Action": "kms:*",
   395        "Resource": "*"
   396      }
   397    ]
   398  }
   399  POLICY
   400  }
   401  
   402  resource "aws_rds_cluster" "default" {
   403    cluster_identifier = "tf-aurora-cluster-test-%d"
   404    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   405    database_name      = "mydb"
   406    master_username    = "foo"
   407    master_password    = "mustbeeightcharaters"
   408    storage_encrypted = true
   409    kms_key_id = "${aws_kms_key.foo.arn}"
   410    skip_final_snapshot = true
   411  }
   412  
   413  resource "aws_rds_cluster_instance" "cluster_instances" {
   414    identifier              = "tf-cluster-instance-%d"
   415    cluster_identifier      = "${aws_rds_cluster.default.id}"
   416    instance_class          = "db.r3.large"
   417    db_parameter_group_name = "${aws_db_parameter_group.bar.name}"
   418  }
   419  
   420  resource "aws_db_parameter_group" "bar" {
   421    name   = "tfcluster-test-group-%d"
   422    family = "aurora5.6"
   423  
   424    parameter {
   425      name         = "back_log"
   426      value        = "32767"
   427      apply_method = "pending-reboot"
   428    }
   429  
   430    tags {
   431      foo = "bar"
   432    }
   433  }
   434  `, n, n, n, n)
   435  }
   436  
   437  func testAccAWSClusterInstanceEnhancedMonitor(n int) string {
   438  	return fmt.Sprintf(`
   439  resource "aws_rds_cluster" "default" {
   440    cluster_identifier = "tf-aurora-cluster-test-%d"
   441    availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
   442    database_name      = "mydb"
   443    master_username    = "foo"
   444    master_password    = "mustbeeightcharaters"
   445    skip_final_snapshot = true
   446  }
   447  
   448  resource "aws_rds_cluster_instance" "cluster_instances" {
   449    identifier              = "tf-cluster-instance-%d"
   450    cluster_identifier      = "${aws_rds_cluster.default.id}"
   451    instance_class          = "db.r3.large"
   452    db_parameter_group_name = "${aws_db_parameter_group.bar.name}"
   453    monitoring_interval     = "60"
   454    monitoring_role_arn     = "${aws_iam_role.tf_enhanced_monitor_role.arn}"
   455  }
   456  
   457  resource "aws_iam_role" "tf_enhanced_monitor_role" {
   458      name = "tf_enhanced_monitor_role-%d"
   459      assume_role_policy = <<EOF
   460  {
   461              "Version": "2012-10-17",
   462              "Statement": [
   463                  {
   464                      "Action": "sts:AssumeRole",
   465                      "Principal": {
   466                          "Service": "monitoring.rds.amazonaws.com"
   467                      },
   468                      "Effect": "Allow",
   469                      "Sid": ""
   470                  }
   471              ]
   472     }
   473  EOF
   474  }
   475  
   476  resource "aws_iam_policy_attachment" "rds_m_attach" {
   477      name = "AmazonRDSEnhancedMonitoringRole"
   478      roles = ["${aws_iam_role.tf_enhanced_monitor_role.name}"]
   479      policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole"
   480  }
   481  
   482  resource "aws_db_parameter_group" "bar" {
   483    name   = "tfcluster-test-group-%d"
   484    family = "aurora5.6"
   485  
   486    parameter {
   487      name         = "back_log"
   488      value        = "32767"
   489      apply_method = "pending-reboot"
   490    }
   491  
   492    tags {
   493      foo = "bar"
   494    }
   495  }
   496  `, n, n, n, n)
   497  }