Hi all!
I am developing a terraform plugin and want to define custom ForceNew logic.
I have a field tables
Type: schema.TypeList, Elem: &schema.Resource.
This tables
map has a field name
of TypeString.
If a name
field is updated then the resource should be recreated. If a new tables
item is added to the list, then an update is allowed, but if an item is removed, a recreate should occur.
This is the code I have written at the moment, but I am not sure why it isn’t working. The ForceNew on list decreasing logic works, but not for the name
change.
customdiff.ForceNewIfChange("tables", func(ctx context.Context, old, new, meta any) bool {
newList := new.([]interface{})
oldList := old.([]interface{})
if len(newList) < len(oldList) {
return true
}
for i := 0; i < len(oldList); i++ {
currentNewMap := newList[i].(map[string]interface{})
currentOldMap := oldList[i].(map[string]interface{})
if currentNewMap["name"].(string) != currentOldMap["name"].(string) {
return true
}
}
return false
})
If I set ForceNew
to true
for the name
field, it will trigger a recreate on an addition to the tables
list.
Thanks in advance for any help.
Charlie