Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23793 from tiwood/fix/23328
DiffSuppressFunc to ignore azurerm_virtual_network.address_space reordering
- Loading branch information
Showing
3 changed files
with
98 additions
and
3 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
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,45 @@ | ||
package suppress | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func ListOrder(key, old, new string, d *schema.ResourceData) bool { | ||
// Taken from https://github.com/hashicorp/terraform-plugin-sdk/issues/477#issuecomment-1238807249 | ||
// For a list, the key is path to the element, rather than the list. | ||
// E.g. "node_groups.2.ips.0" | ||
lastDotIndex := strings.LastIndex(key, ".") | ||
if lastDotIndex != -1 { | ||
key = key[:lastDotIndex] | ||
} | ||
|
||
oldData, newData := d.GetChange(key) | ||
if oldData == nil || newData == nil { | ||
return false | ||
} | ||
|
||
sOld := make([]string, len(oldData.([]interface{}))) | ||
sNew := make([]string, len(newData.([]interface{}))) | ||
|
||
for i, v := range oldData.([]interface{}) { | ||
sOld[i] = fmt.Sprint(v) | ||
} | ||
|
||
for i, v := range newData.([]interface{}) { | ||
sNew[i] = fmt.Sprint(v) | ||
} | ||
|
||
return stringSlicesAreEqual(sOld, sNew) | ||
} | ||
|
||
func stringSlicesAreEqual(a []string, b []string) bool { | ||
sort.Strings(a) | ||
sort.Strings(b) | ||
|
||
return reflect.DeepEqual(a, b) | ||
} |
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,48 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package suppress | ||
|
||
import "testing" | ||
|
||
func TestStringSlicesAreEqual(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
SliceA []string | ||
SliceB []string | ||
Suppress bool | ||
}{ | ||
{ | ||
Name: "empty", | ||
SliceA: []string{""}, | ||
SliceB: []string{""}, | ||
Suppress: true, | ||
}, | ||
{ | ||
Name: "same", | ||
SliceA: []string{"value1", "value2"}, | ||
SliceB: []string{"value1", "value2"}, | ||
Suppress: true, | ||
}, | ||
{ | ||
Name: "different_order", | ||
SliceA: []string{"value1", "value2"}, | ||
SliceB: []string{"value2", "value1"}, | ||
Suppress: true, | ||
}, | ||
{ | ||
Name: "different_values", | ||
SliceA: []string{"value1", "value2"}, | ||
SliceB: []string{"value1", "value2", "value3"}, | ||
Suppress: false, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
if stringSlicesAreEqual(tc.SliceA, tc.SliceB) != tc.Suppress { | ||
t.Fatalf("Expected stringSlicesAreEqual to return %t for '%q' == '%q'", tc.Suppress, tc.SliceA, tc.SliceB) | ||
} | ||
}) | ||
} | ||
} |