package aws import ( "fmt" "regexp" "testing" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/rds" ) func TestAccAWSRDSCluster_basic(t *testing.T) { var v rds.DBCluster resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSClusterDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccAWSClusterConfig(acctest.RandInt()), Check: resource.ComposeTestCheckFunc( testAccCheckAWSClusterExists("aws_rds_cluster.default", &v), resource.TestCheckResourceAttr( "aws_rds_cluster.default", "storage_encrypted", "false"), resource.TestCheckResourceAttr( "aws_rds_cluster.default", "db_cluster_parameter_group_name", "default.aurora5.6"), resource.TestCheckResourceAttrSet( "aws_rds_cluster.default", "reader_endpoint"), ), }, }, }) } func TestAccAWSRDSCluster_updateTags(t *testing.T) { var v rds.DBCluster ri := acctest.RandInt() resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSClusterDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccAWSClusterConfig(ri), Check: resource.ComposeTestCheckFunc( testAccCheckAWSClusterExists("aws_rds_cluster.default", &v), resource.TestCheckResourceAttr( "aws_rds_cluster.default", "tags.%", "1"), ), }, resource.TestStep{ Config: testAccAWSClusterConfigUpdatedTags(ri), Check: resource.ComposeTestCheckFunc( testAccCheckAWSClusterExists("aws_rds_cluster.default", &v), resource.TestCheckResourceAttr( "aws_rds_cluster.default", "tags.%", "2"), ), }, }, }) } func TestAccAWSRDSCluster_kmsKey(t *testing.T) { var v rds.DBCluster keyRegex := regexp.MustCompile("^arn:aws:kms:") resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSClusterDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccAWSClusterConfig_kmsKey(acctest.RandInt()), Check: resource.ComposeTestCheckFunc( testAccCheckAWSClusterExists("aws_rds_cluster.default", &v), resource.TestMatchResourceAttr( "aws_rds_cluster.default", "kms_key_id", keyRegex), ), }, }, }) } func TestAccAWSRDSCluster_encrypted(t *testing.T) { var v rds.DBCluster resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSClusterDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccAWSClusterConfig_encrypted(acctest.RandInt()), Check: resource.ComposeTestCheckFunc( testAccCheckAWSClusterExists("aws_rds_cluster.default", &v), resource.TestCheckResourceAttr( "aws_rds_cluster.default", "storage_encrypted", "true"), resource.TestCheckResourceAttr( "aws_rds_cluster.default", "db_cluster_parameter_group_name", "default.aurora5.6"), ), }, }, }) } func TestAccAWSRDSCluster_backupsUpdate(t *testing.T) { var v rds.DBCluster ri := acctest.RandInt() resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSClusterDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccAWSClusterConfig_backups(ri), Check: resource.ComposeTestCheckFunc( testAccCheckAWSClusterExists("aws_rds_cluster.default", &v), resource.TestCheckResourceAttr( "aws_rds_cluster.default", "preferred_backup_window", "07:00-09:00"), resource.TestCheckResourceAttr( "aws_rds_cluster.default", "backup_retention_period", "5"), resource.TestCheckResourceAttr( "aws_rds_cluster.default", "preferred_maintenance_window", "tue:04:00-tue:04:30"), ), }, resource.TestStep{ Config: testAccAWSClusterConfig_backupsUpdate(ri), Check: resource.ComposeTestCheckFunc( testAccCheckAWSClusterExists("aws_rds_cluster.default", &v), resource.TestCheckResourceAttr( "aws_rds_cluster.default", "preferred_backup_window", "03:00-09:00"), resource.TestCheckResourceAttr( "aws_rds_cluster.default", "backup_retention_period", "10"), resource.TestCheckResourceAttr( "aws_rds_cluster.default", "preferred_maintenance_window", "wed:01:00-wed:01:30"), ), }, }, }) } func testAccCheckAWSClusterDestroy(s *terraform.State) error { for _, rs := range s.RootModule().Resources { if rs.Type != "aws_rds_cluster" { continue } // Try to find the Group conn := testAccProvider.Meta().(*AWSClient).rdsconn var err error resp, err := conn.DescribeDBClusters( &rds.DescribeDBClustersInput{ DBClusterIdentifier: aws.String(rs.Primary.ID), }) if err == nil { if len(resp.DBClusters) != 0 && *resp.DBClusters[0].DBClusterIdentifier == rs.Primary.ID { return fmt.Errorf("DB Cluster %s still exists", rs.Primary.ID) } } // Return nil if the cluster is already destroyed if awsErr, ok := err.(awserr.Error); ok { if awsErr.Code() == "DBClusterNotFoundFault" { return nil } } return err } return nil } func testAccCheckAWSClusterExists(n string, v *rds.DBCluster) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) } if rs.Primary.ID == "" { return fmt.Errorf("No DB Instance ID is set") } conn := testAccProvider.Meta().(*AWSClient).rdsconn resp, err := conn.DescribeDBClusters(&rds.DescribeDBClustersInput{ DBClusterIdentifier: aws.String(rs.Primary.ID), }) if err != nil { return err } for _, c := range resp.DBClusters { if *c.DBClusterIdentifier == rs.Primary.ID { *v = *c return nil } } return fmt.Errorf("DB Cluster (%s) not found", rs.Primary.ID) } } func testAccAWSClusterConfig(n int) string { return fmt.Sprintf(` resource "aws_rds_cluster" "default" { cluster_identifier = "tf-aurora-cluster-%d" availability_zones = ["us-west-2a","us-west-2b","us-west-2c"] database_name = "mydb" master_username = "foo" master_password = "mustbeeightcharaters" db_cluster_parameter_group_name = "default.aurora5.6" tags { Environment = "production" } }`, n) } func testAccAWSClusterConfigUpdatedTags(n int) string { return fmt.Sprintf(` resource "aws_rds_cluster" "default" { cluster_identifier = "tf-aurora-cluster-%d" availability_zones = ["us-west-2a","us-west-2b","us-west-2c"] database_name = "mydb" master_username = "foo" master_password = "mustbeeightcharaters" db_cluster_parameter_group_name = "default.aurora5.6" tags { Environment = "production" AnotherTag = "test" } }`, n) } func testAccAWSClusterConfig_kmsKey(n int) string { return fmt.Sprintf(` resource "aws_kms_key" "foo" { description = "Terraform acc test %d" policy = <