Merge pull request #20923 from hashicorp/jbardin/planned-computed

Allow all planned computed diff fields
pull/20935/head
James Bardin 7 years ago committed by GitHub
commit 90b70bba08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,7 +19,7 @@ func testResource() *schema.Resource {
},
CustomizeDiff: func(d *schema.ResourceDiff, _ interface{}) error {
if d.HasChange("required") {
if d.HasChange("optional") {
d.SetNewComputed("planned_computed")
}
return nil
@ -176,7 +176,7 @@ func testResourceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("computed_list", []string{"listval1", "listval2"})
d.Set("computed_set", []string{"setval1", "setval2"})
d.Set("planned_computed", d.Get("required"))
d.Set("planned_computed", d.Get("optional"))
// if there is no "set" value, erroneously set it to an empty set. This
// might change a null value to an empty set, but we should be able to

@ -847,25 +847,27 @@ func TestResource_plannedComputed(t *testing.T) {
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource" "foo" {
required = "ok"
required = "ok"
required_map = {
key = "value"
}
optional = "hi"
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"test_resource.foo", "planned_computed", "ok",
"test_resource.foo", "planned_computed", "hi",
),
),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource" "foo" {
required = "changed"
required = "ok"
required_map = {
key = "value"
}
optional = "changed"
}
`),
Check: resource.ComposeTestCheckFunc(
@ -916,3 +918,58 @@ func TestDiffApply_map(t *testing.T) {
t.Fatalf("expected:%#v got:%#v", expect, newAttrs)
}
}
func TestResource_dependsComputed(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
variable "change" {
default = false
}
resource "test_resource" "foo" {
required = "ok"
required_map = {
key = "value"
}
optional = var.change ? "after" : ""
}
resource "test_resource" "bar" {
count = var.change ? 1 : 0
required = test_resource.foo.planned_computed
required_map = {
key = "value"
}
}
`),
},
resource.TestStep{
Config: strings.TrimSpace(`
variable "change" {
default = true
}
resource "test_resource" "foo" {
required = "ok"
required_map = {
key = "value"
}
optional = var.change ? "after" : ""
}
resource "test_resource" "bar" {
count = var.change ? 1 : 0
required = test_resource.foo.planned_computed
required_map = {
key = "value"
}
}
`),
},
},
})
}

@ -611,17 +611,6 @@ func (s *GRPCProviderServer) PlanResourceChange(_ context.Context, req *proto.Pl
priorState = &terraform.InstanceState{}
}
// if we're not creating a new resource, remove any new computed fields
if !create {
for attr, d := range diff.Attributes {
// If there's no change, then don't let this go through as NewComputed.
// This usually only happens when Old and New are both empty.
if d.NewComputed && d.Old == d.New {
delete(diff.Attributes, attr)
}
}
}
// now we need to apply the diff to the prior state, so get the planned state
plannedAttrs, err := diff.Apply(priorState.Attributes, res.CoreConfigSchemaWhenShimmed())
schema.FixupAsSingleInstanceStateOut(

Loading…
Cancel
Save