github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/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.ok", "0"), 38 resource.TestCheckResourceAttr( 39 "datadog_monitor.foo", "thresholds.warning", "1"), 40 resource.TestCheckResourceAttr( 41 "datadog_monitor.foo", "thresholds.critical", "2"), 42 resource.TestCheckResourceAttr( 43 "datadog_monitor.foo", "require_full_window", "true"), 44 resource.TestCheckResourceAttr( 45 "datadog_monitor.foo", "locked", "false"), 46 ), 47 }, 48 }, 49 }) 50 } 51 52 func TestAccDatadogMonitor_Updated(t *testing.T) { 53 resource.Test(t, resource.TestCase{ 54 PreCheck: func() { testAccPreCheck(t) }, 55 Providers: testAccProviders, 56 CheckDestroy: testAccCheckDatadogMonitorDestroy, 57 Steps: []resource.TestStep{ 58 resource.TestStep{ 59 Config: testAccCheckDatadogMonitorConfig, 60 Check: resource.ComposeTestCheckFunc( 61 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 62 resource.TestCheckResourceAttr( 63 "datadog_monitor.foo", "name", "name for monitor foo"), 64 resource.TestCheckResourceAttr( 65 "datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"), 66 resource.TestCheckResourceAttr( 67 "datadog_monitor.foo", "escalation_message", "the situation has escalated @pagerduty"), 68 resource.TestCheckResourceAttr( 69 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"), 70 resource.TestCheckResourceAttr( 71 "datadog_monitor.foo", "type", "metric alert"), 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", "thresholds.ok", "0"), 78 resource.TestCheckResourceAttr( 79 "datadog_monitor.foo", "thresholds.warning", "1"), 80 resource.TestCheckResourceAttr( 81 "datadog_monitor.foo", "thresholds.critical", "2"), 82 resource.TestCheckResourceAttr( 83 "datadog_monitor.foo", "notify_audit", "false"), 84 resource.TestCheckResourceAttr( 85 "datadog_monitor.foo", "timeout_h", "60"), 86 resource.TestCheckResourceAttr( 87 "datadog_monitor.foo", "include_tags", "true"), 88 resource.TestCheckResourceAttr( 89 "datadog_monitor.foo", "require_full_window", "true"), 90 resource.TestCheckResourceAttr( 91 "datadog_monitor.foo", "locked", "false"), 92 ), 93 }, 94 resource.TestStep{ 95 Config: testAccCheckDatadogMonitorConfigUpdated, 96 Check: resource.ComposeTestCheckFunc( 97 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 98 resource.TestCheckResourceAttr( 99 "datadog_monitor.foo", "name", "name for monitor bar"), 100 resource.TestCheckResourceAttr( 101 "datadog_monitor.foo", "message", "a different message Notify: @hipchat-channel"), 102 resource.TestCheckResourceAttr( 103 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3"), 104 resource.TestCheckResourceAttr( 105 "datadog_monitor.foo", "escalation_message", "the situation has escalated! @pagerduty"), 106 resource.TestCheckResourceAttr( 107 "datadog_monitor.foo", "type", "metric alert"), 108 resource.TestCheckResourceAttr( 109 "datadog_monitor.foo", "notify_no_data", "true"), 110 resource.TestCheckResourceAttr( 111 "datadog_monitor.foo", "no_data_timeframe", "20"), 112 resource.TestCheckResourceAttr( 113 "datadog_monitor.foo", "renotify_interval", "40"), 114 resource.TestCheckResourceAttr( 115 "datadog_monitor.foo", "thresholds.ok", "0"), 116 resource.TestCheckResourceAttr( 117 "datadog_monitor.foo", "thresholds.warning", "1"), 118 resource.TestCheckResourceAttr( 119 "datadog_monitor.foo", "thresholds.critical", "3"), 120 resource.TestCheckResourceAttr( 121 "datadog_monitor.foo", "notify_audit", "true"), 122 resource.TestCheckResourceAttr( 123 "datadog_monitor.foo", "timeout_h", "70"), 124 resource.TestCheckResourceAttr( 125 "datadog_monitor.foo", "include_tags", "false"), 126 resource.TestCheckResourceAttr( 127 "datadog_monitor.foo", "silenced.*", "0"), 128 resource.TestCheckResourceAttr( 129 "datadog_monitor.foo", "require_full_window", "false"), 130 resource.TestCheckResourceAttr( 131 "datadog_monitor.foo", "locked", "true"), 132 ), 133 }, 134 }, 135 }) 136 } 137 138 func TestAccDatadogMonitor_TrimWhitespace(t *testing.T) { 139 resource.Test(t, resource.TestCase{ 140 PreCheck: func() { testAccPreCheck(t) }, 141 Providers: testAccProviders, 142 CheckDestroy: testAccCheckDatadogMonitorDestroy, 143 Steps: []resource.TestStep{ 144 resource.TestStep{ 145 Config: testAccCheckDatadogMonitorConfigWhitespace, 146 Check: resource.ComposeTestCheckFunc( 147 testAccCheckDatadogMonitorExists("datadog_monitor.foo"), 148 resource.TestCheckResourceAttr( 149 "datadog_monitor.foo", "name", "name for monitor foo"), 150 resource.TestCheckResourceAttr( 151 "datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"), 152 resource.TestCheckResourceAttr( 153 "datadog_monitor.foo", "type", "metric alert"), 154 resource.TestCheckResourceAttr( 155 "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"), 156 resource.TestCheckResourceAttr( 157 "datadog_monitor.foo", "notify_no_data", "false"), 158 resource.TestCheckResourceAttr( 159 "datadog_monitor.foo", "renotify_interval", "60"), 160 resource.TestCheckResourceAttr( 161 "datadog_monitor.foo", "thresholds.ok", "0"), 162 resource.TestCheckResourceAttr( 163 "datadog_monitor.foo", "thresholds.warning", "1"), 164 resource.TestCheckResourceAttr( 165 "datadog_monitor.foo", "thresholds.critical", "2"), 166 ), 167 }, 168 }, 169 }) 170 } 171 172 func testAccCheckDatadogMonitorDestroy(s *terraform.State) error { 173 client := testAccProvider.Meta().(*datadog.Client) 174 175 if err := destroyHelper(s, client); err != nil { 176 return err 177 } 178 return nil 179 } 180 181 func testAccCheckDatadogMonitorExists(n string) resource.TestCheckFunc { 182 return func(s *terraform.State) error { 183 client := testAccProvider.Meta().(*datadog.Client) 184 if err := existsHelper(s, client); err != nil { 185 return err 186 } 187 return nil 188 } 189 } 190 191 const testAccCheckDatadogMonitorConfig = ` 192 resource "datadog_monitor" "foo" { 193 name = "name for monitor foo" 194 type = "metric alert" 195 message = "some message Notify: @hipchat-channel" 196 escalation_message = "the situation has escalated @pagerduty" 197 198 query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2" 199 200 thresholds { 201 ok = 0 202 warning = 1 203 critical = 2 204 } 205 206 notify_no_data = false 207 renotify_interval = 60 208 209 notify_audit = false 210 timeout_h = 60 211 include_tags = true 212 require_full_window = true 213 locked = false 214 } 215 ` 216 217 const testAccCheckDatadogMonitorConfigUpdated = ` 218 resource "datadog_monitor" "foo" { 219 name = "name for monitor bar" 220 type = "metric alert" 221 message = "a different message Notify: @hipchat-channel" 222 escalation_message = "the situation has escalated @pagerduty" 223 224 query = "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3" 225 226 thresholds { 227 ok = 0 228 warning = 1 229 critical = 3 230 } 231 232 notify_no_data = true 233 no_data_timeframe = 20 234 renotify_interval = 40 235 escalation_message = "the situation has escalated! @pagerduty" 236 notify_audit = true 237 timeout_h = 70 238 include_tags = false 239 require_full_window = false 240 locked = true 241 silenced { 242 "*" = 0 243 } 244 } 245 ` 246 247 const testAccCheckDatadogMonitorConfigWhitespace = ` 248 resource "datadog_monitor" "foo" { 249 name = "name for monitor foo" 250 type = "metric alert" 251 message = <<EOF 252 some message Notify: @hipchat-channel 253 EOF 254 escalation_message = <<EOF 255 the situation has escalated @pagerduty 256 EOF 257 query = <<EOF 258 avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2 259 EOF 260 thresholds { 261 ok = 0 262 warning = 1 263 critical = 2 264 } 265 266 notify_no_data = false 267 renotify_interval = 60 268 269 notify_audit = false 270 timeout_h = 60 271 include_tags = true 272 } 273 ` 274 275 func destroyHelper(s *terraform.State, client *datadog.Client) error { 276 for _, r := range s.RootModule().Resources { 277 i, _ := strconv.Atoi(r.Primary.ID) 278 if _, err := client.GetMonitor(i); err != nil { 279 if strings.Contains(err.Error(), "404 Not Found") { 280 continue 281 } 282 return fmt.Errorf("Received an error retrieving monitor %s", err) 283 } 284 return fmt.Errorf("Monitor still exists") 285 } 286 return nil 287 } 288 289 func existsHelper(s *terraform.State, client *datadog.Client) error { 290 for _, r := range s.RootModule().Resources { 291 i, _ := strconv.Atoi(r.Primary.ID) 292 if _, err := client.GetMonitor(i); err != nil { 293 return fmt.Errorf("Received an error retrieving monitor %s", err) 294 } 295 } 296 return nil 297 }