Added missing update support for new aws_db_parameter_group resource

This commit is contained in:
Rob Costanzo 2014-10-22 17:03:57 -07:00
parent de16b10555
commit 3b54001a1b
4 changed files with 51 additions and 34 deletions

View File

@ -1,10 +1,12 @@
package aws
import (
"bytes"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/rds"
@ -14,7 +16,7 @@ func resourceAwsDbParameterGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDbParameterGroupCreate,
Read: resourceAwsDbParameterGroupRead,
Update: nil,
Update: resourceAwsDbParameterGroupUpdate,
Delete: resourceAwsDbParameterGroupDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
@ -33,7 +35,7 @@ func resourceAwsDbParameterGroup() *schema.Resource {
ForceNew: true,
},
"parameter": &schema.Schema{
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
@ -52,11 +54,21 @@ func resourceAwsDbParameterGroup() *schema.Resource {
},
},
},
Set: resourceAwsDbParameterHash,
},
},
}
}
func resourceAwsDbParameterHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["value"].(string)))
return hashcode.String(buf.String())
}
func resourceAwsDbParameterGroupCreate(d *schema.ResourceData, meta interface{}) error {
p := meta.(*ResourceProvider)
rdsconn := p.rdsconn
@ -76,26 +88,46 @@ func resourceAwsDbParameterGroupCreate(d *schema.ResourceData, meta interface{})
d.SetId(createOpts.DBParameterGroupName)
log.Printf("[INFO] DB Parameter Group ID: %s", d.Id())
if d.Get("parameter") != "" {
return resourceAwsDbParameterGroupUpdate(d, meta)
}
func resourceAwsDbParameterGroupUpdate(d *schema.ResourceData, meta interface{}) error {
p := meta.(*ResourceProvider)
rdsconn := p.rdsconn
if d.HasChange("parameter") {
o, n := d.GetChange("parameter")
if o == nil {
o = new(schema.Set)
}
if n == nil {
n = new(schema.Set)
}
os := o.(*schema.Set)
ns := n.(*schema.Set)
// Expand the "parameter" set to goamz compat []rds.Parameter
parameters, err := expandParameters(d.Get("parameter").([]interface{}))
parameters, err := expandParameters(ns.Difference(os).List())
if err != nil {
return err
}
modifyOpts := rds.ModifyDBParameterGroup{
DBParameterGroupName: d.Get("name").(string),
Parameters: parameters,
}
if len(parameters) > 0 {
modifyOpts := rds.ModifyDBParameterGroup{
DBParameterGroupName: d.Get("name").(string),
Parameters: parameters,
}
log.Printf("[DEBUG] Modify DB Parameter Group: %#v", modifyOpts)
_, err = rdsconn.ModifyDBParameterGroup(&modifyOpts)
if err != nil {
return fmt.Errorf("Error modifying DB Parameter Group: %s", err)
log.Printf("[DEBUG] Modify DB Parameter Group: %#v", modifyOpts)
_, err = rdsconn.ModifyDBParameterGroup(&modifyOpts)
if err != nil {
return fmt.Errorf("Error modifying DB Parameter Group: %s", err)
}
}
}
return resourceAwsDbParameterGroupRead(d, meta)
return nil
}
func resourceAwsDbParameterGroupDelete(d *schema.ResourceData, meta interface{}) error {

View File

@ -29,9 +29,9 @@ func TestAccAWSDBParameterGroup(t *testing.T) {
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_client"),
"aws_db_parameter_group.bar", "parameter.0.name", "collation_connection"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.0.value", "utf8"),
"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(
@ -47,15 +47,15 @@ func TestAccAWSDBParameterGroup(t *testing.T) {
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.2.apply_method", "immediate"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.3.name", "collation_connection"),
"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", "collation_server"),
"aws_db_parameter_group.bar", "parameter.4.name", "character_set_client"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.4.value", "utf8_unicode_ci"),
"aws_db_parameter_group.bar", "parameter.4.value", "utf8"),
resource.TestCheckResourceAttr(
"aws_db_parameter_group.bar", "parameter.4.apply_method", "immediate"),
),

View File

@ -1,7 +1,6 @@
package aws
import (
"sort"
"strings"
"github.com/hashicorp/terraform/helper/schema"
@ -186,9 +185,8 @@ func flattenListeners(list []elb.Listener) []map[string]interface{} {
return result
}
// Flattens and sorts by name an array of Parameters into a []map[string]interface{}
// Flattens an array of Parameters into a []map[string]interface{}
func flattenParameters(list []rds.Parameter) []map[string]interface{} {
sort.Sort(ByParameterName(list))
result := make([]map[string]interface{}, 0, len(list))
for _, i := range list {
result = append(result, map[string]interface{}{
@ -208,8 +206,3 @@ func expandStringList(configured []interface{}) []string {
}
return vs
}
type ByParameterName []rds.Parameter
func (a ByParameterName) Len() int { return len(a) }
func (a ByParameterName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByParameterName) Less(i, j int) bool { return a[i].ParameterName < a[j].ParameterName }

View File

@ -294,16 +294,8 @@ func Test_flattenParameters(t *testing.T) {
ParameterName: "character_set_client",
ParameterValue: "utf8",
},
rds.Parameter{
ParameterName: "character_set_aaaclient",
ParameterValue: "utf8",
},
},
Output: []map[string]interface{}{
map[string]interface{}{
"name": "character_set_aaaclient",
"value": "utf8",
},
map[string]interface{}{
"name": "character_set_client",
"value": "utf8",