mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-02 12:17:39 -06:00
helper/schema: Computed -> NewValueKnown, add comments to GetOkExists
Added some more context to GetOkExists, moved Computed to NewValueKnown to accommodate some changes that will be coming up in HCL2 that may make "Computed" less intuitive of a function name, and updated the docs for NewValueKnown as well.
This commit is contained in:
parent
274b933077
commit
fdfe89ed1d
@ -381,20 +381,24 @@ func (d *ResourceDiff) GetOk(key string) (interface{}, bool) {
|
||||
// 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.
|
||||
//
|
||||
// This is nearly the same function as GetOk, yet it does not check
|
||||
// for the zero value of the attribute's type. This allows for attributes
|
||||
// without a default, to fully check for a literal assignment, regardless
|
||||
// of the zero-value for that type.
|
||||
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 {
|
||||
// NewValueKnown returns true if the new value for the given key is available
|
||||
// as its final value at diff time. If the return value is false, this means
|
||||
// either the value is based of interpolation that was unavailable at diff
|
||||
// time, or that the value was explicitly marked as computed by SetNewComputed.
|
||||
func (d *ResourceDiff) NewValueKnown(key string) bool {
|
||||
r := d.get(strings.Split(key, "."), "newDiff")
|
||||
return r.Computed
|
||||
return !r.Computed
|
||||
}
|
||||
|
||||
// HasChange checks to see if there is a change between state and the diff, or
|
||||
|
@ -1498,7 +1498,7 @@ func TestResourceDiffGetOkExistsSetNewComputed(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceDiffComputed(t *testing.T) {
|
||||
func TestResourceDiffNewValueKnown(t *testing.T) {
|
||||
cases := []struct {
|
||||
Name string
|
||||
Schema map[string]*Schema
|
||||
@ -1529,7 +1529,7 @@ func TestResourceDiffComputed(t *testing.T) {
|
||||
},
|
||||
},
|
||||
Key: "availability_zone",
|
||||
Expected: false,
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
Name: "in config, has state, no diff",
|
||||
@ -1551,7 +1551,7 @@ func TestResourceDiffComputed(t *testing.T) {
|
||||
Attributes: map[string]*terraform.ResourceAttrDiff{},
|
||||
},
|
||||
Key: "availability_zone",
|
||||
Expected: false,
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
Name: "computed attribute, in state, no diff",
|
||||
@ -1571,7 +1571,7 @@ func TestResourceDiffComputed(t *testing.T) {
|
||||
Attributes: map[string]*terraform.ResourceAttrDiff{},
|
||||
},
|
||||
Key: "availability_zone",
|
||||
Expected: false,
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
Name: "optional and computed attribute, in state, no config",
|
||||
@ -1592,7 +1592,7 @@ func TestResourceDiffComputed(t *testing.T) {
|
||||
Attributes: map[string]*terraform.ResourceAttrDiff{},
|
||||
},
|
||||
Key: "availability_zone",
|
||||
Expected: false,
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
Name: "optional and computed attribute, in state, with config",
|
||||
@ -1615,7 +1615,7 @@ func TestResourceDiffComputed(t *testing.T) {
|
||||
Attributes: map[string]*terraform.ResourceAttrDiff{},
|
||||
},
|
||||
Key: "availability_zone",
|
||||
Expected: false,
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
Name: "computed value, through config reader",
|
||||
@ -1646,7 +1646,7 @@ func TestResourceDiffComputed(t *testing.T) {
|
||||
Attributes: map[string]*terraform.ResourceAttrDiff{},
|
||||
},
|
||||
Key: "availability_zone",
|
||||
Expected: true,
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
Name: "computed value, through diff reader",
|
||||
@ -1683,7 +1683,7 @@ func TestResourceDiffComputed(t *testing.T) {
|
||||
},
|
||||
},
|
||||
Key: "availability_zone",
|
||||
Expected: true,
|
||||
Expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
@ -1691,7 +1691,7 @@ func TestResourceDiffComputed(t *testing.T) {
|
||||
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
|
||||
d := newResourceDiff(tc.Schema, tc.Config, tc.State, tc.Diff)
|
||||
|
||||
actual := d.Computed(tc.Key)
|
||||
actual := d.NewValueKnown(tc.Key)
|
||||
if tc.Expected != actual {
|
||||
t.Fatalf("%s: expected ok: %t, got: %t", tc.Name, tc.Expected, actual)
|
||||
}
|
||||
@ -1699,7 +1699,7 @@ func TestResourceDiffComputed(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceDiffComputedSetNew(t *testing.T) {
|
||||
func TestResourceDiffNewValueKnownSetNew(t *testing.T) {
|
||||
tc := struct {
|
||||
Schema map[string]*Schema
|
||||
State *terraform.InstanceState
|
||||
@ -1744,19 +1744,19 @@ func TestResourceDiffComputedSetNew(t *testing.T) {
|
||||
},
|
||||
Key: "availability_zone",
|
||||
Value: "bar",
|
||||
Expected: false,
|
||||
Expected: true,
|
||||
}
|
||||
|
||||
d := newResourceDiff(tc.Schema, tc.Config, tc.State, tc.Diff)
|
||||
d.SetNew(tc.Key, tc.Value)
|
||||
|
||||
actual := d.Computed(tc.Key)
|
||||
actual := d.NewValueKnown(tc.Key)
|
||||
if tc.Expected != actual {
|
||||
t.Fatalf("expected ok: %t, got: %t", tc.Expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceDiffComputedSetNewComputed(t *testing.T) {
|
||||
func TestResourceDiffNewValueKnownSetNewComputed(t *testing.T) {
|
||||
tc := struct {
|
||||
Schema map[string]*Schema
|
||||
State *terraform.InstanceState
|
||||
@ -1781,13 +1781,13 @@ func TestResourceDiffComputedSetNewComputed(t *testing.T) {
|
||||
Attributes: map[string]*terraform.ResourceAttrDiff{},
|
||||
},
|
||||
Key: "availability_zone",
|
||||
Expected: true,
|
||||
Expected: false,
|
||||
}
|
||||
|
||||
d := newResourceDiff(tc.Schema, tc.Config, tc.State, tc.Diff)
|
||||
d.SetNewComputed(tc.Key)
|
||||
|
||||
actual := d.Computed(tc.Key)
|
||||
actual := d.NewValueKnown(tc.Key)
|
||||
if tc.Expected != actual {
|
||||
t.Fatalf("expected ok: %t, got: %t", tc.Expected, actual)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user