helper/schema: Track updated keys in ResourceDiff

This ensures that when we hook this into the main diff logic, that we
can just re-diff the keys that are modified, ensuring that the diff is
not re-run on keys that were not touched, or on keys that were
intentionally removed.
This commit is contained in:
Chris Marchesi 2017-05-25 10:03:37 -07:00 committed by Martin Atkins
parent aeb793f968
commit f5f4e0329f

View File

@ -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
}