forked from hashicorp/terraform-provider-aws
/
resource_aws_redshift_snapshot_copy_grant.go
251 lines (197 loc) · 6.91 KB
/
resource_aws_redshift_snapshot_copy_grant.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
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/redshift"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsRedshiftSnapshotCopyGrant() *schema.Resource {
return &schema.Resource{
// There is no API for updating/modifying grants, hence no Update
// Instead changes to most fields will force a new resource
Create: resourceAwsRedshiftSnapshotCopyGrantCreate,
Read: resourceAwsRedshiftSnapshotCopyGrantRead,
Delete: resourceAwsRedshiftSnapshotCopyGrantDelete,
Exists: resourceAwsRedshiftSnapshotCopyGrantExists,
Schema: map[string]*schema.Schema{
"snapshot_copy_grant_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"kms_key_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"tags": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceAwsRedshiftSnapshotCopyGrantCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).redshiftconn
grantName := d.Get("snapshot_copy_grant_name").(string)
input := redshift.CreateSnapshotCopyGrantInput{
SnapshotCopyGrantName: aws.String(grantName),
}
if v, ok := d.GetOk("kms_key_id"); ok {
input.KmsKeyId = aws.String(v.(string))
}
input.Tags = tagsFromMapRedshift(d.Get("tags").(map[string]interface{}))
log.Printf("[DEBUG]: Adding new Redshift SnapshotCopyGrant: %s", input)
var out *redshift.CreateSnapshotCopyGrantOutput
var err error
out, err = conn.CreateSnapshotCopyGrant(&input)
if err != nil {
return fmt.Errorf("error creating Redshift Snapshot Copy Grant (%s): %s", grantName, err)
}
log.Printf("[DEBUG] Created new Redshift SnapshotCopyGrant: %s", *out.SnapshotCopyGrant.SnapshotCopyGrantName)
d.SetId(grantName)
return resourceAwsRedshiftSnapshotCopyGrantRead(d, meta)
}
func resourceAwsRedshiftSnapshotCopyGrantRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).redshiftconn
grantName := d.Id()
log.Printf("[DEBUG] Looking for grant: %s", grantName)
grant, err := findAwsRedshiftSnapshotCopyGrantWithRetry(conn, grantName)
if err != nil {
return err
}
if grant == nil {
log.Printf("[WARN] %s Redshift snapshot copy grant not found, removing from state file", grantName)
d.SetId("")
return nil
}
d.Set("kms_key_id", grant.KmsKeyId)
d.Set("snapshot_copy_grant_name", grant.SnapshotCopyGrantName)
if err := d.Set("tags", tagsToMapRedshift(grant.Tags)); err != nil {
return fmt.Errorf("Error setting Redshift Snapshot Copy Grant Tags: %#v", err)
}
return nil
}
func resourceAwsRedshiftSnapshotCopyGrantDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).redshiftconn
grantName := d.Id()
deleteInput := redshift.DeleteSnapshotCopyGrantInput{
SnapshotCopyGrantName: aws.String(grantName),
}
log.Printf("[DEBUG] Deleting snapshot copy grant: %s", grantName)
_, err := conn.DeleteSnapshotCopyGrant(&deleteInput)
if err != nil {
if isAWSErr(err, redshift.ErrCodeSnapshotCopyGrantNotFoundFault, "") {
return nil
}
return err
}
log.Printf("[DEBUG] Checking if grant is deleted: %s", grantName)
err = waitForAwsRedshiftSnapshotCopyGrantToBeDeleted(conn, grantName)
return err
}
func resourceAwsRedshiftSnapshotCopyGrantExists(d *schema.ResourceData, meta interface{}) (bool, error) {
conn := meta.(*AWSClient).redshiftconn
grantName := d.Id()
log.Printf("[DEBUG] Looking for Grant: %s", grantName)
grant, err := findAwsRedshiftSnapshotCopyGrantWithRetry(conn, grantName)
if err != nil {
return false, err
}
if grant != nil {
return true, err
}
return false, nil
}
func getAwsRedshiftSnapshotCopyGrant(grants []*redshift.SnapshotCopyGrant, grantName string) *redshift.SnapshotCopyGrant {
for _, grant := range grants {
if *grant.SnapshotCopyGrantName == grantName {
return grant
}
}
return nil
}
/*
In the functions below it is not possible to use retryOnAwsCodes function, as there
is no get grant call, so an error has to be created if the grant is or isn't returned
by the describe grants call when expected.
*/
// NB: This function only retries the grant not being returned and some edge cases, while AWS Errors
// are handled by the findAwsRedshiftSnapshotCopyGrant function
func findAwsRedshiftSnapshotCopyGrantWithRetry(conn *redshift.Redshift, grantName string) (*redshift.SnapshotCopyGrant, error) {
var grant *redshift.SnapshotCopyGrant
err := resource.Retry(3*time.Minute, func() *resource.RetryError {
var err error
grant, err = findAwsRedshiftSnapshotCopyGrant(conn, grantName, nil)
if err != nil {
if serr, ok := err.(*resource.NotFoundError); ok {
// Force a retry if the grant should exist
return resource.RetryableError(serr)
}
return resource.NonRetryableError(err)
}
return nil
})
return grant, err
}
// Used by the tests as well
func waitForAwsRedshiftSnapshotCopyGrantToBeDeleted(conn *redshift.Redshift, grantName string) error {
err := resource.Retry(3*time.Minute, func() *resource.RetryError {
grant, err := findAwsRedshiftSnapshotCopyGrant(conn, grantName, nil)
if err != nil {
if isAWSErr(err, redshift.ErrCodeSnapshotCopyGrantNotFoundFault, "") {
return nil
}
}
if grant != nil {
// Force a retry if the grant still exists
return resource.RetryableError(
fmt.Errorf("[DEBUG] Grant still exists while expected to be deleted: %s", *grant.SnapshotCopyGrantName))
}
return resource.NonRetryableError(err)
})
return err
}
// The DescribeSnapshotCopyGrants API defaults to listing only 100 grants
// Use a marker to iterate over all grants in "pages"
// NB: This function only retries on AWS Errors
func findAwsRedshiftSnapshotCopyGrant(conn *redshift.Redshift, grantName string, marker *string) (*redshift.SnapshotCopyGrant, error) {
input := redshift.DescribeSnapshotCopyGrantsInput{
MaxRecords: aws.Int64(int64(100)),
}
// marker and grant name are mutually exclusive
if marker != nil {
input.Marker = marker
} else {
input.SnapshotCopyGrantName = aws.String(grantName)
}
var out *redshift.DescribeSnapshotCopyGrantsOutput
var err error
var grant *redshift.SnapshotCopyGrant
err = resource.Retry(3*time.Minute, func() *resource.RetryError {
out, err = conn.DescribeSnapshotCopyGrants(&input)
if err != nil {
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return nil, err
}
grant = getAwsRedshiftSnapshotCopyGrant(out.SnapshotCopyGrants, grantName)
if grant != nil {
return grant, nil
} else if out.Marker != nil {
log.Printf("[DEBUG] Snapshot copy grant not found but marker returned, getting next page via marker: %s", aws.StringValue(out.Marker))
return findAwsRedshiftSnapshotCopyGrant(conn, grantName, out.Marker)
}
return nil, &resource.NotFoundError{
Message: fmt.Sprintf("[DEBUG] Grant %s not found", grantName),
LastRequest: input,
}
}