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"
|
2015-03-17 23:22:30 +01:00
|
|
|
"strings"
|
2014-10-22 23:22:30 +02:00
|
|
|
"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"
|
2015-02-26 16:33:33 +01:00
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/rds"
|
2014-10-22 23:22:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
2016-06-26 22:20:52 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
|
|
|
|
2014-10-22 23:22:30 +02:00
|
|
|
Schema: map[string]*schema.Schema{
|
2015-12-11 13:07:50 +01:00
|
|
|
"arn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2014-10-22 23:22:30 +02:00
|
|
|
"name": &schema.Schema{
|
2017-03-31 19:22:57 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ConflictsWith: []string{"name_prefix"},
|
|
|
|
ValidateFunc: validateDbParamGroupName,
|
|
|
|
},
|
|
|
|
"name_prefix": &schema.Schema{
|
2015-10-08 10:39:11 +02:00
|
|
|
Type: schema.TypeString,
|
2017-03-31 19:22:57 +02:00
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
2015-10-08 10:39:11 +02:00
|
|
|
ForceNew: true,
|
2017-03-31 19:22:57 +02:00
|
|
|
ValidateFunc: validateDbParamGroupNamePrefix,
|
2014-10-22 23:22:30 +02:00
|
|
|
},
|
|
|
|
"family": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2016-04-18 23:06:15 +02:00
|
|
|
Optional: true,
|
2014-10-22 23:22:30 +02:00
|
|
|
ForceNew: true,
|
2016-04-18 23:06:15 +02:00
|
|
|
Default: "Managed by Terraform",
|
2014-10-22 23:22:30 +02:00
|
|
|
},
|
|
|
|
"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,
|
|
|
|
},
|
2015-01-30 17:00:14 +01:00
|
|
|
"apply_method": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "immediate",
|
|
|
|
},
|
2014-10-22 23:22:30 +02:00
|
|
|
},
|
|
|
|
},
|
2014-10-23 02:03:57 +02:00
|
|
|
Set: resourceAwsDbParameterHash,
|
2014-10-22 23:22:30 +02:00
|
|
|
},
|
2015-12-11 13:07:50 +01:00
|
|
|
|
|
|
|
"tags": tagsSchema(),
|
2014-10-22 23:22:30 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsDbParameterGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-02-27 17:50:21 +01:00
|
|
|
rdsconn := meta.(*AWSClient).rdsconn
|
2015-12-11 13:07:50 +01:00
|
|
|
tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{}))
|
2014-10-22 23:22:30 +02:00
|
|
|
|
2017-03-31 19:22:57 +02:00
|
|
|
var groupName string
|
|
|
|
if v, ok := d.GetOk("name"); ok {
|
|
|
|
groupName = v.(string)
|
|
|
|
} else if v, ok := d.GetOk("name_prefix"); ok {
|
|
|
|
groupName = resource.PrefixedUniqueId(v.(string))
|
|
|
|
} else {
|
|
|
|
groupName = resource.UniqueId()
|
|
|
|
}
|
2017-04-03 01:56:25 +02:00
|
|
|
d.Set("name", groupName)
|
2017-03-31 19:22:57 +02:00
|
|
|
|
2015-04-15 22:02:52 +02:00
|
|
|
createOpts := rds.CreateDBParameterGroupInput{
|
2017-03-31 19:22:57 +02:00
|
|
|
DBParameterGroupName: aws.String(groupName),
|
2015-02-26 16:33:33 +01:00
|
|
|
DBParameterGroupFamily: aws.String(d.Get("family").(string)),
|
|
|
|
Description: aws.String(d.Get("description").(string)),
|
2015-12-11 13:07:50 +01:00
|
|
|
Tags: tags,
|
2014-10-22 23:22:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2015-02-26 16:33:33 +01:00
|
|
|
d.SetId(*createOpts.DBParameterGroupName)
|
2014-10-22 23:22:30 +02:00
|
|
|
log.Printf("[INFO] DB Parameter Group ID: %s", d.Id())
|
|
|
|
|
2014-10-23 02:03:57 +02:00
|
|
|
return resourceAwsDbParameterGroupUpdate(d, meta)
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsDbParameterGroupRead(d *schema.ResourceData, meta interface{}) error {
|
2015-02-27 17:50:21 +01:00
|
|
|
rdsconn := meta.(*AWSClient).rdsconn
|
2014-11-21 17:58:34 +01:00
|
|
|
|
2015-04-15 22:02:52 +02:00
|
|
|
describeOpts := rds.DescribeDBParameterGroupsInput{
|
2015-02-26 16:33:33 +01:00
|
|
|
DBParameterGroupName: aws.String(d.Id()),
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
describeResp, err := rdsconn.DescribeDBParameterGroups(&describeOpts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(describeResp.DBParameterGroups) != 1 ||
|
2015-02-26 16:33:33 +01:00
|
|
|
*describeResp.DBParameterGroups[0].DBParameterGroupName != d.Id() {
|
2014-11-21 17:58:34 +01:00
|
|
|
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
|
2015-04-15 22:02:52 +02:00
|
|
|
describeParametersOpts := rds.DescribeDBParametersInput{
|
2015-02-26 16:33:33 +01:00
|
|
|
DBParameterGroupName: aws.String(d.Id()),
|
|
|
|
Source: aws.String("user"),
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
describeParametersResp, err := rdsconn.DescribeDBParameters(&describeParametersOpts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:28:18 +02:00
|
|
|
d.Set("parameter", flattenParameters(describeParametersResp.Parameters))
|
2014-11-21 17:58:34 +01:00
|
|
|
|
2015-12-11 13:07:50 +01:00
|
|
|
paramGroup := describeResp.DBParameterGroups[0]
|
2016-10-08 06:52:50 +02:00
|
|
|
arn, err := buildRDSPGARN(d.Id(), meta.(*AWSClient).partition, meta.(*AWSClient).accountid, meta.(*AWSClient).region)
|
2015-12-11 13:07:50 +01:00
|
|
|
if err != nil {
|
|
|
|
name := "<empty>"
|
|
|
|
if paramGroup.DBParameterGroupName != nil && *paramGroup.DBParameterGroupName != "" {
|
|
|
|
name = *paramGroup.DBParameterGroupName
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] Error building ARN for DB Parameter Group, not setting Tags for Param Group %s", name)
|
|
|
|
} else {
|
|
|
|
d.Set("arn", arn)
|
|
|
|
resp, err := rdsconn.ListTagsForResource(&rds.ListTagsForResourceInput{
|
|
|
|
ResourceName: aws.String(arn),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn)
|
|
|
|
}
|
|
|
|
|
|
|
|
var dt []*rds.Tag
|
|
|
|
if len(resp.TagList) > 0 {
|
|
|
|
dt = resp.TagList
|
|
|
|
}
|
|
|
|
d.Set("tags", tagsToMapRDS(dt))
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-23 02:03:57 +02:00
|
|
|
func resourceAwsDbParameterGroupUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-02-27 17:50:21 +01:00
|
|
|
rdsconn := meta.(*AWSClient).rdsconn
|
2014-10-23 02:03:57 +02:00
|
|
|
|
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)
|
|
|
|
|
2015-03-12 22:49:38 +01:00
|
|
|
// Expand the "parameter" set to aws-sdk-go compat []rds.Parameter
|
2015-04-16 22:28:18 +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 {
|
2016-07-25 14:25:36 +02:00
|
|
|
// 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([]*rds.Parameter, 0)
|
|
|
|
if len(parameters) <= maxParams {
|
|
|
|
paramsToModify, parameters = parameters[:], nil
|
|
|
|
} else {
|
|
|
|
paramsToModify, parameters = parameters[:maxParams], parameters[maxParams:]
|
|
|
|
}
|
|
|
|
modifyOpts := rds.ModifyDBParameterGroupInput{
|
|
|
|
DBParameterGroupName: aws.String(d.Get("name").(string)),
|
|
|
|
Parameters: paramsToModify,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Modify DB Parameter Group: %s", modifyOpts)
|
|
|
|
_, err = rdsconn.ModifyDBParameterGroup(&modifyOpts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error modifying DB Parameter Group: %s", err)
|
|
|
|
}
|
2014-10-23 02:03:57 +02:00
|
|
|
}
|
2016-07-25 14:25:36 +02:00
|
|
|
d.SetPartial("parameter")
|
2014-10-22 23:22:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 06:52:50 +02:00
|
|
|
if arn, err := buildRDSPGARN(d.Id(), meta.(*AWSClient).partition, meta.(*AWSClient).accountid, meta.(*AWSClient).region); err == nil {
|
2015-12-11 13:07:50 +01:00
|
|
|
if err := setTagsRDS(rdsconn, d, arn); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
d.SetPartial("tags")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-06-05 16:38:23 +02:00
|
|
|
conn := meta.(*AWSClient).rdsconn
|
|
|
|
return resource.Retry(3*time.Minute, func() *resource.RetryError {
|
2015-04-15 22:02:52 +02:00
|
|
|
deleteOpts := rds.DeleteDBParameterGroupInput{
|
2015-02-26 16:33:33 +01:00
|
|
|
DBParameterGroupName: aws.String(d.Id()),
|
2014-10-22 23:22:30 +02:00
|
|
|
}
|
|
|
|
|
2017-06-05 16:38:23 +02:00
|
|
|
_, err := conn.DeleteDBParameterGroup(&deleteOpts)
|
|
|
|
if err != nil {
|
|
|
|
awsErr, ok := err.(awserr.Error)
|
|
|
|
if ok && awsErr.Code() == "DBParameterGroupNotFoundFault" {
|
|
|
|
return resource.RetryableError(err)
|
2014-10-22 23:22:30 +02:00
|
|
|
}
|
2017-06-05 16:38:23 +02:00
|
|
|
if ok && awsErr.Code() == "InvalidDBParameterGroupState" {
|
|
|
|
return resource.RetryableError(err)
|
2014-10-22 23:22:30 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-05 16:38:23 +02:00
|
|
|
return resource.NonRetryableError(err)
|
|
|
|
})
|
2014-10-22 23:22:30 +02:00
|
|
|
}
|
2014-11-21 17:58:34 +01:00
|
|
|
|
|
|
|
func resourceAwsDbParameterHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
|
2015-03-17 23:22:30 +01:00
|
|
|
// Store the value as a lower case string, to match how we store them in flattenParameters
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(m["value"].(string))))
|
2014-11-21 17:58:34 +01:00
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
2015-10-08 10:39:11 +02:00
|
|
|
|
2016-10-08 06:52:50 +02:00
|
|
|
func buildRDSPGARN(identifier, partition, accountid, region string) (string, error) {
|
|
|
|
if partition == "" {
|
|
|
|
return "", fmt.Errorf("Unable to construct RDS ARN because of missing AWS partition")
|
|
|
|
}
|
2016-08-07 09:36:00 +02:00
|
|
|
if accountid == "" {
|
|
|
|
return "", fmt.Errorf("Unable to construct RDS ARN because of missing AWS Account ID")
|
2015-12-11 13:07:50 +01:00
|
|
|
}
|
2016-10-08 06:52:50 +02:00
|
|
|
arn := fmt.Sprintf("arn:%s:rds:%s:%s:pg:%s", partition, region, accountid, identifier)
|
2015-12-11 13:07:50 +01:00
|
|
|
return arn, nil
|
2016-08-07 09:36:00 +02:00
|
|
|
|
2015-12-11 13:07:50 +01:00
|
|
|
}
|