Skip to content

Commit

Permalink
azurerm_backup_policy_vm - Support for the days and `include_last…
Browse files Browse the repository at this point in the history
…_days` properties (#21434)
  • Loading branch information
ziyeqf authored May 23, 2023
1 parent cf5f894 commit 5cd9104
Show file tree
Hide file tree
Showing 3 changed files with 316 additions and 15 deletions.
211 changes: 201 additions & 10 deletions internal/services/recoveryservices/backup_policy_vm_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,20 @@ func expandBackupProtectionPolicyVMRetentionMonthly(d *pluginsdk.ResourceData, t
if rb, ok := d.Get("retention_monthly").([]interface{}); ok && len(rb) > 0 {
block := rb[0].(map[string]interface{})

scheduleFormat := protectionpolicies.RetentionScheduleFormatWeekly
var weekly *protectionpolicies.WeeklyRetentionFormat = nil
var daily *protectionpolicies.DailyRetentionFormat = nil
if v, ok := block["days"]; ok && v.(*pluginsdk.Set).Len() > 0 {
scheduleFormat = protectionpolicies.RetentionScheduleFormatDaily
daily = expandBackupProtectionPolicyVMRetentionDailyFormat(block)
} else {
weekly = expandBackupProtectionPolicyVMRetentionWeeklyFormat(block)
}

retention := protectionpolicies.MonthlyRetentionSchedule{
RetentionScheduleFormatType: pointer.To(protectionpolicies.RetentionScheduleFormatWeekly), // this is always weekly ¯\_(ツ)_/¯
RetentionScheduleDaily: nil, // and this is always nil..
RetentionScheduleWeekly: expandBackupProtectionPolicyVMRetentionWeeklyFormat(block),
RetentionScheduleFormatType: &scheduleFormat,
RetentionScheduleDaily: daily,
RetentionScheduleWeekly: weekly,
RetentionTimes: &times,
RetentionDuration: &protectionpolicies.RetentionDuration{
Count: pointer.To(int64(block["count"].(int))),
Expand All @@ -468,10 +478,20 @@ func expandBackupProtectionPolicyVMRetentionYearly(d *pluginsdk.ResourceData, ti
if rb, ok := d.Get("retention_yearly").([]interface{}); ok && len(rb) > 0 {
block := rb[0].(map[string]interface{})

scheduleFormat := protectionpolicies.RetentionScheduleFormatWeekly
var weekly *protectionpolicies.WeeklyRetentionFormat = nil
var daily *protectionpolicies.DailyRetentionFormat = nil
if v, ok := block["days"]; ok && v.(*pluginsdk.Set).Len() > 0 {
scheduleFormat = protectionpolicies.RetentionScheduleFormatDaily
daily = expandBackupProtectionPolicyVMRetentionDailyFormat(block)
} else {
weekly = expandBackupProtectionPolicyVMRetentionWeeklyFormat(block)
}

retention := protectionpolicies.YearlyRetentionSchedule{
RetentionScheduleFormatType: pointer.To(protectionpolicies.RetentionScheduleFormatWeekly), // this is always weekly ¯\_(ツ)_/¯
RetentionScheduleDaily: nil, // and this is always nil..
RetentionScheduleWeekly: expandBackupProtectionPolicyVMRetentionWeeklyFormat(block),
RetentionScheduleFormatType: &scheduleFormat,
RetentionScheduleDaily: daily,
RetentionScheduleWeekly: weekly,
RetentionTimes: &times,
RetentionDuration: &protectionpolicies.RetentionDuration{
Count: pointer.To(int64(block["count"].(int))),
Expand Down Expand Up @@ -515,6 +535,32 @@ func expandBackupProtectionPolicyVMRetentionWeeklyFormat(block map[string]interf
return &weekly
}

func expandBackupProtectionPolicyVMRetentionDailyFormat(block map[string]interface{}) *protectionpolicies.DailyRetentionFormat {
days := make([]protectionpolicies.Day, 0)

if block["include_last_days"].(bool) {
days = append(days, protectionpolicies.Day{
Date: pointer.To(int64(0)),
IsLast: pointer.To(true),
})
}

if v, ok := block["days"].(*pluginsdk.Set); ok {
for _, day := range v.List() {
days = append(days, protectionpolicies.Day{
Date: pointer.To(int64(day.(int))),
IsLast: pointer.To(false),
})
}
}

daily := protectionpolicies.DailyRetentionFormat{
DaysOfTheMonth: &days,
}

return &daily
}

func flattenBackupProtectionPolicyVMResourceGroup(rpDetail protectionpolicies.InstantRPAdditionalDetails) []interface{} {
if rpDetail.AzureBackupRGNamePrefix == nil {
return nil
Expand Down Expand Up @@ -650,6 +696,10 @@ func flattenBackupProtectionPolicyVMRetentionMonthly(monthly *protectionpolicies
block["weekdays"], block["weeks"] = flattenBackupProtectionPolicyVMRetentionWeeklyFormat(weekly)
}

if daily := monthly.RetentionScheduleDaily; daily != nil {
block["days"], block["include_last_days"] = flattenBackupProtectionPolicyVMRetentionDailyFormat(daily)
}

return []interface{}{block}
}

Expand All @@ -674,6 +724,10 @@ func flattenBackupProtectionPolicyVMRetentionYearly(yearly *protectionpolicies.Y
block["months"] = pluginsdk.NewSet(pluginsdk.HashString, slice)
}

if daily := yearly.RetentionScheduleDaily; daily != nil {
block["days"], block["include_last_days"] = flattenBackupProtectionPolicyVMRetentionDailyFormat(daily)
}

return []interface{}{block}
}

Expand All @@ -697,6 +751,21 @@ func flattenBackupProtectionPolicyVMRetentionWeeklyFormat(retention *protectionp
return weekdays, weeks
}

func flattenBackupProtectionPolicyVMRetentionDailyFormat(retention *protectionpolicies.DailyRetentionFormat) (days []interface{}, includeLastDay bool) {
if dotm := retention.DaysOfTheMonth; dotm != nil {
for _, d := range *dotm {
if d.Date != nil && *d.Date != 0 {
days = append(days, *d.Date)
}
if d.IsLast != nil && *d.IsLast {
includeLastDay = true
}
}
}

return days, includeLastDay
}

func resourceBackupProtectionPolicyVMWaitForUpdate(ctx context.Context, client *protectionpolicies.ProtectionPoliciesClient, id protectionpolicies.BackupPolicyId, d *pluginsdk.ResourceData) error {
state := &pluginsdk.StateChangeConf{
MinTimeout: 30 * time.Second,
Expand Down Expand Up @@ -930,7 +999,7 @@ func resourceBackupProtectionPolicyVMSchema() map[string]*pluginsdk.Schema {

"weeks": {
Type: pluginsdk.TypeSet,
Required: true,
Optional: true,
Set: set.HashStringIgnoreCase,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Expand All @@ -942,17 +1011,78 @@ func resourceBackupProtectionPolicyVMSchema() map[string]*pluginsdk.Schema {
string(protectionpolicies.WeekOfMonthLast),
}, false),
},
ConflictsWith: []string{
"retention_monthly.0.days",
"retention_monthly.0.include_last_days",
},
AtLeastOneOf: []string{
"retention_monthly.0.weekdays",
"retention_monthly.0.weeks",
"retention_monthly.0.days",
"retention_monthly.0.include_last_days",
},
RequiredWith: []string{
"retention_monthly.0.weekdays",
},
},

"weekdays": {
Type: pluginsdk.TypeSet,
Required: true,
Optional: true,
Set: set.HashStringIgnoreCase,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.IsDayOfTheWeek(true),
},
RequiredWith: []string{
"retention_monthly.0.weeks",
},
ConflictsWith: []string{
"retention_monthly.0.days",
"retention_monthly.0.include_last_days",
},
AtLeastOneOf: []string{
"retention_monthly.0.weekdays",
"retention_monthly.0.weeks",
"retention_monthly.0.days",
"retention_monthly.0.include_last_days",
},
},

"days": {
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeInt,
ValidateFunc: validation.IntBetween(1, 31), // days in months
},
ConflictsWith: []string{
"retention_monthly.0.weeks",
"retention_monthly.0.weekdays",
},
AtLeastOneOf: []string{
"retention_monthly.0.weekdays",
"retention_monthly.0.weeks",
"retention_monthly.0.days",
"retention_monthly.0.include_last_days",
},
},

"include_last_days": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
ConflictsWith: []string{
"retention_monthly.0.weeks",
"retention_monthly.0.weekdays",
},
AtLeastOneOf: []string{
"retention_monthly.0.weekdays",
"retention_monthly.0.weeks",
"retention_monthly.0.days",
"retention_monthly.0.include_last_days",
},
},
},
},
Expand Down Expand Up @@ -983,7 +1113,7 @@ func resourceBackupProtectionPolicyVMSchema() map[string]*pluginsdk.Schema {

"weeks": {
Type: pluginsdk.TypeSet,
Required: true,
Optional: true,
Set: set.HashStringIgnoreCase,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Expand All @@ -995,17 +1125,78 @@ func resourceBackupProtectionPolicyVMSchema() map[string]*pluginsdk.Schema {
string(protectionpolicies.WeekOfMonthLast),
}, false),
},
RequiredWith: []string{
"retention_yearly.0.weekdays",
},
ConflictsWith: []string{
"retention_yearly.0.days",
"retention_yearly.0.include_last_days",
},
AtLeastOneOf: []string{
"retention_yearly.0.weeks",
"retention_yearly.0.weekdays",
"retention_yearly.0.days",
"retention_yearly.0.include_last_days",
},
},

"weekdays": {
Type: pluginsdk.TypeSet,
Required: true,
Optional: true,
Set: set.HashStringIgnoreCase,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.IsDayOfTheWeek(true),
},
RequiredWith: []string{
"retention_yearly.0.weeks",
},
ConflictsWith: []string{
"retention_yearly.0.days",
"retention_yearly.0.include_last_days",
},
AtLeastOneOf: []string{
"retention_yearly.0.weeks",
"retention_yearly.0.weekdays",
"retention_yearly.0.days",
"retention_yearly.0.include_last_days",
},
},

"days": {
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeInt,
ValidateFunc: validation.IntBetween(1, 31), // days in months
},
ConflictsWith: []string{
"retention_yearly.0.weeks",
"retention_yearly.0.weekdays",
},
AtLeastOneOf: []string{
"retention_yearly.0.weeks",
"retention_yearly.0.weekdays",
"retention_yearly.0.days",
"retention_yearly.0.include_last_days",
},
},

"include_last_days": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
ConflictsWith: []string{
"retention_yearly.0.weeks",
"retention_yearly.0.weekdays",
},
AtLeastOneOf: []string{
"retention_yearly.0.weeks",
"retention_yearly.0.weekdays",
"retention_yearly.0.days",
"retention_yearly.0.include_last_days",
},
},
},
},
Expand Down
Loading

0 comments on commit 5cd9104

Please sign in to comment.