From 166ca356950dfefef782fe0ca4ad7fc3a325e06e Mon Sep 17 00:00:00 2001 From: Stefan Sundin Date: Wed, 29 Aug 2018 19:25:44 -0700 Subject: [PATCH 1/2] Support resetting parameters in aws_db_parameter_group and aws_rds_cluster_parameter_group. --- aws/resource_aws_db_parameter_group.go | 20 +++++++++++++++++++ ...esource_aws_rds_cluster_parameter_group.go | 20 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/aws/resource_aws_db_parameter_group.go b/aws/resource_aws_db_parameter_group.go index faafcb3583b..1dc9ef2e2a1 100644 --- a/aws/resource_aws_db_parameter_group.go +++ b/aws/resource_aws_db_parameter_group.go @@ -295,6 +295,26 @@ func resourceAwsDbParameterGroupUpdate(d *schema.ResourceData, meta interface{}) } d.SetPartial("parameter") } + + // Reset parameters that have been removed + parameters, err = expandParameters(os.Difference(ns).List()) + if err != nil { + return err + } + if len(parameters) > 0 { + parameterGroupName := d.Get("name").(string) + resetOpts := rds.ResetDBParameterGroupInput{ + DBParameterGroupName: aws.String(parameterGroupName), + Parameters: parameters, + } + + log.Printf("[DEBUG] Reset DB Parameter Group: %s", resetOpts) + _, err = rdsconn.ResetDBParameterGroup(&resetOpts) + if err != nil { + return fmt.Errorf("Error resetting DB Parameter Group: %s", err) + } + d.SetPartial("parameter") + } } if err := setTagsRDS(rdsconn, d, d.Get("arn").(string)); err != nil { diff --git a/aws/resource_aws_rds_cluster_parameter_group.go b/aws/resource_aws_rds_cluster_parameter_group.go index 577a5b6f4b3..5e4d4b19ebd 100644 --- a/aws/resource_aws_rds_cluster_parameter_group.go +++ b/aws/resource_aws_rds_cluster_parameter_group.go @@ -234,6 +234,26 @@ func resourceAwsRDSClusterParameterGroupUpdate(d *schema.ResourceData, meta inte } d.SetPartial("parameter") } + + // Reset parameters that have been removed + parameters, err = expandParameters(os.Difference(ns).List()) + if err != nil { + return err + } + if len(parameters) > 0 { + parameterGroupName := d.Get("name").(string) + resetOpts := rds.ResetDBClusterParameterGroupInput{ + DBClusterParameterGroupName: aws.String(parameterGroupName), + Parameters: parameters, + } + + log.Printf("[DEBUG] Reset DB Cluster Parameter Group: %s", resetOpts) + _, err = rdsconn.ResetDBClusterParameterGroup(&resetOpts) + if err != nil { + return fmt.Errorf("Error resetting DB Cluster Parameter Group: %s", err) + } + d.SetPartial("parameter") + } } if err := setTagsRDS(rdsconn, d, d.Get("arn").(string)); err != nil { From a4963dd5f9bae50972bedfb44692a2d1ceb362e1 Mon Sep 17 00:00:00 2001 From: Stefan Sundin Date: Fri, 31 Aug 2018 11:36:26 -0700 Subject: [PATCH 2/2] Try to add a test. --- aws/resource_aws_db_parameter_group_test.go | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/aws/resource_aws_db_parameter_group_test.go b/aws/resource_aws_db_parameter_group_test.go index 7f7ba29372f..a3aca8e80be 100644 --- a/aws/resource_aws_db_parameter_group_test.go +++ b/aws/resource_aws_db_parameter_group_test.go @@ -291,6 +291,15 @@ func TestAccAWSDBParameterGroup_basic(t *testing.T) { "aws_db_parameter_group.bar", "arn", regexp.MustCompile(fmt.Sprintf("^arn:[^:]+:rds:[^:]+:\\d{12}:pg:%s", groupName))), ), }, + { + Config: testAccAWSDBParameterGroupConfig(groupName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v), + testAccCheckAWSDBParameterGroupAttributes(&v, groupName), + testAccCheckAWSDBParameterNotUserDefined("aws_db_parameter_group.bar", "collation_connection"), + testAccCheckAWSDBParameterNotUserDefined("aws_db_parameter_group.bar", "collation_server"), + ), + }, }, }) } @@ -539,6 +548,45 @@ func testAccCheckAWSDBParameterGroupExists(n string, v *rds.DBParameterGroup) re } } +func testAccCheckAWSDBParameterNotUserDefined(n, paramName string) 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 Parameter Group ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).rdsconn + + opts := rds.DescribeDBParametersInput{ + DBParameterGroupName: aws.String(rs.Primary.ID), + Source: aws.String("user"), + } + fmt.Printf("test: %s\n", rs.Primary.ID) + + userDefined := false + conn.DescribeDBParametersPages(&opts, func(page *rds.DescribeDBParametersOutput, lastPage bool) bool { + for _, param := range page.Parameters { + fmt.Printf("param: %s %s\n", *param.ParameterName, *param.Source) + if *param.ParameterName == paramName { + userDefined = true + return false + } + } + return true + }) + + if userDefined { + return fmt.Errorf("DB Parameter is user defined") + } + + return nil + } +} + func randomString(strlen int) string { rand.Seed(time.Now().UTC().UnixNano()) const chars = "abcdefghijklmnopqrstuvwxyz"