github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/datadog/resource_datadog_monitor_test.go (about) 1 package datadog 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 "testing" 8 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 "github.com/zorkian/go-datadog-api" 12 ) 13 14 func TestAccDatadogMonitor_Basic(t *testing.T) { 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckDatadogMonitorDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccCheckDatadogMonitorConfig, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 24 resource.TestCheckResourceAttr( 25 "datadog_monitor.foo", "name", "name for monitor foo"), 26 resource.TestCheckResourceAttr( 27 "datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"), 28 resource.TestCheckResourceAttr( 29 "datadog_monitor.foo", "type", "metric alert"), 30 resource.TestCheckResourceAttr( 31 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"), 32 resource.TestCheckResourceAttr( 33 "datadog_monitor.foo", "notify_no_data", "false"), 34 resource.TestCheckResourceAttr( 35 "datadog_monitor.foo", "renotify_interval", "60"), 36 resource.TestCheckResourceAttr( 37 "datadog_monitor.foo", "thresholds.warning", "1.0"), 38 resource.TestCheckResourceAttr( 39 "datadog_monitor.foo", "thresholds.critical", "2.0"), 40 resource.TestCheckResourceAttr( 41 "datadog_monitor.foo", "require_full_window", "true"), 42 resource.TestCheckResourceAttr( 43 "datadog_monitor.foo", "locked", "false"), 44 resource.TestCheckResourceAttr( 45 "datadog_monitor.foo", "tags.0", "foo:bar"), 46 resource.TestCheckResourceAttr( 47 "datadog_monitor.foo", "tags.1", "baz"), 48 ), 49 }, 50 }, 51 }) 52 } 53 54 func TestAccDatadogMonitor_BasicNoTreshold(t *testing.T) { 55 resource.Test(t, resource.TestCase{ 56 PreCheck: func() { testAccPreCheck(t) }, 57 Providers: testAccProviders, 58 CheckDestroy: testAccCheckDatadogMonitorDestroy, 59 Steps: []resource.TestStep{ 60 resource.TestStep{ 61 Config: testAccCheckDatadogMonitorConfigNoThresholds, 62 Check: resource.ComposeTestCheckFunc( 63 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 64 resource.TestCheckResourceAttr( 65 "datadog_monitor.foo", "name", "name for monitor foo"), 66 resource.TestCheckResourceAttr( 67 "datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"), 68 resource.TestCheckResourceAttr( 69 "datadog_monitor.foo", "type", "metric alert"), 70 resource.TestCheckResourceAttr( 71 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"), 72 resource.TestCheckResourceAttr( 73 "datadog_monitor.foo", "notify_no_data", "false"), 74 resource.TestCheckResourceAttr( 75 "datadog_monitor.foo", "renotify_interval", "60"), 76 resource.TestCheckResourceAttr( 77 "datadog_monitor.foo", "require_full_window", "true"), 78 resource.TestCheckResourceAttr( 79 "datadog_monitor.foo", "locked", "false"), 80 resource.TestCheckResourceAttr( 81 "datadog_monitor.foo", "tags.0", "foo:bar"), 82 resource.TestCheckResourceAttr( 83 "datadog_monitor.foo", "tags.1", "bar:baz"), 84 ), 85 }, 86 }, 87 }) 88 } 89 90 func TestAccDatadogMonitor_Updated(t *testing.T) { 91 resource.Test(t, resource.TestCase{ 92 PreCheck: func() { testAccPreCheck(t) }, 93 Providers: testAccProviders, 94 CheckDestroy: testAccCheckDatadogMonitorDestroy, 95 Steps: []resource.TestStep{ 96 resource.TestStep{ 97 Config: testAccCheckDatadogMonitorConfig, 98 Check: resource.ComposeTestCheckFunc( 99 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 100 resource.TestCheckResourceAttr( 101 "datadog_monitor.foo", "name", "name for monitor foo"), 102 resource.TestCheckResourceAttr( 103 "datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"), 104 resource.TestCheckResourceAttr( 105 "datadog_monitor.foo", "escalation_message", "the situation has escalated @pagerduty"), 106 resource.TestCheckResourceAttr( 107 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"), 108 resource.TestCheckResourceAttr( 109 "datadog_monitor.foo", "type", "metric alert"), 110 resource.TestCheckResourceAttr( 111 "datadog_monitor.foo", "notify_no_data", "false"), 112 resource.TestCheckResourceAttr( 113 "datadog_monitor.foo", "renotify_interval", "60"), 114 resource.TestCheckResourceAttr( 115 "datadog_monitor.foo", "thresholds.warning", "1.0"), 116 resource.TestCheckResourceAttr( 117 "datadog_monitor.foo", "thresholds.critical", "2.0"), 118 resource.TestCheckResourceAttr( 119 "datadog_monitor.foo", "notify_audit", "false"), 120 resource.TestCheckResourceAttr( 121 "datadog_monitor.foo", "timeout_h", "60"), 122 resource.TestCheckResourceAttr( 123 "datadog_monitor.foo", "include_tags", "true"), 124 resource.TestCheckResourceAttr( 125 "datadog_monitor.foo", "require_full_window", "true"), 126 resource.TestCheckResourceAttr( 127 "datadog_monitor.foo", "locked", "false"), 128 resource.TestCheckResourceAttr( 129 "datadog_monitor.foo", "tags.0", "foo:bar"), 130 resource.TestCheckResourceAttr( 131 "datadog_monitor.foo", "tags.1", "baz"), 132 ), 133 }, 134 resource.TestStep{ 135 Config: testAccCheckDatadogMonitorConfigUpdated, 136 Check: resource.ComposeTestCheckFunc( 137 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 138 resource.TestCheckResourceAttr( 139 "datadog_monitor.foo", "name", "name for monitor bar"), 140 resource.TestCheckResourceAttr( 141 "datadog_monitor.foo", "message", "a different message Notify: @hipchat-channel"), 142 resource.TestCheckResourceAttr( 143 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3"), 144 resource.TestCheckResourceAttr( 145 "datadog_monitor.foo", "escalation_message", "the situation has escalated! @pagerduty"), 146 resource.TestCheckResourceAttr( 147 "datadog_monitor.foo", "type", "metric alert"), 148 resource.TestCheckResourceAttr( 149 "datadog_monitor.foo", "notify_no_data", "true"), 150 resource.TestCheckResourceAttr( 151 "datadog_monitor.foo", "no_data_timeframe", "20"), 152 resource.TestCheckResourceAttr( 153 "datadog_monitor.foo", "renotify_interval", "40"), 154 resource.TestCheckResourceAttr( 155 "datadog_monitor.foo", "thresholds.ok", "0.0"), 156 resource.TestCheckResourceAttr( 157 "datadog_monitor.foo", "thresholds.warning", "1.0"), 158 resource.TestCheckResourceAttr( 159 "datadog_monitor.foo", "thresholds.critical", "3.0"), 160 resource.TestCheckResourceAttr( 161 "datadog_monitor.foo", "notify_audit", "true"), 162 resource.TestCheckResourceAttr( 163 "datadog_monitor.foo", "timeout_h", "70"), 164 resource.TestCheckResourceAttr( 165 "datadog_monitor.foo", "include_tags", "false"), 166 resource.TestCheckResourceAttr( 167 "datadog_monitor.foo", "silenced.*", "0"), 168 resource.TestCheckResourceAttr( 169 "datadog_monitor.foo", "require_full_window", "false"), 170 resource.TestCheckResourceAttr( 171 "datadog_monitor.foo", "locked", "true"), 172 resource.TestCheckResourceAttr( 173 "datadog_monitor.foo", "tags.0", "baz:qux"), 174 resource.TestCheckResourceAttr( 175 "datadog_monitor.foo", "tags.1", "quux"), 176 ), 177 }, 178 }, 179 }) 180 } 181 182 func TestAccDatadogMonitor_TrimWhitespace(t *testing.T) { 183 resource.Test(t, resource.TestCase{ 184 PreCheck: func() { testAccPreCheck(t) }, 185 Providers: testAccProviders, 186 CheckDestroy: testAccCheckDatadogMonitorDestroy, 187 Steps: []resource.TestStep{ 188 resource.TestStep{ 189 Config: testAccCheckDatadogMonitorConfigWhitespace, 190 Check: resource.ComposeTestCheckFunc( 191 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 192 resource.TestCheckResourceAttr( 193 "datadog_monitor.foo", "name", "name for monitor foo"), 194 resource.TestCheckResourceAttr( 195 "datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"), 196 resource.TestCheckResourceAttr( 197 "datadog_monitor.foo", "type", "metric alert"), 198 resource.TestCheckResourceAttr( 199 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"), 200 resource.TestCheckResourceAttr( 201 "datadog_monitor.foo", "notify_no_data", "false"), 202 resource.TestCheckResourceAttr( 203 "datadog_monitor.foo", "renotify_interval", "60"), 204 resource.TestCheckResourceAttr( 205 "datadog_monitor.foo", "thresholds.ok", "0.0"), 206 resource.TestCheckResourceAttr( 207 "datadog_monitor.foo", "thresholds.warning", "1.0"), 208 resource.TestCheckResourceAttr( 209 "datadog_monitor.foo", "thresholds.critical", "2.0"), 210 ), 211 }, 212 }, 213 }) 214 } 215 216 func TestAccDatadogMonitor_Basic_float_int(t *testing.T) { 217 resource.Test(t, resource.TestCase{ 218 PreCheck: func() { testAccPreCheck(t) }, 219 Providers: testAccProviders, 220 CheckDestroy: testAccCheckDatadogMonitorDestroy, 221 Steps: []resource.TestStep{ 222 resource.TestStep{ 223 Config: testAccCheckDatadogMonitorConfig_ints, 224 Check: resource.ComposeTestCheckFunc( 225 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 226 resource.TestCheckResourceAttr( 227 "datadog_monitor.foo", "thresholds.warning", "1"), 228 resource.TestCheckResourceAttr( 229 "datadog_monitor.foo", "thresholds.critical", "2"), 230 ), 231 }, 232 233 resource.TestStep{ 234 Config: testAccCheckDatadogMonitorConfig_ints_mixed, 235 Check: resource.ComposeTestCheckFunc( 236 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 237 resource.TestCheckResourceAttr( 238 "datadog_monitor.foo", "thresholds.warning", "1.0"), 239 resource.TestCheckResourceAttr( 240 "datadog_monitor.foo", "thresholds.critical", "3.0"), 241 ), 242 }, 243 }, 244 }) 245 } 246 247 func testAccCheckDatadogMonitorDestroy(s *terraform.State) error { 248 client := testAccProvider.Meta().(*datadog.Client) 249 250 if err := destroyHelper(s, client); err != nil { 251 return err 252 } 253 return nil 254 } 255 256 func testAccCheckDatadogMonitorExists(n string) resource.TestCheckFunc { 257 return func(s *terraform.State) error { 258 client := testAccProvider.Meta().(*datadog.Client) 259 if err := existsHelper(s, client); err != nil { 260 return err 261 } 262 return nil 263 } 264 } 265 266 const testAccCheckDatadogMonitorConfig = ` 267 resource "datadog_monitor" "foo" { 268 name = "name for monitor foo" 269 type = "metric alert" 270 message = "some message Notify: @hipchat-channel" 271 escalation_message = "the situation has escalated @pagerduty" 272 273 query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2" 274 275 thresholds { 276 warning = "1.0" 277 critical = "2.0" 278 } 279 280 notify_no_data = false 281 renotify_interval = 60 282 283 notify_audit = false 284 timeout_h = 60 285 include_tags = true 286 require_full_window = true 287 locked = false 288 tags = ["foo:bar", "baz"] 289 } 290 ` 291 const testAccCheckDatadogMonitorConfigNoThresholds = ` 292 resource "datadog_monitor" "foo" { 293 name = "name for monitor foo" 294 type = "metric alert" 295 message = "some message Notify: @hipchat-channel" 296 escalation_message = "the situation has escalated @pagerduty" 297 298 query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2" 299 300 notify_no_data = false 301 renotify_interval = 60 302 303 notify_audit = false 304 timeout_h = 60 305 include_tags = true 306 require_full_window = true 307 locked = false 308 tags = ["foo:bar", "bar:baz"] 309 } 310 ` 311 312 const testAccCheckDatadogMonitorConfig_ints = ` 313 resource "datadog_monitor" "foo" { 314 name = "name for monitor foo" 315 type = "metric alert" 316 message = "some message Notify: @hipchat-channel" 317 escalation_message = "the situation has escalated @pagerduty" 318 319 query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2" 320 321 thresholds { 322 warning = 1 323 critical = 2 324 } 325 326 notify_no_data = false 327 renotify_interval = 60 328 329 notify_audit = false 330 timeout_h = 60 331 include_tags = true 332 require_full_window = true 333 locked = false 334 335 tags = ["foo:bar", "baz"] 336 } 337 ` 338 339 const testAccCheckDatadogMonitorConfig_ints_mixed = ` 340 resource "datadog_monitor" "foo" { 341 name = "name for monitor foo" 342 type = "metric alert" 343 message = "some message Notify: @hipchat-channel" 344 escalation_message = "the situation has escalated @pagerduty" 345 346 query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 3" 347 348 thresholds { 349 warning = 1 350 critical = 3.0 351 } 352 353 notify_no_data = false 354 renotify_interval = 60 355 356 notify_audit = false 357 timeout_h = 60 358 include_tags = true 359 require_full_window = true 360 locked = false 361 362 tags = ["foo:bar", "baz"] 363 } 364 ` 365 366 const testAccCheckDatadogMonitorConfigUpdated = ` 367 resource "datadog_monitor" "foo" { 368 name = "name for monitor bar" 369 type = "metric alert" 370 message = "a different message Notify: @hipchat-channel" 371 escalation_message = "the situation has escalated @pagerduty" 372 373 query = "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3" 374 375 thresholds { 376 ok = "0.0" 377 warning = "1.0" 378 critical = "3.0" 379 } 380 381 notify_no_data = true 382 no_data_timeframe = 20 383 renotify_interval = 40 384 escalation_message = "the situation has escalated! @pagerduty" 385 notify_audit = true 386 timeout_h = 70 387 include_tags = false 388 require_full_window = false 389 locked = true 390 silenced { 391 "*" = 0 392 } 393 tags = ["baz:qux", "quux"] 394 } 395 ` 396 397 const testAccCheckDatadogMonitorConfigWhitespace = ` 398 resource "datadog_monitor" "foo" { 399 name = "name for monitor foo" 400 type = "metric alert" 401 message = <<EOF 402 some message Notify: @hipchat-channel 403 EOF 404 escalation_message = <<EOF 405 the situation has escalated @pagerduty 406 EOF 407 query = <<EOF 408 avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2 409 EOF 410 thresholds { 411 ok = "0.0" 412 warning = "1.0" 413 critical = "2.0" 414 } 415 416 notify_no_data = false 417 renotify_interval = 60 418 419 notify_audit = false 420 timeout_h = 60 421 include_tags = true 422 } 423 ` 424 425 func destroyHelper(s *terraform.State, client *datadog.Client) error { 426 for _, r := range s.RootModule().Resources { 427 i, _ := strconv.Atoi(r.Primary.ID) 428 if _, err := client.GetMonitor(i); err != nil { 429 if strings.Contains(err.Error(), "404 Not Found") { 430 continue 431 } 432 return fmt.Errorf("Received an error retrieving monitor %s", err) 433 } 434 return fmt.Errorf("Monitor still exists") 435 } 436 return nil 437 } 438 439 func existsHelper(s *terraform.State, client *datadog.Client) error { 440 for _, r := range s.RootModule().Resources { 441 i, _ := strconv.Atoi(r.Primary.ID) 442 if _, err := client.GetMonitor(i); err != nil { 443 return fmt.Errorf("Received an error retrieving monitor %s", err) 444 } 445 } 446 return nil 447 }