diff --git a/helper/schema/resource_diff.go b/helper/schema/resource_diff.go index 30fd741ab3..7b44f5bbb1 100644 --- a/helper/schema/resource_diff.go +++ b/helper/schema/resource_diff.go @@ -112,6 +112,12 @@ type ResourceDiff struct { // A writer that writes overridden new fields. newWriter *newValueWriter + + // Tracks which keys have been updated by SetNew, SetNewComputed, and SetDiff + // to ensure that the diff does not get re-run on keys that were not touched, + // or diffs that were just removed (re-running on the latter would just roll + // back the removal). + updatedKeys map[string]bool } // newResourceDiff creates a new ResourceDiff instance. @@ -180,9 +186,21 @@ func newResourceDiff(schema map[string]*Schema, config *terraform.ResourceConfig Readers: readers, } + d.updatedKeys = make(map[string]bool) + return d } +// UpdatedKeys returns the keys that were updated by SetNew, SetNewComputed, or +// SetDiff. These are the only keys that ad iff should be re-calculated for. +func (d *ResourceDiff) UpdatedKeys() []string { + s := make([]string, 0) + for k := range d.updatedKeys { + s = append(s, k) + } + return s +} + // ClearAll wipes the current diff. This cannot be undone - use only if you // need to create a whole new diff from scatch, such as when you are leaning on // the provider completely to create the diff. @@ -270,6 +288,8 @@ func (d *ResourceDiff) SetDiff(key string, old, new interface{}, computed bool) return fmt.Errorf("Cannot set new diff value for key %s: %s", key, err) } + d.updatedKeys[key] = true + return nil }