This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
forked from hashicorp/terraform-provider-aws
/
resource_aws_db_parameter_group.go
340 lines (299 loc) · 9.99 KB
/
resource_aws_db_parameter_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package aws
import (
"bytes"
"fmt"
"log"
"strings"
"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/rds"
)
func resourceAwsDbParameterGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDbParameterGroupCreate,
Read: resourceAwsDbParameterGroupRead,
Update: resourceAwsDbParameterGroupUpdate,
Delete: resourceAwsDbParameterGroupDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: validateDbParamGroupName,
},
"name_prefix": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name"},
ValidateFunc: validateDbParamGroupNamePrefix,
},
"family": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "Managed by Terraform",
},
"parameter": {
Type: schema.TypeSet,
Optional: true,
ForceNew: false,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
"apply_method": {
Type: schema.TypeString,
Optional: true,
Default: "immediate",
},
},
},
Set: resourceAwsDbParameterHash,
},
"tags": tagsSchema(),
},
}
}
func resourceAwsDbParameterGroupCreate(d *schema.ResourceData, meta interface{}) error {
rdsconn := meta.(*AWSClient).rdsconn
tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{}))
var groupName string
if v, ok := d.GetOk("name"); ok {
groupName = v.(string)
} else if v, ok := d.GetOk("name_prefix"); ok {
groupName = resource.PrefixedUniqueId(v.(string))
} else {
groupName = resource.UniqueId()
}
d.Set("name", groupName)
createOpts := rds.CreateDBParameterGroupInput{
DBParameterGroupName: aws.String(groupName),
DBParameterGroupFamily: aws.String(d.Get("family").(string)),
Description: aws.String(d.Get("description").(string)),
Tags: tags,
}
log.Printf("[DEBUG] Create DB Parameter Group: %#v", createOpts)
resp, err := rdsconn.CreateDBParameterGroup(&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(aws.StringValue(resp.DBParameterGroup.DBParameterGroupName))
d.Set("arn", resp.DBParameterGroup.DBParameterGroupArn)
log.Printf("[INFO] DB Parameter Group ID: %s", d.Id())
return resourceAwsDbParameterGroupUpdate(d, meta)
}
func resourceAwsDbParameterGroupRead(d *schema.ResourceData, meta interface{}) error {
rdsconn := meta.(*AWSClient).rdsconn
describeOpts := rds.DescribeDBParameterGroupsInput{
DBParameterGroupName: aws.String(d.Id()),
}
describeResp, err := rdsconn.DescribeDBParameterGroups(&describeOpts)
if err != nil {
if isAWSErr(err, rds.ErrCodeDBParameterGroupNotFoundFault, "") {
log.Printf("[WARN] DB Parameter Group (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
if len(describeResp.DBParameterGroups) != 1 ||
*describeResp.DBParameterGroups[0].DBParameterGroupName != d.Id() {
return fmt.Errorf("Unable to find Parameter Group: %#v", describeResp.DBParameterGroups)
}
d.Set("name", describeResp.DBParameterGroups[0].DBParameterGroupName)
d.Set("family", describeResp.DBParameterGroups[0].DBParameterGroupFamily)
d.Set("description", describeResp.DBParameterGroups[0].Description)
configParams := d.Get("parameter").(*schema.Set)
describeParametersOpts := rds.DescribeDBParametersInput{
DBParameterGroupName: aws.String(d.Id()),
}
if configParams.Len() < 1 {
// if we don't have any params in the ResourceData already, two possibilities
// first, we don't have a config available to us. Second, we do, but it has
// no parameters. We're going to assume the first, to be safe. In this case,
// we're only going to ask for the user-modified values, because any defaults
// the user may have _also_ set are indistinguishable from the hundreds of
// defaults AWS sets. If the user hasn't set any parameters, this will return
// an empty list anyways, so we just make some unnecessary requests. But in
// the more common case (I assume) of an import, this will make fewer requests
// and "do the right thing".
describeParametersOpts.Source = aws.String("user")
}
var parameters []*rds.Parameter
err = rdsconn.DescribeDBParametersPages(&describeParametersOpts,
func(describeParametersResp *rds.DescribeDBParametersOutput, lastPage bool) bool {
parameters = append(parameters, describeParametersResp.Parameters...)
return !lastPage
})
if err != nil {
return err
}
var userParams []*rds.Parameter
if configParams.Len() < 1 {
// if we have no config/no parameters in config, we've already asked for only
// user-modified values, so we can just use the entire response.
userParams = parameters
} else {
// if we have a config available to us, we have two possible classes of value
// in the config. On the one hand, the user could have specified a parameter
// that _actually_ changed things, in which case its Source would be set to
// user. On the other, they may have specified a parameter that coincides with
// the default value. In that case, the Source will be set to "system" or
// "engine-default". We need to set the union of all "user" Source parameters
// _and_ the "system"/"engine-default" Source parameters _that appear in the
// config_ in the state, or the user gets a perpetual diff. See
// terraform-providers/terraform-provider-aws#593 for more context and details.
confParams, err := expandParameters(configParams.List())
if err != nil {
return err
}
for _, param := range parameters {
if param.Source == nil || param.ParameterName == nil {
continue
}
if *param.Source == "user" {
userParams = append(userParams, param)
continue
}
var paramFound bool
for _, cp := range confParams {
if cp.ParameterName == nil {
continue
}
if *cp.ParameterName == *param.ParameterName {
userParams = append(userParams, param)
break
}
}
if !paramFound {
log.Printf("[DEBUG] Not persisting %s to state, as its source is %q and it isn't in the config", *param.ParameterName, *param.Source)
}
}
}
err = d.Set("parameter", flattenParameters(userParams))
if err != nil {
return fmt.Errorf("error setting 'parameter' in state: %#v", err)
}
arn := aws.StringValue(describeResp.DBParameterGroups[0].DBParameterGroupArn)
d.Set("arn", arn)
resp, err := rdsconn.ListTagsForResource(&rds.ListTagsForResourceInput{
ResourceName: aws.String(arn),
})
if err != nil {
log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn)
}
var dt []*rds.Tag
if len(resp.TagList) > 0 {
dt = resp.TagList
}
d.Set("tags", tagsToMapRDS(dt))
return nil
}
func resourceAwsDbParameterGroupUpdate(d *schema.ResourceData, meta interface{}) error {
rdsconn := meta.(*AWSClient).rdsconn
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 []rds.Parameter
parameters, err := expandParameters(ns.Difference(os).List())
if err != nil {
return err
}
if len(parameters) > 0 {
// We can only modify 20 parameters at a time, so walk them until
// we've got them all.
maxParams := 20
for parameters != nil {
var paramsToModify []*rds.Parameter
if len(parameters) <= maxParams {
paramsToModify, parameters = parameters[:], nil
} else {
paramsToModify, parameters = parameters[:maxParams], parameters[maxParams:]
}
modifyOpts := rds.ModifyDBParameterGroupInput{
DBParameterGroupName: aws.String(d.Get("name").(string)),
Parameters: paramsToModify,
}
log.Printf("[DEBUG] Modify DB Parameter Group: %s", modifyOpts)
_, err = rdsconn.ModifyDBParameterGroup(&modifyOpts)
if err != nil {
return fmt.Errorf("Error modifying DB Parameter Group: %s", err)
}
}
d.SetPartial("parameter")
}
}
if err := setTagsRDS(rdsconn, d, d.Get("arn").(string)); err != nil {
return err
} else {
d.SetPartial("tags")
}
d.Partial(false)
return resourceAwsDbParameterGroupRead(d, meta)
}
func resourceAwsDbParameterGroupDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).rdsconn
return resource.Retry(3*time.Minute, func() *resource.RetryError {
deleteOpts := rds.DeleteDBParameterGroupInput{
DBParameterGroupName: aws.String(d.Id()),
}
_, err := conn.DeleteDBParameterGroup(&deleteOpts)
if err != nil {
awsErr, ok := err.(awserr.Error)
if ok && awsErr.Code() == "DBParameterGroupNotFoundFault" {
return resource.RetryableError(err)
}
if ok && awsErr.Code() == "InvalidDBParameterGroupState" {
return resource.RetryableError(err)
}
}
return resource.NonRetryableError(err)
})
}
func resourceAwsDbParameterHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
// Store the value as a lower case string, to match how we store them in flattenParameters
buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(m["value"].(string))))
return hashcode.String(buf.String())
}