diff --git a/aws/import_aws_redshift_cluster_test.go b/aws/import_aws_redshift_cluster_test.go index b6cea189654..5c00a3bdfca 100644 --- a/aws/import_aws_redshift_cluster_test.go +++ b/aws/import_aws_redshift_cluster_test.go @@ -1,7 +1,6 @@ package aws import ( - "fmt" "testing" "github.com/hashicorp/terraform/helper/acctest" @@ -10,7 +9,7 @@ import ( func TestAccAWSRedshiftCluster_importBasic(t *testing.T) { resourceName := "aws_redshift_cluster.default" - config := fmt.Sprintf(testAccAWSRedshiftClusterConfig_basic, acctest.RandInt()) + config := testAccAWSRedshiftClusterConfig_basic(acctest.RandInt()) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, diff --git a/aws/resource_aws_redshift_cluster.go b/aws/resource_aws_redshift_cluster.go index e80ac0a3ee5..82c26ac5c6f 100644 --- a/aws/resource_aws_redshift_cluster.go +++ b/aws/resource_aws_redshift_cluster.go @@ -52,6 +52,7 @@ func resourceAwsRedshiftCluster() *schema.Resource { "master_username": { Type: schema.TypeString, Optional: true, + ForceNew: true, ValidateFunc: validateRedshiftClusterMasterUsername, }, diff --git a/aws/resource_aws_redshift_cluster_test.go b/aws/resource_aws_redshift_cluster_test.go index 8093c0d494a..a822ba98e22 100644 --- a/aws/resource_aws_redshift_cluster_test.go +++ b/aws/resource_aws_redshift_cluster_test.go @@ -57,7 +57,7 @@ func TestAccAWSRedshiftCluster_basic(t *testing.T) { var v redshift.Cluster ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() - config := fmt.Sprintf(testAccAWSRedshiftClusterConfig_basic, ri) + config := testAccAWSRedshiftClusterConfig_basic(ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -102,7 +102,7 @@ func TestAccAWSRedshiftCluster_kmsKey(t *testing.T) { var v redshift.Cluster ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() - config := fmt.Sprintf(testAccAWSRedshiftClusterConfig_kmsKey, ri, ri) + config := testAccAWSRedshiftClusterConfig_kmsKey(ri) keyRegex := regexp.MustCompile("^arn:aws:([a-zA-Z0-9\\-])+:([a-z]{2}-[a-z]+-\\d{1})?:(\\d{12})?:(.*)$") resource.Test(t, resource.TestCase{ @@ -129,8 +129,8 @@ func TestAccAWSRedshiftCluster_enhancedVpcRoutingEnabled(t *testing.T) { var v redshift.Cluster ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() - preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_enhancedVpcRoutingEnabled, ri) - postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_enhancedVpcRoutingDisabled, ri) + preConfig := testAccAWSRedshiftClusterConfig_enhancedVpcRoutingEnabled(ri) + postConfig := testAccAWSRedshiftClusterConfig_enhancedVpcRoutingDisabled(ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -193,8 +193,8 @@ func TestAccAWSRedshiftCluster_iamRoles(t *testing.T) { var v redshift.Cluster ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() - preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_iamRoles, ri, ri, ri) - postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updateIamRoles, ri, ri, ri) + preConfig := testAccAWSRedshiftClusterConfig_iamRoles(ri) + postConfig := testAccAWSRedshiftClusterConfig_updateIamRoles(ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -256,8 +256,8 @@ func TestAccAWSRedshiftCluster_updateNodeCount(t *testing.T) { var v redshift.Cluster ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() - preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_basic, ri) - postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updateNodeCount, ri) + preConfig := testAccAWSRedshiftClusterConfig_basic(ri) + postConfig := testAccAWSRedshiftClusterConfig_updateNodeCount(ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -289,8 +289,8 @@ func TestAccAWSRedshiftCluster_tags(t *testing.T) { var v redshift.Cluster ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() - preConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_tags, ri) - postConfig := fmt.Sprintf(testAccAWSRedshiftClusterConfig_updatedTags, ri) + preConfig := testAccAWSRedshiftClusterConfig_tags(ri) + postConfig := testAccAWSRedshiftClusterConfig_updatedTags(ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -320,6 +320,39 @@ func TestAccAWSRedshiftCluster_tags(t *testing.T) { }) } +func TestAccAWSRedshiftCluster_forceNewUsername(t *testing.T) { + var first, second redshift.Cluster + + ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() + preConfig := testAccAWSRedshiftClusterConfig_basic(ri) + postConfig := testAccAWSRedshiftClusterConfig_updatedUsername(ri) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSRedshiftClusterDestroy, + Steps: []resource.TestStep{ + { + Config: preConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &first), + testAccCheckAWSRedshiftClusterMasterUsername(&first, "foo_test"), + resource.TestCheckResourceAttr("aws_redshift_cluster.default", "master_username", "foo_test"), + ), + }, + + { + Config: postConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &second), + testAccCheckAWSRedshiftClusterMasterUsername(&second, "new_username"), + resource.TestCheckResourceAttr("aws_redshift_cluster.default", "master_username", "new_username"), + ), + }, + }, + }) +} + func testAccCheckAWSRedshiftClusterDestroy(s *terraform.State) error { for _, rs := range s.RootModule().Resources { if rs.Type != "aws_redshift_cluster" { @@ -441,6 +474,15 @@ func testAccCheckAWSRedshiftClusterExists(n string, v *redshift.Cluster) resourc } } +func testAccCheckAWSRedshiftClusterMasterUsername(c *redshift.Cluster, value string) resource.TestCheckFunc { + return func(s *terraform.State) error { + if *c.MasterUsername != value { + return fmt.Errorf("Expected cluster's MasterUsername: %q, given: %q", value, *c.MasterUsername) + } + return nil + } +} + func TestResourceAWSRedshiftClusterIdentifierValidation(t *testing.T) { cases := []struct { Value string @@ -581,7 +623,8 @@ func TestResourceAWSRedshiftClusterMasterPasswordValidation(t *testing.T) { } } -var testAccAWSRedshiftClusterConfig_updateNodeCount = ` +func testAccAWSRedshiftClusterConfig_updateNodeCount(rInt int) string { + return fmt.Sprintf(` resource "aws_redshift_cluster" "default" { cluster_identifier = "tf-redshift-cluster-%d" availability_zone = "us-west-2a" @@ -594,9 +637,11 @@ resource "aws_redshift_cluster" "default" { number_of_nodes = 2 skip_final_snapshot = true } -` +`, rInt) +} -var testAccAWSRedshiftClusterConfig_basic = ` +func testAccAWSRedshiftClusterConfig_basic(rInt int) string { + return fmt.Sprintf(` resource "aws_redshift_cluster" "default" { cluster_identifier = "tf-redshift-cluster-%d" availability_zone = "us-west-2a" @@ -607,7 +652,8 @@ resource "aws_redshift_cluster" "default" { automated_snapshot_retention_period = 0 allow_version_upgrade = false skip_final_snapshot = true -}` +}`, rInt) +} func testAccAWSRedshiftClusterConfigWithFinalSnapshot(rInt int) string { return fmt.Sprintf(` @@ -625,7 +671,8 @@ resource "aws_redshift_cluster" "default" { }`, rInt, rInt) } -var testAccAWSRedshiftClusterConfig_kmsKey = ` +func testAccAWSRedshiftClusterConfig_kmsKey(rInt int) string { + return fmt.Sprintf(` resource "aws_kms_key" "foo" { description = "Terraform acc test %d" policy = <