-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
provider/aws elasticache parameter group #2276
Merged
catsby
merged 8 commits into
hashicorp:master
from
SamClinckspoor:resource-aws-elasticache-parameter-group
Jun 26, 2015
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a6010e6
frist commit for aws_elasticahce_parameter_group
SamClinckspoor 14b7dd3
add resource file
SamClinckspoor 7f9c4e4
added test
SamClinckspoor dedbac5
added documentation
SamClinckspoor 7b559a9
added extra test for structure
SamClinckspoor c79d821
add docu link
SamClinckspoor c22f271
Cleaned indentation of example
SamClinckspoor c92b7a9
fixed possibly incorrectly returning destroyed
SamClinckspoor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
builtin/providers/aws/resource_aws_elasticache_parameter_group.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
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/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/elasticache" | ||
) | ||
|
||
func resourceAwsElasticacheParameterGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsElasticacheParameterGroupCreate, | ||
Read: resourceAwsElasticacheParameterGroupRead, | ||
Update: resourceAwsElasticacheParameterGroupUpdate, | ||
Delete: resourceAwsElasticacheParameterGroupDelete, | ||
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{ | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
ForceNew: false, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"value": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
Set: resourceAwsElasticacheParameterHash, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsElasticacheParameterGroupCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).elasticacheconn | ||
|
||
createOpts := elasticache.CreateCacheParameterGroupInput{ | ||
CacheParameterGroupName: aws.String(d.Get("name").(string)), | ||
CacheParameterGroupFamily: aws.String(d.Get("family").(string)), | ||
Description: aws.String(d.Get("description").(string)), | ||
} | ||
|
||
log.Printf("[DEBUG] Create Cache Parameter Group: %#v", createOpts) | ||
_, err := conn.CreateCacheParameterGroup(&createOpts) | ||
if err != nil { | ||
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.CacheParameterGroupName) | ||
log.Printf("[INFO] Cache Parameter Group ID: %s", d.Id()) | ||
|
||
return resourceAwsElasticacheParameterGroupUpdate(d, meta) | ||
} | ||
|
||
func resourceAwsElasticacheParameterGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).elasticacheconn | ||
|
||
describeOpts := elasticache.DescribeCacheParameterGroupsInput{ | ||
CacheParameterGroupName: aws.String(d.Id()), | ||
} | ||
|
||
describeResp, err := conn.DescribeCacheParameterGroups(&describeOpts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(describeResp.CacheParameterGroups) != 1 || | ||
*describeResp.CacheParameterGroups[0].CacheParameterGroupName != d.Id() { | ||
return fmt.Errorf("Unable to find Parameter Group: %#v", describeResp.CacheParameterGroups) | ||
} | ||
|
||
d.Set("name", describeResp.CacheParameterGroups[0].CacheParameterGroupName) | ||
d.Set("family", describeResp.CacheParameterGroups[0].CacheParameterGroupFamily) | ||
d.Set("description", describeResp.CacheParameterGroups[0].Description) | ||
|
||
// Only include user customized parameters as there's hundreds of system/default ones | ||
describeParametersOpts := elasticache.DescribeCacheParametersInput{ | ||
CacheParameterGroupName: aws.String(d.Id()), | ||
Source: aws.String("user"), | ||
} | ||
|
||
describeParametersResp, err := conn.DescribeCacheParameters(&describeParametersOpts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.Set("parameter", flattenElastiCacheParameters(describeParametersResp.Parameters)) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsElasticacheParameterGroupUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).elasticacheconn | ||
|
||
d.Partial(true) | ||
|
||
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 aws-sdk-go compat []elasticacheconn.Parameter | ||
parameters, err := expandElastiCacheParameters(ns.Difference(os).List()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(parameters) > 0 { | ||
modifyOpts := elasticache.ModifyCacheParameterGroupInput{ | ||
CacheParameterGroupName: aws.String(d.Get("name").(string)), | ||
ParameterNameValues: parameters, | ||
} | ||
|
||
log.Printf("[DEBUG] Modify Cache Parameter Group: %#v", modifyOpts) | ||
_, err = conn.ModifyCacheParameterGroup(&modifyOpts) | ||
if err != nil { | ||
return fmt.Errorf("Error modifying Cache Parameter Group: %s", err) | ||
} | ||
} | ||
d.SetPartial("parameter") | ||
} | ||
|
||
d.Partial(false) | ||
|
||
return resourceAwsElasticacheParameterGroupRead(d, meta) | ||
} | ||
|
||
func resourceAwsElasticacheParameterGroupDelete(d *schema.ResourceData, meta interface{}) error { | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{"pending"}, | ||
Target: "destroyed", | ||
Refresh: resourceAwsElasticacheParameterGroupDeleteRefreshFunc(d, meta), | ||
Timeout: 3 * time.Minute, | ||
MinTimeout: 1 * time.Second, | ||
} | ||
_, err := stateConf.WaitForState() | ||
return err | ||
} | ||
|
||
func resourceAwsElasticacheParameterGroupDeleteRefreshFunc( | ||
d *schema.ResourceData, | ||
meta interface{}) resource.StateRefreshFunc { | ||
conn := meta.(*AWSClient).elasticacheconn | ||
|
||
return func() (interface{}, string, error) { | ||
|
||
deleteOpts := elasticache.DeleteCacheParameterGroupInput{ | ||
CacheParameterGroupName: aws.String(d.Id()), | ||
} | ||
|
||
if _, err := conn.DeleteCacheParameterGroup(&deleteOpts); err != nil { | ||
elasticahceerr, ok := err.(awserr.Error) | ||
if !ok { | ||
return d, "error", err | ||
} | ||
|
||
if elasticahceerr.Code() != "CacheParameterGroupNotFoundFault" { | ||
return d, "error", err | ||
} | ||
} | ||
|
||
return d, "destroyed", nil | ||
} | ||
} | ||
|
||
func resourceAwsElasticacheParameterHash(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()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if
ok
here, but notCode() != "CacheParameterGroupNotFoundFault"
, we fall through toreturn d, "destroyed", nil
, yeah?We should probably re-write this that
if ok
do theCode()
check, otherwise returnreturn d, "error", err