Skip to content

Commit c2ff9a0

Browse files
committed
Type Block need handle attribute marked as removed contionally.
In some provider, e.g. AzureRM, TypeSet is use quite prevalent to hold a set of sub-resources (e.g. security_rule in azurerm_network_security_group). The reason for using TypeSet instead of TypeList might because service return a collection of info with arbitrary order, or other reasons. In this case: Given a slice of nested blocks, if one of the nested block (B1) attributes is optional and has no default value, and user didn't specify a value for it. Then if another nested block (B2) changed, which triggers some diff, then B1 will also be replaced. That is because the optional attribute contribute a diff of "zero value" -> `null`, which changed the hash of the block. This fix is to carefully handle this case. We keep attribute marked as `NewRemoved` after a `diffString` only when all the attributes are marked as such. Otherwise, as long as one attribute is not marked to be removed, that means this block will be kept. Then we will manipulate the attributes in this block, which being marked as removed just because user didn't specify a value and the original value is the zero value. This keeps consistent as other Resource types (e.g. List Resource). (Though those type just remove the attribute from diff set, while for Set we need to return the complete state as we will not depend on the old state during diff apply).
1 parent d3b1ac9 commit c2ff9a0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

helper/schema/schema.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,14 +1269,39 @@ func (m schemaMap) diffSet(
12691269
for _, code := range list {
12701270
switch t := schema.Elem.(type) {
12711271
case *Resource:
1272+
1273+
// For resource block, we need to check whether the whole resource should be removed.
1274+
// If one of the attribute is not marked to be removed, than we need to manipulate
1275+
// those be marked just because their new value is not specified and the old value is
1276+
// the zero value. So as to keep consistent as other Resource types (e.g. List Resource).
1277+
// (Though those type just remove the attribute from diff set, while for Set we need to return the
1278+
// complete state as we will not depend on the old state during diff apply).
1279+
isSubKNewRemoved := true
1280+
12721281
// This is a complex resource
12731282
for k2, schema := range t.Schema {
12741283
subK := fmt.Sprintf("%s.%s.%s", k, code, k2)
12751284
err := m.diff(subK, schema, diff, d, true)
12761285
if err != nil {
12771286
return err
12781287
}
1288+
1289+
if subV, ok := diff.Attributes[subK]; ok && !subV.NewRemoved {
1290+
isSubKNewRemoved = false
1291+
}
12791292
}
1293+
if !isSubKNewRemoved {
1294+
for k2 := range t.Schema {
1295+
subK := fmt.Sprintf("%s.%s.%s", k, code, k2)
1296+
if subV, ok := diff.Attributes[subK]; ok && subV.NewRemoved {
1297+
if subV.NewRemoved && subV.Old == schema.ZeroValue() {
1298+
subV.NewRemoved = false
1299+
subV.New = subV.Old
1300+
}
1301+
}
1302+
}
1303+
}
1304+
12801305
case *Schema:
12811306
// Copy the schema so that we can set Computed/ForceNew from
12821307
// the parent schema (the TypeSet).

0 commit comments

Comments
 (0)