@@ -3,9 +3,6 @@ package asserts
33import (
44 "context"
55 "fmt"
6- "math"
7-
8- "time"
96
107 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
118 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
@@ -70,28 +67,23 @@ func makeResourceAlertConfig() *common.Resource {
7067 ).WithLister (assertsListerFunction (listAlertConfigs ))
7168}
7269
73- func resourceAlertConfigCreate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
74- client := meta .(* common.Client ).AssertsAPIClient
75- if client == nil {
76- return diag .Errorf ("Asserts API client is not configured" )
77- }
78-
79- stackID := meta .(* common.Client ).GrafanaStackID
80- if stackID == 0 {
81- return diag .Errorf ("stack_id must be set in provider configuration for Asserts resources" )
70+ func resourceAlertConfigCreate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
71+ client , stackID , diags := validateAssertsClient (meta )
72+ if diags .HasError () {
73+ return diags
8274 }
8375 name := d .Get ("name" ).(string )
8476 matchLabels := make (map [string ]string )
8577 alertLabels := make (map [string ]string )
8678
8779 if v , ok := d .GetOk ("match_labels" ); ok {
88- for k , val := range v .(map [string ]any ) {
80+ for k , val := range v .(map [string ]interface {} ) {
8981 matchLabels [k ] = val .(string )
9082 }
9183 }
9284
9385 if v , ok := d .GetOk ("alert_labels" ); ok {
94- for k , val := range v .(map [string ]any ) {
86+ for k , val := range v .(map [string ]interface {} ) {
9587 alertLabels [k ] = val .(string )
9688 }
9789 }
@@ -136,42 +128,23 @@ func resourceAlertConfigCreate(ctx context.Context, d *schema.ResourceData, meta
136128 return resourceAlertConfigRead (ctx , d , meta )
137129}
138130
139- func resourceAlertConfigRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
140- client := meta .(* common.Client ).AssertsAPIClient
141- if client == nil {
142- return diag .Errorf ("Asserts API client is not configured" )
143- }
144-
145- stackID := meta .(* common.Client ).GrafanaStackID
146- if stackID == 0 {
147- return diag .Errorf ("stack_id must be set in provider configuration for Asserts resources" )
131+ func resourceAlertConfigRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
132+ client , stackID , diags := validateAssertsClient (meta )
133+ if diags .HasError () {
134+ return diags
148135 }
149136 name := d .Id ()
150137
151138 // Retry logic for read operation to handle eventual consistency
152139 var foundConfig * assertsapi.AlertConfigDto
153- retryCount := 0
154- maxRetries := 10
155- err := retry .RetryContext (ctx , 60 * time .Second , func () * retry.RetryError {
156- retryCount ++
157-
158- // Exponential backoff: 1s, 2s, 4s, 8s, etc. (capped at 8s)
159- if retryCount > 1 {
160- backoffDuration := time .Duration (1 << int (math .Min (float64 (retryCount - 2 ), 3 ))) * time .Second
161- time .Sleep (backoffDuration )
162- }
163-
140+ err := withRetryRead (ctx , func (retryCount , maxRetries int ) * retry.RetryError {
164141 // Get all alert configs using the generated client API
165142 request := client .AlertConfigurationAPI .GetAllAlertConfigs (ctx ).
166143 XScopeOrgID (fmt .Sprintf ("%d" , stackID ))
167144
168145 alertConfigs , _ , err := request .Execute ()
169146 if err != nil {
170- // If we've retried many times and still getting API errors, give up
171- if retryCount >= maxRetries {
172- return retry .NonRetryableError (fmt .Errorf ("failed to get alert configurations after %d retries: %w" , retryCount , err ))
173- }
174- return retry .RetryableError (fmt .Errorf ("failed to get alert configurations: %w" , err ))
147+ return createAPIError ("get alert configurations" , retryCount , maxRetries , err )
175148 }
176149
177150 // Find our specific config
@@ -182,12 +155,11 @@ func resourceAlertConfigRead(ctx context.Context, d *schema.ResourceData, meta a
182155 }
183156 }
184157
185- // If we've retried many times and still not found, give up
158+ // Check if we should give up or retry
186159 if retryCount >= maxRetries {
187- return retry . NonRetryableError ( fmt . Errorf ( "alert configuration %s not found after %d retries - may indicate a permanent issue " , name , retryCount ) )
160+ return createNonRetryableError ( "alert configuration" , name , retryCount )
188161 }
189-
190- return retry .RetryableError (fmt .Errorf ("alert configuration %s not found (attempt %d/%d)" , name , retryCount , maxRetries ))
162+ return createRetryableError ("alert configuration" , name , retryCount , maxRetries )
191163 })
192164
193165 if err != nil {
@@ -229,29 +201,24 @@ func resourceAlertConfigRead(ctx context.Context, d *schema.ResourceData, meta a
229201 return nil
230202}
231203
232- func resourceAlertConfigUpdate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
233- client := meta .(* common.Client ).AssertsAPIClient
234- if client == nil {
235- return diag .Errorf ("Asserts API client is not configured" )
236- }
237-
238- stackID := meta .(* common.Client ).GrafanaStackID
239- if stackID == 0 {
240- return diag .Errorf ("stack_id must be set in provider configuration for Asserts resources" )
204+ func resourceAlertConfigUpdate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
205+ client , stackID , diags := validateAssertsClient (meta )
206+ if diags .HasError () {
207+ return diags
241208 }
242209
243210 name := d .Get ("name" ).(string )
244211 matchLabels := make (map [string ]string )
245212 alertLabels := make (map [string ]string )
246213
247214 if v , ok := d .GetOk ("match_labels" ); ok {
248- for k , val := range v .(map [string ]any ) {
215+ for k , val := range v .(map [string ]interface {} ) {
249216 matchLabels [k ] = val .(string )
250217 }
251218 }
252219
253220 if v , ok := d .GetOk ("alert_labels" ); ok {
254- for k , val := range v .(map [string ]any ) {
221+ for k , val := range v .(map [string ]interface {} ) {
255222 alertLabels [k ] = val .(string )
256223 }
257224 }
@@ -294,15 +261,10 @@ func resourceAlertConfigUpdate(ctx context.Context, d *schema.ResourceData, meta
294261 return resourceAlertConfigRead (ctx , d , meta )
295262}
296263
297- func resourceAlertConfigDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
298- client := meta .(* common.Client ).AssertsAPIClient
299- if client == nil {
300- return diag .Errorf ("Asserts API client is not configured" )
301- }
302-
303- stackID := meta .(* common.Client ).GrafanaStackID
304- if stackID == 0 {
305- return diag .Errorf ("stack_id must be set in provider configuration for Asserts resources" )
264+ func resourceAlertConfigDelete (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
265+ client , stackID , diags := validateAssertsClient (meta )
266+ if diags .HasError () {
267+ return diags
306268 }
307269 name := d .Id ()
308270
0 commit comments