Skip to content

Commit df86e19

Browse files
authored
feat: Changes export and auto_export_enabled to Optional only in mongodbatlas_cloud_backup_schedule resource (#3500)
* change export and auto_export_enabled to Optional only * changelog * add test check on unsetting optional attributes * specify optional in doc * add migration test * aws provider in mig test * fix * remove todo
1 parent 0cbcc5d commit df86e19

File tree

5 files changed

+88
-29
lines changed

5 files changed

+88
-29
lines changed

.changelog/3500.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:breaking-change
2+
resource/mongodbatlas_cloud_backup_schedule: Changes `export` and `auto_export_enabled` to Optional only
3+
```

docs/resources/cloud_backup_schedule.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,12 @@ resource "mongodbatlas_cloud_backup_schedule" "test" {
228228
* `policy_item_weekly` - (Optional) Weekly policy item. See [below](#policy_item_weekly)
229229
* `policy_item_monthly` - (Optional) Monthly policy item. See [below](#policy_item_monthly)
230230
* `policy_item_yearly` - (Optional) Yearly policy item. See [below](#policy_item_yearly)
231-
* `auto_export_enabled` - Flag that indicates whether MongoDB Cloud automatically exports Cloud Backup Snapshots to the Export Bucket. Once enabled, it must be disabled by explicitly setting the value to `false`. Value can be one of the following:
231+
* `auto_export_enabled` - (Optional) Flag that indicates whether MongoDB Cloud automatically exports Cloud Backup Snapshots to the Export Bucket. Value can be one of the following:
232232
* true - Enables automatic export of cloud backup snapshots to the Export Bucket.
233233
* false - Disables automatic export of cloud backup snapshots to the Export Bucket. (default)
234234
* `use_org_and_group_names_in_export_prefix` - Specify true to use organization and project names instead of organization and project UUIDs in the path for the metadata files that Atlas uploads to your bucket after it finishes exporting the snapshots. To learn more about the metadata files that Atlas uploads, see [Export Cloud Backup Snapshot](https://www.mongodb.com/docs/atlas/backup/cloud-backup/export/#std-label-cloud-provider-snapshot-export).
235235
* `copy_settings` - List that contains a document for each copy setting item in the desired backup policy. See [below](#copy_settings)
236-
* `export` - Policy for automatically exporting Cloud Backup Snapshots. `auto_export_enabled` must be set to true when defining this attribute. See [below](#export)
236+
* `export` - Policy for automatically exporting Cloud Backup Snapshots. See [below](#export)
237237
### export
238238
* `export_bucket_id` - Unique identifier of the mongodbatlas_cloud_backup_snapshot_export_bucket export_bucket_id value.
239239
* `frequency_type` - Frequency associated with the export snapshot item: `weekly`, `monthly`, `yearly`, `daily` (requires reaching out to Customer Support)

internal/service/cloudbackupschedule/resource_cloud_backup_schedule.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ func Resource() *schema.Resource {
6363
"auto_export_enabled": {
6464
Type: schema.TypeBool,
6565
Optional: true,
66-
Computed: true,
6766
},
6867
"use_org_and_group_names_in_export_prefix": {
6968
Type: schema.TypeBool,
@@ -116,7 +115,6 @@ func Resource() *schema.Resource {
116115
Type: schema.TypeList,
117116
MaxItems: 1,
118117
Optional: true,
119-
Computed: true,
120118
Elem: &schema.Resource{
121119
Schema: map[string]*schema.Schema{
122120
"export_bucket_id": {
@@ -436,14 +434,6 @@ func setSchemaFieldsExceptCopySettings(d *schema.ResourceData, backupPolicy *adm
436434
return diag.Errorf(errorSnapshotBackupScheduleSetting, "id_policy", clusterName, err)
437435
}
438436

439-
if err := d.Set("export", FlattenExport(backupPolicy)); err != nil {
440-
return diag.Errorf(errorSnapshotBackupScheduleSetting, "export", clusterName, err)
441-
}
442-
443-
if err := d.Set("auto_export_enabled", backupPolicy.GetAutoExportEnabled()); err != nil {
444-
return diag.Errorf(errorSnapshotBackupScheduleSetting, "auto_export_enabled", clusterName, err)
445-
}
446-
447437
if err := d.Set("use_org_and_group_names_in_export_prefix", backupPolicy.GetUseOrgAndGroupNamesInExportPrefix()); err != nil {
448438
return diag.Errorf(errorSnapshotBackupScheduleSetting, "use_org_and_group_names_in_export_prefix", clusterName, err)
449439
}
@@ -573,7 +563,7 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, connV220240530 *admi
573563
}
574564

575565
if v, ok := d.GetOk("export"); ok {
576-
req.Export = expandAutoExportPolicy(v.([]any), d)
566+
req.Export = expandAutoExportPolicy(v.([]any))
577567
}
578568

579569
if d.HasChange("use_org_and_group_names_in_export_prefix") {
@@ -700,16 +690,12 @@ func expandCopySettingOldSDK(tfMap map[string]any) *admin20240530.DiskBackupCopy
700690
return copySetting
701691
}
702692

703-
func expandAutoExportPolicy(items []any, d *schema.ResourceData) *admin.AutoExportPolicy {
693+
func expandAutoExportPolicy(items []any) *admin.AutoExportPolicy {
704694
itemObj := items[0].(map[string]any)
705-
706-
if autoExportEnabled := d.Get("auto_export_enabled"); autoExportEnabled != nil && autoExportEnabled.(bool) {
707-
return &admin.AutoExportPolicy{
708-
ExportBucketId: conversion.StringPtr(itemObj["export_bucket_id"].(string)),
709-
FrequencyType: conversion.StringPtr(itemObj["frequency_type"].(string)),
710-
}
695+
return &admin.AutoExportPolicy{
696+
ExportBucketId: conversion.StringPtr(itemObj["export_bucket_id"].(string)),
697+
FrequencyType: conversion.StringPtr(itemObj["frequency_type"].(string)),
711698
}
712-
return nil
713699
}
714700

715701
func ExpandPolicyItems(items []any, frequencyType string) *[]admin.DiskBackupApiPolicyItem {

internal/service/cloudbackupschedule/resource_cloud_backup_schedule_migration_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
78
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
89
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
910
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig"
@@ -127,3 +128,55 @@ func TestMigBackupRSCloudBackupSchedule_copySettings(t *testing.T) {
127128
},
128129
})
129130
}
131+
132+
func TestMigBackupRSCloudBackupSchedule_export(t *testing.T) {
133+
mig.SkipIfVersionBelow(t, "2.0.0") // in 2.0.0 we made auto_export_enabled and export fields optional only
134+
var (
135+
clusterInfo = acc.GetClusterInfo(t, &acc.ClusterRequest{CloudBackup: true, ResourceDependencyName: "mongodbatlas_cloud_backup_snapshot_export_bucket.test"})
136+
policyName = acc.RandomName()
137+
roleName = acc.RandomIAMRole()
138+
bucketName = acc.RandomS3BucketName()
139+
140+
configWithExport = configExportPolicies(&clusterInfo, policyName, roleName, bucketName, true, true)
141+
configWithoutExport = configExportPolicies(&clusterInfo, policyName, roleName, bucketName, false, false)
142+
)
143+
144+
resource.ParallelTest(t, resource.TestCase{
145+
PreCheck: mig.PreCheckBasicSleep(t),
146+
CheckDestroy: checkDestroy,
147+
Steps: []resource.TestStep{
148+
// Step 1: Apply config with export and auto_export_enabled (old provider)
149+
{
150+
ExternalProviders: mig.ExternalProvidersWithAWS(),
151+
Config: configWithExport,
152+
Check: resource.ComposeAggregateTestCheckFunc(
153+
checkExists(resourceName),
154+
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
155+
resource.TestCheckResourceAttr(resourceName, "auto_export_enabled", "true"),
156+
resource.TestCheckResourceAttr(resourceName, "export.#", "1"),
157+
),
158+
},
159+
// Step 2: Remove export and auto_export_enabled, expect empty plan (old provider)
160+
{
161+
ExternalProviders: mig.ExternalProvidersWithAWS(),
162+
Config: configWithExport,
163+
ConfigPlanChecks: resource.ConfigPlanChecks{
164+
PreApply: []plancheck.PlanCheck{
165+
plancheck.ExpectEmptyPlan(),
166+
},
167+
},
168+
},
169+
// Step 3: Apply config without export and auto_export_enabled (new provider)
170+
{
171+
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
172+
Config: configWithoutExport,
173+
Check: resource.ComposeAggregateTestCheckFunc(
174+
checkExists(resourceName),
175+
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
176+
resource.TestCheckResourceAttr(resourceName, "auto_export_enabled", "false"),
177+
resource.TestCheckResourceAttr(resourceName, "export.#", "0"),
178+
),
179+
},
180+
},
181+
})
182+
}

internal/service/cloudbackupschedule/resource_cloud_backup_schedule_test.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func TestAccBackupRSCloudBackupSchedule_export(t *testing.T) {
165165

166166
Steps: []resource.TestStep{
167167
{
168-
Config: configExportPolicies(&clusterInfo, policyName, roleName, bucketName),
168+
Config: configExportPolicies(&clusterInfo, policyName, roleName, bucketName, true, true),
169169
Check: resource.ComposeAggregateTestCheckFunc(
170170
checkExists(resourceName),
171171
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
@@ -179,6 +179,15 @@ func TestAccBackupRSCloudBackupSchedule_export(t *testing.T) {
179179
resource.TestCheckResourceAttr(resourceName, "policy_item_daily.0.retention_value", "4"),
180180
),
181181
},
182+
{
183+
Config: configExportPolicies(&clusterInfo, policyName, roleName, bucketName, false, false),
184+
Check: resource.ComposeAggregateTestCheckFunc(
185+
checkExists(resourceName),
186+
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterInfo.Name),
187+
resource.TestCheckResourceAttr(resourceName, "auto_export_enabled", "false"),
188+
resource.TestCheckResourceAttr(resourceName, "export.#", "0"),
189+
),
190+
},
182191
},
183192
})
184193
}
@@ -934,12 +943,23 @@ func configAdvancedPolicies(info *acc.ClusterInfo, p *admin20240530.DiskBackupSn
934943
`, info.TerraformNameRef, info.ProjectID, p.GetReferenceHourOfDay(), p.GetReferenceMinuteOfHour(), p.GetRestoreWindowDays())
935944
}
936945

