github.com/ottenhoff/terraform@v0.7.0-rc1.0.20160607213102-ac2d195cc560/builtin/providers/aws/resource_aws_elasticache_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 13 "github.com/aws/aws-sdk-go/aws" 14 "github.com/aws/aws-sdk-go/aws/awserr" 15 "github.com/aws/aws-sdk-go/service/elasticache" 16 ) 17 18 func resourceAwsElasticacheParameterGroup() *schema.Resource { 19 return &schema.Resource{ 20 Create: resourceAwsElasticacheParameterGroupCreate, 21 Read: resourceAwsElasticacheParameterGroupRead, 22 Update: resourceAwsElasticacheParameterGroupUpdate, 23 Delete: resourceAwsElasticacheParameterGroupDelete, 24 Schema: map[string]*schema.Schema{ 25 "name": &schema.Schema{ 26 Type: schema.TypeString, 27 ForceNew: true, 28 Required: true, 29 }, 30 "family": &schema.Schema{ 31 Type: schema.TypeString, 32 Required: true, 33 ForceNew: true, 34 }, 35 "description": &schema.Schema{ 36 Type: schema.TypeString, 37 Optional: true, 38 ForceNew: true, 39 Default: "Managed by Terraform", 40 }, 41 "parameter": &schema.Schema{ 42 Type: schema.TypeSet, 43 Optional: true, 44 ForceNew: false, 45 Elem: &schema.Resource{ 46 Schema: map[string]*schema.Schema{ 47 "name": &schema.Schema{ 48 Type: schema.TypeString, 49 Required: true, 50 }, 51 "value": &schema.Schema{ 52 Type: schema.TypeString, 53 Required: true, 54 }, 55 }, 56 }, 57 Set: resourceAwsElasticacheParameterHash, 58 }, 59 }, 60 } 61 } 62 63 func resourceAwsElasticacheParameterGroupCreate(d *schema.ResourceData, meta interface{}) error { 64 conn := meta.(*AWSClient).elasticacheconn 65 66 createOpts := elasticache.CreateCacheParameterGroupInput{ 67 CacheParameterGroupName: aws.String(d.Get("name").(string)), 68 CacheParameterGroupFamily: aws.String(d.Get("family").(string)), 69 Description: aws.String(d.Get("description").(string)), 70 } 71 72 log.Printf("[DEBUG] Create Cache Parameter Group: %#v", createOpts) 73 _, err := conn.CreateCacheParameterGroup(&createOpts) 74 if err != nil { 75 return fmt.Errorf("Error creating Cache Parameter Group: %s", err) 76 } 77 78 d.Partial(true) 79 d.SetPartial("name") 80 d.SetPartial("family") 81 d.SetPartial("description") 82 d.Partial(false) 83 84 d.SetId(*createOpts.CacheParameterGroupName) 85 log.Printf("[INFO] Cache Parameter Group ID: %s", d.Id()) 86 87 return resourceAwsElasticacheParameterGroupUpdate(d, meta) 88 } 89 90 func resourceAwsElasticacheParameterGroupRead(d *schema.ResourceData, meta interface{}) error { 91 conn := meta.(*AWSClient).elasticacheconn 92 93 describeOpts := elasticache.DescribeCacheParameterGroupsInput{ 94 CacheParameterGroupName: aws.String(d.Id()), 95 } 96 97 describeResp, err := conn.DescribeCacheParameterGroups(&describeOpts) 98 if err != nil { 99 return err 100 } 101 102 if len(describeResp.CacheParameterGroups) != 1 || 103 *describeResp.CacheParameterGroups[0].CacheParameterGroupName != d.Id() { 104 return fmt.Errorf("Unable to find Parameter Group: %#v", describeResp.CacheParameterGroups) 105 } 106 107 d.Set("name", describeResp.CacheParameterGroups[0].CacheParameterGroupName) 108 d.Set("family", describeResp.CacheParameterGroups[0].CacheParameterGroupFamily) 109 d.Set("description", describeResp.CacheParameterGroups[0].Description) 110 111 // Only include user customized parameters as there's hundreds of system/default ones 112 describeParametersOpts := elasticache.DescribeCacheParametersInput{ 113 CacheParameterGroupName: aws.String(d.Id()), 114 Source: aws.String("user"), 115 } 116 117 describeParametersResp, err := conn.DescribeCacheParameters(&describeParametersOpts) 118 if err != nil { 119 return err 120 } 121 122 d.Set("parameter", flattenElastiCacheParameters(describeParametersResp.Parameters)) 123 124 return nil 125 } 126 127 func resourceAwsElasticacheParameterGroupUpdate(d *schema.ResourceData, meta interface{}) error { 128 conn := meta.(*AWSClient).elasticacheconn 129 130 d.Partial(true) 131 132 if d.HasChange("parameter") { 133 o, n := d.GetChange("parameter") 134 if o == nil { 135 o = new(schema.Set) 136 } 137 if n == nil { 138 n = new(schema.Set) 139 } 140 141 os := o.(*schema.Set) 142 ns := n.(*schema.Set) 143 144 // Expand the "parameter" set to aws-sdk-go compat []elasticacheconn.Parameter 145 parameters, err := expandElastiCacheParameters(ns.Difference(os).List()) 146 if err != nil { 147 return err 148 } 149 150 if len(parameters) > 0 { 151 modifyOpts := elasticache.ModifyCacheParameterGroupInput{ 152 CacheParameterGroupName: aws.String(d.Get("name").(string)), 153 ParameterNameValues: parameters, 154 } 155 156 log.Printf("[DEBUG] Modify Cache Parameter Group: %#v", modifyOpts) 157 _, err = conn.ModifyCacheParameterGroup(&modifyOpts) 158 if err != nil { 159 return fmt.Errorf("Error modifying Cache Parameter Group: %s", err) 160 } 161 } 162 d.SetPartial("parameter") 163 } 164 165 d.Partial(false) 166 167 return resourceAwsElasticacheParameterGroupRead(d, meta) 168 } 169 170 func resourceAwsElasticacheParameterGroupDelete(d *schema.ResourceData, meta interface{}) error { 171 stateConf := &resource.StateChangeConf{ 172 Pending: []string{"pending"}, 173 Target: []string{"destroyed"}, 174 Refresh: resourceAwsElasticacheParameterGroupDeleteRefreshFunc(d, meta), 175 Timeout: 3 * time.Minute, 176 MinTimeout: 1 * time.Second, 177 } 178 _, err := stateConf.WaitForState() 179 return err 180 } 181 182 func resourceAwsElasticacheParameterGroupDeleteRefreshFunc( 183 d *schema.ResourceData, 184 meta interface{}) resource.StateRefreshFunc { 185 conn := meta.(*AWSClient).elasticacheconn 186 187 return func() (interface{}, string, error) { 188 189 deleteOpts := elasticache.DeleteCacheParameterGroupInput{ 190 CacheParameterGroupName: aws.String(d.Id()), 191 } 192 193 if _, err := conn.DeleteCacheParameterGroup(&deleteOpts); err != nil { 194 elasticahceerr, ok := err.(awserr.Error) 195 if ok && elasticahceerr.Code() == "CacheParameterGroupNotFoundFault" { 196 d.SetId("") 197 return d, "error", err 198 } 199 return d, "error", err 200 } 201 return d, "destroyed", nil 202 } 203 } 204 205 func resourceAwsElasticacheParameterHash(v interface{}) int { 206 var buf bytes.Buffer 207 m := v.(map[string]interface{}) 208 buf.WriteString(fmt.Sprintf("%s-", m["name"].(string))) 209 buf.WriteString(fmt.Sprintf("%s-", m["value"].(string))) 210 211 return hashcode.String(buf.String()) 212 }