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