Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Emptying cloud_back_schedule "copy_settings" #2387

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2387.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/mongodbatlas_cloud_backup_schedule: Updates `copy_settings` on changes (even when empty)
```
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,9 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, connV2 *admin.APICli
}

req := &admin.DiskBackupSnapshotSchedule{}

if v, ok := d.GetOk("copy_settings"); ok && len(v.([]any)) > 0 {
req.CopySettings = expandCopySettings(v.([]any))
copySettings := d.Get("copy_settings")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only time d.Get("copy_settings") will return nil is when copy_settings doesn't exist in the schema.

if copySettings != nil && (conversion.HasElementsSliceOrMap(copySettings) || d.HasChange("copy_settings")) {
EspenAlbert marked this conversation as resolved.
Show resolved Hide resolved
req.CopySettings = expandCopySettings(copySettings.([]any))
}

var policiesItem []admin.DiskBackupApiPolicyItem
Expand Down Expand Up @@ -642,11 +642,7 @@ func expandCopySetting(tfMap map[string]any) *admin.DiskBackupCopySetting {
}

func expandCopySettings(tfList []any) *[]admin.DiskBackupCopySetting {
if len(tfList) == 0 {
return nil
}

var copySettings []admin.DiskBackupCopySetting
copySettings := make([]admin.DiskBackupCopySetting, 0)

for _, tfMapRaw := range tfList {
tfMap, ok := tfMapRaw.(map[string]any)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,49 +254,66 @@ func TestAccBackupRSCloudBackupSchedule_copySettings(t *testing.T) {
var (
projectID = acc.ProjectIDExecution(t)
clusterName = acc.RandomClusterName()
checkMap = map[string]string{
"cluster_name": clusterName,
"reference_hour_of_day": "3",
"reference_minute_of_hour": "45",
"restore_window_days": "1",
"policy_item_hourly.#": "1",
"policy_item_daily.#": "1",
"policy_item_weekly.#": "1",
"policy_item_monthly.#": "1",
"policy_item_yearly.#": "1",
"policy_item_hourly.0.frequency_interval": "1",
"policy_item_hourly.0.retention_unit": "days",
"policy_item_hourly.0.retention_value": "1",
"policy_item_daily.0.frequency_interval": "1",
"policy_item_daily.0.retention_unit": "days",
"policy_item_daily.0.retention_value": "2",
"policy_item_weekly.0.frequency_interval": "4",
"policy_item_weekly.0.retention_unit": "weeks",
"policy_item_weekly.0.retention_value": "3",
"policy_item_monthly.0.frequency_interval": "5",
"policy_item_monthly.0.retention_unit": "months",
"policy_item_monthly.0.retention_value": "4",
"policy_item_yearly.0.frequency_interval": "1",
"policy_item_yearly.0.retention_unit": "years",
"policy_item_yearly.0.retention_value": "1",
}
copySettingsChecks = map[string]string{
"copy_settings.#": "1",
"copy_settings.0.cloud_provider": "AWS",
"copy_settings.0.region_name": "US_EAST_1",
"copy_settings.0.should_copy_oplogs": "true",
}
emptyCopySettingsChecks = map[string]string{
"copy_settings.#": "0",
}
)
checksDefault := acc.AddAttrChecks(resourceName, []resource.TestCheckFunc{checkExists(resourceName)}, checkMap)
checksCreate := acc.AddAttrChecks(resourceName, checksDefault, copySettingsChecks)
checksUpdate := acc.AddAttrChecks(resourceName, checksDefault, emptyCopySettingsChecks)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configCopySettings(projectID, clusterName, &admin.DiskBackupSnapshotSchedule{
Config: configCopySettings(projectID, clusterName, false, &admin.DiskBackupSnapshotSchedule{
ReferenceHourOfDay: conversion.Pointer(3),
ReferenceMinuteOfHour: conversion.Pointer(45),
RestoreWindowDays: conversion.Pointer(1),
}),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterName),
resource.TestCheckResourceAttr(resourceName, "reference_hour_of_day", "3"),
resource.TestCheckResourceAttr(resourceName, "reference_minute_of_hour", "45"),
resource.TestCheckResourceAttr(resourceName, "restore_window_days", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.#", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_daily.#", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_weekly.#", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_monthly.#", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_yearly.#", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.frequency_interval", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.retention_unit", "days"),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.retention_value", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_daily.0.frequency_interval", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_daily.0.retention_unit", "days"),
resource.TestCheckResourceAttr(resourceName, "policy_item_daily.0.retention_value", "2"),
resource.TestCheckResourceAttr(resourceName, "policy_item_weekly.0.frequency_interval", "4"),
resource.TestCheckResourceAttr(resourceName, "policy_item_weekly.0.retention_unit", "weeks"),
resource.TestCheckResourceAttr(resourceName, "policy_item_weekly.0.retention_value", "3"),
resource.TestCheckResourceAttr(resourceName, "policy_item_monthly.0.frequency_interval", "5"),
resource.TestCheckResourceAttr(resourceName, "policy_item_monthly.0.retention_unit", "months"),
resource.TestCheckResourceAttr(resourceName, "policy_item_monthly.0.retention_value", "4"),
resource.TestCheckResourceAttr(resourceName, "policy_item_yearly.0.frequency_interval", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_yearly.0.retention_unit", "years"),
resource.TestCheckResourceAttr(resourceName, "policy_item_yearly.0.retention_value", "1"),
resource.TestCheckResourceAttr(resourceName, "copy_settings.0.cloud_provider", "AWS"),
resource.TestCheckResourceAttr(resourceName, "copy_settings.0.region_name", "US_EAST_1"),
resource.TestCheckResourceAttr(resourceName, "copy_settings.0.should_copy_oplogs", "true"),
),
Check: resource.ComposeAggregateTestCheckFunc(checksCreate...),
},
{
Config: configCopySettings(projectID, clusterName, true, &admin.DiskBackupSnapshotSchedule{
ReferenceHourOfDay: conversion.Pointer(3),
ReferenceMinuteOfHour: conversion.Pointer(45),
RestoreWindowDays: conversion.Pointer(1),
}),
Check: resource.ComposeAggregateTestCheckFunc(checksUpdate...),
},
},
})
Expand Down Expand Up @@ -507,7 +524,23 @@ func configDefault(info *acc.ClusterInfo, p *admin.DiskBackupSnapshotSchedule) s
`, info.ClusterNameStr, info.ProjectIDStr, p.GetReferenceHourOfDay(), p.GetReferenceMinuteOfHour(), p.GetRestoreWindowDays())
}

