restrict the ComputedKeys usage to containers

Computed primitive values must see the UnknownConfigValue or they are
assumed to be unchanged. Restrict the usage of the protov5 ComputedKeys
to containers.
This commit is contained in:
James Bardin 2019-05-06 19:19:10 -04:00
parent 325344beaf
commit 7075bc9a4d
3 changed files with 54 additions and 7 deletions

View File

@ -54,6 +54,11 @@ func testResource() *schema.Resource {
Computed: true, Computed: true,
ForceNew: true, ForceNew: true,
}, },
"optional_computed": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"computed_read_only": { "computed_read_only": {
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,

View File

@ -992,3 +992,40 @@ resource "test_resource" "foo" {
}, },
}) })
} }
func TestResource_replacedOptionalComputed(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_nested" "a" {
}
resource "test_resource" "foo" {
required = "yep"
required_map = {
key = "value"
}
optional_computed = test_resource_nested.a.id
}
`),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_nested" "b" {
}
resource "test_resource" "foo" {
required = "yep"
required_map = {
key = "value"
}
optional_computed = test_resource_nested.b.id
}
`),
},
},
})
}

View File

@ -2,6 +2,7 @@ package schema
import ( import (
"fmt" "fmt"
"log"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -94,13 +95,17 @@ func (r *ConfigFieldReader) readField(
} }
if protoVersion5 { if protoVersion5 {
// Check if the value itself is unknown. switch schema.Type {
// The new protocol shims will add unknown values to this list of case TypeList, TypeSet, TypeMap, typeObject:
// ComputedKeys. THis is the only way we have to indicate that a // Check if the value itself is unknown.
// collection is unknown in the config // The new protocol shims will add unknown values to this list of
for _, unknown := range r.Config.ComputedKeys { // ComputedKeys. This is the only way we have to indicate that a
if k == unknown { // collection is unknown in the config
return FieldReadResult{Computed: true, Exists: true}, nil for _, unknown := range r.Config.ComputedKeys {
if k == unknown {
log.Printf("[DEBUG] setting computed for %q from ComputedKeys", k)
return FieldReadResult{Computed: true, Exists: true}, nil
}
} }
} }
} }