Skip to content

Commit

Permalink
refactor(forceNew): update FlexibleForceNew func
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason-Zhang9309 committed Jan 9, 2025
1 parent 55adc5f commit cc83dfd
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions huaweicloud/config/force_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ import (
// when non-updatable parameters are changed
// if ForceNew is enabled, the resource will be recreated
// if ForceNew is not enabled, an error will be raise
func FlexibleForceNew(keys []string) schema.CustomizeDiffFunc {
// if there is DiffSuppressFunc in the schema, this func need resource schema to make DiffSuppressFunc work
func FlexibleForceNew(keys []string, resourceSchemas ...map[string]*schema.Schema) schema.CustomizeDiffFunc {
return func(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
var resourceSchema map[string]*schema.Schema
if len(resourceSchemas) > 0 {
resourceSchema = resourceSchemas[0]
}

cfg := meta.(*Config)
var err error
forceNew := cfg.GetForceNew(d)
keysExpand := expandKeys(keys, d)

if forceNew {
for _, k := range keysExpand {
if err := d.ForceNew(k); err != nil {
Expand All @@ -32,7 +39,12 @@ func FlexibleForceNew(keys []string) schema.CustomizeDiffFunc {
for _, k := range keysExpand {
if d.Id() != "" && d.HasChange(k) {
oldValue, newValue := d.GetChange(k)
err = multierror.Append(err, fmt.Errorf("%s can't be updated, %v -> %v", k, oldValue, newValue))
if resourceSchema != nil && resourceSchema[k] != nil && resourceSchema[k].DiffSuppressFunc != nil &&
resourceSchema[k].DiffSuppressFunc(k, oldValue.(string), newValue.(string), nil) {
log.Printf("[DEBUG] ignoring change of %s due to DiffSuppressFunc, %v -> %v", k, oldValue, newValue)
} else {
err = multierror.Append(err, fmt.Errorf("%s can't be updated, %v -> %v", k, oldValue, newValue))
}
}
}
}
Expand Down

0 comments on commit cc83dfd

Please sign in to comment.