func configCopySettings(projectID, clusterName string, p *admin.DiskBackupSnapshotSchedule) string {
func configCopySettings(projectID, clusterName string, emptyCopySettings bool, p *admin.DiskBackupSnapshotSchedule) string {
var copySettings string
if !emptyCopySettings {
copySettings = `
copy_settings {
cloud_provider = "AWS"
frequencies = ["HOURLY",
"DAILY",
"WEEKLY",
"MONTHLY",
"YEARLY",
"ON_DEMAND"]
region_name = "US_EAST_1"
replication_spec_id = mongodbatlas_cluster.my_cluster.replication_specs.*.id[0]
should_copy_oplogs = true
}`
}
return fmt.Sprintf(`
resource "mongodbatlas_cluster" "my_cluster" {
project_id = %[1]q
Expand Down Expand Up @@ -564,20 +597,9 @@ func configCopySettings(projectID, clusterName string, p *admin.DiskBackupSnapsh
retention_unit = "years"
retention_value = 1
}
copy_settings {
cloud_provider = "AWS"
frequencies = ["HOURLY",
"DAILY",
"WEEKLY",
"MONTHLY",
"YEARLY",
"ON_DEMAND"]
region_name = "US_EAST_1"
replication_spec_id = mongodbatlas_cluster.my_cluster.replication_specs.*.id[0]
should_copy_oplogs = true
}
%s
}
`, projectID, clusterName, p.GetReferenceHourOfDay(), p.GetReferenceMinuteOfHour(), p.GetRestoreWindowDays())
`, projectID, clusterName, p.GetReferenceHourOfDay(), p.GetReferenceMinuteOfHour(), p.GetRestoreWindowDays(), copySettings)
}

func configOnePolicy(info *acc.ClusterInfo, p *admin.DiskBackupSnapshotSchedule) string {
Expand Down