@@ -12,6 +12,7 @@ import (
12
12
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
13
13
"github.com/elastic/terraform-provider-elasticstack/internal/models"
14
14
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
15
+ fwdiags "github.com/hashicorp/terraform-plugin-framework/diag"
15
16
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
16
17
)
17
18
@@ -245,16 +246,19 @@ func DeleteIndexTemplate(ctx context.Context, apiClient *clients.ApiClient, temp
245
246
return diags
246
247
}
247
248
248
- func PutIndex (ctx context.Context , apiClient * clients.ApiClient , index * models.Index , params * models.PutIndexParams ) diag.Diagnostics {
249
- var diags diag.Diagnostics
249
+ func PutIndex (ctx context.Context , apiClient * clients.ApiClient , index * models.Index , params * models.PutIndexParams ) fwdiags.Diagnostics {
250
250
indexBytes , err := json .Marshal (index )
251
251
if err != nil {
252
- return diag .FromErr (err )
252
+ return fwdiags.Diagnostics {
253
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
254
+ }
253
255
}
254
256
255
257
esClient , err := apiClient .GetESClient ()
256
258
if err != nil {
257
- return diag .FromErr (err )
259
+ return fwdiags.Diagnostics {
260
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
261
+ }
258
262
}
259
263
260
264
opts := []func (* esapi.IndicesCreateRequest ){
@@ -272,45 +276,46 @@ func PutIndex(ctx context.Context, apiClient *clients.ApiClient, index *models.I
272
276
opts ... ,
273
277
)
274
278
if err != nil {
275
- return diag .FromErr (err )
279
+ return fwdiags.Diagnostics {
280
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
281
+ }
276
282
}
277
283
defer res .Body .Close ()
278
- if diags := utils .CheckError (res , fmt .Sprintf ("Unable to create index: %s" , index .Name )); diags .HasError () {
279
- return diags
280
- }
281
- return diags
284
+ diags := utils .CheckError (res , fmt .Sprintf ("Unable to create index: %s" , index .Name ))
285
+ return utils .FrameworkDiagsFromSDK (diags )
282
286
}
283
287
284
- func DeleteIndex (ctx context.Context , apiClient * clients.ApiClient , name string ) diag.Diagnostics {
285
- var diags diag.Diagnostics
286
-
288
+ func DeleteIndex (ctx context.Context , apiClient * clients.ApiClient , name string ) fwdiags.Diagnostics {
287
289
esClient , err := apiClient .GetESClient ()
288
290
if err != nil {
289
- return diag .FromErr (err )
291
+ return fwdiags.Diagnostics {
292
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
293
+ }
290
294
}
291
295
res , err := esClient .Indices .Delete ([]string {name }, esClient .Indices .Delete .WithContext (ctx ))
292
296
if err != nil {
293
- return diag .FromErr (err )
297
+ return fwdiags.Diagnostics {
298
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
299
+ }
294
300
}
295
301
defer res .Body .Close ()
296
- if diags := utils .CheckError (res , fmt .Sprintf ("Unable to delete the index: %s" , name )); diags .HasError () {
297
- return diags
298
- }
299
-
300
- return diags
302
+ diags := utils .CheckError (res , fmt .Sprintf ("Unable to delete the index: %s" , name ))
303
+ return utils .FrameworkDiagsFromSDK (diags )
301
304
}
302
305
303
- func GetIndex (ctx context.Context , apiClient * clients.ApiClient , name string ) (* models.Index , diag.Diagnostics ) {
304
- var diags diag.Diagnostics
305
-
306
+ func GetIndex (ctx context.Context , apiClient * clients.ApiClient , name string ) (* models.Index , fwdiags.Diagnostics ) {
306
307
esClient , err := apiClient .GetESClient ()
307
308
if err != nil {
308
- return nil , diag .FromErr (err )
309
+ return nil , fwdiags.Diagnostics {
310
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
311
+ }
309
312
}
310
313
req := esClient .Indices .Get .WithFlatSettings (true )
311
314
res , err := esClient .Indices .Get ([]string {name }, req , esClient .Indices .Get .WithContext (ctx ))
312
315
if err != nil {
313
- return nil , diag .FromErr (err )
316
+ return nil , fwdiags.Diagnostics {
317
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
318
+ }
314
319
}
315
320
defer res .Body .Close ()
316
321
// if there is no index found, return the empty struct, which should force the creation of the index
@@ -319,94 +324,104 @@ func GetIndex(ctx context.Context, apiClient *clients.ApiClient, name string) (*
319
324
}
320
325
321
326
if diags := utils .CheckError (res , fmt .Sprintf ("Unable to get requested index: %s" , name )); diags .HasError () {
322
- return nil , diags
327
+ return nil , utils . FrameworkDiagsFromSDK ( diags )
323
328
}
324
329
325
330
indices := make (map [string ]models.Index )
326
331
if err := json .NewDecoder (res .Body ).Decode (& indices ); err != nil {
327
- return nil , diag .FromErr (err )
332
+ return nil , fwdiags.Diagnostics {
333
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
334
+ }
328
335
}
329
336
index := indices [name ]
330
- return & index , diags
337
+ return & index , nil
331
338
}
332
339
333
- func DeleteIndexAlias (ctx context.Context , apiClient * clients.ApiClient , index string , aliases []string ) diag.Diagnostics {
334
- var diags diag.Diagnostics
340
+ func DeleteIndexAlias (ctx context.Context , apiClient * clients.ApiClient , index string , aliases []string ) fwdiags.Diagnostics {
335
341
esClient , err := apiClient .GetESClient ()
336
342
if err != nil {
337
- return diag .FromErr (err )
343
+ return fwdiags.Diagnostics {
344
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
345
+ }
338
346
}
339
347
res , err := esClient .Indices .DeleteAlias ([]string {index }, aliases , esClient .Indices .DeleteAlias .WithContext (ctx ))
340
348
if err != nil {
341
- return diag .FromErr (err )
349
+ return fwdiags.Diagnostics {
350
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
351
+ }
342
352
}
343
353
defer res .Body .Close ()
344
- if diags := utils .CheckError (res , fmt .Sprintf ("Unable to delete aliases '%v' for index '%s'" , index , aliases )); diags .HasError () {
345
- return diags
346
- }
347
- return diags
354
+ diags := utils .CheckError (res , fmt .Sprintf ("Unable to delete aliases '%v' for index '%s'" , index , aliases ))
355
+ return utils .FrameworkDiagsFromSDK (diags )
348
356
}
349
357
350
- func UpdateIndexAlias (ctx context.Context , apiClient * clients.ApiClient , index string , alias * models.IndexAlias ) diag.Diagnostics {
351
- var diags diag.Diagnostics
358
+ func UpdateIndexAlias (ctx context.Context , apiClient * clients.ApiClient , index string , alias * models.IndexAlias ) fwdiags.Diagnostics {
352
359
aliasBytes , err := json .Marshal (alias )
353
360
if err != nil {
354
- return diag .FromErr (err )
361
+ return fwdiags.Diagnostics {
362
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
363
+ }
355
364
}
356
365
esClient , err := apiClient .GetESClient ()
357
366
if err != nil {
358
- return diag .FromErr (err )
367
+ return fwdiags.Diagnostics {
368
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
369
+ }
359
370
}
360
371
req := esClient .Indices .PutAlias .WithBody (bytes .NewReader (aliasBytes ))
361
372
res , err := esClient .Indices .PutAlias ([]string {index }, alias .Name , req , esClient .Indices .PutAlias .WithContext (ctx ))
362
373
if err != nil {
363
- return diag .FromErr (err )
374
+ return fwdiags.Diagnostics {
375
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
376
+ }
364
377
}
365
378
defer res .Body .Close ()
366
- if diags := utils .CheckError (res , fmt .Sprintf ("Unable to update alias '%v' for index '%s'" , index , alias .Name )); diags .HasError () {
367
- return diags
368
- }
369
- return diags
379
+ diags := utils .CheckError (res , fmt .Sprintf ("Unable to update alias '%v' for index '%s'" , index , alias .Name ))
380
+ return utils .FrameworkDiagsFromSDK (diags )
370
381
}
371
382
372
- func UpdateIndexSettings (ctx context.Context , apiClient * clients.ApiClient , index string , settings map [string ]interface {}) diag.Diagnostics {
373
- var diags diag.Diagnostics
383
+ func UpdateIndexSettings (ctx context.Context , apiClient * clients.ApiClient , index string , settings map [string ]interface {}) fwdiags.Diagnostics {
374
384
settingsBytes , err := json .Marshal (settings )
375
385
if err != nil {
376
- return diag .FromErr (err )
386
+ return fwdiags.Diagnostics {
387
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
388
+ }
377
389
}
378
390
esClient , err := apiClient .GetESClient ()
379
391
if err != nil {
380
- return diag .FromErr (err )
392
+ return fwdiags.Diagnostics {
393
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
394
+ }
381
395
}
382
396
req := esClient .Indices .PutSettings .WithIndex (index )
383
397
res , err := esClient .Indices .PutSettings (bytes .NewReader (settingsBytes ), req , esClient .Indices .PutSettings .WithContext (ctx ))
384
398
if err != nil {
385
- return diag .FromErr (err )
399
+ return fwdiags.Diagnostics {
400
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
401
+ }
386
402
}
387
403
defer res .Body .Close ()
388
- if diags := utils .CheckError (res , "Unable to update index settings" ); diags .HasError () {
389
- return diags
390
- }
391
- return diags
404
+ diags := utils .CheckError (res , "Unable to update index settings" )
405
+ return utils .FrameworkDiagsFromSDK (diags )
392
406
}
393
407
394
- func UpdateIndexMappings (ctx context.Context , apiClient * clients.ApiClient , index , mappings string ) diag.Diagnostics {
395
- var diags diag.Diagnostics
408
+ func UpdateIndexMappings (ctx context.Context , apiClient * clients.ApiClient , index , mappings string ) fwdiags.Diagnostics {
396
409
esClient , err := apiClient .GetESClient ()
397
410
if err != nil {
398
- return diag .FromErr (err )
411
+ return fwdiags.Diagnostics {
412
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
413
+ }
399
414
}
400
415
req := esClient .Indices .PutMapping .WithIndex (index )
401
416
res , err := esClient .Indices .PutMapping (strings .NewReader (mappings ), req , esClient .Indices .PutMapping .WithContext (ctx ))
402
417
if err != nil {
403
- return diag .FromErr (err )
418
+ return fwdiags.Diagnostics {
419
+ fwdiags .NewErrorDiagnostic (err .Error (), err .Error ()),
420
+ }
404
421
}
405
422
defer res .Body .Close ()
406
- if diags := utils .CheckError (res , "Unable to update index mappings" ); diags .HasError () {
407
- return diags
408
- }
409
- return diags
423
+ diags := utils .CheckError (res , "Unable to update index mappings" )
424
+ return utils .FrameworkDiagsFromSDK (diags )
410
425
}
411
426
412
427
func PutDataStream (ctx context.Context , apiClient * clients.ApiClient , dataStreamName string ) diag.Diagnostics {
0 commit comments