From 4d067f4d6d027e9be9b40f8a02819d7fedcdadfb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 15 Jan 2015 10:35:44 -0800 Subject: [PATCH] helper/schema: don't put things into the state that don't exist or are computed [GH-805] --- helper/schema/resource_data.go | 11 +++++---- helper/schema/resource_data_test.go | 37 ++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/helper/schema/resource_data.go b/helper/schema/resource_data.go index 650cf2b601..96aa088be6 100644 --- a/helper/schema/resource_data.go +++ b/helper/schema/resource_data.go @@ -84,8 +84,7 @@ func (d *ResourceData) GetChange(key string) (interface{}, interface{}) { } // GetOk returns the data for the given key and whether or not the key -// existed or not in the configuration. The second boolean result will also -// be false if a key is given that isn't in the schema at all. +// has been set. // // The first result will not necessarilly be nil if the value doesn't exist. // The second result should be checked to determine this information. @@ -213,9 +212,11 @@ func (d *ResourceData) State() *terraform.InstanceState { } raw := d.get([]string{k}, source) - rawMap[k] = raw.Value - if raw.ValueProcessed != nil { - rawMap[k] = raw.ValueProcessed + if raw.Exists && !raw.Computed { + rawMap[k] = raw.Value + if raw.ValueProcessed != nil { + rawMap[k] = raw.ValueProcessed + } } } mapW := &MapFieldWriter{Schema: d.schema} diff --git a/helper/schema/resource_data_test.go b/helper/schema/resource_data_test.go index 4666f509f7..c351260cf2 100644 --- a/helper/schema/resource_data_test.go +++ b/helper/schema/resource_data_test.go @@ -1919,9 +1919,7 @@ func TestResourceDataState(t *testing.T) { Partial: []string{}, Result: &terraform.InstanceState{ - Attributes: map[string]string{ - "availability_zone": "", - }, + Attributes: map[string]string{}, }, }, @@ -1994,9 +1992,7 @@ func TestResourceDataState(t *testing.T) { }, Result: &terraform.InstanceState{ - Attributes: map[string]string{ - "ports.#": "0", - }, + Attributes: map[string]string{}, }, }, @@ -2176,9 +2172,7 @@ func TestResourceDataState(t *testing.T) { Partial: []string{}, Result: &terraform.InstanceState{ - Attributes: map[string]string{ - "ports.#": "0", - }, + Attributes: map[string]string{}, }, }, @@ -2239,6 +2233,31 @@ func TestResourceDataState(t *testing.T) { Attributes: map[string]string{}, }, }, + + // #21 + { + Schema: map[string]*Schema{ + "foo": &Schema{ + Type: TypeString, + Optional: true, + Computed: true, + }, + }, + + State: nil, + + Diff: &terraform.InstanceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "foo": &terraform.ResourceAttrDiff{ + NewComputed: true, + }, + }, + }, + + Result: &terraform.InstanceState{ + Attributes: map[string]string{}, + }, + }, } for i, tc := range cases {