@@ -319,40 +319,131 @@ func diffObjectParam(paramKey, _, _ string, d *schema.ResourceData) bool {
319
319
320
320
func SuppressMapDiffs () schema.SchemaDiffSuppressFunc {
321
321
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
322
325
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 )
324
327
return true
325
328
}
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 )
332
333
}
333
334
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
347
415
}
348
416
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 )
351
420
return false
352
421
}
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 )
354
433
return true
355
434
}
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
356
447
}
357
448
358
449
// TakeObjectsDifferent is a method that used to recursively get the complement of object A (objA) compared to
0 commit comments