@@ -218,26 +218,49 @@ func (r *resourceAlertGroupingSetting) Create(ctx context.Context, req resource.
218218
219219func (r * resourceAlertGroupingSetting ) Read (ctx context.Context , req resource.ReadRequest , resp * resource.ReadResponse ) {
220220 var id types.String
221+ var expected resourceAlertGroupingSettingModel
221222
222- resp .Diagnostics .Append (req .State .GetAttribute (ctx , path .Root ("id" ), & id )... )
223+ resp .Diagnostics .Append (req .State .Get (ctx , & expected )... ) // Get expected state
224+ if resp .Diagnostics .HasError () {
225+ return
226+ }
227+ resp .Diagnostics .Append (req .State .GetAttribute (ctx , path .Root ("id" ), & id )... ) // Get ID
223228 if resp .Diagnostics .HasError () {
224229 return
225230 }
226231 log .Printf ("[INFO] Reading PagerDuty alert grouping setting %s" , id )
227232
228- state , err := requestGetAlertGroupingSetting (ctx , r .client , id .ValueString (), false )
229- if err != nil {
230- if util .IsNotFoundError (err ) {
231- resp .State .RemoveResource (ctx )
233+ const maxRetries = 6
234+ const retryInterval = 10 * time .Second
235+ var lastErr error
236+
237+ for i := 0 ; i < maxRetries ; i ++ {
238+ state , err := requestGetAlertGroupingSetting (ctx , r .client , id .ValueString (), false )
239+ if err != nil {
240+ if util .IsNotFoundError (err ) {
241+ resp .State .RemoveResource (ctx )
242+ return
243+ }
244+ lastErr = err
245+ log .Printf ("[WARN] Error reading alert grouping setting (attempt %d/%d): %v" , i + 1 , maxRetries , err )
246+ time .Sleep (retryInterval )
247+ continue
248+ }
249+
250+ if isAlertGroupingConfigConsistent (ctx , & expected , & state ) {
251+ resp .Diagnostics .Append (resp .State .Set (ctx , state )... ) // Only update state if config is consistent
232252 return
233253 }
234- resp .Diagnostics .AddError (
235- fmt .Sprintf ("Error reading PagerDuty alert grouping setting %s" , id ),
236- err .Error (),
237- )
238- return
254+ log .Printf ("[WARN] Inconsistent config from PagerDuty API for alert grouping setting %s (attempt %d/%d). Retrying..." , id .ValueString (), i + 1 , maxRetries )
255+ time .Sleep (retryInterval )
256+ }
257+
258+ msg := "PagerDuty API returned inconsistent or incomplete alert grouping setting config after retries. Keeping last known good state. Manual intervention may be required."
259+ log .Printf ("[ERROR] %s ID=%s" , msg , id .ValueString ())
260+ resp .Diagnostics .AddWarning ("PagerDuty API inconsistency" , msg )
261+ if lastErr != nil {
262+ resp .Diagnostics .AddError ("Last error from PagerDuty API" , lastErr .Error ())
239263 }
240- resp .Diagnostics .Append (resp .State .Set (ctx , state )... )
241264}
242265
243266func (r * resourceAlertGroupingSetting ) Update (ctx context.Context , req resource.UpdateRequest , resp * resource.UpdateResponse ) {
@@ -580,3 +603,23 @@ func (r *resourceAlertGroupingSetting) UsesTimeout(ctx context.Context, s Schema
580603 t := pagerduty .AlertGroupingSettingType (typeValue .ValueString ())
581604 return t == pagerduty .AlertGroupingSettingTimeType
582605}
606+
607+ // isAlertGroupingConfigConsistent compares the expected and actual alert grouping config.
608+ func isAlertGroupingConfigConsistent (ctx context.Context , expected , actual * resourceAlertGroupingSettingModel ) bool {
609+ if expected == nil || actual == nil {
610+ return false
611+ }
612+ // Compare Type
613+ if ! expected .Type .Equal (actual .Type ) {
614+ return false
615+ }
616+ // Compare Config (object)
617+ if ! expected .Config .Equal (actual .Config ) {
618+ return false
619+ }
620+ // Compare Services
621+ if ! expected .Services .Equal (actual .Services ) {
622+ return false
623+ }
624+ return true
625+ }
0 commit comments