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