Skip to content

Commit

Permalink
Restore support for empty prefix in 'ResourceDiff.GetChangedKeysPrefix'.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit authored and bflad committed Dec 17, 2021
1 parent 7e0a333 commit 9475a53
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
8 changes: 4 additions & 4 deletions helper/schema/resource_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ func (d *ResourceDiff) clear(key string) error {
return nil
}

// GetChangedKeysPrefix helps to implement Resource.CustomizeDiff
// where we need to act on all nested fields
// without calling out each one separately
// GetChangedKeysPrefix helps to implement Resource.CustomizeDiff where we need to act
// on all nested fields without calling out each one separately.
// An empty prefix is supported, returning all changed keys.
func (d *ResourceDiff) GetChangedKeysPrefix(prefix string) []string {
keys := make([]string, 0)
for k := range d.diff.Attributes {
if k == prefix || childAddrOf(k, prefix) {
if k == prefix || childAddrOf(k, prefix) || prefix == "" {
keys = append(keys, k)
}
}
Expand Down
30 changes: 30 additions & 0 deletions helper/schema/resource_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,36 @@ func TestGetChangedKeysPrefix(t *testing.T) {
"foo",
},
},
{
Name: "basic primitive diff with empty prefix",
Schema: map[string]*Schema{
"foo": {
Type: TypeString,
Optional: true,
Computed: true,
},
},
State: &terraform.InstanceState{
Attributes: map[string]string{
"foo": "bar",
},
},
Config: testConfig(t, map[string]interface{}{
"foo": "baz",
}),
Diff: &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"foo": {
Old: "bar",
New: "baz",
},
},
},
Key: "",
ExpectedKeys: []string{
"foo",
},
},
{
Name: "nested field filtering",
Schema: map[string]*Schema{
Expand Down

0 comments on commit 9475a53

Please sign in to comment.