@@ -280,13 +280,27 @@ func resourceMongoDBAtlasCloudBackupSchedule() *schema.Resource {
280
280
}
281
281
282
282
func resourceMongoDBAtlasCloudBackupScheduleCreate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
283
+ var diags diag.Diagnostics
283
284
conn := meta .(* MongoDBClient ).Atlas
284
285
projectID := d .Get ("project_id" ).(string )
285
286
clusterName := d .Get ("cluster_name" ).(string )
286
287
287
- err := cloudBackupScheduleCreateOrUpdate (ctx , conn , d , projectID , clusterName )
288
- if err != nil {
289
- return diag .Errorf (errorSnapshotBackupScheduleCreate , err )
288
+ // When a new cluster is created with the backup feature enabled,
289
+ // MongoDB Atlas automatically generates a default backup policy for that cluster.
290
+ // As a result, we need to first delete the default policies to avoid having
291
+ // the infrastructure differs from the TF configuration file.
292
+ if _ , _ , err := conn .CloudProviderSnapshotBackupPolicies .Delete (ctx , projectID , clusterName ); err != nil {
293
+ diagWarning := diag.Diagnostic {
294
+ Severity : diag .Warning ,
295
+ Summary : "Error deleting default backup schedule" ,
296
+ Detail : fmt .Sprintf ("error deleting default MongoDB Cloud Backup Schedule (%s): %s" , clusterName , err ),
297
+ }
298
+ diags = append (diags , diagWarning )
299
+ }
300
+
301
+ if err := cloudBackupScheduleCreateOrUpdate (ctx , conn , d , projectID , clusterName ); err != nil {
302
+ diags = append (diags , diag .Errorf (errorSnapshotBackupScheduleCreate , err )... )
303
+ return diags
290
304
}
291
305
292
306
d .SetId (encodeStateID (map [string ]string {
@@ -440,23 +454,6 @@ func resourceMongoDBAtlasCloudBackupScheduleImportState(ctx context.Context, d *
440
454
}
441
455
442
456
func cloudBackupScheduleCreateOrUpdate (ctx context.Context , conn * matlas.Client , d * schema.ResourceData , projectID , clusterName string ) error {
443
- _ , policyMonthlyOK := d .GetOk ("policy_item_monthly" )
444
- _ , policyWeeklyOK := d .GetOk ("policy_item_weekly" )
445
- _ , policyDailyOK := d .GetOk ("policy_item_daily" )
446
- _ , policyhourlyOK := d .GetOk ("policy_item_hourly" )
447
-
448
- // When a new cluster is created with the backup feature enabled,
449
- // MongoDB Atlas automatically generates a default backup policy for that cluster.
450
- // However, in the scenario where the user hasn't provided a backup policy,
451
- // we want to make sure that the default backup policy is removed first.
452
- // This is to avoid having the infrastructure differs from the TF configuration file.
453
- if ! policyMonthlyOK && ! policyWeeklyOK && ! policyDailyOK && ! policyhourlyOK {
454
- _ , _ , err := conn .CloudProviderSnapshotBackupPolicies .Delete (ctx , projectID , clusterName )
455
- if err != nil {
456
- log .Printf ("error deleting MongoDB Cloud Backup Schedule (%s): %s" , clusterName , err )
457
- }
458
- }
459
-
460
457
// Get policies items
461
458
resp , _ , err := conn .CloudProviderSnapshotBackupPolicies .Get (ctx , projectID , clusterName )
462
459
if err != nil {
@@ -477,6 +474,7 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client,
477
474
if v , ok := d .GetOk ("policy_item_hourly" ); ok {
478
475
item := v .([]interface {})
479
476
itemObj := item [0 ].(map [string ]interface {})
477
+ policyItem .ID = policyItemID (itemObj )
480
478
policyItem .FrequencyType = snapshotScheduleHourly
481
479
policyItem .RetentionUnit = itemObj ["retention_unit" ].(string )
482
480
policyItem .FrequencyInterval = itemObj ["frequency_interval" ].(int )
@@ -486,6 +484,7 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client,
486
484
if v , ok := d .GetOk ("policy_item_daily" ); ok {
487
485
item := v .([]interface {})
488
486
itemObj := item [0 ].(map [string ]interface {})
487
+ policyItem .ID = policyItemID (itemObj )
489
488
policyItem .FrequencyType = snapshotScheduleDaily
490
489
policyItem .RetentionUnit = itemObj ["retention_unit" ].(string )
491
490
policyItem .FrequencyInterval = itemObj ["frequency_interval" ].(int )
@@ -496,6 +495,7 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client,
496
495
items := v .([]interface {})
497
496
for _ , s := range items {
498
497
itemObj := s .(map [string ]interface {})
498
+ policyItem .ID = policyItemID (itemObj )
499
499
policyItem .FrequencyType = snapshotScheduleWeekly
500
500
policyItem .RetentionUnit = itemObj ["retention_unit" ].(string )
501
501
policyItem .FrequencyInterval = itemObj ["frequency_interval" ].(int )
@@ -507,6 +507,7 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client,
507
507
items := v .([]interface {})
508
508
for _ , s := range items {
509
509
itemObj := s .(map [string ]interface {})
510
+ policyItem .ID = policyItemID (itemObj )
510
511
policyItem .FrequencyType = snapshotScheduleMonthly
511
512
policyItem .RetentionUnit = itemObj ["retention_unit" ].(string )
512
513
policyItem .FrequencyInterval = itemObj ["frequency_interval" ].(int )
@@ -641,3 +642,15 @@ func expandCopySettings(tfList []interface{}) []matlas.CopySetting {
641
642
}
642
643
return copySettings
643
644
}
645
+
646
+ func policyItemID (policyState map [string ]interface {}) string {
647
+ // if the policyItem has the ID field, this is the update operation
648
+ // we return the ID that was stored in the TF state
649
+ if val , ok := policyState ["id" ]; ok {
650
+ if id , ok := val .(string ); ok {
651
+ return id
652
+ }
653
+ }
654
+
655
+ return ""
656
+ }
0 commit comments