From 4fcb90814f867c838abc2b8a87b3fc80007bf198 Mon Sep 17 00:00:00 2001 From: Matthew Brewer Date: Tue, 22 Mar 2016 14:47:08 -0400 Subject: [PATCH] Make datadog message, escalation_message, and query work with heredoc --- .../datadog/resource_datadog_monitor.go | 11 +++- .../datadog/resource_datadog_monitor_test.go | 62 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/builtin/providers/datadog/resource_datadog_monitor.go b/builtin/providers/datadog/resource_datadog_monitor.go index af2b89f53e3c..011bef4d90cb 100644 --- a/builtin/providers/datadog/resource_datadog_monitor.go +++ b/builtin/providers/datadog/resource_datadog_monitor.go @@ -27,14 +27,23 @@ func resourceDatadogMonitor() *schema.Resource { "message": &schema.Schema{ Type: schema.TypeString, Required: true, + StateFunc: func(val interface{}) string { + return strings.TrimSpace(val.(string)) + }, }, "escalation_message": &schema.Schema{ Type: schema.TypeString, Optional: true, + StateFunc: func(val interface{}) string { + return strings.TrimSpace(val.(string)) + }, }, "query": &schema.Schema{ Type: schema.TypeString, Required: true, + StateFunc: func(val interface{}) string { + return strings.TrimSpace(val.(string)) + }, }, "type": &schema.Schema{ Type: schema.TypeString, @@ -296,7 +305,7 @@ func resourceDatadogMonitorUpdate(d *schema.ResourceData, meta interface{}) erro m.Options = o if err = client.UpdateMonitor(m); err != nil { - return fmt.Errorf("error updating montor: %s", err.Error()) + return fmt.Errorf("error updating monitor: %s", err.Error()) } return resourceDatadogMonitorRead(d, meta) diff --git a/builtin/providers/datadog/resource_datadog_monitor_test.go b/builtin/providers/datadog/resource_datadog_monitor_test.go index f91019d410b5..94a8d3e4a037 100644 --- a/builtin/providers/datadog/resource_datadog_monitor_test.go +++ b/builtin/providers/datadog/resource_datadog_monitor_test.go @@ -121,6 +121,40 @@ func TestAccDatadogMonitor_Updated(t *testing.T) { }) } +func TestAccDatadogMonitor_TrimWhitespace(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckDatadogMonitorDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckDatadogMonitorConfigWhitespace, + Check: resource.ComposeTestCheckFunc( + testAccCheckDatadogMonitorExists("datadog_monitor.foo"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "name", "name for monitor foo"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "type", "metric alert"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "notify_no_data", "false"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "renotify_interval", "60"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "thresholds.ok", "0"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "thresholds.warning", "1"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "thresholds.critical", "2"), + ), + }, + }, + }) +} + func testAccCheckDatadogMonitorDestroy(s *terraform.State) error { client := testAccProvider.Meta().(*datadog.Client) @@ -191,6 +225,34 @@ resource "datadog_monitor" "foo" { } ` +const testAccCheckDatadogMonitorConfigWhitespace = ` +resource "datadog_monitor" "foo" { + name = "name for monitor foo" + type = "metric alert" + message = < 2 +EOF + thresholds { + ok = 0 + warning = 1 + critical = 2 + } + + notify_no_data = false + renotify_interval = 60 + + notify_audit = false + timeout_h = 60 + include_tags = true +} +` + func destroyHelper(s *terraform.State, client *datadog.Client) error { for _, r := range s.RootModule().Resources { i, _ := strconv.Atoi(r.Primary.ID)