937-
func configExportPolicies(info *acc.ClusterInfo, policyName, roleName, bucketName string) string {
946+
func configExportPolicies(info *acc.ClusterInfo, policyName, roleName, bucketName string, includeAutoExport, includeExport bool) string {
947+
autoExport := ""
948+
export := ""
949+
if includeAutoExport {
950+
autoExport = "auto_export_enabled = true"
951+
}
952+
if includeExport {
953+
export = `export {
954+
export_bucket_id = mongodbatlas_cloud_backup_snapshot_export_bucket.test.export_bucket_id
955+
frequency_type = "monthly"
956+
}`
957+
}
938958
return info.TerraformStr + fmt.Sprintf(`
939959
resource "mongodbatlas_cloud_backup_schedule" "schedule_test" {
940960
cluster_name = %[1]s
941961
project_id = %[2]q
942-
auto_export_enabled = true
962+
%[6]s
943963
reference_hour_of_day = 20
944964
reference_minute_of_hour = "05"
945965
restore_window_days = 4
@@ -966,10 +986,7 @@ func configExportPolicies(info *acc.ClusterInfo, policyName, roleName, bucketNam
966986
retention_value = 4
967987
}
968988
969-
export {
970-
export_bucket_id = mongodbatlas_cloud_backup_snapshot_export_bucket.test.export_bucket_id
971-
frequency_type = "monthly"
972-
}
989+
%[7]s
973990
}
974991
975992
resource "aws_s3_bucket" "backup" {
@@ -1040,7 +1057,7 @@ func configExportPolicies(info *acc.ClusterInfo, policyName, roleName, bucketNam
10401057
}
10411058
EOF
10421059
}
1043-
`, info.TerraformNameRef, info.ProjectID, policyName, roleName, bucketName)
1060+
`, info.TerraformNameRef, info.ProjectID, policyName, roleName, bucketName, autoExport, export)
10441061
}
10451062

10461063
func importStateIDFunc(resourceName string) resource.ImportStateIdFunc {

0 commit comments

Comments
 (0)