Skip to content

Commit 2c54609

Browse files
committed
fix: deep comparison of alerts
1 parent 6ac9a7d commit 2c54609

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

provider/resource_rediscloud_active_active_database_helpers.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package provider
22

33
import (
44
"context"
5+
"fmt"
56
"sort"
67

78
"github.com/RedisLabs/rediscloud-go-api/redis"
@@ -295,8 +296,7 @@ func addPasswordIfOverridden(
295296
}
296297
}
297298

298-
// addAlertsIfOverridden adds override_global_alert to region config if count differs from global.
299-
// Note: Active-Active API doesn't return global alerts separately, so we compare counts.
299+
// addAlertsIfOverridden adds override_global_alert to region config if alerts differ from global.
300300
func addAlertsIfOverridden(
301301
ctx context.Context,
302302
regionDbConfig map[string]interface{},
@@ -306,7 +306,9 @@ func addAlertsIfOverridden(
306306
) {
307307
globalAlerts := d.Get("global_alert").(*schema.Set).List()
308308
regionAlerts := pro.FlattenAlerts(regionDb.Alerts)
309-
shouldAdd := len(globalAlerts) != len(regionAlerts)
309+
310+
// Compare alert content, not just counts
311+
shouldAdd := !alertsEqualContent(globalAlerts, regionAlerts)
310312

311313
tflog.Debug(ctx, "Read: Alerts comparison", map[string]interface{}{
312314
"region": region,
@@ -322,6 +324,36 @@ func addAlertsIfOverridden(
322324
}
323325
}
324326

327+
// alertsEqualContent compares two alert lists for equality by comparing their content.
328+
// Takes []interface{} (from state) and []map[string]interface{} (from API flatten) for comparison.
329+
func alertsEqualContent(globalAlerts []interface{}, regionAlerts []map[string]interface{}) bool {
330+
if len(globalAlerts) != len(regionAlerts) {
331+
return false
332+
}
333+
334+
// Convert global alerts to a set of "name:value" strings
335+
globalSet := make(map[string]bool, len(globalAlerts))
336+
for _, alert := range globalAlerts {
337+
alertMap := alert.(map[string]interface{})
338+
name := alertMap["name"].(string)
339+
value := alertMap["value"]
340+
key := fmt.Sprintf("%s:%v", name, value)
341+
globalSet[key] = true
342+
}
343+
344+
// Check if all region alerts exist in global set
345+
for _, alertMap := range regionAlerts {
346+
name := alertMap["name"].(string)
347+
value := alertMap["value"]
348+
key := fmt.Sprintf("%s:%v", name, value)
349+
if !globalSet[key] {
350+
return false
351+
}
352+
}
353+
354+
return true
355+
}
356+
325357
// addRemoteBackupIfConfigured adds remote_backup to region config if it exists in both API and state.
326358
func addRemoteBackupIfConfigured(
327359
ctx context.Context,

0 commit comments

Comments
 (0)