diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 64fb92355d..be3278eaba 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -248,15 +248,25 @@ func (m schemaMap) diffList( newLen = len(vs) // If the counts are not the same, then record that diff - if oldLen != newLen { + changed := oldLen != newLen + computed := oldLen == 0 && newLen == 0 && schema.Computed + if changed || computed { countSchema := &Schema{ Type: TypeInt, + Computed: schema.Computed, ForceNew: schema.ForceNew, } + oldStr := "" + newStr := "" + if !computed { + oldStr = strconv.FormatInt(int64(oldLen), 10) + newStr = strconv.FormatInt(int64(newLen), 10) + } + diff.Attributes[k+".#"] = countSchema.finalizeDiff(&terraform.ResourceAttrDiff{ - Old: strconv.FormatInt(int64(oldLen), 10), - New: strconv.FormatInt(int64(newLen), 10), + Old: oldStr, + New: newStr, }) } diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index 30cacaff20..8588cb8915 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -181,7 +181,7 @@ func TestSchemaMap_Diff(t *testing.T) { Diff: &terraform.ResourceDiff{ Attributes: map[string]*terraform.ResourceAttrDiff{ "ports.#": &terraform.ResourceAttrDiff{ - Old: "", + Old: "0", New: "3", }, "ports.0": &terraform.ResourceAttrDiff{ @@ -285,7 +285,7 @@ func TestSchemaMap_Diff(t *testing.T) { Diff: &terraform.ResourceDiff{ Attributes: map[string]*terraform.ResourceAttrDiff{ "ports.#": &terraform.ResourceAttrDiff{ - Old: "", + Old: "0", New: "3", RequiresNew: true, }, @@ -310,6 +310,32 @@ func TestSchemaMap_Diff(t *testing.T) { Err: false, }, + { + Schema: map[string]*Schema{ + "ports": &Schema{ + Type: TypeList, + Optional: true, + Computed: true, + Elem: &Schema{Type: TypeInt}, + }, + }, + + State: nil, + + Config: map[string]interface{}{}, + + Diff: &terraform.ResourceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "ports.#": &terraform.ResourceAttrDiff{ + Old: "", + NewComputed: true, + }, + }, + }, + + Err: false, + }, + /* * List of structure decode */ @@ -343,7 +369,7 @@ func TestSchemaMap_Diff(t *testing.T) { Diff: &terraform.ResourceDiff{ Attributes: map[string]*terraform.ResourceAttrDiff{ "ingress.#": &terraform.ResourceAttrDiff{ - Old: "", + Old: "0", New: "1", }, "ingress.0.from": &terraform.ResourceAttrDiff{ @@ -632,7 +658,6 @@ func TestSchemaMap_Diff(t *testing.T) { } for i, tc := range cases { - if i != 1 { continue } c, err := config.NewRawConfig(tc.Config) if err != nil { t.Fatalf("err: %s", err)