Skip to content

Commit 4fcb908

Browse files
author
Matthew Brewer
committed
Make datadog message, escalation_message, and query work with heredoc
1 parent 539cbe2 commit 4fcb908

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

builtin/providers/datadog/resource_datadog_monitor.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,23 @@ func resourceDatadogMonitor() *schema.Resource {
2727
"message": &schema.Schema{
2828
Type: schema.TypeString,
2929
Required: true,
30+
StateFunc: func(val interface{}) string {
31+
return strings.TrimSpace(val.(string))
32+
},
3033
},
3134
"escalation_message": &schema.Schema{
3235
Type: schema.TypeString,
3336
Optional: true,
37+
StateFunc: func(val interface{}) string {
38+
return strings.TrimSpace(val.(string))
39+
},
3440
},
3541
"query": &schema.Schema{
3642
Type: schema.TypeString,
3743
Required: true,
44+
StateFunc: func(val interface{}) string {
45+
return strings.TrimSpace(val.(string))
46+
},
3847
},
3948
"type": &schema.Schema{
4049
Type: schema.TypeString,
@@ -296,7 +305,7 @@ func resourceDatadogMonitorUpdate(d *schema.ResourceData, meta interface{}) erro
296305
m.Options = o
297306

298307
if err = client.UpdateMonitor(m); err != nil {
299-
return fmt.Errorf("error updating montor: %s", err.Error())
308+
return fmt.Errorf("error updating monitor: %s", err.Error())
300309
}
301310

302311
return resourceDatadogMonitorRead(d, meta)

builtin/providers/datadog/resource_datadog_monitor_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,40 @@ func TestAccDatadogMonitor_Updated(t *testing.T) {
121121
})
122122
}
123123

124+
func TestAccDatadogMonitor_TrimWhitespace(t *testing.T) {
125+
resource.Test(t, resource.TestCase{
126+
PreCheck: func() { testAccPreCheck(t) },
127+
Providers: testAccProviders,
128+
CheckDestroy: testAccCheckDatadogMonitorDestroy,
129+
Steps: []resource.TestStep{
130+
resource.TestStep{
131+
Config: testAccCheckDatadogMonitorConfigWhitespace,
132+
Check: resource.ComposeTestCheckFunc(
133+
testAccCheckDatadogMonitorExists("datadog_monitor.foo"),
134+
resource.TestCheckResourceAttr(
135+
"datadog_monitor.foo", "name", "name for monitor foo"),
136+
resource.TestCheckResourceAttr(
137+
"datadog_monitor.foo", "message", "some message Notify: @hipchat-channel"),
138+
resource.TestCheckResourceAttr(
139+
"datadog_monitor.foo", "type", "metric alert"),
140+
resource.TestCheckResourceAttr(
141+
"datadog_monitor.foo", "query", "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"),
142+
resource.TestCheckResourceAttr(
143+
"datadog_monitor.foo", "notify_no_data", "false"),
144+
resource.TestCheckResourceAttr(
145+
"datadog_monitor.foo", "renotify_interval", "60"),
146+
resource.TestCheckResourceAttr(
147+
"datadog_monitor.foo", "thresholds.ok", "0"),
148+
resource.TestCheckResourceAttr(
149+
"datadog_monitor.foo", "thresholds.warning", "1"),
150+
resource.TestCheckResourceAttr(
151+
"datadog_monitor.foo", "thresholds.critical", "2"),
152+
),
153+
},
154+
},
155+
})
156+
}
157+
124158
func testAccCheckDatadogMonitorDestroy(s *terraform.State) error {
125159
client := testAccProvider.Meta().(*datadog.Client)
126160

@@ -191,6 +225,34 @@ resource "datadog_monitor" "foo" {
191225
}
192226
`
193227

228+
const testAccCheckDatadogMonitorConfigWhitespace = `
229+
resource "datadog_monitor" "foo" {
230+
name = "name for monitor foo"
231+
type = "metric alert"
232+
message = <<EOF
233+
some message Notify: @hipchat-channel
234+
EOF
235+
escalation_message = <<EOF
236+
the situation has escalated @pagerduty
237+
EOF
238+
query = <<EOF
239+
avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2
240+
EOF
241+
thresholds {
242+
ok = 0
243+
warning = 1
244+
critical = 2
245+
}
246+
247+
notify_no_data = false
248+
renotify_interval = 60
249+
250+
notify_audit = false
251+
timeout_h = 60
252+
include_tags = true
253+
}
254+
`
255+
194256
func destroyHelper(s *terraform.State, client *datadog.Client) error {
195257
for _, r := range s.RootModule().Resources {
196258
i, _ := strconv.Atoi(r.Primary.ID)

0 commit comments

Comments
 (0)