github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_ses_event_destination_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/service/ses" 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAWSSESEventDestination_basic(t *testing.T) { 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { 16 testAccPreCheck(t) 17 }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckSESEventDestinationDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccAWSSESEventDestinationConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckAwsSESEventDestinationExists("aws_ses_configuration_set.test"), 25 resource.TestCheckResourceAttr( 26 "aws_ses_event_destination.kinesis", "name", "event-destination-kinesis"), 27 resource.TestCheckResourceAttr( 28 "aws_ses_event_destination.cloudwatch", "name", "event-destination-cloudwatch"), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func testAccCheckSESEventDestinationDestroy(s *terraform.State) error { 36 conn := testAccProvider.Meta().(*AWSClient).sesConn 37 38 for _, rs := range s.RootModule().Resources { 39 if rs.Type != "aws_ses_configuration_set" { 40 continue 41 } 42 43 response, err := conn.ListConfigurationSets(&ses.ListConfigurationSetsInput{}) 44 if err != nil { 45 return err 46 } 47 48 found := false 49 for _, element := range response.ConfigurationSets { 50 if *element.Name == fmt.Sprintf("some-configuration-set-%d", edRandomInteger) { 51 found = true 52 } 53 } 54 55 if found { 56 return fmt.Errorf("The configuration set still exists") 57 } 58 59 } 60 61 return nil 62 63 } 64 65 func testAccCheckAwsSESEventDestinationExists(n string) resource.TestCheckFunc { 66 return func(s *terraform.State) error { 67 rs, ok := s.RootModule().Resources[n] 68 if !ok { 69 return fmt.Errorf("SES event destination not found: %s", n) 70 } 71 72 if rs.Primary.ID == "" { 73 return fmt.Errorf("SES event destination ID not set") 74 } 75 76 conn := testAccProvider.Meta().(*AWSClient).sesConn 77 78 response, err := conn.ListConfigurationSets(&ses.ListConfigurationSetsInput{}) 79 if err != nil { 80 return err 81 } 82 83 found := false 84 for _, element := range response.ConfigurationSets { 85 if *element.Name == fmt.Sprintf("some-configuration-set-%d", edRandomInteger) { 86 found = true 87 } 88 } 89 90 if !found { 91 return fmt.Errorf("The configuration set was not created") 92 } 93 94 return nil 95 } 96 } 97 98 var edRandomInteger = acctest.RandInt() 99 var testAccAWSSESEventDestinationConfig = fmt.Sprintf(` 100 resource "aws_s3_bucket" "bucket" { 101 bucket = "tf-test-bucket-format" 102 acl = "private" 103 } 104 105 resource "aws_iam_role" "firehose_role" { 106 name = "firehose_test_role_test" 107 assume_role_policy = <<EOF 108 { 109 "Version": "2012-10-17", 110 "Statement": [ 111 { 112 "Action": "sts:AssumeRole", 113 "Principal": { 114 "Service": "firehose.amazonaws.com" 115 }, 116 "Effect": "Allow", 117 "Sid": "" 118 }, 119 { 120 "Effect": "Allow", 121 "Principal": { 122 "Service": "ses.amazonaws.com" 123 }, 124 "Action": "sts:AssumeRole" 125 } 126 ] 127 } 128 EOF 129 } 130 131 resource "aws_kinesis_firehose_delivery_stream" "test_stream" { 132 name = "terraform-kinesis-firehose-test-stream-test" 133 destination = "s3" 134 s3_configuration { 135 role_arn = "${aws_iam_role.firehose_role.arn}" 136 bucket_arn = "${aws_s3_bucket.bucket.arn}" 137 } 138 } 139 140 resource "aws_iam_role_policy" "firehose_delivery_policy" { 141 name = "tf-delivery-policy-test" 142 role = "${aws_iam_role.firehose_role.id}" 143 policy = "${data.aws_iam_policy_document.fh_felivery_document.json}" 144 } 145 146 data "aws_iam_policy_document" "fh_felivery_document" { 147 statement { 148 sid = "GiveSESPermissionToPutFirehose" 149 actions = [ 150 "firehose:PutRecord", 151 "firehose:PutRecordBatch", 152 ] 153 resources = [ 154 "*", 155 ] 156 } 157 } 158 159 resource "aws_ses_configuration_set" "test" { 160 name = "some-configuration-set-%d" 161 } 162 163 resource "aws_ses_event_destination" "kinesis" { 164 name = "event-destination-kinesis", 165 configuration_set_name = "${aws_ses_configuration_set.test.name}", 166 enabled = true, 167 matching_types = ["bounce", "send"], 168 169 kinesis_destination = { 170 stream_arn = "${aws_kinesis_firehose_delivery_stream.test_stream.arn}", 171 role_arn = "${aws_iam_role.firehose_role.arn}" 172 } 173 } 174 175 resource "aws_ses_event_destination" "cloudwatch" { 176 name = "event-destination-cloudwatch", 177 configuration_set_name = "${aws_ses_configuration_set.test.name}", 178 enabled = true, 179 matching_types = ["bounce", "send"], 180 181 cloudwatch_destination = { 182 default_value = "default" 183 dimension_name = "dimension" 184 value_source = "emailHeader" 185 } 186 } 187 `, edRandomInteger)