From 67395306e178e36e81b5db4aeda2f9349039d1ac Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 22 Apr 2019 16:41:26 -0400 Subject: [PATCH] delete unknown values from apply config altogether removeConfigUnknowns need to remove the value completely from the config map. Removing this value allows GetOk and GetOkExists to indicate if the value was set in the config in the case of an Optional+Computed attribute. --- builtin/providers/test/resource.go | 13 +++++++++++++ builtin/providers/test/resource_test.go | 19 +++++++++++++++++++ helper/schema/shims.go | 2 +- helper/schema/shims_test.go | 9 ++------- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/builtin/providers/test/resource.go b/builtin/providers/test/resource.go index 355831046..6cd5b71a0 100644 --- a/builtin/providers/test/resource.go +++ b/builtin/providers/test/resource.go @@ -141,6 +141,13 @@ func testResource() *schema.Resource { Computed: true, Description: "copied the required field during apply, and plans computed when changed", }, + // this should return unset from GetOkExists + "get_ok_exists_false": { + Type: schema.TypeBool, + Computed: true, + Optional: true, + Description: "do not set in config", + }, }, } } @@ -186,6 +193,12 @@ func testResourceRead(d *schema.ResourceData, meta interface{}) error { d.Set("set", []interface{}{}) } + // This should not show as set unless it's set in the config + _, ok := d.GetOkExists("get_ok_exists_false") + if ok { + return errors.New("get_ok_exists_false should not be set") + } + return nil } diff --git a/builtin/providers/test/resource_test.go b/builtin/providers/test/resource_test.go index c85bb338e..959cee5b8 100644 --- a/builtin/providers/test/resource_test.go +++ b/builtin/providers/test/resource_test.go @@ -973,3 +973,22 @@ resource "test_resource" "bar" { }, }) } + +func TestResource_optionalComputedBool(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: testAccCheckResourceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource" "foo" { + required = "yep" + required_map = { + key = "value" + } +} + `), + }, + }, + }) +} diff --git a/helper/schema/shims.go b/helper/schema/shims.go index dc840204b..d43028e58 100644 --- a/helper/schema/shims.go +++ b/helper/schema/shims.go @@ -51,7 +51,7 @@ func removeConfigUnknowns(cfg map[string]interface{}) { switch v := v.(type) { case string: if v == config.UnknownVariableValue { - cfg[k] = "" + delete(cfg, k) } case []interface{}: for _, i := range v { diff --git a/helper/schema/shims_test.go b/helper/schema/shims_test.go index 7c523f42b..659d0fd70 100644 --- a/helper/schema/shims_test.go +++ b/helper/schema/shims_test.go @@ -3643,22 +3643,17 @@ func TestRemoveConfigUnknowns(t *testing.T) { } expect := map[string]interface{}{ - "id": "", "route_rules": []interface{}{ map[string]interface{}{ - "cidr_block": "", "destination": "0.0.0.0/0", "destination_type": "CIDR_BLOCK", "network_entity_id": "1", }, map[string]interface{}{ - "cidr_block": "", "destination": "0.0.0.0/0", "destination_type": "CIDR_BLOCK", "sub_block": []interface{}{ - map[string]interface{}{ - "computed": "", - }, + map[string]interface{}{}, }, }, }, @@ -3667,6 +3662,6 @@ func TestRemoveConfigUnknowns(t *testing.T) { removeConfigUnknowns(cfg) if !reflect.DeepEqual(cfg, expect) { - t.Fatalf("\nexpected: %#v\ngot: %#v", expect, cfg) + t.Fatalf("\nexpected: %#v\ngot: %#v", expect, cfg) } }