Fixed incorrect ForcesNew settings for parameters and added better acceptance tests to ensure updates are working as expected

This commit is contained in:
Rob Costanzo 2014-10-23 11:16:39 -07:00
parent 3b54001a1b
commit 0e3afa6d3d
4 changed files with 79 additions and 23 deletions

View File

@ -37,7 +37,7 @@ func resourceAwsDbParameterGroup() *schema.Resource {
"parameter": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
ForceNew: false,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"apply_method": &schema.Schema{
@ -85,6 +85,12 @@ func resourceAwsDbParameterGroupCreate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error creating DB Parameter Group: %s", err)
}
d.Partial(true)
d.SetPartial("name")
d.SetPartial("family")
d.SetPartial("description")
d.Partial(false)
d.SetId(createOpts.DBParameterGroupName)
log.Printf("[INFO] DB Parameter Group ID: %s", d.Id())
@ -95,6 +101,8 @@ func resourceAwsDbParameterGroupUpdate(d *schema.ResourceData, meta interface{})
p := meta.(*ResourceProvider)
rdsconn := p.rdsconn
d.Partial(true)
if d.HasChange("parameter") {
o, n := d.GetChange("parameter")
if o == nil {
@ -125,9 +133,12 @@ func resourceAwsDbParameterGroupUpdate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error modifying DB Parameter Group: %s", err)
}
}
d.SetPartial("parameter")
}
return nil
d.Partial(false)
return resourceAwsDbParameterGroupRead(d, meta)
}
func resourceAwsDbParameterGroupDelete(d *schema.ResourceData, meta interface{}) error {
@ -175,6 +186,9 @@ func resourceAwsDbParameterGroupRead(d *schema.ResourceData, meta interface{}) e
return err
}
// apply_method is only relevant for creates and AWS does not maintain its state.
d.Set("parameter", flattenParameters(describeParametersResp.Parameters))
return nil

View File

@ -19,6 +19,31 @@ func TestAccAWSDBParameterGroup(t *testing.T) {
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSDBParameterGroupConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v),
testAccCheckAWSDBParameterGroupAttributes(&v),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "name", "parameter-group-test-terraform"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "family", "mysql5.6"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "description", "Test parameter group for terraform"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.0.name", "character_set_results"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.0.value", "utf8"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.1.name", "character_set_server"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.1.value", "utf8"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.2.name", "character_set_client"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.2.value", "utf8"),
),
},
resource.TestStep{
Config: testAccAWSDBParameterGroupAddParametersConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v),
testAccCheckAWSDBParameterGroupAttributes(&v),
@ -32,32 +57,22 @@ func TestAccAWSDBParameterGroup(t *testing.T) {
"aws_db_parameter_group.bar", "parameter.0.name", "collation_connection"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.0.value", "utf8_unicode_ci"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.0.apply_method", "immediate"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.1.name", "character_set_results"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.1.value", "utf8"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.1.apply_method", "immediate"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.2.name", "character_set_server"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.2.value", "utf8"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.2.apply_method", "immediate"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.3.name", "collation_server"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.3.value", "utf8_unicode_ci"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.3.apply_method", "immediate"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.4.name", "character_set_client"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.4.value", "utf8"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.4.apply_method", "immediate"),
),
},
},
@ -177,6 +192,29 @@ func testAccCheckAWSDBParameterGroupExists(n string, v *rds.DBParameterGroup) re
}
const testAccAWSDBParameterGroupConfig = `
resource "aws_db_parameter_group" "bar" {
name = "parameter-group-test-terraform"
family = "mysql5.6"
description = "Test parameter group for terraform"
parameter {
name = "character_set_server"
value = "utf8"
apply_method = "immediate"
}
parameter {
name = "character_set_client"
value = "utf8"
apply_method = "immediate"
}
parameter{
name = "character_set_results"
value = "utf8"
apply_method = "immediate"
}
}
`
const testAccAWSDBParameterGroupAddParametersConfig = `
resource "aws_db_parameter_group" "bar" {
name = "parameter-group-test-terraform"
family = "mysql5.6"

View File

@ -192,6 +192,9 @@ func flattenParameters(list []rds.Parameter) []map[string]interface{} {
result = append(result, map[string]interface{}{
"name": strings.ToLower(i.ParameterName),
"value": strings.ToLower(i.ParameterValue),
// apply_method is only used on create/modify and is not returned when reading from AWS.
// This is a dummy value to print out as it's not used when doing the state diff for updating
"apply_method": "applied",
})
}
return result

View File

@ -299,6 +299,7 @@ func Test_flattenParameters(t *testing.T) {
map[string]interface{}{
"name": "character_set_client",
"value": "utf8",
"apply_method": "applied",
},
},
},