Skip to content

Commit ec1dfb4

Browse files
committed
chore(origin): refactor the refresh function
1 parent fe8d2ee commit ec1dfb4

7 files changed

+932
-109
lines changed

huaweicloud/services/evs/resource_huaweicloud_evs_snapshot.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ func ResourceEvsSnapshot() *schema.Resource {
8787
Computed: true,
8888
},
8989
"metadata_origin": {
90-
Type: schema.TypeMap,
91-
Computed: true,
92-
Elem: &schema.Schema{Type: schema.TypeString},
90+
Type: schema.TypeMap,
91+
Optional: true,
92+
Computed: true,
93+
Elem: &schema.Schema{Type: schema.TypeString},
94+
DiffSuppressFunc: utils.SuppressDiffAll,
9395
Description: utils.SchemaDesc(
9496
`The script configuration value of this change is also the original value used for comparison with
9597
the new value next time the change is made. The corresponding parameter name is 'metadata'.`,

huaweicloud/services/evs/resource_huaweicloud_evsv3_snapshot.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ func ResourceV3Snapshot() *schema.Resource {
9797
Computed: true,
9898
},
9999
"metadata_origin": {
100-
Type: schema.TypeMap,
101-
Computed: true,
102-
Elem: &schema.Schema{Type: schema.TypeString},
100+
Type: schema.TypeMap,
101+
Optional: true,
102+
Computed: true,
103+
Elem: &schema.Schema{Type: schema.TypeString},
104+
DiffSuppressFunc: utils.SuppressDiffAll,
103105
Description: utils.SchemaDesc(
104106
`The script configuration value of this change is also the original value used for
105107
comparison with the new value next time the change is made. The corresponding parameter name is

huaweicloud/services/fgs/resource_huaweicloud_fgs_function.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ CIDR blocks used by the service.`,
596596
"lts_custom_tag": {
597597
Type: schema.TypeMap,
598598
Optional: true,
599+
Computed: true,
599600
Elem: &schema.Schema{Type: schema.TypeString},
600601
DiffSuppressFunc: utils.SuppressMapDiffs(),
601602
// The custom tags can be set to empty, so computed behavior cannot be supported.
@@ -644,9 +645,11 @@ CIDR blocks used by the service.`,
644645
Description: `The version of the function.`,
645646
},
646647
"lts_custom_tag_origin": {
647-
Type: schema.TypeMap,
648-
Computed: true,
649-
Elem: &schema.Schema{Type: schema.TypeString},
648+
Type: schema.TypeMap,
649+
Optional: true,
650+
Computed: true,
651+
Elem: &schema.Schema{Type: schema.TypeString},
652+
DiffSuppressFunc: utils.SuppressDiffAll,
650653
Description: utils.SchemaDesc(
651654
`The script configuration value of this change is also the original value used for comparison with
652655
the new value next time the change is made. The corresponding parameter name is 'lts_custom_tag'.`,

huaweicloud/utils/diff_suppress_funcs.go renamed to huaweicloud/utils/resource_diff.go

Lines changed: 114 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -319,40 +319,131 @@ func diffObjectParam(paramKey, _, _ string, d *schema.ResourceData) bool {
319319

320320
func SuppressMapDiffs() schema.SchemaDiffSuppressFunc {
321321
return func(paramKey, o, n string, d *schema.ResourceData) bool {
322+
log.Printf("[DEBUG] SuppressMapDiffs: called with paramKey='%s', old='%s', new='%s'", paramKey, o, n)
323+
324+
// Ignore length change judgment, because this method will judge each changed key one by one
322325
if strings.HasSuffix(paramKey, ".%") {
323-
// Ignore the length judgment, because this method will judge each changed key one by one.
326+
log.Printf("[DEBUG] SuppressMapDiffs: ignoring length change for '%s'", paramKey)
324327
return true
325328
}
326-
if n != "" {
327-
log.Printf("[DEBUG] The new value is found and report this change directly, the value is: %s", n)
328-
// When the new value is not empty and the key is in the *terraform.InstanceDiff.Attributes list, it means
329-
// that the value has been modified compared to the value returned by the console, report this change
330-
// directly.
331-
return false
329+
330+
// Handle the case where the entire map is being changed
331+
if !strings.Contains(paramKey, ".") {
332+
return suppressEntireMapDiff(paramKey, d)
332333
}
333334

334-
var (
335-
// The absolute path of the current key.
336-
categories = strings.Split(paramKey, ".")
337-
mapCategory = strings.Join(categories[:len(categories)-1], ".")
338-
originMapCategory = fmt.Sprintf("%s_origin", mapCategory)
339-
keyName = categories[len(categories)-1]
340-
)
341-
342-
originMap, ok := d.Get(originMapCategory).(map[string]interface{})
343-
if !ok {
344-
log.Printf("[WARN] Unable to find corresponding origin parameter (%s) in current category, please check your schema definition",
345-
originMapCategory)
346-
return true
335+
// Handle single key changes
336+
return suppressSingleKeyDiff(paramKey, d)
337+
}
338+
}
339+
340+
// suppressEntireMapDiff handles changes to the entire map
341+
func suppressEntireMapDiff(paramKey string, d *schema.ResourceData) bool {
342+
originMapCategory := fmt.Sprintf("%s_origin", paramKey)
343+
log.Printf("[DEBUG] SuppressMapDiffs: handling entire map change for '%s', origin map category: '%s'",
344+
paramKey, originMapCategory)
345+
346+
originMapVal := d.Get(originMapCategory)
347+
if originMapVal == nil {
348+
log.Printf("[DEBUG] SuppressMapDiffs: origin map '%s' is nil, suppressing diff for entire map '%s'",
349+
originMapCategory, paramKey)
350+
return true
351+
}
352+
353+
originMap := TryMapValueAnalysis(originMapVal)
354+
if len(originMap) == 0 {
355+
log.Printf("[DEBUG] SuppressMapDiffs: origin map '%s' is empty, suppressing diff for entire map '%s'",
356+
originMapCategory, paramKey)
357+
return true
358+
}
359+
360+
// For entire map changes, we need to check if all keys in the new value exist in origin
361+
// This is a simplified approach - in practice, you might want more sophisticated comparison
362+
log.Printf("[DEBUG] SuppressMapDiffs: entire map '%s' change detected, checking against origin", paramKey)
363+
return false // For now, report the change and let individual key suppression handle it
364+
}
365+
366+
// suppressSingleKeyDiff handles changes to a single key
367+
func suppressSingleKeyDiff(paramKey string, d *schema.ResourceData) bool {
368+
categories := strings.Split(paramKey, ".")
369+
mapCategory := strings.Join(categories[:len(categories)-1], ".")
370+
originMapCategory := fmt.Sprintf("%s_origin", mapCategory)
371+
keyName := categories[len(categories)-1]
372+
373+
log.Printf("[DEBUG] SuppressMapDiffs: processing key '%s', mapCategory='%s', originMapCategory='%s', keyName='%s'",
374+
paramKey, mapCategory, originMapCategory, keyName)
375+
376+
// Get origin map (last local configuration)
377+
originMapVal := d.Get(originMapCategory)
378+
originMap := TryMapValueAnalysis(originMapVal)
379+
log.Printf("[DEBUG] SuppressMapDiffs: origin map '%s' content: %+v", originMapCategory, originMap)
380+
381+
// Get current configuration map
382+
currentMapVal := d.Get(mapCategory)
383+
if currentMapVal == nil {
384+
log.Printf("[DEBUG] SuppressMapDiffs: current map '%s' is nil, suppressing diff for key '%s'",
385+
mapCategory, keyName)
386+
return true
387+
}
388+
389+
currentMap := TryMapValueAnalysis(currentMapVal)
390+
log.Printf("[DEBUG] SuppressMapDiffs: current map '%s' content: %+v", mapCategory, currentMap)
391+
392+
// Check if the key exists in current configuration
393+
existsInCurrent := keyExists(currentMap, keyName)
394+
existsInOrigin := keyExists(originMap, keyName)
395+
isOriginEmpty := originMapVal == nil || len(originMap) == 0
396+
397+
// Determine suppression based on key existence
398+
return determineSuppression(existsInCurrent, existsInOrigin, isOriginEmpty, keyName)
399+
}
400+
401+
// keyExists checks if a key exists in the map
402+
func keyExists(m map[string]interface{}, key string) bool {
403+
_, exists := m[key]
404+
return exists
405+
}
406+
407+
// determineSuppression determines whether to suppress the diff based on key existence
408+
func determineSuppression(existsInCurrent, existsInOrigin, isOriginEmpty bool, keyName string) bool {
409+
if existsInCurrent {
410+
// The key exists in current configuration
411+
if isOriginEmpty {
412+
// Origin is empty or nil, report the change (locally added)
413+
log.Printf("[DEBUG][SuppressMapDiffs] The key '%s' found in current config but origin is empty", keyName)
414+
return false
347415
}
348416

349-
if _, ok := originMap[keyName]; ok {
350-
// The key is found in the origin parameter value and report this change directly.
417+
if existsInOrigin {
418+
// The key exists in both current config and origin, report this change
419+
log.Printf("[DEBUG][SuppressMapDiffs] The key '%s' found in both current config and origin", keyName)
351420
return false
352421
}
353-
// The relevant value is not configured locally, and the change is ignored.
422+
423+
// The key exists in current config but not in origin (locally added)
424+
log.Printf("[DEBUG][SuppressMapDiffs] The key '%s' found in current config but not in origin", keyName)
425+
return false
426+
}
427+
428+
// The key does not exist in current configuration
429+
if isOriginEmpty {
430+
// Origin is empty or nil, suppress the diff (remotely added)
431+
log.Printf("[DEBUG][SuppressMapDiffs] The key '%s' not found in current config and origin is empty, suppressing diff",
432+
keyName)
354433
return true
355434
}
435+
436+
if existsInOrigin {
437+
// The key exists in origin but not in current config (locally removed)
438+
log.Printf("[DEBUG][SuppressMapDiffs] The key '%s' found in origin but not in current config (locally removed)",
439+
keyName)
440+
return false
441+
}
442+
443+
// The key does not exist in either current config or origin (remotely added)
444+
log.Printf("[DEBUG][SuppressMapDiffs] The key '%s' not found in either current config or origin (remotely added), suppressing diff",
445+
keyName)
446+
return true
356447
}
357448

358449
// TakeObjectsDifferent is a method that used to recursively get the complement of object A (objA) compared to

huaweicloud/utils/diff_suppress_funcs_test.go renamed to huaweicloud/utils/resource_diff_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
88
)
99

10-
func TestDiffSuppressFunc_ContainsAllKeyValues(t *testing.T) {
10+
func TestResourceDiffunc_ContainsAllKeyValues(t *testing.T) {
1111
var (
1212
compareObject = map[string]interface{}{
1313
"A": map[string]interface{}{
@@ -81,7 +81,7 @@ func TestDiffSuppressFunc_ContainsAllKeyValues(t *testing.T) {
8181
t.Logf("All processing results of the ContainsAllKeyValues method meets expectation")
8282
}
8383

84-
func TestDiffSuppressFunc_FindDecreaseKeys(t *testing.T) {
84+
func TestResourceDiffunc_FindDecreaseKeys(t *testing.T) {
8585
var (
8686
decreaseCacls = []map[string]interface{}{
8787
{
@@ -124,7 +124,7 @@ func TestDiffSuppressFunc_FindDecreaseKeys(t *testing.T) {
124124
t.Logf("All processing results of the FindDecreaseKeys method meets expectation")
125125
}
126126

127-
func TestDiffSuppressFunc_TakeObjectsDifferent(t *testing.T) {
127+
func TestResourceDiffunc_TakeObjectsDifferent(t *testing.T) {
128128
var (
129129
diffCacls = []map[string]interface{}{
130130
{

0 commit comments

Comments
 (0)