From ef08adeb65b944a2ca37bfbdd0d8b72a2ba3a1cf Mon Sep 17 00:00:00 2001 From: Paul Stack Date: Wed, 1 Feb 2017 14:42:00 +0000 Subject: [PATCH] provider/aws: Fix panic in aws_rds_cluster missing parameter error (#11600) message Fixes: #11568 ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSRDSCluster_missingUserNameCausesError' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/02/01 12:11:14 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSRDSCluster_missingUserNameCausesError -timeout 120m === RUN TestAccAWSRDSCluster_missingUserNameCausesError --- PASS: TestAccAWSRDSCluster_missingUserNameCausesError (3.22s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 3.243s ``` The error message for a required parameter being missing has a wrong parameter baked into it. Therefore, when the error message tried to fire, it was throwing a panic. Added a test to make sure that we know the condition still fires and with a correct message --- .../providers/aws/resource_aws_rds_cluster.go | 4 +-- .../aws/resource_aws_rds_cluster_test.go | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/builtin/providers/aws/resource_aws_rds_cluster.go b/builtin/providers/aws/resource_aws_rds_cluster.go index 2318f4639..b6a0400da 100644 --- a/builtin/providers/aws/resource_aws_rds_cluster.go +++ b/builtin/providers/aws/resource_aws_rds_cluster.go @@ -350,11 +350,11 @@ func resourceAwsRDSClusterCreate(d *schema.ResourceData, meta interface{}) error } else { if _, ok := d.GetOk("master_password"); !ok { - return fmt.Errorf(`provider.aws: aws_rds_cluster: %s: "master_password": required field is not set`, d.Get("name").(string)) + return fmt.Errorf(`provider.aws: aws_rds_cluster: %s: "master_password": required field is not set`, d.Get("database_name").(string)) } if _, ok := d.GetOk("master_username"); !ok { - return fmt.Errorf(`provider.aws: aws_rds_cluster: %s: "master_username": required field is not set`, d.Get("name").(string)) + return fmt.Errorf(`provider.aws: aws_rds_cluster: %s: "master_username": required field is not set`, d.Get("database_name").(string)) } createOpts := &rds.CreateDBClusterInput{ diff --git a/builtin/providers/aws/resource_aws_rds_cluster_test.go b/builtin/providers/aws/resource_aws_rds_cluster_test.go index 395612ab8..f9b5cb837 100644 --- a/builtin/providers/aws/resource_aws_rds_cluster_test.go +++ b/builtin/providers/aws/resource_aws_rds_cluster_test.go @@ -38,6 +38,22 @@ func TestAccAWSRDSCluster_basic(t *testing.T) { }) } +/// This is a regression test to make sure that we always cover the scenario as hightlighted in +/// https://github.com/hashicorp/terraform/issues/11568 +func TestAccAWSRDSCluster_missingUserNameCausesError(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSClusterDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSClusterConfigWithoutUserNameAndPassword(acctest.RandInt()), + ExpectError: regexp.MustCompile(`required field is not set`), + }, + }, + }) +} + func TestAccAWSRDSCluster_updateTags(t *testing.T) { var v rds.DBCluster ri := acctest.RandInt() @@ -228,6 +244,15 @@ resource "aws_rds_cluster" "default" { }`, n) } +func testAccAWSClusterConfigWithoutUserNameAndPassword(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" +}`, n) +} + func testAccAWSClusterConfigUpdatedTags(n int) string { return fmt.Sprintf(` resource "aws_rds_cluster" "default" {