Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/redshift_cluster: Make master_username ForceNew #2202

Merged
merged 2 commits into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions aws/import_aws_redshift_cluster_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand All @@ -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) },
Expand Down
1 change: 1 addition & 0 deletions aws/resource_aws_redshift_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func resourceAwsRedshiftCluster() *schema.Resource {
"master_username": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateRedshiftClusterMasterUsername,
},

Expand Down
131 changes: 103 additions & 28 deletions aws/resource_aws_redshift_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand Down Expand Up @@ -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{
Expand All @@ -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) },
Expand Down Expand Up @@ -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) },
Expand Down Expand Up @@ -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) },
Expand Down Expand Up @@ -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) },
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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(`
Expand All @@ -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 = <<POLICY
Expand Down Expand Up @@ -659,9 +706,11 @@ resource "aws_redshift_cluster" "default" {
kms_key_id = "${aws_kms_key.foo.arn}"
encrypted = true
skip_final_snapshot = true
}`
}`, rInt, rInt)
}

var testAccAWSRedshiftClusterConfig_enhancedVpcRoutingEnabled = `
func testAccAWSRedshiftClusterConfig_enhancedVpcRoutingEnabled(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
cluster_identifier = "tf-redshift-cluster-%d"
availability_zone = "us-west-2a"
Expand All @@ -674,9 +723,11 @@ resource "aws_redshift_cluster" "default" {
enhanced_vpc_routing = true
skip_final_snapshot = true
}
`
`, rInt)
}

var testAccAWSRedshiftClusterConfig_enhancedVpcRoutingDisabled = `
func testAccAWSRedshiftClusterConfig_enhancedVpcRoutingDisabled(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
cluster_identifier = "tf-redshift-cluster-%d"
availability_zone = "us-west-2a"
Expand All @@ -689,7 +740,8 @@ resource "aws_redshift_cluster" "default" {
enhanced_vpc_routing = false
skip_final_snapshot = true
}
`
`, rInt)
}

func testAccAWSRedshiftClusterConfig_loggingDisabled(rInt int) string {
return fmt.Sprintf(`
Expand Down Expand Up @@ -755,7 +807,8 @@ EOF
}`, rInt, rInt, rInt, rInt)
}

var testAccAWSRedshiftClusterConfig_tags = `
func testAccAWSRedshiftClusterConfig_tags(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
cluster_identifier = "tf-redshift-cluster-%d"
availability_zone = "us-west-2a"
Expand All @@ -771,9 +824,11 @@ resource "aws_redshift_cluster" "default" {
cluster = "reader"
Type = "master"
}
}`
}`, rInt)
}

var testAccAWSRedshiftClusterConfig_updatedTags = `
func testAccAWSRedshiftClusterConfig_updatedTags(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
cluster_identifier = "tf-redshift-cluster-%d"
availability_zone = "us-west-2a"
Expand All @@ -787,7 +842,8 @@ resource "aws_redshift_cluster" "default" {
tags {
environment = "Production"
}
}`
}`, rInt)
}

func testAccAWSRedshiftClusterConfig_notPubliclyAccessible(rInt int) string {
return fmt.Sprintf(`
Expand Down Expand Up @@ -909,7 +965,8 @@ func testAccAWSRedshiftClusterConfig_updatePubliclyAccessible(rInt int) string {
}`, rInt, rInt)
}

var testAccAWSRedshiftClusterConfig_iamRoles = `
func testAccAWSRedshiftClusterConfig_iamRoles(rInt int) string {
return fmt.Sprintf(`
resource "aws_iam_role" "ec2-role" {
name = "test-role-ec2-%d"
path = "/"
Expand All @@ -933,9 +990,11 @@ resource "aws_redshift_cluster" "default" {
allow_version_upgrade = false
iam_roles = ["${aws_iam_role.ec2-role.arn}", "${aws_iam_role.lambda-role.arn}"]
skip_final_snapshot = true
}`
}`, rInt, rInt, rInt)
}

var testAccAWSRedshiftClusterConfig_updateIamRoles = `
func testAccAWSRedshiftClusterConfig_updateIamRoles(rInt int) string {
return fmt.Sprintf(`
resource "aws_iam_role" "ec2-role" {
name = "test-role-ec2-%d"
path = "/"
Expand All @@ -959,4 +1018,20 @@ resource "aws_iam_role" "ec2-role" {
allow_version_upgrade = false
iam_roles = ["${aws_iam_role.ec2-role.arn}"]
skip_final_snapshot = true
}`
}`, rInt, rInt, rInt)
}

func testAccAWSRedshiftClusterConfig_updatedUsername(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
cluster_identifier = "tf-redshift-cluster-%d"
availability_zone = "us-west-2a"
database_name = "mydb"
master_username = "new_username"
master_password = "Mustbe8characters"
node_type = "dc1.large"
automated_snapshot_retention_period = 0
allow_version_upgrade = false
skip_final_snapshot = true
}`, rInt)
}