Skip to content

Commit 15047a2

Browse files
INTMDB-867: [Terraform] Fix backup tests (#1236)
* Update resource_mongodbatlas_advanced_cluster_test.go * Update resource_mongodbatlas_cloud_backup_schedule_test.go * Revert "Update resource_mongodbatlas_cloud_backup_schedule_test.go" This reverts commit 8850f3f. * Update resource_mongodbatlas_cloud_backup_schedule.go * Revert "Update resource_mongodbatlas_cloud_backup_schedule.go" This reverts commit 25f6e5c. * updated tests * fixed logic * Update resource_mongodbatlas_cloud_backup_schedule_test.go * Update resource_mongodbatlas_cloud_backup_schedule.go * Update resource_mongodbatlas_online_archive_test.go
1 parent 5d0a904 commit 15047a2

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

mongodbatlas/resource_mongodbatlas_cloud_backup_schedule.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ func resourceMongoDBAtlasCloudBackupScheduleDelete(ctx context.Context, d *schem
402402
return diag.Errorf("error deleting MongoDB Cloud Backup Schedule (%s): %s", clusterName, err)
403403
}
404404

405+
d.SetId("")
406+
405407
return nil
406408
}
407409

@@ -438,14 +440,30 @@ func resourceMongoDBAtlasCloudBackupScheduleImportState(ctx context.Context, d *
438440
}
439441

440442
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+
}
442459

443460
// Get policies items
444461
resp, _, err := conn.CloudProviderSnapshotBackupPolicies.Get(ctx, projectID, clusterName)
445462
if err != nil {
446463
log.Printf("error getting MongoDB Cloud Backup Schedule (%s): %s", clusterName, err)
447464
}
448465

466+
req := &matlas.CloudProviderSnapshotBackupPolicy{}
449467
policy := matlas.Policy{}
450468
policyItem := matlas.PolicyItem{}
451469
var policiesItem []matlas.PolicyItem

mongodbatlas/resource_mongodbatlas_online_archive_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const (
2525
replication_specs {
2626
num_shards = 1
2727
regions_config {
28-
region_name = "US_EAST_2"
28+
region_name = "US_EAST_1"
2929
electable_nodes = 3
3030
priority = 7
3131
read_only_nodes = 0

0 commit comments

Comments
 (0)