github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_db_parameter_group.go (about)

     1  package aws
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"log"
     7  	"time"
     8  
     9  	"github.com/hashicorp/terraform/helper/hashcode"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  	"github.com/mitchellh/goamz/rds"
    13  )
    14  
    15  func resourceAwsDbParameterGroup() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsDbParameterGroupCreate,
    18  		Read:   resourceAwsDbParameterGroupRead,
    19  		Update: resourceAwsDbParameterGroupUpdate,
    20  		Delete: resourceAwsDbParameterGroupDelete,
    21  		Schema: map[string]*schema.Schema{
    22  			"name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				ForceNew: true,
    25  				Required: true,
    26  			},
    27  			"family": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  				ForceNew: true,
    31  			},
    32  			"description": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  			},
    37  			"parameter": &schema.Schema{
    38  				Type:     schema.TypeSet,
    39  				Optional: true,
    40  				ForceNew: false,
    41  				Elem: &schema.Resource{
    42  					Schema: map[string]*schema.Schema{
    43  						"name": &schema.Schema{
    44  							Type:     schema.TypeString,
    45  							Required: true,
    46  						},
    47  						"value": &schema.Schema{
    48  							Type:     schema.TypeString,
    49  							Required: true,
    50  						},
    51  					},
    52  				},
    53  				Set: resourceAwsDbParameterHash,
    54  			},
    55  		},
    56  	}
    57  }
    58  
    59  func resourceAwsDbParameterGroupCreate(d *schema.ResourceData, meta interface{}) error {
    60  	rdsconn := meta.(*AWSClient).rdsconn
    61  
    62  	createOpts := rds.CreateDBParameterGroup{
    63  		DBParameterGroupName:   d.Get("name").(string),
    64  		DBParameterGroupFamily: d.Get("family").(string),
    65  		Description:            d.Get("description").(string),
    66  	}
    67  
    68  	log.Printf("[DEBUG] Create DB Parameter Group: %#v", createOpts)
    69  	_, err := rdsconn.CreateDBParameterGroup(&createOpts)
    70  	if err != nil {
    71  		return fmt.Errorf("Error creating DB Parameter Group: %s", err)
    72  	}
    73  
    74  	d.Partial(true)
    75  	d.SetPartial("name")
    76  	d.SetPartial("family")
    77  	d.SetPartial("description")
    78  	d.Partial(false)
    79  
    80  	d.SetId(createOpts.DBParameterGroupName)
    81  	log.Printf("[INFO] DB Parameter Group ID: %s", d.Id())
    82  
    83  	return resourceAwsDbParameterGroupUpdate(d, meta)
    84  }
    85  
    86  func resourceAwsDbParameterGroupRead(d *schema.ResourceData, meta interface{}) error {
    87  	rdsconn := meta.(*AWSClient).rdsconn
    88  
    89  	describeOpts := rds.DescribeDBParameterGroups{
    90  		DBParameterGroupName: d.Id(),
    91  	}
    92  
    93  	describeResp, err := rdsconn.DescribeDBParameterGroups(&describeOpts)
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	if len(describeResp.DBParameterGroups) != 1 ||
    99  		describeResp.DBParameterGroups[0].DBParameterGroupName != d.Id() {
   100  		return fmt.Errorf("Unable to find Parameter Group: %#v", describeResp.DBParameterGroups)
   101  	}
   102  
   103  	d.Set("name", describeResp.DBParameterGroups[0].DBParameterGroupName)
   104  	d.Set("family", describeResp.DBParameterGroups[0].DBParameterGroupFamily)
   105  	d.Set("description", describeResp.DBParameterGroups[0].Description)
   106  
   107  	// Only include user customized parameters as there's hundreds of system/default ones
   108  	describeParametersOpts := rds.DescribeDBParameters{
   109  		DBParameterGroupName: d.Id(),
   110  		Source:               "user",
   111  	}
   112  
   113  	describeParametersResp, err := rdsconn.DescribeDBParameters(&describeParametersOpts)
   114  	if err != nil {
   115  		return err
   116  	}
   117  
   118  	d.Set("parameter", flattenParameters(describeParametersResp.Parameters))
   119  
   120  	return nil
   121  }
   122  
   123  func resourceAwsDbParameterGroupUpdate(d *schema.ResourceData, meta interface{}) error {
   124  	rdsconn := meta.(*AWSClient).rdsconn
   125  
   126  	d.Partial(true)
   127  
   128  	if d.HasChange("parameter") {
   129  		o, n := d.GetChange("parameter")
   130  		if o == nil {
   131  			o = new(schema.Set)
   132  		}
   133  		if n == nil {
   134  			n = new(schema.Set)
   135  		}
   136  
   137  		os := o.(*schema.Set)
   138  		ns := n.(*schema.Set)
   139  
   140  		// Expand the "parameter" set to goamz compat []rds.Parameter
   141  		parameters, err := expandParameters(ns.Difference(os).List())
   142  		if err != nil {
   143  			return err
   144  		}
   145  
   146  		if len(parameters) > 0 {
   147  			modifyOpts := rds.ModifyDBParameterGroup{
   148  				DBParameterGroupName: d.Get("name").(string),
   149  				Parameters:           parameters,
   150  			}
   151  
   152  			log.Printf("[DEBUG] Modify DB Parameter Group: %#v", modifyOpts)
   153  			_, err = rdsconn.ModifyDBParameterGroup(&modifyOpts)
   154  			if err != nil {
   155  				return fmt.Errorf("Error modifying DB Parameter Group: %s", err)
   156  			}
   157  		}
   158  		d.SetPartial("parameter")
   159  	}
   160  
   161  	d.Partial(false)
   162  
   163  	return resourceAwsDbParameterGroupRead(d, meta)
   164  }
   165  
   166  func resourceAwsDbParameterGroupDelete(d *schema.ResourceData, meta interface{}) error {
   167  	stateConf := &resource.StateChangeConf{
   168  		Pending:    []string{"pending"},
   169  		Target:     "destroyed",
   170  		Refresh:    resourceAwsDbParameterGroupDeleteRefreshFunc(d, meta),
   171  		Timeout:    3 * time.Minute,
   172  		MinTimeout: 1 * time.Second,
   173  	}
   174  	_, err := stateConf.WaitForState()
   175  	return err
   176  }
   177  
   178  func resourceAwsDbParameterGroupDeleteRefreshFunc(
   179  	d *schema.ResourceData,
   180  	meta interface{}) resource.StateRefreshFunc {
   181  	rdsconn := meta.(*AWSClient).rdsconn
   182  
   183  	return func() (interface{}, string, error) {
   184  
   185  		deleteOpts := rds.DeleteDBParameterGroup{
   186  			DBParameterGroupName: d.Id(),
   187  		}
   188  
   189  		if _, err := rdsconn.DeleteDBParameterGroup(&deleteOpts); err != nil {
   190  			rdserr, ok := err.(*rds.Error)
   191  			if !ok {
   192  				return d, "error", err
   193  			}
   194  
   195  			if rdserr.Code != "DBParameterGroupNotFoundFault" {
   196  				return d, "error", err
   197  			}
   198  		}
   199  
   200  		return d, "destroyed", nil
   201  	}
   202  }
   203  
   204  func resourceAwsDbParameterHash(v interface{}) int {
   205  	var buf bytes.Buffer
   206  	m := v.(map[string]interface{})
   207  	buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
   208  	buf.WriteString(fmt.Sprintf("%s-", m["value"].(string)))
   209  
   210  	return hashcode.String(buf.String())
   211  }