@@ -16,7 +16,7 @@ import (
16
16
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
17
17
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
18
18
"github.com/mwielbut/pointy"
19
- admin20231001002 "go.mongodb.org/atlas-sdk/v20231001002 /admin"
19
+ "go.mongodb.org/atlas-sdk/v20231115005 /admin"
20
20
)
21
21
22
22
const (
@@ -27,19 +27,19 @@ const (
27
27
28
28
func Resource () * schema.Resource {
29
29
return & schema.Resource {
30
- Schema : getMongoDBAtlasOnlineArchiveSchema (),
31
- CreateContext : resourceMongoDBAtlasOnlineArchiveCreate ,
32
- ReadContext : resourceMongoDBAtlasOnlineArchiveRead ,
33
- DeleteContext : resourceMongoDBAtlasOnlineArchiveDelete ,
34
- UpdateContext : resourceMongoDBAtlasOnlineArchiveUpdate ,
30
+ Schema : resourceSchema (),
31
+ CreateContext : resourceCreate ,
32
+ ReadContext : resourceRead ,
33
+ DeleteContext : resourceDelete ,
34
+ UpdateContext : resourceUpdate ,
35
35
Importer : & schema.ResourceImporter {
36
- StateContext : resourceMongoDBAtlasOnlineArchiveImportState ,
36
+ StateContext : resourceImport ,
37
37
},
38
38
}
39
39
}
40
40
41
41
// https://docs.atlas.mongodb.com/reference/api/online-archive-create-one
42
- func getMongoDBAtlasOnlineArchiveSchema () map [string ]* schema.Schema {
42
+ func resourceSchema () map [string ]* schema.Schema {
43
43
return map [string ]* schema.Schema {
44
44
// argument values
45
45
"project_id" : {
@@ -217,13 +217,13 @@ func getMongoDBAtlasOnlineArchiveSchema() map[string]*schema.Schema {
217
217
}
218
218
}
219
219
220
- func resourceMongoDBAtlasOnlineArchiveCreate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
221
- conn20231001002 := meta .(* config.MongoDBClient ).Atlas20231001002
220
+ func resourceCreate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
221
+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
222
222
projectID := d .Get ("project_id" ).(string )
223
223
clusterName := d .Get ("cluster_name" ).(string )
224
224
225
225
inputRequest := mapToArchivePayload (d )
226
- outputRequest , _ , err := conn20231001002 .OnlineArchiveApi .CreateOnlineArchive (ctx , projectID , clusterName , & inputRequest ).Execute ()
226
+ outputRequest , _ , err := connV2 .OnlineArchiveApi .CreateOnlineArchive (ctx , projectID , clusterName , & inputRequest ).Execute ()
227
227
228
228
if err != nil {
229
229
return diag .FromErr (fmt .Errorf (errorOnlineArchivesCreate , err ))
@@ -241,7 +241,7 @@ func resourceMongoDBAtlasOnlineArchiveCreate(ctx context.Context, d *schema.Reso
241
241
stateConf := & retry.StateChangeConf {
242
242
Pending : []string {"PENDING" , "ARCHIVING" , "PAUSING" , "PAUSED" , "ORPHANED" , "REPEATING" },
243
243
Target : []string {"IDLE" , "ACTIVE" },
244
- Refresh : resourceOnlineRefreshFunc (ctx , projectID , clusterName , archiveID , conn20231001002 ),
244
+ Refresh : resourceOnlineRefreshFunc (ctx , projectID , clusterName , archiveID , connV2 ),
245
245
Timeout : 3 * time .Hour ,
246
246
MinTimeout : 1 * time .Minute ,
247
247
Delay : 3 * time .Minute ,
@@ -254,10 +254,10 @@ func resourceMongoDBAtlasOnlineArchiveCreate(ctx context.Context, d *schema.Reso
254
254
}
255
255
}
256
256
257
- return resourceMongoDBAtlasOnlineArchiveRead (ctx , d , meta )
257
+ return resourceRead (ctx , d , meta )
258
258
}
259
259
260
- func resourceOnlineRefreshFunc (ctx context.Context , projectID , clusterName , archiveID string , client * admin20231001002 .APIClient ) retry.StateRefreshFunc {
260
+ func resourceOnlineRefreshFunc (ctx context.Context , projectID , clusterName , archiveID string , client * admin .APIClient ) retry.StateRefreshFunc {
261
261
return func () (any , string , error ) {
262
262
c , resp , err := client .OnlineArchiveApi .GetOnlineArchive (ctx , projectID , archiveID , clusterName ).Execute ()
263
263
@@ -285,15 +285,15 @@ func resourceOnlineRefreshFunc(ctx context.Context, projectID, clusterName, arch
285
285
}
286
286
}
287
287
288
- func resourceMongoDBAtlasOnlineArchiveRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
289
- conn20231001002 := meta .(* config.MongoDBClient ).Atlas20231001002
288
+ func resourceRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
289
+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
290
290
ids := conversion .DecodeStateID (d .Id ())
291
291
292
292
archiveID := ids ["archive_id" ]
293
293
projectID := ids ["project_id" ]
294
294
clusterName := ids ["cluster_name" ]
295
295
296
- onlineArchive , resp , err := conn20231001002 .OnlineArchiveApi .GetOnlineArchive (context .Background (), projectID , archiveID , clusterName ).Execute ()
296
+ onlineArchive , resp , err := connV2 .OnlineArchiveApi .GetOnlineArchive (context .Background (), projectID , archiveID , clusterName ).Execute ()
297
297
if err != nil {
298
298
if resp != nil && resp .StatusCode == http .StatusNotFound {
299
299
d .SetId ("" )
@@ -312,7 +312,7 @@ func resourceMongoDBAtlasOnlineArchiveRead(ctx context.Context, d *schema.Resour
312
312
return nil
313
313
}
314
314
315
- func resourceMongoDBAtlasOnlineArchiveDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
315
+ func resourceDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
316
316
conn := meta .(* config.MongoDBClient ).Atlas
317
317
ids := conversion .DecodeStateID (d .Id ())
318
318
atlasID := ids ["archive_id" ]
@@ -332,8 +332,8 @@ func resourceMongoDBAtlasOnlineArchiveDelete(ctx context.Context, d *schema.Reso
332
332
return nil
333
333
}
334
334
335
- func resourceMongoDBAtlasOnlineArchiveImportState (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
336
- conn20231001002 := meta .(* config.MongoDBClient ).Atlas20231001002
335
+ func resourceImport (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
336
+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
337
337
parts := strings .Split (d .Id (), "-" )
338
338
339
339
var projectID , clusterName , archiveID string
@@ -350,7 +350,7 @@ func resourceMongoDBAtlasOnlineArchiveImportState(ctx context.Context, d *schema
350
350
projectID , clusterName , archiveID = parts [0 ], parts [1 ], parts [2 ]
351
351
}
352
352
353
- outOnlineArchive , _ , err := conn20231001002 .OnlineArchiveApi .GetOnlineArchive (ctx , projectID , archiveID , clusterName ).Execute ()
353
+ outOnlineArchive , _ , err := connV2 .OnlineArchiveApi .GetOnlineArchive (ctx , projectID , archiveID , clusterName ).Execute ()
354
354
355
355
if err != nil {
356
356
return nil , fmt .Errorf ("could not import Online Archive %s in project %s, error %s" , archiveID , projectID , err .Error ())
@@ -377,9 +377,9 @@ func resourceMongoDBAtlasOnlineArchiveImportState(ctx context.Context, d *schema
377
377
return []* schema.ResourceData {d }, nil
378
378
}
379
379
380
- func mapToArchivePayload (d * schema.ResourceData ) admin20231001002 .BackupOnlineArchiveCreate {
380
+ func mapToArchivePayload (d * schema.ResourceData ) admin .BackupOnlineArchiveCreate {
381
381
// shared input
382
- requestInput := admin20231001002 .BackupOnlineArchiveCreate {
382
+ requestInput := admin .BackupOnlineArchiveCreate {
383
383
DbName : d .Get ("db_name" ).(string ),
384
384
CollName : d .Get ("coll_name" ).(string ),
385
385
}
@@ -393,57 +393,55 @@ func mapToArchivePayload(d *schema.ResourceData) admin20231001002.BackupOnlineAr
393
393
requestInput .Schedule = mapSchedule (d )
394
394
395
395
if partitions , ok := d .GetOk ("partition_fields" ); ok {
396
- list := partitions .([]any )
397
-
398
- if len (list ) > 0 {
399
- partitionList := make ([]admin20231001002.PartitionField , 0 , len (list ))
396
+ if list := partitions .([]any ); len (list ) > 0 {
397
+ partitionList := make ([]admin.PartitionField , 0 , len (list ))
400
398
for _ , partition := range list {
401
399
item := partition .(map [string ]any )
402
- query := admin20231001002 .PartitionField {
400
+ query := admin .PartitionField {
403
401
FieldName : item ["field_name" ].(string ),
404
402
Order : item ["order" ].(int ),
405
403
}
406
-
407
404
if dbType , ok := item ["field_type" ]; ok && dbType != nil {
408
405
if dbType .(string ) != "" {
409
- query .FieldType = admin20231001002 .PtrString (dbType .(string ))
406
+ query .FieldType = admin .PtrString (dbType .(string ))
410
407
}
411
408
}
412
-
413
409
partitionList = append (partitionList , query )
414
410
}
415
-
416
- requestInput .PartitionFields = partitionList
411
+ requestInput .PartitionFields = & partitionList
417
412
}
418
413
}
419
414
420
415
return requestInput
421
416
}
422
417
423
- func resourceMongoDBAtlasOnlineArchiveUpdate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
424
- conn20231001002 := meta .(* config.MongoDBClient ).Atlas20231001002
418
+ func resourceUpdate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
419
+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
425
420
426
421
ids := conversion .DecodeStateID (d .Id ())
427
422
428
423
atlasID := ids ["archive_id" ]
429
424
projectID := ids ["project_id" ]
430
425
clusterName := ids ["cluster_name" ]
431
426
427
+ if dataProcessRegionHasChange := d .HasChange ("data_process_region" ); dataProcessRegionHasChange {
428
+ return diag .FromErr (fmt .Errorf ("error updating Mongo Online Archive id: %s, data_process_region can't be modified" , atlasID ))
429
+ }
430
+
432
431
// if the criteria or the pausedHasChange is enable then perform an update
433
432
pausedHasChange := d .HasChange ("paused" )
434
433
criteriaHasChange := d .HasChange ("criteria" )
435
434
dataExpirationRuleHasChange := d .HasChange ("data_expiration_rule" )
436
- dataProcessRegionHasChange := d .HasChange ("data_process_region" )
437
435
scheduleHasChange := d .HasChange ("schedule" )
438
436
439
437
collectionTypeHasChange := d .HasChange ("collection_type" )
440
438
441
439
// nothing to do, let's go
442
- if ! pausedHasChange && ! criteriaHasChange && ! collectionTypeHasChange && ! scheduleHasChange && ! dataExpirationRuleHasChange && ! dataProcessRegionHasChange {
440
+ if ! pausedHasChange && ! criteriaHasChange && ! collectionTypeHasChange && ! scheduleHasChange && ! dataExpirationRuleHasChange {
443
441
return nil
444
442
}
445
443
446
- request := admin20231001002 .BackupOnlineArchive {}
444
+ request := admin .BackupOnlineArchive {}
447
445
448
446
// reading current value
449
447
if pausedHasChange {
@@ -459,39 +457,30 @@ func resourceMongoDBAtlasOnlineArchiveUpdate(ctx context.Context, d *schema.Reso
459
457
newExpirationRule := mapDataExpirationRule (d )
460
458
if newExpirationRule == nil {
461
459
// expiration rule has been removed from tf config, empty dataExpirationRule object needs to be sent in patch request
462
- request .DataExpirationRule = & admin20231001002 .DataExpirationRule {}
460
+ request .DataExpirationRule = & admin .DataExpirationRule {}
463
461
} else {
464
462
request .DataExpirationRule = newExpirationRule
465
463
}
466
464
}
467
465
468
- if dataProcessRegionHasChange {
469
- newDataProcessRegion := mapDataProcessRegion (d )
470
- if newDataProcessRegion == nil {
471
- request .DataProcessRegion = & admin20231001002.DataProcessRegion {}
472
- } else {
473
- request .DataProcessRegion = newDataProcessRegion
474
- }
475
- }
476
-
477
466
if scheduleHasChange {
478
467
request .Schedule = mapSchedule (d )
479
468
}
480
469
481
470
if collType := d .Get ("collection_type" ).(string ); collectionTypeHasChange && collType != "" {
482
- request .CollectionType = admin20231001002 .PtrString (collType )
471
+ request .CollectionType = admin .PtrString (collType )
483
472
}
484
473
485
- _ , _ , err := conn20231001002 .OnlineArchiveApi .UpdateOnlineArchive (ctx , projectID , atlasID , clusterName , & request ).Execute ()
474
+ _ , _ , err := connV2 .OnlineArchiveApi .UpdateOnlineArchive (ctx , projectID , atlasID , clusterName , & request ).Execute ()
486
475
487
476
if err != nil {
488
477
return diag .FromErr (fmt .Errorf ("error updating Mongo Online Archive id: %s %s" , atlasID , err .Error ()))
489
478
}
490
479
491
- return resourceMongoDBAtlasOnlineArchiveRead (ctx , d , meta )
480
+ return resourceRead (ctx , d , meta )
492
481
}
493
482
494
- func fromOnlineArchiveToMap (in * admin20231001002 .BackupOnlineArchive ) map [string ]any {
483
+ func fromOnlineArchiveToMap (in * admin .BackupOnlineArchive ) map [string ]any {
495
484
// computed attribute
496
485
schemaVals := map [string ]any {
497
486
"cluster_name" : in .ClusterName ,
@@ -567,7 +556,7 @@ func fromOnlineArchiveToMap(in *admin20231001002.BackupOnlineArchive) map[string
567
556
schemaVals ["data_process_region" ] = []any {dataProcessRegion }
568
557
}
569
558
570
- partitionFields := in .PartitionFields
559
+ partitionFields := in .GetPartitionFields ()
571
560
if len (partitionFields ) == 0 {
572
561
return schemaVals
573
562
}
@@ -586,10 +575,10 @@ func fromOnlineArchiveToMap(in *admin20231001002.BackupOnlineArchive) map[string
586
575
return schemaVals
587
576
}
588
577
589
- func mapDataExpirationRule (d * schema.ResourceData ) * admin20231001002 .DataExpirationRule {
578
+ func mapDataExpirationRule (d * schema.ResourceData ) * admin .DataExpirationRule {
590
579
if dataExpireRules , ok := d .GetOk ("data_expiration_rule" ); ok && len (dataExpireRules .([]any )) > 0 {
591
580
dataExpireRule := dataExpireRules .([]any )[0 ].(map [string ]any )
592
- result := admin20231001002 .DataExpirationRule {}
581
+ result := admin .DataExpirationRule {}
593
582
if expireAfterDays , ok := dataExpireRule ["expire_after_days" ]; ok {
594
583
result .ExpireAfterDays = pointy .Int (expireAfterDays .(int ))
595
584
}
@@ -598,10 +587,10 @@ func mapDataExpirationRule(d *schema.ResourceData) *admin20231001002.DataExpirat
598
587
return nil
599
588
}
600
589
601
- func mapDataProcessRegion (d * schema.ResourceData ) * admin20231001002. DataProcessRegion {
590
+ func mapDataProcessRegion (d * schema.ResourceData ) * admin. CreateDataProcessRegion {
602
591
if dataProcessRegions , ok := d .GetOk ("data_process_region" ); ok && len (dataProcessRegions .([]any )) > 0 {
603
592
dataProcessRegion := dataProcessRegions .([]any )[0 ].(map [string ]any )
604
- result := admin20231001002. DataProcessRegion {}
593
+ result := admin. CreateDataProcessRegion {}
605
594
if cloudProvider , ok := dataProcessRegion ["cloud_provider" ]; ok {
606
595
result .CloudProvider = pointy .String (cloudProvider .(string ))
607
596
}
@@ -613,43 +602,43 @@ func mapDataProcessRegion(d *schema.ResourceData) *admin20231001002.DataProcessR
613
602
return nil
614
603
}
615
604
616
- func mapCriteria (d * schema.ResourceData ) admin20231001002 .Criteria {
605
+ func mapCriteria (d * schema.ResourceData ) admin .Criteria {
617
606
criteriaList := d .Get ("criteria" ).([]any )
618
607
619
608
criteria := criteriaList [0 ].(map [string ]any )
620
609
621
- criteriaInput := admin20231001002 .Criteria {
622
- Type : admin20231001002 .PtrString (criteria ["type" ].(string )),
610
+ criteriaInput := admin .Criteria {
611
+ Type : admin .PtrString (criteria ["type" ].(string )),
623
612
}
624
613
625
614
if criteriaInput .Type != nil && * criteriaInput .Type == "DATE" {
626
615
if dateField := criteria ["date_field" ].(string ); dateField != "" {
627
- criteriaInput .DateField = admin20231001002 .PtrString (dateField )
616
+ criteriaInput .DateField = admin .PtrString (dateField )
628
617
}
629
618
630
619
criteriaInput .ExpireAfterDays = pointy .Int (criteria ["expire_after_days" ].(int ))
631
620
632
621
// optional
633
622
if dformat , ok := criteria ["date_format" ]; ok && dformat .(string ) != "" {
634
- criteriaInput .DateFormat = admin20231001002 .PtrString (dformat .(string ))
623
+ criteriaInput .DateFormat = admin .PtrString (dformat .(string ))
635
624
}
636
625
}
637
626
638
627
if criteriaInput .Type != nil && * criteriaInput .Type == "CUSTOM" {
639
628
if query := criteria ["query" ].(string ); query != "" {
640
- criteriaInput .Query = admin20231001002 .PtrString (query )
629
+ criteriaInput .Query = admin .PtrString (query )
641
630
}
642
631
}
643
632
644
633
// Pending update client missing QUERY field
645
634
return criteriaInput
646
635
}
647
636
648
- func mapSchedule (d * schema.ResourceData ) * admin20231001002 .OnlineArchiveSchedule {
637
+ func mapSchedule (d * schema.ResourceData ) * admin .OnlineArchiveSchedule {
649
638
// scheduleInput := &matlas.OnlineArchiveSchedule{
650
639
651
640
// We have to provide schedule.type="DEFAULT" when the schedule block is not provided or removed
652
- scheduleInput := & admin20231001002 .OnlineArchiveSchedule {
641
+ scheduleInput := & admin .OnlineArchiveSchedule {
653
642
Type : scheduleTypeDefault ,
654
643
}
655
644
@@ -668,7 +657,7 @@ func mapSchedule(d *schema.ResourceData) *admin20231001002.OnlineArchiveSchedule
668
657
}
669
658
670
659
scheduleTFConfig := scheduleTFConfigList [0 ].(map [string ]any )
671
- scheduleInput = & admin20231001002 .OnlineArchiveSchedule {
660
+ scheduleInput = & admin .OnlineArchiveSchedule {
672
661
Type : scheduleTFConfig ["type" ].(string ),
673
662
}
674
663
0 commit comments