Skip to content

Commit

Permalink
[Az.RecoveryServices.Backup] Improve Policy Error Code (Azure#26788)
Browse files Browse the repository at this point in the history
* fix error messages

* pass custom retention constants for vault policy validation

* fix unit test

---------

Co-authored-by: ianna1-admin <[email protected]>
  • Loading branch information
IannGeorges and ianna1-admin authored Dec 3, 2024
1 parent 6936a17 commit 27283dc
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,64 @@ public override void Validate()
/// </summary>
public new void Validate(ScheduleRunType ScheduleRunFrequency = 0)
{
base.Validate(ScheduleRunFrequency);

int MinDurationCountInDays = 1, MaxDurationCountInDays = PolicyConstants.AfsSnapshotRetentionDaysMax;

if (SnapshotRetentionInDays < MinDurationCountInDays || SnapshotRetentionInDays > MaxDurationCountInDays)
{
throw new ArgumentException(Resources.SnapshotRetentionInDaysInvalidException);
}

if (IsDailyScheduleEnabled)
{
if (DailySchedule == null)
{
throw new ArgumentException(Resources.DailyScheduleEnabledButScheduleIsNullException);
}
else
{
DailySchedule.BackupManagementType = BackupManagementType;
DailySchedule.Validate(ScheduleRunFrequency, PolicyConstants.AfsDailyRetentionDaysMin, PolicyConstants.AfsVaultDailyRetentionDaysMax);
}
}

if (IsWeeklyScheduleEnabled)
{
if (WeeklySchedule == null)
{
throw new ArgumentException(Resources.WeeklyScheduleEnabledButScheduleIsNullException);
}
else
{
WeeklySchedule.BackupManagementType = BackupManagementType;
WeeklySchedule.Validate(ScheduleRunFrequency, PolicyConstants.AfsWeeklyRetentionMin, PolicyConstants.AfsVaultWeeklyRetentionMax);
}
}

if (IsMonthlyScheduleEnabled)
{
if (MonthlySchedule == null)
{
throw new ArgumentException(Resources.MonthlyScheduleEnabledButScheduleIsNullException);
}
else
{
MonthlySchedule.BackupManagementType = BackupManagementType;
MonthlySchedule.Validate(ScheduleRunFrequency, PolicyConstants.AfsMonthlyRetentionMin, PolicyConstants.AfsVaultMonthlyRetentionMax);
}
}

if (IsYearlyScheduleEnabled)
{
if (YearlySchedule == null)
{
throw new ArgumentException(Resources.YearlyScheduleEnabledButScheduleIsNullException);
}
else
{
YearlySchedule.BackupManagementType = BackupManagementType;
YearlySchedule.Validate(ScheduleRunFrequency, PolicyConstants.AfsYearlyRetentionMin, PolicyConstants.AfsVaultYearlyRetentionMax);
}
}
}

public override string ToString()
Expand Down Expand Up @@ -308,17 +358,26 @@ public class DailyRetentionSchedule : RetentionScheduleBase
public int DurationCountInDays { get; set; }

// no extra fields
public override void Validate(ScheduleRunType ScheduleRunFrequency = 0)
public void Validate(ScheduleRunType ScheduleRunFrequency = 0, int MinDuration = 0, int MaxDuration = 0)
{
int MinDurationCountInDays = 7, MaxDurationCountInDays = PolicyConstants.MaxAllowedRetentionDurationCount;
if(BackupManagementType == Management.RecoveryServices.Backup.Models.BackupManagementType.AzureStorage)
{
MinDurationCountInDays = PolicyConstants.AfsDailyRetentionDaysMin;
MaxDurationCountInDays = PolicyConstants.AfsVaultDailyRetentionDaysMax;
if (MinDuration != 0 && MaxDuration != 0)
{
MinDurationCountInDays = MinDuration;
MaxDurationCountInDays = MaxDuration;
}
else
{
MinDurationCountInDays = PolicyConstants.AfsDailyRetentionDaysMin;
MaxDurationCountInDays = PolicyConstants.AfsDailyRetentionDaysMax;
}
}

if (DurationCountInDays < MinDurationCountInDays || DurationCountInDays > MaxDurationCountInDays)
{
throw new ArgumentException(Resources.RetentionDurationCountInDaysInvalidException);
throw new ArgumentException(string.Format(Resources.RetentionDurationCountInvalidException, "Days", MinDurationCountInDays, MaxDurationCountInDays));
}

base.Validate(ScheduleRunFrequency);
Expand All @@ -345,17 +404,26 @@ public class WeeklyRetentionSchedule : RetentionScheduleBase
/// </summary>
public List<DayOfWeek> DaysOfTheWeek { get; set; }

public override void Validate(ScheduleRunType ScheduleRunFrequency = 0)
public void Validate(ScheduleRunType ScheduleRunFrequency = 0, int MinDuration = 0, int MaxDuration = 0)
{
int MinDurationCountInWeeks = 1, MaxDurationCountInWeeks = PolicyConstants.MaxAllowedRetentionDurationCountWeekly;
if(BackupManagementType == Management.RecoveryServices.Backup.Models.BackupManagementType.AzureStorage)
{
MinDurationCountInWeeks = PolicyConstants.AfsWeeklyRetentionMin;
MaxDurationCountInWeeks = PolicyConstants.AfsVaultWeeklyRetentionMax;
if (MinDuration != 0 && MaxDuration != 0)
{
MinDurationCountInWeeks = MinDuration;
MaxDurationCountInWeeks = MaxDuration;
}
else
{
MinDurationCountInWeeks = PolicyConstants.AfsWeeklyRetentionMin;
MaxDurationCountInWeeks = PolicyConstants.AfsWeeklyRetentionMax;
}
}

if (DurationCountInWeeks < MinDurationCountInWeeks || DurationCountInWeeks > MaxDurationCountInWeeks)
{
throw new ArgumentException(Resources.RetentionDurationCountInvalidException);
throw new ArgumentException(string.Format(Resources.RetentionDurationCountInvalidException, "Weeks", MinDurationCountInWeeks, MaxDurationCountInWeeks));
}

if (DaysOfTheWeek == null || DaysOfTheWeek.Count == 0 || DaysOfTheWeek.Count != DaysOfTheWeek.Distinct().Count())
Expand Down Expand Up @@ -403,20 +471,28 @@ public MonthlyRetentionSchedule()
{
}

public override void Validate(ScheduleRunType ScheduleRunFrequency = 0)
public void Validate(ScheduleRunType ScheduleRunFrequency = 0, int MinDuration = 0, int MaxDuration = 0)
{
base.Validate(ScheduleRunFrequency);

int MinDurationCountInMonths = 1, MaxDurationCountInMonths = PolicyConstants.MaxAllowedRetentionDurationCountMonthly;
if (BackupManagementType == Management.RecoveryServices.Backup.Models.BackupManagementType.AzureStorage)
{
MinDurationCountInMonths = PolicyConstants.AfsMonthlyRetentionMin;
MaxDurationCountInMonths = PolicyConstants.AfsVaultMonthlyRetentionMax;
if (MinDuration != 0 && MaxDuration != 0)
{
MinDurationCountInMonths = MinDuration;
MaxDurationCountInMonths = MaxDuration;
}
else
{
MinDurationCountInMonths = PolicyConstants.AfsMonthlyRetentionMin;
MaxDurationCountInMonths = PolicyConstants.AfsMonthlyRetentionMax;
}
}

if (DurationCountInMonths < MinDurationCountInMonths || DurationCountInMonths > MaxDurationCountInMonths)
{
throw new ArgumentException(Resources.RetentionDurationCountInvalidException);
throw new ArgumentException(string.Format(Resources.RetentionDurationCountInvalidException, "Months", MinDurationCountInMonths, MaxDurationCountInMonths));
}

if (RetentionScheduleFormatType == RetentionScheduleFormat.Daily)
Expand Down Expand Up @@ -487,19 +563,28 @@ public YearlyRetentionSchedule()

}

public override void Validate(ScheduleRunType ScheduleRunFrequency = 0)
public void Validate(ScheduleRunType ScheduleRunFrequency = 0, int MinDuration = 0, int MaxDuration = 0)
{
base.Validate(ScheduleRunFrequency);

int MinDurationCountInYears = 1, MaxDurationCountInYears = PolicyConstants.MaxAllowedRetentionDurationCountYearly;
if (BackupManagementType == Management.RecoveryServices.Backup.Models.BackupManagementType.AzureStorage)
{
MinDurationCountInYears = PolicyConstants.AfsYearlyRetentionMin;
MaxDurationCountInYears = PolicyConstants.AfsVaultYearlyRetentionMax;
if (MinDuration != 0 && MaxDuration != 0)
{
MinDurationCountInYears = MinDuration;
MaxDurationCountInYears = MaxDuration;
}
else
{
MinDurationCountInYears = PolicyConstants.AfsYearlyRetentionMin;
MaxDurationCountInYears = PolicyConstants.AfsYearlyRetentionMax;
}
}

if (DurationCountInYears < MinDurationCountInYears || DurationCountInYears > MaxDurationCountInYears)
{
throw new ArgumentException(Resources.RetentionDurationCountInvalidException);
throw new ArgumentException(string.Format(Resources.RetentionDurationCountInvalidException, "Years", MinDurationCountInYears, MaxDurationCountInYears));
}

if (MonthsOfYear == null || MonthsOfYear.Count == 0 || MonthsOfYear.Count != MonthsOfYear.Distinct().Count())
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
<value>In Monthly and Yearly retention schedules, RetentionFormatType cannot be 'Daily' if Weekly BackupSchedule is selected</value>
</data>
<data name="RetentionDurationCountInvalidException" xml:space="preserve">
<value>RetentionDuration in Days/Weeks/Months/Years should be from 1 - 9999</value>
<value>RetentionDuration in {0} should be from {1} - {2}</value>
</data>
<data name="WeeklyRetentionScheduleNullException" xml:space="preserve">
<value>If Weekly backup schedule is enabled, then IsDailyScheduleEnabled should be false and Weekly retention schedule should not be null, IsWeeklyScheduleEnabled should be true</value>
Expand Down Expand Up @@ -860,7 +860,7 @@ Please contact Microsoft for further assistance.</value>
<value>Unable to fetch CRR access token. Please retry the operation or contact Microsoft support if issue persists</value>
</data>
<data name="AFSPolicyUpdateNotAllowed" xml:space="preserve">
<value>Modifying existing policies to stop data transfer to the vault and retain backups only as snapshots is not supported. Please create a new policy to opt-out of the data transfer to the recovery services vault.</value>
<value>Switching the backup tier from vaulted backup to snapshot is not possible. Please create a new policy for snapshot-only backups.</value>
</data>
<data name="AFSPolicyUpdateWarning" xml:space="preserve">
<value>Changing the backup tier keeps current snapshots as-is under the existing policy. Future backups will be stored in the vault with new retention settings. This action is irreversible and incurs additional costs. Switching from vault to snapshot requires reconfiguration. Learn more at https://learn.microsoft.com/en-us/azure/backup/azure-file-share-backup-overview?tabs=snapshot.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ function Test-AzureFSVaultRestore
-VaultId $vault.ID `
-Policy $snapshotPolicy `
-Item $item
} "Modifying existing policies to stop data transfer to the vault and retain backups only as snapshots is not supported. Please create a new policy to opt-out of the data transfer to the recovery services vault."
} "Switching the backup tier from vaulted backup to snapshot is not possible. Please create a new policy for snapshot-only backups."

$item = Get-AzRecoveryServicesBackupItem -VaultId $vault.ID -BackupManagementType AzureStorage -WorkloadType AzureFiles

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function Test-AzureFSVaultPolicy
-RetentionPolicy $retentionPolicy `
-SchedulePolicy $schedulePolicy `
-Policy $policy } `
"Modifying existing policies to stop data transfer to the vault and retain backups only as snapshots is not supported. Please create a new policy to opt-out of the data transfer to the recovery services vault."
"Switching the backup tier from vaulted backup to snapshot is not possible. Please create a new policy for snapshot-only backups."

# Delete policy
Remove-AzRecoveryServicesBackupProtectionPolicy `
Expand Down

0 comments on commit 27283dc

Please sign in to comment.