-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Copy pathresource_aws_ses_event_destination_test.go
220 lines (187 loc) · 5.66 KB
/
resource_aws_ses_event_destination_test.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
package aws
import (
"fmt"
"testing"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSSESEventDestination_basic(t *testing.T) {
rString := acctest.RandString(8)
bucketName := fmt.Sprintf("tf-acc-bucket-ses-event-dst-%s", rString)
roleName := fmt.Sprintf("tf_acc_role_ses_event_dst_%s", rString)
streamName := fmt.Sprintf("tf_acc_stream_ses_event_dst_%s", rString)
policyName := fmt.Sprintf("tf_acc_policy_ses_event_dst_%s", rString)
topicName := fmt.Sprintf("tf_acc_topic_ses_event_dst_%s", rString)
sesCfgSetName := fmt.Sprintf("tf_acc_cfg_ses_event_dst_%s", rString)
sesEventDstNameKinesis := fmt.Sprintf("tf_acc_event_dst_kinesis_%s", rString)
sesEventDstNameCw := fmt.Sprintf("tf_acc_event_dst_cloudwatch_%s", rString)
sesEventDstNameSns := fmt.Sprintf("tf_acc_event_dst_sns_%s", rString)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckSESEventDestinationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSESEventDestinationConfig(bucketName, roleName, streamName, policyName, topicName,
sesCfgSetName, sesEventDstNameKinesis, sesEventDstNameCw, sesEventDstNameSns),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsSESEventDestinationExists("aws_ses_configuration_set.test"),
resource.TestCheckResourceAttr(
"aws_ses_event_destination.kinesis", "name", sesEventDstNameKinesis),
resource.TestCheckResourceAttr(
"aws_ses_event_destination.cloudwatch", "name", sesEventDstNameCw),
resource.TestCheckResourceAttr(
"aws_ses_event_destination.sns", "name", sesEventDstNameSns),
),
},
},
})
}
func testAccCheckSESEventDestinationDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).sesConn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_ses_configuration_set" {
continue
}
response, err := conn.ListConfigurationSets(&ses.ListConfigurationSetsInput{})
if err != nil {
return err
}
found := false
for _, element := range response.ConfigurationSets {
if *element.Name == rs.Primary.ID {
found = true
}
}
if found {
return fmt.Errorf("The configuration set still exists")
}
}
return nil
}
func testAccCheckAwsSESEventDestinationExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("SES event destination not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("SES event destination ID not set")
}
conn := testAccProvider.Meta().(*AWSClient).sesConn
response, err := conn.ListConfigurationSets(&ses.ListConfigurationSetsInput{})
if err != nil {
return err
}
found := false
for _, element := range response.ConfigurationSets {
if *element.Name == rs.Primary.ID {
found = true
}
}
if !found {
return fmt.Errorf("The configuration set was not created")
}
return nil
}
}
func testAccAWSSESEventDestinationConfig(bucketName, roleName, streamName, policyName, topicName,
sesCfgSetName, sesEventDstNameKinesis, sesEventDstNameCw, sesEventDstNameSns string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "bucket" {
bucket = "%s"
acl = "private"
}
resource "aws_iam_role" "firehose_role" {
name = "%s"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "firehose.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
},
{
"Effect": "Allow",
"Principal": {
"Service": "ses.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_kinesis_firehose_delivery_stream" "test_stream" {
name = "%s"
destination = "s3"
s3_configuration {
role_arn = "${aws_iam_role.firehose_role.arn}"
bucket_arn = "${aws_s3_bucket.bucket.arn}"
}
}
resource "aws_iam_role_policy" "firehose_delivery_policy" {
name = "%s"
role = "${aws_iam_role.firehose_role.id}"
policy = "${data.aws_iam_policy_document.fh_felivery_document.json}"
}
data "aws_iam_policy_document" "fh_felivery_document" {
statement {
sid = "GiveSESPermissionToPutFirehose"
actions = [
"firehose:PutRecord",
"firehose:PutRecordBatch",
]
resources = [
"*",
]
}
}
resource "aws_sns_topic" "ses_destination" {
name = "%s"
}
resource "aws_ses_configuration_set" "test" {
name = "%s"
}
resource "aws_ses_event_destination" "kinesis" {
name = "%s"
configuration_set_name = "${aws_ses_configuration_set.test.name}"
enabled = true
matching_types = ["bounce", "send"]
kinesis_destination {
stream_arn = "${aws_kinesis_firehose_delivery_stream.test_stream.arn}"
role_arn = "${aws_iam_role.firehose_role.arn}"
}
}
resource "aws_ses_event_destination" "cloudwatch" {
name = "%s",
configuration_set_name = "${aws_ses_configuration_set.test.name}"
enabled = true
matching_types = ["bounce", "send"]
cloudwatch_destination {
default_value = "default"
dimension_name = "dimension"
value_source = "emailHeader"
}
}
resource "aws_ses_event_destination" "sns" {
name = "%s"
configuration_set_name = "${aws_ses_configuration_set.test.name}"
enabled = true
matching_types = ["bounce", "send"]
sns_destination {
topic_arn = "${aws_sns_topic.ses_destination.arn}"
}
}
`, bucketName, roleName, streamName, policyName, topicName,
sesCfgSetName, sesEventDstNameKinesis, sesEventDstNameCw, sesEventDstNameSns)
}