github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_rds_cluster_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  
    12  	"github.com/aws/aws-sdk-go/aws"
    13  	"github.com/aws/aws-sdk-go/aws/awserr"
    14  	"github.com/aws/aws-sdk-go/service/rds"
    15  )
    16  
    17  func TestAccAWSRDSCluster_basic(t *testing.T) {
    18  	var v rds.DBCluster
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckAWSClusterDestroy,
    24  		Steps: []resource.TestStep{
    25  			{
    26  				Config: testAccAWSClusterConfig(acctest.RandInt()),
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckAWSClusterExists("aws_rds_cluster.default", &v),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_rds_cluster.default", "storage_encrypted", "false"),
    31  					resource.TestCheckResourceAttr(
    32  						"aws_rds_cluster.default", "db_cluster_parameter_group_name", "default.aurora5.6"),
    33  					resource.TestCheckResourceAttrSet(
    34  						"aws_rds_cluster.default", "reader_endpoint"),
    35  				),
    36  			},
    37  		},
    38  	})
    39  }
    40  
    41  func TestAccAWSRDSCluster_updateTags(t *testing.T) {
    42  	var v rds.DBCluster
    43  	ri := acctest.RandInt()
    44  
    45  	resource.Test(t, resource.TestCase{
    46  		PreCheck:     func() { testAccPreCheck(t) },
    47  		Providers:    testAccProviders,
    48  		CheckDestroy: testAccCheckAWSClusterDestroy,
    49  		Steps: []resource.TestStep{
    50  			{
    51  				Config: testAccAWSClusterConfig(ri),
    52  				Check: resource.ComposeTestCheckFunc(
    53  					testAccCheckAWSClusterExists("aws_rds_cluster.default", &v),
    54  					resource.TestCheckResourceAttr(
    55  						"aws_rds_cluster.default", "tags.%", "1"),
    56  				),
    57  			},
    58  			{
    59  				Config: testAccAWSClusterConfigUpdatedTags(ri),
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testAccCheckAWSClusterExists("aws_rds_cluster.default", &v),
    62  					resource.TestCheckResourceAttr(
    63  						"aws_rds_cluster.default", "tags.%", "2"),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func TestAccAWSRDSCluster_kmsKey(t *testing.T) {
    71  	var v rds.DBCluster
    72  	keyRegex := regexp.MustCompile("^arn:aws:kms:")
    73  
    74  	resource.Test(t, resource.TestCase{
    75  		PreCheck:     func() { testAccPreCheck(t) },
    76  		Providers:    testAccProviders,
    77  		CheckDestroy: testAccCheckAWSClusterDestroy,
    78  		Steps: []resource.TestStep{
    79  			{
    80  				Config: testAccAWSClusterConfig_kmsKey(acctest.RandInt()),
    81  				Check: resource.ComposeTestCheckFunc(
    82  					testAccCheckAWSClusterExists("aws_rds_cluster.default", &v),
    83  					resource.TestMatchResourceAttr(
    84  						"aws_rds_cluster.default", "kms_key_id", keyRegex),
    85  				),
    86  			},
    87  		},
    88  	})
    89  }
    90  
    91  func TestAccAWSRDSCluster_encrypted(t *testing.T) {
    92  	var v rds.DBCluster
    93  
    94  	resource.Test(t, resource.TestCase{
    95  		PreCheck:     func() { testAccPreCheck(t) },
    96  		Providers:    testAccProviders,
    97  		CheckDestroy: testAccCheckAWSClusterDestroy,
    98  		Steps: []resource.TestStep{
    99  			{
   100  				Config: testAccAWSClusterConfig_encrypted(acctest.RandInt()),
   101  				Check: resource.ComposeTestCheckFunc(
   102  					testAccCheckAWSClusterExists("aws_rds_cluster.default", &v),
   103  					resource.TestCheckResourceAttr(
   104  						"aws_rds_cluster.default", "storage_encrypted", "true"),
   105  					resource.TestCheckResourceAttr(
   106  						"aws_rds_cluster.default", "db_cluster_parameter_group_name", "default.aurora5.6"),
   107  				),
   108  			},
   109  		},
   110  	})
   111  }
   112  
   113  func TestAccAWSRDSCluster_backupsUpdate(t *testing.T) {
   114  	var v rds.DBCluster
   115  
   116  	ri := acctest.RandInt()
   117  	resource.Test(t, resource.TestCase{
   118  		PreCheck:     func() { testAccPreCheck(t) },
   119  		Providers:    testAccProviders,
   120  		CheckDestroy: testAccCheckAWSClusterDestroy,
   121  		Steps: []resource.TestStep{
   122  			{
   123  				Config: testAccAWSClusterConfig_backups(ri),
   124  				Check: resource.ComposeTestCheckFunc(
   125  					testAccCheckAWSClusterExists("aws_rds_cluster.default", &v),
   126  					resource.TestCheckResourceAttr(
   127  						"aws_rds_cluster.default", "preferred_backup_window", "07:00-09:00"),
   128  					resource.TestCheckResourceAttr(
   129  						"aws_rds_cluster.default", "backup_retention_period", "5"),
   130  					resource.TestCheckResourceAttr(
   131  						"aws_rds_cluster.default", "preferred_maintenance_window", "tue:04:00-tue:04:30"),
   132  				),
   133  			},
   134  
   135  			resource.TestStep{
   136  				Config: testAccAWSClusterConfig_backupsUpdate(ri),
   137  				Check: resource.ComposeTestCheckFunc(
   138  					testAccCheckAWSClusterExists("aws_rds_cluster.default", &v),
   139  					resource.TestCheckResourceAttr(
   140  						"aws_rds_cluster.default", "preferred_backup_window", "03:00-09:00"),
   141  					resource.TestCheckResourceAttr(
   142  						"aws_rds_cluster.default", "backup_retention_period", "10"),
   143  					resource.TestCheckResourceAttr(
   144  						"aws_rds_cluster.default", "preferred_maintenance_window", "wed:01:00-wed:01:30"),
   145  				),
   146  			},
   147  		},
   148  	})
   149  }
   150  
   151  func testAccCheckAWSClusterDestroy(s *terraform.State) error {
   152  	for _, rs := range s.RootModule().Resources {
   153  		if rs.Type != "aws_rds_cluster" {
   154  			continue
   155  		}
   156  
   157  		// Try to find the Group
   158  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   159  		var err error
   160  		resp, err := conn.DescribeDBClusters(
   161  			&rds.DescribeDBClustersInput{
   162  				DBClusterIdentifier: aws.String(rs.Primary.ID),
   163  			})
   164  
   165  		if err == nil {
   166  			if len(resp.DBClusters) != 0 &&
   167  				*resp.DBClusters[0].DBClusterIdentifier == rs.Primary.ID {
   168  				return fmt.Errorf("DB Cluster %s still exists", rs.Primary.ID)
   169  			}
   170  		}
   171  
   172  		// Return nil if the cluster is already destroyed
   173  		if awsErr, ok := err.(awserr.Error); ok {
   174  			if awsErr.Code() == "DBClusterNotFoundFault" {
   175  				return nil
   176  			}
   177  		}
   178  
   179  		return err
   180  	}
   181  
   182  	return nil
   183  }
   184  
   185  func testAccCheckAWSClusterExists(n string, v *rds.DBCluster) resource.TestCheckFunc {
   186  	return func(s *terraform.State) error {
   187  		rs, ok := s.RootModule().Resources[n]
   188  		if !ok {
   189  			return fmt.Errorf("Not found: %s", n)
   190  		}
   191  
   192  		if rs.Primary.ID == "" {
   193  			return fmt.Errorf("No DB Instance ID is set")
   194  		}
   195  
   196  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   197  		resp, err := conn.DescribeDBClusters(&rds.DescribeDBClustersInput{
   198  			DBClusterIdentifier: aws.String(rs.Primary.ID),
   199  		})
   200  
   201  		if err != nil {
   202  			return err
   203  		}
   204  
   205  		for _, c := range resp.DBClusters {
   206  			if *c.DBClusterIdentifier == rs.Primary.ID {
   207  				*v = *c
   208  				return nil
   209  			}
   210  		}
   211  
   212  		return fmt.Errorf("DB Cluster (%s) not found", rs.Primary.ID)
   213  	}
   214  }
   215  
   216  func testAccAWSClusterConfig(n int) string {
   217  	return fmt.Sprintf(`
   218  resource "aws_rds_cluster" "default" {
   219    cluster_identifier = "tf-aurora-cluster-%d"
   220    availability_zones = ["us-west-2a","us-west-2b","us-west-2c"]
   221    database_name = "mydb"
   222    master_username = "foo"
   223    master_password = "mustbeeightcharaters"
   224    db_cluster_parameter_group_name = "default.aurora5.6"
   225    tags {
   226      Environment = "production"
   227    }
   228  }`, n)
   229  }
   230  
   231  func testAccAWSClusterConfigUpdatedTags(n int) string {
   232  	return fmt.Sprintf(`
   233  resource "aws_rds_cluster" "default" {
   234    cluster_identifier = "tf-aurora-cluster-%d"
   235    availability_zones = ["us-west-2a","us-west-2b","us-west-2c"]
   236    database_name = "mydb"
   237    master_username = "foo"
   238    master_password = "mustbeeightcharaters"
   239    db_cluster_parameter_group_name = "default.aurora5.6"
   240    tags {
   241      Environment = "production"
   242      AnotherTag = "test"
   243    }
   244  }`, n)
   245  }
   246  
   247  func testAccAWSClusterConfig_kmsKey(n int) string {
   248  	return fmt.Sprintf(`
   249  
   250   resource "aws_kms_key" "foo" {
   251       description = "Terraform acc test %d"
   252       policy = <<POLICY
   253   {
   254     "Version": "2012-10-17",
   255     "Id": "kms-tf-1",
   256     "Statement": [
   257       {
   258         "Sid": "Enable IAM User Permissions",
   259         "Effect": "Allow",
   260         "Principal": {
   261           "AWS": "*"
   262         },
   263         "Action": "kms:*",
   264         "Resource": "*"
   265       }
   266     ]
   267   }
   268   POLICY
   269   }
   270  
   271   resource "aws_rds_cluster" "default" {
   272     cluster_identifier = "tf-aurora-cluster-%d"
   273     availability_zones = ["us-west-2a","us-west-2b","us-west-2c"]
   274     database_name = "mydb"
   275     master_username = "foo"
   276     master_password = "mustbeeightcharaters"
   277     db_cluster_parameter_group_name = "default.aurora5.6"
   278     storage_encrypted = true
   279     kms_key_id = "${aws_kms_key.foo.arn}"
   280   }`, n, n)
   281  }
   282  
   283  func testAccAWSClusterConfig_encrypted(n int) string {
   284  	return fmt.Sprintf(`
   285  resource "aws_rds_cluster" "default" {
   286    cluster_identifier = "tf-aurora-cluster-%d"
   287    availability_zones = ["us-west-2a","us-west-2b","us-west-2c"]
   288    database_name = "mydb"
   289    master_username = "foo"
   290    master_password = "mustbeeightcharaters"
   291    storage_encrypted = true
   292  }`, n)
   293  }
   294  
   295  func testAccAWSClusterConfig_backups(n int) string {
   296  	return fmt.Sprintf(`
   297  resource "aws_rds_cluster" "default" {
   298    cluster_identifier = "tf-aurora-cluster-%d"
   299    availability_zones = ["us-west-2a","us-west-2b","us-west-2c"]
   300    database_name = "mydb"
   301    master_username = "foo"
   302    master_password = "mustbeeightcharaters"
   303    backup_retention_period = 5
   304    preferred_backup_window = "07:00-09:00"
   305    preferred_maintenance_window = "tue:04:00-tue:04:30"
   306  }`, n)
   307  }
   308  
   309  func testAccAWSClusterConfig_backupsUpdate(n int) string {
   310  	return fmt.Sprintf(`
   311  resource "aws_rds_cluster" "default" {
   312    cluster_identifier = "tf-aurora-cluster-%d"
   313    availability_zones = ["us-west-2a","us-west-2b","us-west-2c"]
   314    database_name = "mydb"
   315    master_username = "foo"
   316    master_password = "mustbeeightcharaters"
   317    backup_retention_period = 10
   318    preferred_backup_window = "03:00-09:00"
   319    preferred_maintenance_window = "wed:01:00-wed:01:30"
   320    apply_immediately = true
   321  }`, n)
   322  }