Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make datadog message, escalation_message, and query work with heredoc #5788

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion builtin/providers/datadog/resource_datadog_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
62 changes: 62 additions & 0 deletions builtin/providers/datadog/resource_datadog_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -191,6 +225,34 @@ resource "datadog_monitor" "foo" {
}
`

const testAccCheckDatadogMonitorConfigWhitespace = `
resource "datadog_monitor" "foo" {
name = "name for monitor foo"
type = "metric alert"
message = <<EOF
some message Notify: @hipchat-channel
EOF
escalation_message = <<EOF
the situation has escalated @pagerduty
EOF
query = <<EOF
avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 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)
Expand Down