github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_rds_cluster_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "math/rand" 6 "testing" 7 "time" 8 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 ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() 21 config := fmt.Sprintf(testAccAWSClusterConfig, ri) 22 23 resource.Test(t, resource.TestCase{ 24 PreCheck: func() { testAccPreCheck(t) }, 25 Providers: testAccProviders, 26 CheckDestroy: testAccCheckAWSClusterDestroy, 27 Steps: []resource.TestStep{ 28 resource.TestStep{ 29 Config: config, 30 Check: resource.ComposeTestCheckFunc( 31 testAccCheckAWSClusterExists("aws_rds_cluster.default", &v), 32 ), 33 }, 34 }, 35 }) 36 } 37 38 func TestAccAWSRDSCluster_backupsUpdate(t *testing.T) { 39 var v rds.DBCluster 40 41 ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() 42 preConfig := fmt.Sprintf(testAccAWSClusterConfig_backups, ri) 43 postConfig := fmt.Sprintf(testAccAWSClusterConfig_backupsUpdate, ri) 44 45 resource.Test(t, resource.TestCase{ 46 PreCheck: func() { testAccPreCheck(t) }, 47 Providers: testAccProviders, 48 CheckDestroy: testAccCheckAWSClusterDestroy, 49 Steps: []resource.TestStep{ 50 resource.TestStep{ 51 Config: preConfig, 52 Check: resource.ComposeTestCheckFunc( 53 testAccCheckAWSClusterExists("aws_rds_cluster.default", &v), 54 resource.TestCheckResourceAttr( 55 "aws_rds_cluster.default", "preferred_backup_window", "07:00-09:00"), 56 resource.TestCheckResourceAttr( 57 "aws_rds_cluster.default", "backup_retention_period", "5"), 58 resource.TestCheckResourceAttr( 59 "aws_rds_cluster.default", "preferred_maintenance_window", "tue:04:00-tue:04:30"), 60 ), 61 }, 62 63 resource.TestStep{ 64 Config: postConfig, 65 Check: resource.ComposeTestCheckFunc( 66 testAccCheckAWSClusterExists("aws_rds_cluster.default", &v), 67 resource.TestCheckResourceAttr( 68 "aws_rds_cluster.default", "preferred_backup_window", "03:00-09:00"), 69 resource.TestCheckResourceAttr( 70 "aws_rds_cluster.default", "backup_retention_period", "10"), 71 resource.TestCheckResourceAttr( 72 "aws_rds_cluster.default", "preferred_maintenance_window", "wed:01:00-wed:01:30"), 73 ), 74 }, 75 }, 76 }) 77 } 78 79 func testAccCheckAWSClusterDestroy(s *terraform.State) error { 80 for _, rs := range s.RootModule().Resources { 81 if rs.Type != "aws_rds_cluster" { 82 continue 83 } 84 85 // Try to find the Group 86 conn := testAccProvider.Meta().(*AWSClient).rdsconn 87 var err error 88 resp, err := conn.DescribeDBClusters( 89 &rds.DescribeDBClustersInput{ 90 DBClusterIdentifier: aws.String(rs.Primary.ID), 91 }) 92 93 if err == nil { 94 if len(resp.DBClusters) != 0 && 95 *resp.DBClusters[0].DBClusterIdentifier == rs.Primary.ID { 96 return fmt.Errorf("DB Cluster %s still exists", rs.Primary.ID) 97 } 98 } 99 100 // Return nil if the cluster is already destroyed 101 if awsErr, ok := err.(awserr.Error); ok { 102 if awsErr.Code() == "DBClusterNotFound" { 103 return nil 104 } 105 } 106 107 return err 108 } 109 110 return nil 111 } 112 113 func testAccCheckAWSClusterExists(n string, v *rds.DBCluster) resource.TestCheckFunc { 114 return func(s *terraform.State) error { 115 rs, ok := s.RootModule().Resources[n] 116 if !ok { 117 return fmt.Errorf("Not found: %s", n) 118 } 119 120 if rs.Primary.ID == "" { 121 return fmt.Errorf("No DB Instance ID is set") 122 } 123 124 conn := testAccProvider.Meta().(*AWSClient).rdsconn 125 resp, err := conn.DescribeDBClusters(&rds.DescribeDBClustersInput{ 126 DBClusterIdentifier: aws.String(rs.Primary.ID), 127 }) 128 129 if err != nil { 130 return err 131 } 132 133 for _, c := range resp.DBClusters { 134 if *c.DBClusterIdentifier == rs.Primary.ID { 135 *v = *c 136 return nil 137 } 138 } 139 140 return fmt.Errorf("DB Cluster (%s) not found", rs.Primary.ID) 141 } 142 } 143 144 var testAccAWSClusterConfig = ` 145 resource "aws_rds_cluster" "default" { 146 cluster_identifier = "tf-aurora-cluster-%d" 147 availability_zones = ["us-west-2a","us-west-2b","us-west-2c"] 148 database_name = "mydb" 149 master_username = "foo" 150 master_password = "mustbeeightcharaters" 151 }` 152 153 var testAccAWSClusterConfig_backups = ` 154 resource "aws_rds_cluster" "default" { 155 cluster_identifier = "tf-aurora-cluster-%d" 156 availability_zones = ["us-west-2a","us-west-2b","us-west-2c"] 157 database_name = "mydb" 158 master_username = "foo" 159 master_password = "mustbeeightcharaters" 160 backup_retention_period = 5 161 preferred_backup_window = "07:00-09:00" 162 preferred_maintenance_window = "tue:04:00-tue:04:30" 163 }` 164 165 var testAccAWSClusterConfig_backupsUpdate = ` 166 resource "aws_rds_cluster" "default" { 167 cluster_identifier = "tf-aurora-cluster-%d" 168 availability_zones = ["us-west-2a","us-west-2b","us-west-2c"] 169 database_name = "mydb" 170 master_username = "foo" 171 master_password = "mustbeeightcharaters" 172 backup_retention_period = 10 173 preferred_backup_window = "03:00-09:00" 174 preferred_maintenance_window = "wed:01:00-wed:01:30" 175 apply_immediately = true 176 }`