|
7 | 7 | "fmt" |
8 | 8 | "net" |
9 | 9 | "net/http" |
| 10 | + "reflect" |
| 11 | + "sort" |
10 | 12 | "strconv" |
11 | 13 | "strings" |
12 | 14 | "testing" |
@@ -762,6 +764,39 @@ func diffSuppressFuncLocality(_, oldValue, newValue string, _ *schema.ResourceDa |
762 | 764 | return expandID(oldValue) == expandID(newValue) |
763 | 765 | } |
764 | 766 |
|
| 767 | +// diffSuppressFuncOrderDiff suppresses diffs for TypeList attributes when the only change is the order of elements. |
| 768 | +// https://github.com/hashicorp/terraform-plugin-sdk/issues/477#issuecomment-1238807249 |
| 769 | +func diffSuppressFuncOrderDiff(k, _, _ string, d *schema.ResourceData) bool { |
| 770 | + // Extract the base key path to the list attribute, ignoring the index and value parts |
| 771 | + lastDotIndex := strings.LastIndex(k, ".") |
| 772 | + baseKey := k |
| 773 | + if lastDotIndex != -1 { |
| 774 | + baseKey = k[:lastDotIndex] |
| 775 | + } |
| 776 | + |
| 777 | + oldList, newList := d.GetChange(baseKey) |
| 778 | + if oldList == nil || newList == nil { |
| 779 | + return false |
| 780 | + } |
| 781 | + |
| 782 | + oldListSlice, newListSlice := oldList.([]interface{}), newList.([]interface{}) |
| 783 | + if len(oldListSlice) != len(newListSlice) { |
| 784 | + return false // Different lengths means there's definitely a change |
| 785 | + } |
| 786 | + |
| 787 | + oldListStr, newListStr := make([]string, len(oldListSlice)), make([]string, len(newListSlice)) |
| 788 | + for i, oldItem := range oldListSlice { |
| 789 | + oldListStr[i] = fmt.Sprint(oldItem) |
| 790 | + } |
| 791 | + for j, newItem := range newListSlice { |
| 792 | + newListStr[j] = fmt.Sprint(newItem) |
| 793 | + } |
| 794 | + sort.Strings(oldListStr) |
| 795 | + sort.Strings(newListStr) |
| 796 | + |
| 797 | + return reflect.DeepEqual(oldListStr, newListStr) |
| 798 | +} |
| 799 | + |
765 | 800 | // TimedOut returns true if the error represents a "wait timed out" condition. |
766 | 801 | // Specifically, TimedOut returns true if the error matches all these conditions: |
767 | 802 | // - err is of type resource.TimeoutError |
|
0 commit comments