@@ -3,9 +3,6 @@ package asserts
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "math"
7
-
8
- "time"
9
6
10
7
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11
8
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
@@ -70,28 +67,23 @@ func makeResourceAlertConfig() *common.Resource {
70
67
).WithLister (assertsListerFunction (listAlertConfigs ))
71
68
}
72
69
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
82
74
}
83
75
name := d .Get ("name" ).(string )
84
76
matchLabels := make (map [string ]string )
85
77
alertLabels := make (map [string ]string )
86
78
87
79
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 {} ) {
89
81
matchLabels [k ] = val .(string )
90
82
}
91
83
}
92
84
93
85
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 {} ) {
95
87
alertLabels [k ] = val .(string )
96
88
}
97
89
}
@@ -136,42 +128,23 @@ func resourceAlertConfigCreate(ctx context.Context, d *schema.ResourceData, meta
136
128
return resourceAlertConfigRead (ctx , d , meta )
137
129
}
138
130
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
148
135
}
149
136
name := d .Id ()
150
137
151
138
// Retry logic for read operation to handle eventual consistency
152
139
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 {
164
141
// Get all alert configs using the generated client API
165
142
request := client .AlertConfigurationAPI .GetAllAlertConfigs (ctx ).
166
143
XScopeOrgID (fmt .Sprintf ("%d" , stackID ))
167
144
168
145
alertConfigs , _ , err := request .Execute ()
169
146
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 )
175
148
}
176
149
177
150
// Find our specific config
@@ -182,12 +155,11 @@ func resourceAlertConfigRead(ctx context.Context, d *schema.ResourceData, meta a
182
155
}
183
156
}
184
157
185
- // If we've retried many times and still not found, give up
158
+ // Check if we should give up or retry
186
159
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 )
188
161
}
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 )
191
163
})
192
164
193
165
if err != nil {
@@ -229,29 +201,24 @@ func resourceAlertConfigRead(ctx context.Context, d *schema.ResourceData, meta a
229
201
return nil
230
202
}
231
203
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
241
208
}
242
209
243
210
name := d .Get ("name" ).(string )
244
211
matchLabels := make (map [string ]string )
245
212
alertLabels := make (map [string ]string )
246
213
247
214
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 {} ) {
249
216
matchLabels [k ] = val .(string )
250
217
}
251
218
}
252
219
253
220
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 {} ) {
255
222
alertLabels [k ] = val .(string )
256
223
}
257
224
}
@@ -294,15 +261,10 @@ func resourceAlertConfigUpdate(ctx context.Context, d *schema.ResourceData, meta
294
261
return resourceAlertConfigRead (ctx , d , meta )
295
262
}
296
263
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
306
268
}
307
269
name := d .Id ()
308
270
0 commit comments