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