Skip to content

Commit

Permalink
fix: make body_template idempotent on whitespace changes
Browse files Browse the repository at this point in the history
HCL is a superset of json. When providing json data through a heredoc,
terraform interprets it as an actual datastructure (maps / lists, etc)
and jsonencodes it itself.

Providing data like that will result in terraform always showing
whitespace changes like this:

```
~ body_template     = jsonencode( # whitespace changes
      {
          text = "This is a sample json"
      }
  )
```

Use a DiffSuppressFunc to only show changes when there are semantic
differences.
  • Loading branch information
Ikke authored and fbreckle committed Nov 30, 2023
1 parent 80b2b9e commit 3f6ce8f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions netbox/resource_netbox_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func resourceNetboxWebhook() *schema.Resource {
"body_template": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
equal, _ := jsonSemanticCompare(oldValue, newValue)
return equal
},
DiffSuppressOnRefresh: true,
},
"http_method": {
Type: schema.TypeString,
Expand Down
8 changes: 6 additions & 2 deletions netbox/resource_netbox_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ resource "netbox_webhook" "test" {
trigger_on_delete = "%s"
payload_url = "%s"
content_types = ["%s"]
body_template = "{\"text\": \"This is a sample json\"}"
body_template = <<-EOT
{"text": "This is a sample json"}
EOT
http_method = "%s"
http_content_type = "%s"
}`, testName, testEnabled, triggerOnCreate, triggerOnUpdate, triggerOnDelete, testPayloadURL, testContentType, testHTTPMethod, testHTTPContentType),
Expand Down Expand Up @@ -115,7 +117,9 @@ resource "netbox_webhook" "test" {
trigger_on_delete = "%s"
payload_url = "%s"
content_types = ["%s", "%s"]
body_template = "{\"text\": \"This is a sample json\"}"
body_template = <<-EOT
{"text": "This is a sample json"}
EOT
}`, testName, testEnabled, triggerOnCreate, triggerOnUpdate, triggerOnDelete, testPayloadURL, testContentType, testContentType1),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_webhook.test", "name", testName+"_updated"),
Expand Down

0 comments on commit 3f6ce8f

Please sign in to comment.