generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add YamlDiffSuppressFunction to avoid unecessary changes in plan (#794)…
… (#808) * Add YamlDiffSuppressFunction to avoid unecessary changes in plan * Add YamlDiffSuppressFunction tests Co-authored-by: Aleksei Larkov <[email protected]>
- Loading branch information
1 parent
92743b4
commit 7be875c
Showing
11 changed files
with
127 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package helpers | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// YamlDiffSuppressFunction returns true if two content of yaml strings are identical. | ||
// That helps to avoid unnecessary changes in plan if the yaml format was changed only, but not the data. | ||
func YamlDiffSuppressFunction(k, old, new string, d *schema.ResourceData) bool { | ||
var oldYaml, newYaml interface{} | ||
if err := yaml.Unmarshal([]byte(old), &oldYaml); err != nil { | ||
return false | ||
} | ||
if err := yaml.Unmarshal([]byte(new), &newYaml); err != nil { | ||
return false | ||
} | ||
return reflect.DeepEqual(oldYaml, newYaml) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package helpers_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/harness/terraform-provider-harness/helpers" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestYamlDiffSuppressFunction(t *testing.T) { | ||
require.True(t, helpers.YamlDiffSuppressFunction("", "", "", nil)) | ||
|
||
require.True(t, helpers.YamlDiffSuppressFunction("", | ||
`--- | ||
field1: value1 | ||
field2: value2 | ||
`, | ||
`--- | ||
field2: value2 | ||
field1: value1 | ||
`, nil)) | ||
|
||
require.True(t, helpers.YamlDiffSuppressFunction("", | ||
`--- | ||
"field1": "value1" | ||
"field2": "value2" | ||
`, | ||
`--- | ||
field2: value2 | ||
field1: value1 | ||
`, nil)) | ||
|
||
require.False(t, helpers.YamlDiffSuppressFunction("", | ||
`--- | ||
field1: value1 | ||
`, | ||
`--- | ||
field2: value2 | ||
`, nil)) | ||
|
||
require.False(t, helpers.YamlDiffSuppressFunction("", | ||
`--- | ||
field1: value1 | ||
`, | ||
`--- | ||
field1: value1 | ||
field2: value2 | ||
`, nil)) | ||
|
||
require.False(t, helpers.YamlDiffSuppressFunction("", | ||
`--- | ||
field1: value1 | ||
field2: value2 | ||
`, | ||
`--- | ||
field1: value1 | ||
`, nil)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters