Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper/schema: Introduce ResourceData type HasChangeExcept and HasChangesExcept receiver methods #558

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions helper/schema/resource_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ func (d *ResourceData) HasChanges(keys ...string) bool {
return false
}

// HasChangesExcept returns whether any keys outside the given keys have been changed.
//
// This function only works with root attribute keys.
func (d *ResourceData) HasChangesExcept(keys ...string) bool {
for attr := range d.diff.Attributes {
rootAttr := strings.Split(attr, ".")[0]
var skipAttr bool

for _, key := range keys {
if rootAttr == key {
skipAttr = true
break
}
}

if !skipAttr && d.HasChange(rootAttr) {
return true
}
}

return false
}

// HasChange returns whether or not the given key has been changed.
func (d *ResourceData) HasChange(key string) bool {
o, n := d.GetChange(key)
Expand All @@ -142,6 +165,25 @@ func (d *ResourceData) HasChange(key string) bool {
return !reflect.DeepEqual(o, n)
}

// HasChangeExcept returns whether any keys outside the given key have been changed.
//
// This function only works with root attribute keys.
func (d *ResourceData) HasChangeExcept(key string) bool {
for attr := range d.diff.Attributes {
rootAttr := strings.Split(attr, ".")[0]

if rootAttr == key {
continue
}

if d.HasChange(rootAttr) {
return true
}
}

return false
}

// Partial is a legacy function that was used for capturing state of specific
// attributes if an update only partially worked. Enabling this flag without
// setting any specific keys with the now removed SetPartial has a useful side
Expand Down
Loading