diff --git a/builtin/providers/datadog/resource_datadog_monitor.go b/builtin/providers/datadog/resource_datadog_monitor.go index 5d686ea093..4d11df308c 100644 --- a/builtin/providers/datadog/resource_datadog_monitor.go +++ b/builtin/providers/datadog/resource_datadog_monitor.go @@ -73,6 +73,7 @@ func resourceDatadogMonitor() *schema.Resource { }, }, }, + DiffSuppressFunc: supressDataDogFloatIntDiff, }, "notify_no_data": &schema.Schema{ Type: schema.TypeBool, @@ -398,3 +399,26 @@ func resourceDatadogImport(d *schema.ResourceData, meta interface{}) ([]*schema. } return []*schema.ResourceData{d}, nil } + +// Ignore any diff that results from the mix of ints or floats returned from the +// DataDog API. +func supressDataDogFloatIntDiff(k, old, new string, d *schema.ResourceData) bool { + oF, err := strconv.ParseFloat(old, 64) + if err != nil { + log.Printf("Error parsing float of old value (%s): %s", old, err) + return false + } + + nF, err := strconv.ParseFloat(new, 64) + if err != nil { + log.Printf("Error parsing float of new value (%s): %s", new, err) + return false + } + + // if the float values of these attributes are equivalent, ignore this + // diff + if oF == nF { + return true + } + return false +} diff --git a/builtin/providers/datadog/resource_datadog_monitor_test.go b/builtin/providers/datadog/resource_datadog_monitor_test.go index f9869828e4..29eda08d9a 100644 --- a/builtin/providers/datadog/resource_datadog_monitor_test.go +++ b/builtin/providers/datadog/resource_datadog_monitor_test.go @@ -177,6 +177,37 @@ func TestAccDatadogMonitor_TrimWhitespace(t *testing.T) { }) } +func TestAccDatadogMonitor_Basic_float_int(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckDatadogMonitorDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckDatadogMonitorConfig_ints, + Check: resource.ComposeTestCheckFunc( + testAccCheckDatadogMonitorExists("datadog_monitor.foo"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "thresholds.warning", "1"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "thresholds.critical", "2"), + ), + }, + + resource.TestStep{ + Config: testAccCheckDatadogMonitorConfig_ints_mixed, + Check: resource.ComposeTestCheckFunc( + testAccCheckDatadogMonitorExists("datadog_monitor.foo"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "thresholds.warning", "1.0"), + resource.TestCheckResourceAttr( + "datadog_monitor.foo", "thresholds.critical", "3.0"), + ), + }, + }, + }) +} + func testAccCheckDatadogMonitorDestroy(s *terraform.State) error { client := testAccProvider.Meta().(*datadog.Client) @@ -225,6 +256,66 @@ resource "datadog_monitor" "foo" { } ` +const testAccCheckDatadogMonitorConfig_ints = ` +resource "datadog_monitor" "foo" { + name = "name for monitor foo" + type = "metric alert" + message = "some message Notify: @hipchat-channel" + escalation_message = "the situation has escalated @pagerduty" + + query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2" + + thresholds { + warning = 1 + critical = 2 + } + + notify_no_data = false + renotify_interval = 60 + + notify_audit = false + timeout_h = 60 + include_tags = true + require_full_window = true + locked = false + + tags { + "foo" = "bar" + "bar" = "baz" + } +} +` + +const testAccCheckDatadogMonitorConfig_ints_mixed = ` +resource "datadog_monitor" "foo" { + name = "name for monitor foo" + type = "metric alert" + message = "some message Notify: @hipchat-channel" + escalation_message = "the situation has escalated @pagerduty" + + query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 3" + + thresholds { + warning = 1 + critical = 3.0 + } + + notify_no_data = false + renotify_interval = 60 + + notify_audit = false + timeout_h = 60 + include_tags = true + require_full_window = true + locked = false + + tags { + "foo" = "bar" + "bar" = "baz" + } +} +` + const testAccCheckDatadogMonitorConfigUpdated = ` resource "datadog_monitor" "foo" { name = "name for monitor bar"