diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 1a56ad5a3..18e0f50e7 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 9511f363b..1e2451a69 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -2021,9 +2021,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, }, }, }, @@ -2333,9 +2334,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", @@ -2422,6 +2424,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 {