@@ -402,6 +402,8 @@ func resourceMongoDBAtlasCloudBackupScheduleDelete(ctx context.Context, d *schem
402
402
return diag .Errorf ("error deleting MongoDB Cloud Backup Schedule (%s): %s" , clusterName , err )
403
403
}
404
404
405
+ d .SetId ("" )
406
+
405
407
return nil
406
408
}
407
409
@@ -438,14 +440,30 @@ func resourceMongoDBAtlasCloudBackupScheduleImportState(ctx context.Context, d *
438
440
}
439
441
440
442
func cloudBackupScheduleCreateOrUpdate (ctx context.Context , conn * matlas.Client , d * schema.ResourceData , projectID , clusterName string ) error {
441
- req := & matlas.CloudProviderSnapshotBackupPolicy {}
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
+ }
442
459
443
460
// Get policies items
444
461
resp , _ , err := conn .CloudProviderSnapshotBackupPolicies .Get (ctx , projectID , clusterName )
445
462
if err != nil {
446
463
log .Printf ("error getting MongoDB Cloud Backup Schedule (%s): %s" , clusterName , err )
447
464
}
448
465
466
+ req := & matlas.CloudProviderSnapshotBackupPolicy {}
449
467
policy := matlas.Policy {}
450
468
policyItem := matlas.PolicyItem {}
451
469
var policiesItem []matlas.PolicyItem
0 commit comments