From 5489d8c549a01a8d7960527cb1c5e94cbc17017d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 28 Oct 2016 15:35:26 -0400 Subject: [PATCH] helper/schema: removed optional items force new Fixes #5138 If an item is optional and is removed completely from the configuration, it should still trigger a destroy/create if the field itself was marked as "ForceNew". See the example in #5138. --- helper/schema/schema.go | 10 ++++---- helper/schema/schema_test.go | 45 +++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 452f0e08d..1f9e91c92 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -285,6 +285,11 @@ func (s *Schema) finalizeDiff( d.New = normalizeBoolString(d.New) } + if s.ForceNew { + // Force new, set it to true in the diff + d.RequiresNew = true + } + if d.NewRemoved { return d } @@ -302,11 +307,6 @@ func (s *Schema) finalizeDiff( } } - if s.ForceNew { - // Force new, set it to true in the diff - d.RequiresNew = true - } - if s.Sensitive { // Set the Sensitive flag so output is hidden in the UI d.Sensitive = true diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index bb96c6a71..e9877c916 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -2019,9 +2019,10 @@ func TestSchemaMap_Diff(t *testing.T) { RequiresNew: true, }, "instances.3": &terraform.ResourceAttrDiff{ - Old: "foo", - New: "", - NewRemoved: true, + Old: "foo", + New: "", + NewRemoved: true, + RequiresNew: true, }, }, }, @@ -2331,9 +2332,10 @@ func TestSchemaMap_Diff(t *testing.T) { New: "2", }, "instances.2": &terraform.ResourceAttrDiff{ - Old: "22", - New: "", - NewRemoved: true, + Old: "22", + New: "", + NewRemoved: true, + RequiresNew: true, }, "instances.3": &terraform.ResourceAttrDiff{ Old: "333", @@ -2420,6 +2422,37 @@ func TestSchemaMap_Diff(t *testing.T) { Err: false, }, + + "removed optional items should trigger ForceNew": { + Schema: map[string]*Schema{ + "description": &Schema{ + Type: TypeString, + ForceNew: true, + Optional: true, + }, + }, + + State: &terraform.InstanceState{ + Attributes: map[string]string{ + "description": "foo", + }, + }, + + Config: map[string]interface{}{}, + + Diff: &terraform.InstanceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "description": &terraform.ResourceAttrDiff{ + Old: "foo", + New: "", + RequiresNew: true, + NewRemoved: true, + }, + }, + }, + + Err: false, + }, } for tn, tc := range cases {