@@ -79,6 +79,20 @@ func isEnableDefaultUserExplicitlySetInConfig(d *schema.ResourceData, regionName
7979 return exists
8080}
8181
82+ // isOverrideGlobalAlertExplicitlySetInConfig checks if override_global_alert was explicitly
83+ // set in the Terraform configuration for a specific region in the override_region block.
84+ func isOverrideGlobalAlertExplicitlySetInConfig (d * schema.ResourceData , regionName string ) bool {
85+ _ , exists := findRegionFieldInCtyValue (d .GetRawConfig (), regionName , "override_global_alert" )
86+ return exists
87+ }
88+
89+ // isOverrideGlobalAlertInActualPersistedState checks if override_global_alert was in the ACTUAL
90+ // persisted Terraform state (not the materialized Go map) for a specific region.
91+ func isOverrideGlobalAlertInActualPersistedState (d * schema.ResourceData , regionName string ) bool {
92+ _ , exists := findRegionFieldInCtyValue (d .GetRawState (), regionName , "override_global_alert" )
93+ return exists
94+ }
95+
8296// isEnableDefaultUserInActualPersistedState checks if enable_default_user was in the ACTUAL
8397// persisted Terraform state (not the materialized Go map) for a specific region.
8498// Uses GetRawState to bypass TypeSet materialization that adds all fields with zero-values.
@@ -296,7 +310,8 @@ func addPasswordIfOverridden(
296310 }
297311}
298312
299- // addAlertsIfOverridden adds override_global_alert to region config if alerts differ from global.
313+ // addAlertsIfOverridden adds override_global_alert to region config using hybrid GetRawConfig/GetRawState logic.
314+ // This prevents drift when alerts are removed from config but API still has stale values.
300315func addAlertsIfOverridden (
301316 ctx context.Context ,
302317 regionDbConfig map [string ]interface {},
@@ -306,17 +321,52 @@ func addAlertsIfOverridden(
306321) {
307322 globalAlerts := d .Get ("global_alert" ).(* schema.Set ).List ()
308323 regionAlerts := pro .FlattenAlerts (regionDb .Alerts )
324+ rawConfig := d .GetRawConfig ()
325+ alertsDiffer := ! alertsEqualContent (globalAlerts , regionAlerts )
309326
310- // Compare alert content, not just counts
311- shouldAdd := ! alertsEqualContent (globalAlerts , regionAlerts )
327+ var shouldAdd bool
328+ var reason string
329+
330+ // Hybrid GetRawConfig/GetRawState strategy (same as enable_default_user)
331+ if ! rawConfig .IsNull () {
332+ // During Apply/Update: Check if explicitly set in config
333+ if isOverrideGlobalAlertExplicitlySetInConfig (d , region ) {
334+ shouldAdd = true
335+ reason = "explicitly set in config"
336+ } else {
337+ // Not in config - don't add even if API differs (user removed it or never set it)
338+ shouldAdd = false
339+ reason = "not in config (inherited or removed)"
340+ }
341+ } else {
342+ // During Refresh: Check if was in persisted state
343+ wasInState := isOverrideGlobalAlertInActualPersistedState (d , region )
344+ if wasInState {
345+ shouldAdd = true
346+ if alertsDiffer {
347+ reason = "was in state, differs from global"
348+ } else {
349+ reason = "was in state, preserving (user explicit)"
350+ }
351+ } else if alertsDiffer {
352+ shouldAdd = true
353+ reason = "not in state, but differs from global"
354+ } else {
355+ shouldAdd = false
356+ reason = "not in state, matches global (inherited)"
357+ }
358+ }
312359
313360 tflog .Debug (ctx , "Read: Alerts comparison" , map [string ]interface {}{
314- "region" : region ,
315- "globalAlertsCount" : len (globalAlerts ),
316- "globalAlerts" : globalAlerts ,
317- "regionAlertsCount" : len (regionAlerts ),
318- "regionAlerts" : regionAlerts ,
319- "shouldAdd" : shouldAdd ,
361+ "region" : region ,
362+ "globalAlertsCount" : len (globalAlerts ),
363+ "globalAlerts" : globalAlerts ,
364+ "regionAlertsCount" : len (regionAlerts ),
365+ "regionAlerts" : regionAlerts ,
366+ "alertsDiffer" : alertsDiffer ,
367+ "getRawConfigAvailable" : ! rawConfig .IsNull (),
368+ "shouldAdd" : shouldAdd ,
369+ "reason" : reason ,
320370 })
321371
322372 if shouldAdd {
0 commit comments