Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

r/aws_rds_cluster_parameter_group and r/aws_db_parameter_group: Resto… #2

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 18 additions & 1 deletion aws/resource_aws_db_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,25 @@ func resourceAwsDbParameterGroupUpdate(d *schema.ResourceData, meta interface{})
}
}

toRemove := map[string]*rds.Parameter{}

for _, p := range expandParameters(os.List()) {
if p.ParameterName != nil {
toRemove[*p.ParameterName] = p
}
}

for _, p := range expandParameters(ns.List()) {
if p.ParameterName != nil {
delete(toRemove, *p.ParameterName)
}
}

// Reset parameters that have been removed
resetParameters := expandParameters(os.Difference(ns).List())
var resetParameters []*rds.Parameter
for _, v := range toRemove {
resetParameters = append(resetParameters, v)
}
if len(resetParameters) > 0 {
maxParams := 20
for resetParameters != nil {
Expand Down
107 changes: 107 additions & 0 deletions aws/resource_aws_db_parameter_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,65 @@ func TestAccAWSDBParameterGroup_MatchDefault(t *testing.T) {
})
}

func TestAccAWSDBParameterGroup_updateParameters(t *testing.T) {
var v rds.DBParameterGroup
resourceName := "aws_db_parameter_group.test"
groupName := fmt.Sprintf("parameter-group-test-terraform-%d", acctest.RandInt())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDBParameterGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDBParameterGroupUpdateParametersInitialConfig(groupName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBParameterGroupExists(resourceName, &v),
testAccCheckAWSDBParameterGroupAttributes(&v, groupName),
resource.TestCheckResourceAttr(resourceName, "name", groupName),
resource.TestCheckResourceAttr(resourceName, "family", "mysql5.6"),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "parameter.*", map[string]string{
"name": "character_set_results",
"value": "utf8",
}),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "parameter.*", map[string]string{
"name": "character_set_server",
"value": "utf8",
}),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "parameter.*", map[string]string{
"name": "character_set_client",
"value": "utf8",
}),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSDBParameterGroupUpdateParametersUpdatedConfig(groupName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBParameterGroupExists(resourceName, &v),
testAccCheckAWSDBParameterGroupAttributes(&v, groupName),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "parameter.*", map[string]string{
"name": "character_set_results",
"value": "ascii",
}),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "parameter.*", map[string]string{
"name": "character_set_server",
"value": "ascii",
}),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "parameter.*", map[string]string{
"name": "character_set_client",
"value": "utf8",
}),
),
},
},
})
}

