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/elasticache_parameter_group: Allow removing parameters #1309

Merged
merged 1 commit into from
Aug 7, 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
76 changes: 51 additions & 25 deletions aws/resource_aws_elasticache_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func resourceAwsElasticacheParameterGroup() *schema.Resource {
"parameter": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: false,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Expand Down Expand Up @@ -144,36 +143,63 @@ func resourceAwsElasticacheParameterGroupUpdate(d *schema.ResourceData, meta int
os := o.(*schema.Set)
ns := n.(*schema.Set)

// Expand the "parameter" set to aws-sdk-go compat []elasticacheconn.Parameter
parameters, err := expandElastiCacheParameters(ns.Difference(os).List())
toRemove, err := expandElastiCacheParameters(os.Difference(ns).List())
if err != nil {
return err
}

if len(parameters) > 0 {
// We can only modify 20 parameters at a time, so walk them until
// we've got them all.
maxParams := 20
for parameters != nil {
paramsToModify := make([]*elasticache.ParameterNameValue, 0)
if len(parameters) <= maxParams {
paramsToModify, parameters = parameters[:], nil
} else {
paramsToModify, parameters = parameters[:maxParams], parameters[maxParams:]
}
modifyOpts := elasticache.ModifyCacheParameterGroupInput{
CacheParameterGroupName: aws.String(d.Get("name").(string)),
ParameterNameValues: paramsToModify,
}

log.Printf("[DEBUG] Modify Cache Parameter Group: %#v", modifyOpts)
_, err = conn.ModifyCacheParameterGroup(&modifyOpts)
if err != nil {
return fmt.Errorf("Error modifying Cache Parameter Group: %s", err)
}
log.Printf("[DEBUG] Parameters to remove: %#v", toRemove)

toAdd, err := expandElastiCacheParameters(ns.Difference(os).List())
if err != nil {
return err
}

log.Printf("[DEBUG] Parameters to add: %#v", toAdd)

// We can only modify 20 parameters at a time, so walk them until
// we've got them all.
maxParams := 20

for len(toRemove) > 0 {
paramsToModify := make([]*elasticache.ParameterNameValue, 0)
if len(toRemove) <= maxParams {
paramsToModify, toRemove = toRemove[:], nil
} else {
paramsToModify, toRemove = toRemove[:maxParams], toRemove[maxParams:]
}
resetOpts := elasticache.ResetCacheParameterGroupInput{
CacheParameterGroupName: aws.String(d.Get("name").(string)),
ParameterNameValues: paramsToModify,
}

log.Printf("[DEBUG] Reset Cache Parameter Group: %s", resetOpts)
_, err = conn.ResetCacheParameterGroup(&resetOpts)
if err != nil {
return fmt.Errorf("Error resetting Cache Parameter Group: %s", err)
}
}

for len(toAdd) > 0 {
paramsToModify := make([]*elasticache.ParameterNameValue, 0)
if len(toAdd) <= maxParams {
paramsToModify, toAdd = toAdd[:], nil
} else {
paramsToModify, toAdd = toAdd[:maxParams], toAdd[maxParams:]
}
modifyOpts := elasticache.ModifyCacheParameterGroupInput{
CacheParameterGroupName: aws.String(d.Get("name").(string)),
ParameterNameValues: paramsToModify,
}

log.Printf("[DEBUG] Modify Cache Parameter Group: %s", modifyOpts)
_, err = conn.ModifyCacheParameterGroup(&modifyOpts)
if err != nil {
return fmt.Errorf("Error modifying Cache Parameter Group: %s", err)
}
d.SetPartial("parameter")
}

d.SetPartial("parameter")
}

d.Partial(false)
Expand Down
38 changes: 37 additions & 1 deletion aws/resource_aws_elasticache_parameter_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestAccAWSElasticacheParameterGroup_basic(t *testing.T) {
})
}

func TestAccAWSElasticacheParameterGroupOnly(t *testing.T) {
func TestAccAWSElasticacheParameterGroup_only(t *testing.T) {
var v elasticache.CacheParameterGroup
rName := fmt.Sprintf("parameter-group-test-terraform-%d", acctest.RandInt())

Expand All @@ -87,6 +87,42 @@ func TestAccAWSElasticacheParameterGroupOnly(t *testing.T) {
})
}

// Regression for https://github.com/terraform-providers/terraform-provider-aws/issues/116
func TestAccAWSElasticacheParameterGroup_removeParam(t *testing.T) {
var v elasticache.CacheParameterGroup
rName := fmt.Sprintf("parameter-group-test-terraform-%d", acctest.RandInt())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSElasticacheParameterGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSElasticacheParameterGroupAddParametersConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheParameterGroupExists("aws_elasticache_parameter_group.bar", &v),
testAccCheckAWSElasticacheParameterGroupAttributes(&v, rName),
resource.TestCheckResourceAttr(
"aws_elasticache_parameter_group.bar", "name", rName),
resource.TestCheckResourceAttr(
"aws_elasticache_parameter_group.bar", "family", "redis2.8"),
),
},
{
Config: testAccAWSElasticacheParameterGroupOnlyConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheParameterGroupExists("aws_elasticache_parameter_group.bar", &v),
testAccCheckAWSElasticacheParameterGroupAttributes(&v, rName),
resource.TestCheckResourceAttr(
"aws_elasticache_parameter_group.bar", "name", rName),
resource.TestCheckResourceAttr(
"aws_elasticache_parameter_group.bar", "family", "redis2.8"),
),
},
},
})
}

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

Expand Down