Skip to content

Commit

Permalink
Avoid resource_control drift due to whitespace in queries (#201)
Browse files Browse the repository at this point in the history
As reported in hashicorp/terraform#23928, reading a string from a file
and comparing it to the terraform state may produce a drift due to
inconsistencies (ie. Linux vs Windows line endings). This PR suppresses
any drift caused by differences between the desired state and the stored
state, by unmarshalling the JSONs and comparing the resulting objects.

Co-authored-by: Simon Aquino <[email protected]>
  • Loading branch information
queeno and Simon Aquino authored Feb 15, 2024
1 parent 545b4e0 commit 0a2ca3b
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/provider/resource_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"reflect"

"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -68,6 +69,12 @@ func resourceWizControl() *schema.Resource {
ValidateDiagFunc: validation.ToDiagFunc(
validation.StringIsJSON,
),
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
var ov, nv interface{}
_ = json.Unmarshal([]byte(oldValue), &ov)
_ = json.Unmarshal([]byte(newValue), &nv)
return reflect.DeepEqual(ov, nv)
},
},
"scope_query": {
Type: schema.TypeString,
Expand All @@ -76,6 +83,12 @@ func resourceWizControl() *schema.Resource {
ValidateDiagFunc: validation.ToDiagFunc(
validation.StringIsJSON,
),
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
var ov, nv interface{}
_ = json.Unmarshal([]byte(oldValue), &ov)
_ = json.Unmarshal([]byte(newValue), &nv)
return reflect.DeepEqual(ov, nv)
},
},
"severity": {
Type: schema.TypeString,
Expand Down

0 comments on commit 0a2ca3b

Please sign in to comment.