Skip to content

Commit f8649fb

Browse files
authored
Trim whitespace on alerting_message_template.template (#611)
* Trim whitespace on alerting_message_template.template Fixes #608. Turns out that using heredoc is introduces leading and/or trailing whitespace which makes it always fail the difference. Signed-off-by: Leandro López (inkel) <[email protected]> * Add test for message_template.template trimming Signed-off-by: Leandro López (inkel) <[email protected]> * Fix linting Signed-off-by: Leandro López (inkel) <[email protected]> Signed-off-by: Leandro López (inkel) <[email protected]>
1 parent 096e2a8 commit f8649fb

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

grafana/resource_alerting_message_template.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ func ResourceMessageTemplate() *schema.Resource {
3535
Type: schema.TypeString,
3636
Required: true,
3737
Description: "The content of the message template.",
38+
StateFunc: func(v interface{}) string {
39+
return strings.TrimSpace(v.(string))
40+
},
3841
},
3942
},
4043
}

grafana/resource_alerting_message_template_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ func TestAccMessageTemplate_basic(t *testing.T) {
3535
ImportState: true,
3636
ImportStateVerify: true,
3737
},
38+
// Test update with heredoc template doesn't change
39+
{
40+
Config: testAccExampleWithReplace(t, "resources/grafana_message_template/resource.tf", map[string]string{
41+
`template = "{{define \"My Reusable Template\" }}\n template content\n{{ end }}"`: `template = <<-EOT
42+
{{define "My Reusable Template" }}
43+
template content
44+
{{ end }}
45+
EOT`,
46+
}),
47+
Check: resource.ComposeTestCheckFunc(
48+
testMessageTemplateCheckExists("grafana_message_template.my_template", &tmpl),
49+
resource.TestCheckResourceAttr("grafana_message_template.my_template", "name", "My Reusable Template"),
50+
resource.TestCheckResourceAttr("grafana_message_template.my_template", "template", "{{define \"My Reusable Template\" }}\n template content\n{{ end }}"),
51+
),
52+
},
3853
// Test update content.
3954
{
4055
Config: testAccExampleWithReplace(t, "resources/grafana_message_template/resource.tf", map[string]string{
@@ -62,6 +77,7 @@ func TestAccMessageTemplate_basic(t *testing.T) {
6277
})
6378
}
6479

80+
//nolint:unparam // `rname` always receives `"grafana_message_template.my_template"`
6581
func testMessageTemplateCheckExists(rname string, mt *gapi.AlertingMessageTemplate) resource.TestCheckFunc {
6682
return func(s *terraform.State) error {
6783
resource, ok := s.RootModule().Resources[rname]

0 commit comments

Comments
 (0)