forked from hashicorp/terraform-provider-aws
/
resource_aws_ses_identity_notification_topic.go
145 lines (115 loc) · 3.92 KB
/
resource_aws_ses_identity_notification_topic.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
package aws
import (
"fmt"
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsSesNotificationTopic() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSesNotificationTopicSet,
Read: resourceAwsSesNotificationTopicRead,
Update: resourceAwsSesNotificationTopicSet,
Delete: resourceAwsSesNotificationTopicDelete,
Schema: map[string]*schema.Schema{
"topic_arn": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"notification_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
ses.NotificationTypeBounce,
ses.NotificationTypeComplaint,
ses.NotificationTypeDelivery,
}, false),
},
"identity": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
},
}
}
func resourceAwsSesNotificationTopicSet(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sesConn
notification := d.Get("notification_type").(string)
identity := d.Get("identity").(string)
setOpts := &ses.SetIdentityNotificationTopicInput{
Identity: aws.String(identity),
NotificationType: aws.String(notification),
}
if v, ok := d.GetOk("topic_arn"); ok && v.(string) != "" {
setOpts.SnsTopic = aws.String(v.(string))
}
d.SetId(fmt.Sprintf("%s|%s", identity, notification))
log.Printf("[DEBUG] Setting SES Identity Notification Topic: %#v", setOpts)
if _, err := conn.SetIdentityNotificationTopic(setOpts); err != nil {
return fmt.Errorf("Error setting SES Identity Notification Topic: %s", err)
}
return resourceAwsSesNotificationTopicRead(d, meta)
}
func resourceAwsSesNotificationTopicRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sesConn
identity, notificationType, err := decodeSesIdentityNotificationTopicId(d.Id())
if err != nil {
return err
}
getOpts := &ses.GetIdentityNotificationAttributesInput{
Identities: []*string{aws.String(identity)},
}
log.Printf("[DEBUG] Reading SES Identity Notification Topic Attributes: %#v", getOpts)
response, err := conn.GetIdentityNotificationAttributes(getOpts)
if err != nil {
return fmt.Errorf("Error reading SES Identity Notification Topic: %s", err)
}
d.Set("topic_arn", "")
if response == nil {
return nil
}
notificationAttributes, notificationAttributesOk := response.NotificationAttributes[identity]
if !notificationAttributesOk {
return nil
}
switch notificationType {
case ses.NotificationTypeBounce:
d.Set("topic_arn", notificationAttributes.BounceTopic)
case ses.NotificationTypeComplaint:
d.Set("topic_arn", notificationAttributes.ComplaintTopic)
case ses.NotificationTypeDelivery:
d.Set("topic_arn", notificationAttributes.DeliveryTopic)
}
return nil
}
func resourceAwsSesNotificationTopicDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sesConn
identity, notificationType, err := decodeSesIdentityNotificationTopicId(d.Id())
if err != nil {
return err
}
setOpts := &ses.SetIdentityNotificationTopicInput{
Identity: aws.String(identity),
NotificationType: aws.String(notificationType),
SnsTopic: nil,
}
log.Printf("[DEBUG] Deleting SES Identity Notification Topic: %#v", setOpts)
if _, err := conn.SetIdentityNotificationTopic(setOpts); err != nil {
return fmt.Errorf("Error deleting SES Identity Notification Topic: %s", err)
}
return resourceAwsSesNotificationTopicRead(d, meta)
}
func decodeSesIdentityNotificationTopicId(id string) (string, string, error) {
parts := strings.Split(id, "|")
if len(parts) != 2 {
return "", "", fmt.Errorf("Unexpected format of ID (%q), expected IDENTITY|TYPE", id)
}
return parts[0], parts[1], nil
}