Skip to content

Commit

Permalink
helper/schema: Add Computed to ResourceDiff, expose GetOkExists
Browse files Browse the repository at this point in the history
This adds a new method to ResourceDiff: Computed, which exposes the
computed read result field to ResourceDiff. In the context of
customizing the diff, this is important as interpolated and otherwise
computed values will show up in the diff as blank, with no way of
determining if the value is actually blank or if it's a computed value
not available at diff customization time. Currently assumptions need to
be made on this, but this does not help in validation scenarios where
one needs to differentiate between an actual blank value and a value
that will be available later.

This is exposed for the most part via NewComputed in the diff, but the
tests cover both the config reader as well (with no diff, even though
this should not come up in normal operation) and also the newDiff reader
when someone sets a new value using SetNew and SetNewComputed.

This commit also exposes GetOkExists. The tests were mostly pulled from
ResourceData but a few were added to ensure that config was being
properly covered as well, in addition to covering SetNew and
SetNewComputed.
  • Loading branch information
vancluever committed Mar 29, 2018
1 parent dfa6232 commit 274b933
Show file tree
Hide file tree
Showing 2 changed files with 777 additions and 0 deletions.
19 changes: 19 additions & 0 deletions helper/schema/resource_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 378,25 @@ func (d *ResourceDiff) GetOk(key string) (interface{}, bool) {
return r.Value, exists
}

// GetOkExists functions the same way as GetOkExists within ResourceData, but
// it also checks the new diff levels to provide data consistent with the
// current state of the customized diff.
func (d *ResourceDiff) GetOkExists(key string) (interface{}, bool) {
r := d.get(strings.Split(key, "."), "newDiff")
exists := r.Exists && !r.Computed
return r.Value, exists
}

// Computed exposes the computed value of a field read result to the caller.
// It's a function unique to ResourceDiff and allows users to check to see if a
// value is currently computed in the collective diff readers. No actual value
// is returned here as computed values are always blank until they are properly
// evaluated in the graph.
func (d *ResourceDiff) Computed(key string) bool {
r := d.get(strings.Split(key, "."), "newDiff")
return r.Computed
}

// HasChange checks to see if there is a change between state and the diff, or
// in the overridden diff.
func (d *ResourceDiff) HasChange(key string) bool {
Expand Down
Loading

0 comments on commit 274b933

Please sign in to comment.