github.com/joshgarnett/terraform@v0.5.4-0.20160219181435-92dc20bb3594/builtin/providers/aws/resource_aws_cloudwatch_event_target_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 8 events "github.com/aws/aws-sdk-go/service/cloudwatchevents" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAWSCloudWatchEventTarget_basic(t *testing.T) { 14 var target events.Target 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckAWSCloudWatchEventTargetDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccAWSCloudWatchEventTargetConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.moobar", &target), 25 resource.TestCheckResourceAttr("aws_cloudwatch_event_target.moobar", "rule", "tf-acc-cw-event-rule-basic"), 26 resource.TestCheckResourceAttr("aws_cloudwatch_event_target.moobar", "target_id", "tf-acc-cw-target-basic"), 27 resource.TestMatchResourceAttr("aws_cloudwatch_event_target.moobar", "arn", 28 regexp.MustCompile(":tf-acc-moon$")), 29 ), 30 }, 31 resource.TestStep{ 32 Config: testAccAWSCloudWatchEventTargetConfigModified, 33 Check: resource.ComposeTestCheckFunc( 34 testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.moobar", &target), 35 resource.TestCheckResourceAttr("aws_cloudwatch_event_target.moobar", "rule", "tf-acc-cw-event-rule-basic"), 36 resource.TestCheckResourceAttr("aws_cloudwatch_event_target.moobar", "target_id", "tf-acc-cw-target-modified"), 37 resource.TestMatchResourceAttr("aws_cloudwatch_event_target.moobar", "arn", 38 regexp.MustCompile(":tf-acc-sun$")), 39 ), 40 }, 41 }, 42 }) 43 } 44 45 func TestAccAWSCloudWatchEventTarget_full(t *testing.T) { 46 var target events.Target 47 48 resource.Test(t, resource.TestCase{ 49 PreCheck: func() { testAccPreCheck(t) }, 50 Providers: testAccProviders, 51 CheckDestroy: testAccCheckAWSCloudWatchEventTargetDestroy, 52 Steps: []resource.TestStep{ 53 resource.TestStep{ 54 Config: testAccAWSCloudWatchEventTargetConfig_full, 55 Check: resource.ComposeTestCheckFunc( 56 testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.foobar", &target), 57 resource.TestCheckResourceAttr("aws_cloudwatch_event_target.foobar", "rule", "tf-acc-cw-event-rule-full"), 58 resource.TestCheckResourceAttr("aws_cloudwatch_event_target.foobar", "target_id", "tf-acc-cw-target-full"), 59 resource.TestMatchResourceAttr("aws_cloudwatch_event_target.foobar", "arn", 60 regexp.MustCompile("^arn:aws:kinesis:.*:stream/terraform-kinesis-test$")), 61 resource.TestCheckResourceAttr("aws_cloudwatch_event_target.foobar", "input", "{ \"source\": [\"aws.cloudtrail\"] }\n"), 62 resource.TestCheckResourceAttr("aws_cloudwatch_event_target.foobar", "input_path", ""), 63 ), 64 }, 65 }, 66 }) 67 } 68 69 func testAccCheckCloudWatchEventTargetExists(n string, rule *events.Target) resource.TestCheckFunc { 70 return func(s *terraform.State) error { 71 rs, ok := s.RootModule().Resources[n] 72 if !ok { 73 return fmt.Errorf("Not found: %s", n) 74 } 75 76 conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn 77 t, err := findEventTargetById(rs.Primary.Attributes["target_id"], 78 rs.Primary.Attributes["rule"], nil, conn) 79 if err != nil { 80 return fmt.Errorf("Event Target not found: %s", err) 81 } 82 83 *rule = *t 84 85 return nil 86 } 87 } 88 89 func testAccCheckAWSCloudWatchEventTargetDestroy(s *terraform.State) error { 90 conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn 91 92 for _, rs := range s.RootModule().Resources { 93 if rs.Type != "aws_cloudwatch_event_target" { 94 continue 95 } 96 97 t, err := findEventTargetById(rs.Primary.Attributes["target_id"], 98 rs.Primary.Attributes["rule"], nil, conn) 99 if err == nil { 100 return fmt.Errorf("CloudWatch Event Target %q still exists: %s", 101 rs.Primary.ID, t) 102 } 103 } 104 105 return nil 106 } 107 108 var testAccAWSCloudWatchEventTargetConfig = ` 109 resource "aws_cloudwatch_event_rule" "foo" { 110 name = "tf-acc-cw-event-rule-basic" 111 schedule_expression = "rate(1 hour)" 112 } 113 114 resource "aws_cloudwatch_event_target" "moobar" { 115 rule = "${aws_cloudwatch_event_rule.foo.name}" 116 target_id = "tf-acc-cw-target-basic" 117 arn = "${aws_sns_topic.moon.arn}" 118 } 119 120 resource "aws_sns_topic" "moon" { 121 name = "tf-acc-moon" 122 } 123 ` 124 125 var testAccAWSCloudWatchEventTargetConfigModified = ` 126 resource "aws_cloudwatch_event_rule" "foo" { 127 name = "tf-acc-cw-event-rule-basic" 128 schedule_expression = "rate(1 hour)" 129 } 130 131 resource "aws_cloudwatch_event_target" "moobar" { 132 rule = "${aws_cloudwatch_event_rule.foo.name}" 133 target_id = "tf-acc-cw-target-modified" 134 arn = "${aws_sns_topic.sun.arn}" 135 } 136 137 resource "aws_sns_topic" "sun" { 138 name = "tf-acc-sun" 139 } 140 ` 141 142 var testAccAWSCloudWatchEventTargetConfig_full = ` 143 resource "aws_cloudwatch_event_rule" "foo" { 144 name = "tf-acc-cw-event-rule-full" 145 schedule_expression = "rate(1 hour)" 146 role_arn = "${aws_iam_role.role.arn}" 147 } 148 149 resource "aws_iam_role" "role" { 150 name = "test_role" 151 assume_role_policy = <<POLICY 152 { 153 "Version": "2012-10-17", 154 "Statement": [ 155 { 156 "Action": "sts:AssumeRole", 157 "Principal": { 158 "Service": "events.amazonaws.com" 159 }, 160 "Effect": "Allow", 161 "Sid": "" 162 } 163 ] 164 } 165 POLICY 166 } 167 168 resource "aws_iam_role_policy" "test_policy" { 169 name = "test_policy" 170 role = "${aws_iam_role.role.id}" 171 policy = <<EOF 172 { 173 "Version": "2012-10-17", 174 "Statement": [ 175 { 176 "Action": [ 177 "kinesis:PutRecord", 178 "kinesis:PutRecords" 179 ], 180 "Resource": [ 181 "*" 182 ], 183 "Effect": "Allow" 184 } 185 ] 186 } 187 EOF 188 } 189 190 resource "aws_cloudwatch_event_target" "foobar" { 191 rule = "${aws_cloudwatch_event_rule.foo.name}" 192 target_id = "tf-acc-cw-target-full" 193 input = <<INPUT 194 { "source": ["aws.cloudtrail"] } 195 INPUT 196 arn = "${aws_kinesis_stream.test_stream.arn}" 197 } 198 199 resource "aws_kinesis_stream" "test_stream" { 200 name = "terraform-kinesis-test" 201 shard_count = 1 202 } 203 `