forked from hashicorp/terraform-provider-aws
/
resource_aws_redshift_event_subscription.go
233 lines (202 loc) · 6.74 KB
/
resource_aws_redshift_event_subscription.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
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/redshift"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsRedshiftEventSubscription() *schema.Resource {
return &schema.Resource{
Create: resourceAwsRedshiftEventSubscriptionCreate,
Read: resourceAwsRedshiftEventSubscriptionRead,
Update: resourceAwsRedshiftEventSubscriptionUpdate,
Delete: resourceAwsRedshiftEventSubscriptionDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(40 * time.Minute),
Delete: schema.DefaultTimeout(40 * time.Minute),
Update: schema.DefaultTimeout(40 * time.Minute),
},
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"sns_topic_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"event_categories": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"source_ids": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"source_type": {
Type: schema.TypeString,
Optional: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"severity": {
Type: schema.TypeString,
Optional: true,
},
"customer_aws_id": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
func resourceAwsRedshiftEventSubscriptionCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).redshiftconn
request := &redshift.CreateEventSubscriptionInput{
SubscriptionName: aws.String(d.Get("name").(string)),
SnsTopicArn: aws.String(d.Get("sns_topic_arn").(string)),
Enabled: aws.Bool(d.Get("enabled").(bool)),
SourceIds: expandStringSet(d.Get("source_ids").(*schema.Set)),
SourceType: aws.String(d.Get("source_type").(string)),
Severity: aws.String(d.Get("severity").(string)),
EventCategories: expandStringSet(d.Get("event_categories").(*schema.Set)),
Tags: tagsFromMapRedshift(d.Get("tags").(map[string]interface{})),
}
log.Println("[DEBUG] Create Redshift Event Subscription:", request)
output, err := conn.CreateEventSubscription(request)
if err != nil || output.EventSubscription == nil {
return fmt.Errorf("Error creating Redshift Event Subscription %s: %s", d.Get("name").(string), err)
}
d.SetId(aws.StringValue(output.EventSubscription.CustSubscriptionId))
return resourceAwsRedshiftEventSubscriptionRead(d, meta)
}
func resourceAwsRedshiftEventSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).redshiftconn
arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "redshift",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("eventsubscription:%s", d.Id()),
}.String()
d.Set("arn", arn)
sub, err := resourceAwsRedshiftEventSubscriptionRetrieve(d.Id(), conn)
if err != nil {
return fmt.Errorf("Error retrieving Redshift Event Subscription %s: %s", d.Id(), err)
}
if sub == nil {
log.Printf("[WARN] Redshift Event Subscription (%s) not found - removing from state", d.Id())
d.SetId("")
return nil
}
if err := d.Set("name", sub.CustSubscriptionId); err != nil {
return err
}
if err := d.Set("sns_topic_arn", sub.SnsTopicArn); err != nil {
return err
}
if err := d.Set("status", sub.Status); err != nil {
return err
}
if err := d.Set("source_type", sub.SourceType); err != nil {
return err
}
if err := d.Set("severity", sub.Severity); err != nil {
return err
}
if err := d.Set("enabled", sub.Enabled); err != nil {
return err
}
if err := d.Set("source_ids", flattenStringList(sub.SourceIdsList)); err != nil {
return err
}
if err := d.Set("event_categories", flattenStringList(sub.EventCategoriesList)); err != nil {
return err
}
if err := d.Set("customer_aws_id", sub.CustomerAwsId); err != nil {
return err
}
if err := d.Set("tags", tagsToMapRedshift(sub.Tags)); err != nil {
return err
}
return nil
}
func resourceAwsRedshiftEventSubscriptionRetrieve(name string, conn *redshift.Redshift) (*redshift.EventSubscription, error) {
request := &redshift.DescribeEventSubscriptionsInput{
SubscriptionName: aws.String(name),
}
describeResp, err := conn.DescribeEventSubscriptions(request)
if err != nil {
if isAWSErr(err, redshift.ErrCodeSubscriptionNotFoundFault, "") {
log.Printf("[WARN] No Redshift Event Subscription by name (%s) found", name)
return nil, nil
}
return nil, fmt.Errorf("Error reading Redshift Event Subscription %s: %s", name, err)
}
if len(describeResp.EventSubscriptionsList) != 1 {
return nil, fmt.Errorf("Unable to find Redshift Event Subscription: %#v", describeResp.EventSubscriptionsList)
}
return describeResp.EventSubscriptionsList[0], nil
}
func resourceAwsRedshiftEventSubscriptionUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).redshiftconn
d.Partial(true)
req := &redshift.ModifyEventSubscriptionInput{
SubscriptionName: aws.String(d.Id()),
SnsTopicArn: aws.String(d.Get("sns_topic_arn").(string)),
Enabled: aws.Bool(d.Get("enabled").(bool)),
SourceIds: expandStringSet(d.Get("source_ids").(*schema.Set)),
SourceType: aws.String(d.Get("source_type").(string)),
Severity: aws.String(d.Get("severity").(string)),
EventCategories: expandStringSet(d.Get("event_categories").(*schema.Set)),
}
log.Printf("[DEBUG] Redshift Event Subscription modification request: %#v", req)
_, err := conn.ModifyEventSubscription(req)
if err != nil {
return fmt.Errorf("Modifying Redshift Event Subscription %s failed: %s", d.Id(), err)
}
if tagErr := setTagsRedshift(conn, d); tagErr != nil {
return tagErr
} else {
d.SetPartial("tags")
}
d.Partial(false)
return nil
}
func resourceAwsRedshiftEventSubscriptionDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).redshiftconn
deleteOpts := redshift.DeleteEventSubscriptionInput{
SubscriptionName: aws.String(d.Id()),
}
if _, err := conn.DeleteEventSubscription(&deleteOpts); err != nil {
if isAWSErr(err, redshift.ErrCodeSubscriptionNotFoundFault, "") {
return nil
}
return fmt.Errorf("Error deleting Redshift Event Subscription %s: %s", d.Id(), err)
}
return nil
}