2014-10-22 23:22:30 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-10-23 02:03:57 +02:00
|
|
|
"bytes"
|
2014-10-22 23:22:30 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2014-10-23 02:03:57 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2014-10-22 23:22:30 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/mitchellh/goamz/rds"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsDbParameterGroup() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsDbParameterGroupCreate,
|
|
|
|
Read: resourceAwsDbParameterGroupRead,
|
2014-10-23 02:03:57 +02:00
|
|
|
Update: resourceAwsDbParameterGroupUpdate,
|
2014-10-22 23:22:30 +02:00
|
|
|
Delete: resourceAwsDbParameterGroupDelete,
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
ForceNew: true,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"family": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"parameter": &schema.Schema{
|
2014-10-23 02:03:57 +02:00
|
|
|
Type: schema.TypeSet,
|
2014-10-22 23:22:30 +02:00
|
|
|
Optional: true,
|
2014-10-23 20:16:39 +02:00
|
|
|
ForceNew: false,
|
2014-10-22 23:22:30 +02:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"value": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-10-23 02:03:57 +02:00
|
|
|
Set: resourceAwsDbParameterHash,
|
2014-10-22 23:22:30 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 02:03:57 +02:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2014-10-22 23:22:30 +02:00
|
|
|
func resourceAwsDbParameterGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
rdsconn := p.rdsconn
|
|
|
|
|
|
|
|
createOpts := rds.CreateDBParameterGroup{
|
|
|
|
DBParameterGroupName: d.Get("name").(string),
|
|
|
|
DBParameterGroupFamily: d.Get("family").(string),
|
|
|
|
Description: d.Get("description").(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Create DB Parameter Group: %#v", createOpts)
|
|
|
|
_, err := rdsconn.CreateDBParameterGroup(&createOpts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating DB Parameter Group: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-23 20:16:39 +02:00
|
|
|
d.Partial(true)
|
|
|
|
d.SetPartial("name")
|
|
|
|
d.SetPartial("family")
|
|
|
|
d.SetPartial("description")
|
|
|
|
d.Partial(false)
|
|
|
|
|
2014-10-22 23:22:30 +02:00
|
|
|
d.SetId(createOpts.DBParameterGroupName)
|
|
|
|
log.Printf("[INFO] DB Parameter Group ID: %s", d.Id())
|
|
|
|
|
2014-10-23 02:03:57 +02:00
|
|
|
return resourceAwsDbParameterGroupUpdate(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsDbParameterGroupUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
rdsconn := p.rdsconn
|
|
|
|
|
2014-10-23 20:16:39 +02:00
|
|
|
d.Partial(true)
|
|
|
|
|
2014-10-23 02:03:57 +02:00
|
|
|
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)
|
|
|
|
|
2014-10-22 23:22:30 +02:00
|
|
|
// Expand the "parameter" set to goamz compat []rds.Parameter
|
2014-10-23 02:03:57 +02:00
|
|
|
parameters, err := expandParameters(ns.Difference(os).List())
|
2014-10-22 23:22:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-10-23 02:03:57 +02:00
|
|
|
if len(parameters) > 0 {
|
|
|
|
modifyOpts := rds.ModifyDBParameterGroup{
|
|
|
|
DBParameterGroupName: d.Get("name").(string),
|
|
|
|
Parameters: parameters,
|
|
|
|
}
|
2014-10-22 23:22:30 +02:00
|
|
|
|
2014-10-23 02:03:57 +02:00
|
|
|
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)
|
|
|
|
}
|
2014-10-22 23:22:30 +02:00
|
|
|
}
|
2014-10-23 20:16:39 +02:00
|
|
|
d.SetPartial("parameter")
|
2014-10-22 23:22:30 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 20:16:39 +02:00
|
|
|
d.Partial(false)
|
|
|
|
|
|
|
|
return resourceAwsDbParameterGroupRead(d, meta)
|
2014-10-22 23:22:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsDbParameterGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"pending"},
|
|
|
|
Target: "destroyed",
|
|
|
|
Refresh: resourceDbParameterGroupDeleteRefreshFunc(d, meta),
|
|
|
|
Timeout: 3 * time.Minute,
|
|
|
|
MinTimeout: 1 * time.Second,
|
|
|
|
}
|
|
|
|
_, err := stateConf.WaitForState()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsDbParameterGroupRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
rdsconn := p.rdsconn
|
|
|
|
|
|
|
|
describeOpts := rds.DescribeDBParameterGroups{
|
|
|
|
DBParameterGroupName: d.Id(),
|
|
|
|
}
|
|
|
|
|
|
|
|
describeResp, err := rdsconn.DescribeDBParameterGroups(&describeOpts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(describeResp.DBParameterGroups) != 1 ||
|
|
|
|
describeResp.DBParameterGroups[0].DBParameterGroupName != d.Id() {
|
|
|
|
return fmt.Errorf("Unable to find Parameter Group: %#v", describeResp.DBParameterGroups)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", describeResp.DBParameterGroups[0].DBParameterGroupName)
|
|
|
|
d.Set("family", describeResp.DBParameterGroups[0].DBParameterGroupFamily)
|
|
|
|
d.Set("description", describeResp.DBParameterGroups[0].Description)
|
|
|
|
|
|
|
|
// Only include user customized parameters as there's hundreds of system/default ones
|
|
|
|
describeParametersOpts := rds.DescribeDBParameters{
|
|
|
|
DBParameterGroupName: d.Id(),
|
|
|
|
Source: "user",
|
|
|
|
}
|
|
|
|
|
|
|
|
describeParametersResp, err := rdsconn.DescribeDBParameters(&describeParametersOpts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("parameter", flattenParameters(describeParametersResp.Parameters))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceDbParameterGroupDeleteRefreshFunc(
|
|
|
|
d *schema.ResourceData,
|
|
|
|
meta interface{}) resource.StateRefreshFunc {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
rdsconn := p.rdsconn
|
|
|
|
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
|
|
|
|
deleteOpts := rds.DeleteDBParameterGroup{
|
|
|
|
DBParameterGroupName: d.Id(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := rdsconn.DeleteDBParameterGroup(&deleteOpts); err != nil {
|
|
|
|
rdserr, ok := err.(*rds.Error)
|
|
|
|
if !ok {
|
|
|
|
return d, "error", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rdserr.Code != "DBParameterGroupNotFoundFault" {
|
|
|
|
return d, "error", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, "destroyed", nil
|
|
|
|
}
|
|
|
|
}
|