@@ -9,19 +9,20 @@ import (
9
9
"strings"
10
10
11
11
"github.com/go-test/deep"
12
+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13
14
"github.com/mwielbut/pointy"
14
15
matlas "go.mongodb.org/atlas/mongodbatlas"
15
16
)
16
17
17
18
func resourceMongoDBAtlasSearchIndex () * schema.Resource {
18
19
return & schema.Resource {
19
- Create : resourceMongoDBAtlasSearchIndexCreate ,
20
- Read : resourceMongoDBAtlasSearchIndexRead ,
21
- Update : resourceMongoDBAtlasSearchIndexUpdate ,
22
- Delete : resourceMongoDBAtlasSearchIndexDelete ,
20
+ CreateContext : resourceMongoDBAtlasSearchIndexCreate ,
21
+ ReadContext : resourceMongoDBAtlasSearchIndexRead ,
22
+ UpdateContext : resourceMongoDBAtlasSearchIndexUpdate ,
23
+ DeleteContext : resourceMongoDBAtlasSearchIndexDelete ,
23
24
Importer : & schema.ResourceImporter {
24
- State : resourceMongoDBAtlasSearchIndexImportState ,
25
+ StateContext : resourceMongoDBAtlasSearchIndexImportState ,
25
26
},
26
27
Schema : returnSearchIndexSchema (),
27
28
}
@@ -227,7 +228,7 @@ func customAnalyzersSchema() *schema.Resource {
227
228
}
228
229
}
229
230
230
- func resourceMongoDBAtlasSearchIndexImportState (d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
231
+ func resourceMongoDBAtlasSearchIndexImportState (ctx context. Context , d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
231
232
conn := meta .(* MongoDBClient ).Atlas
232
233
233
234
parts := strings .SplitN (d .Id (), "--" , 3 )
@@ -239,7 +240,7 @@ func resourceMongoDBAtlasSearchIndexImportState(d *schema.ResourceData, meta int
239
240
clusterName := parts [1 ]
240
241
indexID := parts [2 ]
241
242
242
- _ , _ , err := conn .Search .GetIndex (context . Background () , projectID , clusterName , indexID )
243
+ _ , _ , err := conn .Search .GetIndex (ctx , projectID , clusterName , indexID )
243
244
if err != nil {
244
245
return nil , fmt .Errorf ("couldn't import search index (%s) in projectID (%s) and Cluster (%s), error: %s" , indexID , projectID , clusterName , err )
245
246
}
@@ -265,33 +266,33 @@ func resourceMongoDBAtlasSearchIndexImportState(d *schema.ResourceData, meta int
265
266
return []* schema.ResourceData {d }, nil
266
267
}
267
268
268
- func resourceMongoDBAtlasSearchIndexDelete (d * schema.ResourceData , meta interface {}) error {
269
+ func resourceMongoDBAtlasSearchIndexDelete (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
269
270
// Get client connection.
270
271
conn := meta .(* MongoDBClient ).Atlas
271
272
ids := decodeStateID (d .Id ())
272
273
projectID := ids ["project_id" ]
273
274
clusterName := ids ["cluster_name" ]
274
275
indexID := ids ["index_id" ]
275
276
276
- _ , err := conn .Search .DeleteIndex (context . Background () , projectID , clusterName , indexID )
277
+ _ , err := conn .Search .DeleteIndex (ctx , projectID , clusterName , indexID )
277
278
if err != nil {
278
- return fmt .Errorf ("error deleting search index (%s): %s" , d .Get ("name" ).(string ), err )
279
+ return diag .Errorf ("error deleting search index (%s): %s" , d .Get ("name" ).(string ), err )
279
280
}
280
281
281
282
return nil
282
283
}
283
284
284
- func resourceMongoDBAtlasSearchIndexUpdate (d * schema.ResourceData , meta interface {}) error {
285
+ func resourceMongoDBAtlasSearchIndexUpdate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
285
286
// Get client connection.
286
287
conn := meta .(* MongoDBClient ).Atlas
287
288
ids := decodeStateID (d .Id ())
288
289
projectID := ids ["project_id" ]
289
290
clusterName := ids ["cluster_name" ]
290
291
indexID := ids ["index_id" ]
291
292
292
- searchIndex , _ , err := conn .Search .GetIndex (context . Background () , projectID , clusterName , indexID )
293
+ searchIndex , _ , err := conn .Search .GetIndex (ctx , projectID , clusterName , indexID )
293
294
if err != nil {
294
- return fmt .Errorf ("error getting search index information: %s" , err )
295
+ return diag .Errorf ("error getting search index information: %s" , err )
295
296
}
296
297
297
298
if d .HasChange ("analyzer" ) {
@@ -332,21 +333,21 @@ func resourceMongoDBAtlasSearchIndexUpdate(d *schema.ResourceData, meta interfac
332
333
searchIndex .IndexID = ""
333
334
_ , _ , err = conn .Search .UpdateIndex (context .Background (), projectID , clusterName , indexID , searchIndex )
334
335
if err != nil {
335
- return fmt .Errorf ("error updating search index (%s): %s" , searchIndex .Name , err )
336
+ return diag .Errorf ("error updating search index (%s): %s" , searchIndex .Name , err )
336
337
}
337
338
338
- return resourceMongoDBAtlasSearchIndexRead (d , meta )
339
+ return resourceMongoDBAtlasSearchIndexRead (ctx , d , meta )
339
340
}
340
341
341
- func resourceMongoDBAtlasSearchIndexRead (d * schema.ResourceData , meta interface {}) error {
342
+ func resourceMongoDBAtlasSearchIndexRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
342
343
// Get client connection.
343
344
conn := meta .(* MongoDBClient ).Atlas
344
345
ids := decodeStateID (d .Id ())
345
346
projectID := ids ["project_id" ]
346
347
clusterName := ids ["cluster_name" ]
347
348
indexID := ids ["index_id" ]
348
349
349
- searchIndex , _ , err := conn .Search .GetIndex (context . Background () , projectID , clusterName , indexID )
350
+ searchIndex , _ , err := conn .Search .GetIndex (ctx , projectID , clusterName , indexID )
350
351
if err != nil {
351
352
// case 404
352
353
// deleted in the backend case
@@ -357,53 +358,53 @@ func resourceMongoDBAtlasSearchIndexRead(d *schema.ResourceData, meta interface{
357
358
return nil
358
359
}
359
360
360
- return fmt .Errorf ("error getting search index information: %s" , err )
361
+ return diag .Errorf ("error getting search index information: %s" , err )
361
362
}
362
363
if err := d .Set ("index_id" , indexID ); err != nil {
363
- return fmt .Errorf ("error setting `index_id` for search index (%s): %s" , d .Id (), err )
364
+ return diag .Errorf ("error setting `index_id` for search index (%s): %s" , d .Id (), err )
364
365
}
365
366
366
367
if err := d .Set ("analyzer" , searchIndex .Analyzer ); err != nil {
367
- return fmt .Errorf ("error setting `analyzer` for search index (%s): %s" , d .Id (), err )
368
+ return diag .Errorf ("error setting `analyzer` for search index (%s): %s" , d .Id (), err )
368
369
}
369
370
370
371
searchIndexCustomAnalyzers , err := flattenSearchIndexCustomAnalyzers (searchIndex .Analyzers )
371
372
if err != nil {
372
- return err
373
+ return diag . FromErr ( err )
373
374
}
374
375
375
376
if err := d .Set ("analyzers" , searchIndexCustomAnalyzers ); err != nil {
376
- return fmt .Errorf ("error setting `analyzer` for search index (%s): %s" , d .Id (), err )
377
+ return diag .Errorf ("error setting `analyzer` for search index (%s): %s" , d .Id (), err )
377
378
}
378
379
379
380
if err := d .Set ("collection_name" , searchIndex .CollectionName ); err != nil {
380
- return fmt .Errorf ("error setting `collectionName` for search index (%s): %s" , d .Id (), err )
381
+ return diag .Errorf ("error setting `collectionName` for search index (%s): %s" , d .Id (), err )
381
382
}
382
383
383
384
if err := d .Set ("database" , searchIndex .Database ); err != nil {
384
- return fmt .Errorf ("error setting `database` for search index (%s): %s" , d .Id (), err )
385
+ return diag .Errorf ("error setting `database` for search index (%s): %s" , d .Id (), err )
385
386
}
386
387
387
388
if err := d .Set ("name" , searchIndex .Name ); err != nil {
388
- return fmt .Errorf ("error setting `name` for search index (%s): %s" , d .Id (), err )
389
+ return diag .Errorf ("error setting `name` for search index (%s): %s" , d .Id (), err )
389
390
}
390
391
391
392
if err := d .Set ("search_analyzer" , searchIndex .SearchAnalyzer ); err != nil {
392
- return fmt .Errorf ("error setting `searchAnalyzer` for search index (%s): %s" , d .Id (), err )
393
+ return diag .Errorf ("error setting `searchAnalyzer` for search index (%s): %s" , d .Id (), err )
393
394
}
394
395
395
396
if err := d .Set ("mappings_dynamic" , searchIndex .Mappings .Dynamic ); err != nil {
396
- return fmt .Errorf ("error setting `mappings_dynamic` for search index (%s): %s" , d .Id (), err )
397
+ return diag .Errorf ("error setting `mappings_dynamic` for search index (%s): %s" , d .Id (), err )
397
398
}
398
399
399
400
if searchIndex .Mappings .Fields != nil {
400
401
searchIndexMappingFields , err := marshallSearchIndexMappingFields (* searchIndex .Mappings .Fields )
401
402
if err != nil {
402
- return err
403
+ return diag . FromErr ( err )
403
404
}
404
405
405
406
if err := d .Set ("mappings_fields" , searchIndexMappingFields ); err != nil {
406
- return fmt .Errorf ("error setting `mappings_fields` for for search index (%s): %s" , d .Id (), err )
407
+ return diag .Errorf ("error setting `mappings_fields` for for search index (%s): %s" , d .Id (), err )
407
408
}
408
409
}
409
410
@@ -597,7 +598,7 @@ func flattenSearchIndexCharFilters(filters []*matlas.AnalyzerCharFilter) ([]map[
597
598
return mapCharFilters , nil
598
599
}
599
600
600
- func resourceMongoDBAtlasSearchIndexCreate (d * schema.ResourceData , meta interface {}) error {
601
+ func resourceMongoDBAtlasSearchIndexCreate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
601
602
// Get client connection.
602
603
conn := meta .(* MongoDBClient ).Atlas
603
604
projectID := d .Get ("project_id" ).(string )
@@ -622,9 +623,9 @@ func resourceMongoDBAtlasSearchIndexCreate(d *schema.ResourceData, meta interfac
622
623
Status : d .Get ("status" ).(string ),
623
624
}
624
625
625
- dbSearchIndexRes , _ , err := conn .Search .CreateIndex (context . Background () , projectID , clusterName , searchIndexRequest )
626
+ dbSearchIndexRes , _ , err := conn .Search .CreateIndex (ctx , projectID , clusterName , searchIndexRequest )
626
627
if err != nil {
627
- return fmt .Errorf ("error creating database user: %s" , err )
628
+ return diag .Errorf ("error creating database user: %s" , err )
628
629
}
629
630
630
631
d .SetId (encodeStateID (map [string ]string {
@@ -633,7 +634,7 @@ func resourceMongoDBAtlasSearchIndexCreate(d *schema.ResourceData, meta interfac
633
634
"index_id" : dbSearchIndexRes .IndexID ,
634
635
}))
635
636
636
- return resourceMongoDBAtlasSearchIndexRead (d , meta )
637
+ return resourceMongoDBAtlasSearchIndexRead (ctx , d , meta )
637
638
}
638
639
639
640
func expandCustomAnalyzers (analyzers []interface {}) []* matlas.CustomAnalyzer {
0 commit comments