func testAccCheckAWSDbParamaterGroupDisappears(v *rds.DBParameterGroup) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).rdsconn
Expand Down Expand Up @@ -1372,6 +1431,54 @@ resource "aws_db_parameter_group" "test" {
`, n)
}

func testAccAWSDBParameterGroupUpdateParametersInitialConfig(n string) string {
return fmt.Sprintf(`
resource "aws_db_parameter_group" "test" {
name = "%s"
family = "mysql5.6"

parameter {
name = "character_set_server"
value = "utf8"
}

parameter {
name = "character_set_client"
value = "utf8"
}

parameter {
name = "character_set_results"
value = "utf8"
}
}
`, n)
}

func testAccAWSDBParameterGroupUpdateParametersUpdatedConfig(n string) string {
return fmt.Sprintf(`
resource "aws_db_parameter_group" "test" {
name = "%s"
family = "mysql5.6"

parameter {
name = "character_set_server"
value = "ascii"
}

parameter {
name = "character_set_client"
value = "utf8"
}

parameter {
name = "character_set_results"
value = "ascii"
}
}
`, n)
}

const testAccDBParameterGroupConfig_namePrefix = `
resource "aws_db_parameter_group" "test" {
name_prefix = "tf-test-"
Expand Down
29 changes: 23 additions & 6 deletions aws/resource_aws_rds_cluster_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,33 @@ func resourceAwsRDSClusterParameterGroupUpdate(d *schema.ResourceData, meta inte
}
}

toRemove := map[string]*rds.Parameter{}

for _, p := range expandParameters(os.List()) {
if p.ParameterName != nil {
toRemove[*p.ParameterName] = p
}
}

for _, p := range expandParameters(ns.List()) {
if p.ParameterName != nil {
delete(toRemove, *p.ParameterName)
}
}

// Reset parameters that have been removed
parameters = expandParameters(os.Difference(ns).List())
if len(parameters) > 0 {
for parameters != nil {
var resetParameters []*rds.Parameter
for _, v := range toRemove {
resetParameters = append(resetParameters, v)
}
if len(resetParameters) > 0 {
for resetParameters != nil {
parameterGroupName := d.Get("name").(string)
var paramsToReset []*rds.Parameter
if len(parameters) <= rdsClusterParameterGroupMaxParamsBulkEdit {
paramsToReset, parameters = parameters[:], nil
if len(resetParameters) <= rdsClusterParameterGroupMaxParamsBulkEdit {
paramsToReset, resetParameters = resetParameters[:], nil
} else {
paramsToReset, parameters = parameters[:rdsClusterParameterGroupMaxParamsBulkEdit], parameters[rdsClusterParameterGroupMaxParamsBulkEdit:]
paramsToReset, resetParameters = resetParameters[:rdsClusterParameterGroupMaxParamsBulkEdit], resetParameters[rdsClusterParameterGroupMaxParamsBulkEdit:]
}

resetOpts := rds.ResetDBClusterParameterGroupInput{
Expand Down
107 changes: 107 additions & 0 deletions aws/resource_aws_rds_cluster_parameter_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,65 @@ func TestAccAWSDBClusterParameterGroup_only(t *testing.T) {
})
}

func TestAccAWSDBClusterParameterGroup_updateParameters(t *testing.T) {
var v rds.DBClusterParameterGroup
resourceName := "aws_rds_cluster_parameter_group.test"
groupName := fmt.Sprintf("cluster-parameter-group-test-tf-%d", acctest.RandInt())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDBClusterParameterGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDBClusterParameterGroupUpdateParametersInitialConfig(groupName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBClusterParameterGroupExists(resourceName, &v),
testAccCheckAWSDBClusterParameterGroupAttributes(&v, groupName),
resource.TestCheckResourceAttr(resourceName, "name", groupName),
resource.TestCheckResourceAttr(resourceName, "family", "aurora5.6"),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "parameter.*", map[string]string{
"name": "character_set_results",
"value": "utf8",
}),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "parameter.*", map[string]string{
"name": "character_set_server",
"value": "utf8",
}),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "parameter.*", map[string]string{
"name": "character_set_client",
"value": "utf8",
}),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSDBClusterParameterGroupUpdateParametersUpdatedConfig(groupName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBClusterParameterGroupExists(resourceName, &v),
testAccCheckAWSDBClusterParameterGroupAttributes(&v, groupName),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "parameter.*", map[string]string{
"name": "character_set_results",
"value": "ascii",
}),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "parameter.*", map[string]string{
"name": "character_set_server",
"value": "ascii",
}),
tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "parameter.*", map[string]string{
"name": "character_set_client",
"value": "utf8",
}),
),
},
},
})
}

func testAccCheckAWSDBClusterParameterGroupDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).rdsconn

Expand Down Expand Up @@ -623,6 +682,54 @@ resource "aws_rds_cluster_parameter_group" "test" {
`, name)
}

func testAccAWSDBClusterParameterGroupUpdateParametersInitialConfig(name string) string {
return fmt.Sprintf(`
resource "aws_rds_cluster_parameter_group" "test" {
name = "%s"
family = "aurora5.6"

parameter {
name = "character_set_server"
value = "utf8"
}

parameter {
name = "character_set_client"
value = "utf8"
}

parameter {
name = "character_set_results"
value = "utf8"
}
}
`, name)
}

func testAccAWSDBClusterParameterGroupUpdateParametersUpdatedConfig(name string) string {
return fmt.Sprintf(`
resource "aws_rds_cluster_parameter_group" "test" {
name = "%s"
family = "aurora5.6"

parameter {
name = "character_set_server"
value = "ascii"
}

parameter {
name = "character_set_client"
value = "utf8"
}

parameter {
name = "character_set_results"
value = "ascii"
}
}
`, name)
}

const testAccAWSDBClusterParameterGroupConfig_namePrefix = `
resource "aws_rds_cluster_parameter_group" "test" {
name_prefix = "tf-test-"
Expand Down