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: Update mongodbatlas_cloud_backup_schedule to add the ID field to policyItems #1357

Merged
merged 3 commits into from
Aug 2, 2023
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
53 changes: 33 additions & 20 deletions mongodbatlas/resource_mongodbatlas_cloud_backup_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,27 @@ func resourceMongoDBAtlasCloudBackupSchedule() *schema.Resource {
}

func resourceMongoDBAtlasCloudBackupScheduleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*MongoDBClient).Atlas
projectID := d.Get("project_id").(string)
clusterName := d.Get("cluster_name").(string)

err := cloudBackupScheduleCreateOrUpdate(ctx, conn, d, projectID, clusterName)
if err != nil {
return diag.Errorf(errorSnapshotBackupScheduleCreate, err)
// When a new cluster is created with the backup feature enabled,
// MongoDB Atlas automatically generates a default backup policy for that cluster.
// As a result, we need to first delete the default policies to avoid having
// the infrastructure differs from the TF configuration file.
if _, _, err := conn.CloudProviderSnapshotBackupPolicies.Delete(ctx, projectID, clusterName); err != nil {
diagWarning := diag.Diagnostic{
Severity: diag.Warning,
Summary: "Error deleting default backup schedule",
Detail: fmt.Sprintf("error deleting default MongoDB Cloud Backup Schedule (%s): %s", clusterName, err),
}
diags = append(diags, diagWarning)
}

if err := cloudBackupScheduleCreateOrUpdate(ctx, conn, d, projectID, clusterName); err != nil {
diags = append(diags, diag.Errorf(errorSnapshotBackupScheduleCreate, err)...)
return diags
}

d.SetId(encodeStateID(map[string]string{
Expand Down Expand Up @@ -440,23 +454,6 @@ func resourceMongoDBAtlasCloudBackupScheduleImportState(ctx context.Context, d *
}

func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client, d *schema.ResourceData, projectID, clusterName string) error {
_, policyMonthlyOK := d.GetOk("policy_item_monthly")
_, policyWeeklyOK := d.GetOk("policy_item_weekly")
_, policyDailyOK := d.GetOk("policy_item_daily")
_, policyhourlyOK := d.GetOk("policy_item_hourly")

// When a new cluster is created with the backup feature enabled,
// MongoDB Atlas automatically generates a default backup policy for that cluster.
// However, in the scenario where the user hasn't provided a backup policy,
// we want to make sure that the default backup policy is removed first.
// This is to avoid having the infrastructure differs from the TF configuration file.
if !policyMonthlyOK && !policyWeeklyOK && !policyDailyOK && !policyhourlyOK {
_, _, err := conn.CloudProviderSnapshotBackupPolicies.Delete(ctx, projectID, clusterName)
if err != nil {
log.Printf("error deleting MongoDB Cloud Backup Schedule (%s): %s", clusterName, err)
}
}

// Get policies items
resp, _, err := conn.CloudProviderSnapshotBackupPolicies.Get(ctx, projectID, clusterName)
if err != nil {
Expand All @@ -477,6 +474,7 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client,
if v, ok := d.GetOk("policy_item_hourly"); ok {
item := v.([]interface{})
itemObj := item[0].(map[string]interface{})
policyItem.ID = policyItemID(itemObj)
policyItem.FrequencyType = snapshotScheduleHourly
policyItem.RetentionUnit = itemObj["retention_unit"].(string)
policyItem.FrequencyInterval = itemObj["frequency_interval"].(int)
Expand All @@ -486,6 +484,7 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client,
if v, ok := d.GetOk("policy_item_daily"); ok {
item := v.([]interface{})
itemObj := item[0].(map[string]interface{})
policyItem.ID = policyItemID(itemObj)
policyItem.FrequencyType = snapshotScheduleDaily
policyItem.RetentionUnit = itemObj["retention_unit"].(string)
policyItem.FrequencyInterval = itemObj["frequency_interval"].(int)
Expand All @@ -496,6 +495,7 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client,
items := v.([]interface{})
for _, s := range items {
itemObj := s.(map[string]interface{})
policyItem.ID = policyItemID(itemObj)
policyItem.FrequencyType = snapshotScheduleWeekly
policyItem.RetentionUnit = itemObj["retention_unit"].(string)
policyItem.FrequencyInterval = itemObj["frequency_interval"].(int)
Expand All @@ -507,6 +507,7 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client,
items := v.([]interface{})
for _, s := range items {
itemObj := s.(map[string]interface{})
policyItem.ID = policyItemID(itemObj)
policyItem.FrequencyType = snapshotScheduleMonthly
policyItem.RetentionUnit = itemObj["retention_unit"].(string)
policyItem.FrequencyInterval = itemObj["frequency_interval"].(int)
Expand Down Expand Up @@ -641,3 +642,15 @@ func expandCopySettings(tfList []interface{}) []matlas.CopySetting {
}
return copySettings
}

func policyItemID(policyState map[string]interface{}) string {
// if the policyItem has the ID field, this is the update operation
// we return the ID that was stored in the TF state
if val, ok := policyState["id"]; ok {
if id, ok := val.(string); ok {
return id
}
}

return ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,19 @@ func TestAccBackupRSCloudBackupSchedule_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "policy_item_daily.#", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_weekly.#", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_monthly.#", "1"),
resource.TestCheckResourceAttrSet(resourceName, "policy_item_hourly.0.id"),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.frequency_interval", "2"),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.retention_unit", "days"),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.retention_value", "1"),
resource.TestCheckResourceAttrSet(resourceName, "policy_item_daily.0.id"),
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", "4"),
resource.TestCheckResourceAttrSet(resourceName, "policy_item_weekly.0.id"),
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", "2"),
resource.TestCheckResourceAttrSet(resourceName, "policy_item_monthly.0.id"),
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", "3"),
Expand All @@ -103,15 +107,19 @@ func TestAccBackupRSCloudBackupSchedule_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "policy_item_daily.#", "1"),
resource.TestCheckResourceAttr(resourceName, "policy_item_weekly.#", "2"),
resource.TestCheckResourceAttr(resourceName, "policy_item_monthly.#", "2"),
resource.TestCheckResourceAttrSet(resourceName, "policy_item_hourly.0.id"),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.frequency_interval", "2"),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.retention_unit", "days"),
resource.TestCheckResourceAttr(resourceName, "policy_item_hourly.0.retention_value", "1"),
resource.TestCheckResourceAttrSet(resourceName, "policy_item_daily.0.id"),
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", "4"),
resource.TestCheckResourceAttrSet(resourceName, "policy_item_weekly.0.id"),
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", "2"),
resource.TestCheckResourceAttrSet(resourceName, "policy_item_monthly.0.id"),
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", "3"),
Expand Down