@@ -280,13 +280,27 @@ func resourceMongoDBAtlasCloudBackupSchedule() *schema.Resource {
280280}
281281
282282func resourceMongoDBAtlasCloudBackupScheduleCreate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
283+ var diags diag.Diagnostics
283284 conn := meta .(* MongoDBClient ).Atlas
284285 projectID := d .Get ("project_id" ).(string )
285286 clusterName := d .Get ("cluster_name" ).(string )
286287
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
290304 }
291305
292306 d .SetId (encodeStateID (map [string ]string {
@@ -440,23 +454,6 @@ func resourceMongoDBAtlasCloudBackupScheduleImportState(ctx context.Context, d *
440454}
441455
442456func 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-
460457 // Get policies items
461458 resp , _ , err := conn .CloudProviderSnapshotBackupPolicies .Get (ctx , projectID , clusterName )
462459 if err != nil {
@@ -477,6 +474,7 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client,
477474 if v , ok := d .GetOk ("policy_item_hourly" ); ok {
478475 item := v .([]interface {})
479476 itemObj := item [0 ].(map [string ]interface {})
477+ policyItem .ID = policyItemID (itemObj )
480478 policyItem .FrequencyType = snapshotScheduleHourly
481479 policyItem .RetentionUnit = itemObj ["retention_unit" ].(string )
482480 policyItem .FrequencyInterval = itemObj ["frequency_interval" ].(int )
@@ -486,6 +484,7 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client,
486484 if v , ok := d .GetOk ("policy_item_daily" ); ok {
487485 item := v .([]interface {})
488486 itemObj := item [0 ].(map [string ]interface {})
487+ policyItem .ID = policyItemID (itemObj )
489488 policyItem .FrequencyType = snapshotScheduleDaily
490489 policyItem .RetentionUnit = itemObj ["retention_unit" ].(string )
491490 policyItem .FrequencyInterval = itemObj ["frequency_interval" ].(int )
@@ -496,6 +495,7 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client,
496495 items := v .([]interface {})
497496 for _ , s := range items {
498497 itemObj := s .(map [string ]interface {})
498+ policyItem .ID = policyItemID (itemObj )
499499 policyItem .FrequencyType = snapshotScheduleWeekly
500500 policyItem .RetentionUnit = itemObj ["retention_unit" ].(string )
501501 policyItem .FrequencyInterval = itemObj ["frequency_interval" ].(int )
@@ -507,6 +507,7 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client,
507507 items := v .([]interface {})
508508 for _ , s := range items {
509509 itemObj := s .(map [string ]interface {})
510+ policyItem .ID = policyItemID (itemObj )
510511 policyItem .FrequencyType = snapshotScheduleMonthly
511512 policyItem .RetentionUnit = itemObj ["retention_unit" ].(string )
512513 policyItem .FrequencyInterval = itemObj ["frequency_interval" ].(int )
@@ -641,3 +642,15 @@ func expandCopySettings(tfList []interface{}) []matlas.CopySetting {
641642 }
642643 return copySettings
643644}
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