github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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_missingTargetId(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: testAccAWSCloudWatchEventTargetConfigMissingTargetId, 55 Check: resource.ComposeTestCheckFunc( 56 testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.moobar", &target), 57 resource.TestCheckResourceAttr("aws_cloudwatch_event_target.moobar", "rule", "tf-acc-cw-event-rule-missing-target-id"), 58 resource.TestMatchResourceAttr("aws_cloudwatch_event_target.moobar", "arn", 59 regexp.MustCompile(":tf-acc-moon$")), 60 ), 61 }, 62 }, 63 }) 64 } 65 66 func TestAccAWSCloudWatchEventTarget_full(t *testing.T) { 67 var target events.Target 68 69 resource.Test(t, resource.TestCase{ 70 PreCheck: func() { testAccPreCheck(t) }, 71 Providers: testAccProviders, 72 CheckDestroy: testAccCheckAWSCloudWatchEventTargetDestroy, 73 Steps: []resource.TestStep{ 74 resource.TestStep{ 75 Config: testAccAWSCloudWatchEventTargetConfig_full, 76 Check: resource.ComposeTestCheckFunc( 77 testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.foobar", &target), 78 resource.TestCheckResourceAttr("aws_cloudwatch_event_target.foobar", "rule", "tf-acc-cw-event-rule-full"), 79 resource.TestCheckResourceAttr("aws_cloudwatch_event_target.foobar", "target_id", "tf-acc-cw-target-full"), 80 resource.TestMatchResourceAttr("aws_cloudwatch_event_target.foobar", "arn", 81 regexp.MustCompile("^arn:aws:kinesis:.*:stream/terraform-kinesis-test$")), 82 resource.TestCheckResourceAttr("aws_cloudwatch_event_target.foobar", "input", "{ \"source\": [\"aws.cloudtrail\"] }\n"), 83 resource.TestCheckResourceAttr("aws_cloudwatch_event_target.foobar", "input_path", ""), 84 ), 85 }, 86 }, 87 }) 88 } 89 90 func testAccCheckCloudWatchEventTargetExists(n string, rule *events.Target) resource.TestCheckFunc { 91 return func(s *terraform.State) error { 92 rs, ok := s.RootModule().Resources[n] 93 if !ok { 94 return fmt.Errorf("Not found: %s", n) 95 } 96 97 conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn 98 t, err := findEventTargetById(rs.Primary.Attributes["target_id"], 99 rs.Primary.Attributes["rule"], nil, conn) 100 if err != nil { 101 return fmt.Errorf("Event Target not found: %s", err) 102 } 103 104 *rule = *t 105 106 return nil 107 } 108 } 109 110 func testAccCheckAWSCloudWatchEventTargetDestroy(s *terraform.State) error { 111 conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn 112 113 for _, rs := range s.RootModule().Resources { 114 if rs.Type != "aws_cloudwatch_event_target" { 115 continue 116 } 117 118 t, err := findEventTargetById(rs.Primary.Attributes["target_id"], 119 rs.Primary.Attributes["rule"], nil, conn) 120 if err == nil { 121 return fmt.Errorf("CloudWatch Event Target %q still exists: %s", 122 rs.Primary.ID, t) 123 } 124 } 125 126 return nil 127 } 128 129 func testAccCheckTargetIdExists(targetId string) resource.TestCheckFunc { 130 return func(s *terraform.State) error { 131 _, ok := s.RootModule().Resources[targetId] 132 if !ok { 133 return fmt.Errorf("Not found: %s", targetId) 134 } 135 136 return nil 137 } 138 } 139 140 var testAccAWSCloudWatchEventTargetConfig = ` 141 resource "aws_cloudwatch_event_rule" "foo" { 142 name = "tf-acc-cw-event-rule-basic" 143 schedule_expression = "rate(1 hour)" 144 } 145 146 resource "aws_cloudwatch_event_target" "moobar" { 147 rule = "${aws_cloudwatch_event_rule.foo.name}" 148 target_id = "tf-acc-cw-target-basic" 149 arn = "${aws_sns_topic.moon.arn}" 150 } 151 152 resource "aws_sns_topic" "moon" { 153 name = "tf-acc-moon" 154 } 155 ` 156 157 var testAccAWSCloudWatchEventTargetConfigMissingTargetId = ` 158 resource "aws_cloudwatch_event_rule" "foo" { 159 name = "tf-acc-cw-event-rule-missing-target-id" 160 schedule_expression = "rate(1 hour)" 161 } 162 163 resource "aws_cloudwatch_event_target" "moobar" { 164 rule = "${aws_cloudwatch_event_rule.foo.name}" 165 arn = "${aws_sns_topic.moon.arn}" 166 } 167 168 resource "aws_sns_topic" "moon" { 169 name = "tf-acc-moon" 170 } 171 ` 172 173 var testAccAWSCloudWatchEventTargetConfigModified = ` 174 resource "aws_cloudwatch_event_rule" "foo" { 175 name = "tf-acc-cw-event-rule-basic" 176 schedule_expression = "rate(1 hour)" 177 } 178 179 resource "aws_cloudwatch_event_target" "moobar" { 180 rule = "${aws_cloudwatch_event_rule.foo.name}" 181 target_id = "tf-acc-cw-target-modified" 182 arn = "${aws_sns_topic.sun.arn}" 183 } 184 185 resource "aws_sns_topic" "sun" { 186 name = "tf-acc-sun" 187 } 188 ` 189 190 var testAccAWSCloudWatchEventTargetConfig_full = ` 191 resource "aws_cloudwatch_event_rule" "foo" { 192 name = "tf-acc-cw-event-rule-full" 193 schedule_expression = "rate(1 hour)" 194 role_arn = "${aws_iam_role.role.arn}" 195 } 196 197 resource "aws_iam_role" "role" { 198 name = "test_role" 199 assume_role_policy = <<POLICY 200 { 201 "Version": "2012-10-17", 202 "Statement": [ 203 { 204 "Action": "sts:AssumeRole", 205 "Principal": { 206 "Service": "events.amazonaws.com" 207 }, 208 "Effect": "Allow", 209 "Sid": "" 210 } 211 ] 212 } 213 POLICY 214 } 215 216 resource "aws_iam_role_policy" "test_policy" { 217 name = "test_policy" 218 role = "${aws_iam_role.role.id}" 219 policy = <<EOF 220 { 221 "Version": "2012-10-17", 222 "Statement": [ 223 { 224 "Action": [ 225 "kinesis:PutRecord", 226 "kinesis:PutRecords" 227 ], 228 "Resource": [ 229 "*" 230 ], 231 "Effect": "Allow" 232 } 233 ] 234 } 235 EOF 236 } 237 238 resource "aws_cloudwatch_event_target" "foobar" { 239 rule = "${aws_cloudwatch_event_rule.foo.name}" 240 target_id = "tf-acc-cw-target-full" 241 input = <<INPUT 242 { "source": ["aws.cloudtrail"] } 243 INPUT 244 arn = "${aws_kinesis_stream.test_stream.arn}" 245 } 246 247 resource "aws_kinesis_stream" "test_stream" { 248 name = "terraform-kinesis-test" 249 shard_count = 1 250 } 251 `