From 3a107d2e50d798443bdf6249b69e5f06db218d47 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 10 Oct 2014 15:58:38 -0700 Subject: [PATCH] helper/schema: set the field to empty if it is a list and computed --- helper/schema/resource_data.go | 20 ++++++-- helper/schema/resource_data_test.go | 71 ++++++++++++++++++++++++++++- helper/schema/schema_test.go | 29 ++++++++++++ 3 files changed, 116 insertions(+), 4 deletions(-) diff --git a/helper/schema/resource_data.go b/helper/schema/resource_data.go index c025b24d8..4d0637fa8 100644 --- a/helper/schema/resource_data.go +++ b/helper/schema/resource_data.go @@ -874,12 +874,19 @@ func (d *ResourceData) stateList( schema *Schema) map[string]string { countRaw := d.get(prefix, []string{"#"}, schema, d.stateSource(prefix)) if !countRaw.Exists { - return nil + if schema.Computed { + // If it is computed, then it always _exists_ in the state, + // it is just empty. + countRaw.Exists = true + countRaw.Value = 0 + } else { + return nil + } } count := countRaw.Value.(int) result := make(map[string]string) - if count > 0 { + if count > 0 || schema.Computed { result[prefix+".#"] = strconv.FormatInt(int64(count), 10) } for i := 0; i < count; i++ { @@ -974,7 +981,14 @@ func (d *ResourceData) stateSet( schema *Schema) map[string]string { raw := d.get(prefix, nil, schema, d.stateSource(prefix)) if !raw.Exists { - return nil + if schema.Computed { + // If it is computed, then it always _exists_ in the state, + // it is just empty. + raw.Exists = true + raw.Value = new(Set) + } else { + return nil + } } set := raw.Value.(*Set) diff --git a/helper/schema/resource_data_test.go b/helper/schema/resource_data_test.go index 3f4c9ed9c..081f92f70 100644 --- a/helper/schema/resource_data_test.go +++ b/helper/schema/resource_data_test.go @@ -1685,7 +1685,9 @@ func TestResourceDataState(t *testing.T) { }, Result: &terraform.InstanceState{ - Attributes: map[string]string{}, + Attributes: map[string]string{ + "config_vars.#": "0", + }, }, }, @@ -1834,6 +1836,40 @@ func TestResourceDataState(t *testing.T) { }, }, + { + Schema: map[string]*Schema{ + "ports": &Schema{ + Type: TypeList, + Optional: true, + Computed: true, + Elem: &Schema{Type: TypeInt}, + }, + }, + + State: nil, + + Diff: &terraform.InstanceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "ports.#": &terraform.ResourceAttrDiff{ + Old: "", + NewComputed: true, + }, + }, + }, + + Partial: []string{}, + + Set: map[string]interface{}{ + "ports": []interface{}{}, + }, + + Result: &terraform.InstanceState{ + Attributes: map[string]string{ + "ports.#": "0", + }, + }, + }, + // List of resources { Schema: map[string]*Schema{ @@ -1974,6 +2010,39 @@ func TestResourceDataState(t *testing.T) { }, }, }, + + { + Schema: map[string]*Schema{ + "ports": &Schema{ + Type: TypeSet, + Optional: true, + Computed: true, + Elem: &Schema{Type: TypeInt}, + Set: func(a interface{}) int { + return a.(int) + }, + }, + }, + + State: nil, + + Diff: &terraform.InstanceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "ports.#": &terraform.ResourceAttrDiff{ + Old: "", + NewComputed: true, + }, + }, + }, + + Partial: []string{}, + + Result: &terraform.InstanceState{ + Attributes: map[string]string{ + "ports.#": "0", + }, + }, + }, } for i, tc := range cases { diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index c30e90f61..34f57c891 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -667,6 +667,35 @@ func TestSchemaMap_Diff(t *testing.T) { Err: false, }, + { + Schema: map[string]*Schema{ + "ports": &Schema{ + Type: TypeSet, + Optional: true, + Computed: true, + Elem: &Schema{Type: TypeInt}, + Set: func(a interface{}) int { + return a.(int) + }, + }, + }, + + State: nil, + + Config: nil, + + Diff: &terraform.InstanceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "ports.#": &terraform.ResourceAttrDiff{ + Old: "", + NewComputed: true, + }, + }, + }, + + Err: false, + }, + { Schema: map[string]*Schema{ "ports": &Schema{