Skip to content

Commit 27283dc

Browse files
IannGeorgesianna1-admin
andauthored
[Az.RecoveryServices.Backup] Improve Policy Error Code (#26788)
* fix error messages * pass custom retention constants for vault policy validation * fix unit test --------- Co-authored-by: ianna1-admin <[email protected]>
1 parent 6936a17 commit 27283dc

File tree

5 files changed

+109
-24
lines changed

5 files changed

+109
-24
lines changed

src/RecoveryServices/RecoveryServices.Backup.Models/CommonModels/PolicyRetentionObjects.cs

Lines changed: 103 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,64 @@ public override void Validate()
219219
/// </summary>
220220
public new void Validate(ScheduleRunType ScheduleRunFrequency = 0)
221221
{
222-
base.Validate(ScheduleRunFrequency);
223-
224222
int MinDurationCountInDays = 1, MaxDurationCountInDays = PolicyConstants.AfsSnapshotRetentionDaysMax;
225223

226224
if (SnapshotRetentionInDays < MinDurationCountInDays || SnapshotRetentionInDays > MaxDurationCountInDays)
227225
{
228226
throw new ArgumentException(Resources.SnapshotRetentionInDaysInvalidException);
229227
}
228+
229+
if (IsDailyScheduleEnabled)
230+
{
231+
if (DailySchedule == null)
232+
{
233+
throw new ArgumentException(Resources.DailyScheduleEnabledButScheduleIsNullException);
234+
}
235+
else
236+
{
237+
DailySchedule.BackupManagementType = BackupManagementType;
238+
DailySchedule.Validate(ScheduleRunFrequency, PolicyConstants.AfsDailyRetentionDaysMin, PolicyConstants.AfsVaultDailyRetentionDaysMax);
239+
}
240+
}
241+
242+
if (IsWeeklyScheduleEnabled)
243+
{
244+
if (WeeklySchedule == null)
245+
{
246+
throw new ArgumentException(Resources.WeeklyScheduleEnabledButScheduleIsNullException);
247+
}
248+
else
249+
{
250+
WeeklySchedule.BackupManagementType = BackupManagementType;
251+
WeeklySchedule.Validate(ScheduleRunFrequency, PolicyConstants.AfsWeeklyRetentionMin, PolicyConstants.AfsVaultWeeklyRetentionMax);
252+
}
253+
}
254+
255+
if (IsMonthlyScheduleEnabled)
256+
{
257+
if (MonthlySchedule == null)
258+
{
259+
throw new ArgumentException(Resources.MonthlyScheduleEnabledButScheduleIsNullException);
260+
}
261+
else
262+
{
263+
MonthlySchedule.BackupManagementType = BackupManagementType;
264+
MonthlySchedule.Validate(ScheduleRunFrequency, PolicyConstants.AfsMonthlyRetentionMin, PolicyConstants.AfsVaultMonthlyRetentionMax);
265+
}
266+
}
267+
268+
if (IsYearlyScheduleEnabled)
269+
{
270+
if (YearlySchedule == null)
271+
{
272+
throw new ArgumentException(Resources.YearlyScheduleEnabledButScheduleIsNullException);
273+
}
274+
else
275+
{
276+
YearlySchedule.BackupManagementType = BackupManagementType;
277+
YearlySchedule.Validate(ScheduleRunFrequency, PolicyConstants.AfsYearlyRetentionMin, PolicyConstants.AfsVaultYearlyRetentionMax);
278+
}
279+
}
230280
}
231281

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

310360
// no extra fields
311-
public override void Validate(ScheduleRunType ScheduleRunFrequency = 0)
361+
public void Validate(ScheduleRunType ScheduleRunFrequency = 0, int MinDuration = 0, int MaxDuration = 0)
312362
{
313363
int MinDurationCountInDays = 7, MaxDurationCountInDays = PolicyConstants.MaxAllowedRetentionDurationCount;
314364
if(BackupManagementType == Management.RecoveryServices.Backup.Models.BackupManagementType.AzureStorage)
315365
{
316-
MinDurationCountInDays = PolicyConstants.AfsDailyRetentionDaysMin;
317-
MaxDurationCountInDays = PolicyConstants.AfsVaultDailyRetentionDaysMax;
366+
if (MinDuration != 0 && MaxDuration != 0)
367+
{
368+
MinDurationCountInDays = MinDuration;
369+
MaxDurationCountInDays = MaxDuration;
370+
}
371+
else
372+
{
373+
MinDurationCountInDays = PolicyConstants.AfsDailyRetentionDaysMin;
374+
MaxDurationCountInDays = PolicyConstants.AfsDailyRetentionDaysMax;
375+
}
318376
}
377+
319378
if (DurationCountInDays < MinDurationCountInDays || DurationCountInDays > MaxDurationCountInDays)
320379
{
321-
throw new ArgumentException(Resources.RetentionDurationCountInDaysInvalidException);
380+
throw new ArgumentException(string.Format(Resources.RetentionDurationCountInvalidException, "Days", MinDurationCountInDays, MaxDurationCountInDays));
322381
}
323382

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

348-
public override void Validate(ScheduleRunType ScheduleRunFrequency = 0)
407+
public void Validate(ScheduleRunType ScheduleRunFrequency = 0, int MinDuration = 0, int MaxDuration = 0)
349408
{
350409
int MinDurationCountInWeeks = 1, MaxDurationCountInWeeks = PolicyConstants.MaxAllowedRetentionDurationCountWeekly;
351410
if(BackupManagementType == Management.RecoveryServices.Backup.Models.BackupManagementType.AzureStorage)
352411
{
353-
MinDurationCountInWeeks = PolicyConstants.AfsWeeklyRetentionMin;
354-
MaxDurationCountInWeeks = PolicyConstants.AfsVaultWeeklyRetentionMax;
412+
if (MinDuration != 0 && MaxDuration != 0)
413+
{
414+
MinDurationCountInWeeks = MinDuration;
415+
MaxDurationCountInWeeks = MaxDuration;
416+
}
417+
else
418+
{
419+
MinDurationCountInWeeks = PolicyConstants.AfsWeeklyRetentionMin;
420+
MaxDurationCountInWeeks = PolicyConstants.AfsWeeklyRetentionMax;
421+
}
355422
}
423+
356424
if (DurationCountInWeeks < MinDurationCountInWeeks || DurationCountInWeeks > MaxDurationCountInWeeks)
357425
{
358-
throw new ArgumentException(Resources.RetentionDurationCountInvalidException);
426+
throw new ArgumentException(string.Format(Resources.RetentionDurationCountInvalidException, "Weeks", MinDurationCountInWeeks, MaxDurationCountInWeeks));
359427
}
360428

361429
if (DaysOfTheWeek == null || DaysOfTheWeek.Count == 0 || DaysOfTheWeek.Count != DaysOfTheWeek.Distinct().Count())
@@ -403,20 +471,28 @@ public MonthlyRetentionSchedule()
403471
{
404472
}
405473

406-
public override void Validate(ScheduleRunType ScheduleRunFrequency = 0)
474+
public void Validate(ScheduleRunType ScheduleRunFrequency = 0, int MinDuration = 0, int MaxDuration = 0)
407475
{
408476
base.Validate(ScheduleRunFrequency);
409477

410478
int MinDurationCountInMonths = 1, MaxDurationCountInMonths = PolicyConstants.MaxAllowedRetentionDurationCountMonthly;
411479
if (BackupManagementType == Management.RecoveryServices.Backup.Models.BackupManagementType.AzureStorage)
412480
{
413-
MinDurationCountInMonths = PolicyConstants.AfsMonthlyRetentionMin;
414-
MaxDurationCountInMonths = PolicyConstants.AfsVaultMonthlyRetentionMax;
481+
if (MinDuration != 0 && MaxDuration != 0)
482+
{
483+
MinDurationCountInMonths = MinDuration;
484+
MaxDurationCountInMonths = MaxDuration;
485+
}
486+
else
487+
{
488+
MinDurationCountInMonths = PolicyConstants.AfsMonthlyRetentionMin;
489+
MaxDurationCountInMonths = PolicyConstants.AfsMonthlyRetentionMax;
490+
}
415491
}
416492

417493
if (DurationCountInMonths < MinDurationCountInMonths || DurationCountInMonths > MaxDurationCountInMonths)
418494
{
419-
throw new ArgumentException(Resources.RetentionDurationCountInvalidException);
495+
throw new ArgumentException(string.Format(Resources.RetentionDurationCountInvalidException, "Months", MinDurationCountInMonths, MaxDurationCountInMonths));
420496
}
421497

422498
if (RetentionScheduleFormatType == RetentionScheduleFormat.Daily)
@@ -487,19 +563,28 @@ public YearlyRetentionSchedule()
487563

488564
}
489565

490-
public override void Validate(ScheduleRunType ScheduleRunFrequency = 0)
566+
public void Validate(ScheduleRunType ScheduleRunFrequency = 0, int MinDuration = 0, int MaxDuration = 0)
491567
{
492568
base.Validate(ScheduleRunFrequency);
493569

494570
int MinDurationCountInYears = 1, MaxDurationCountInYears = PolicyConstants.MaxAllowedRetentionDurationCountYearly;
495571
if (BackupManagementType == Management.RecoveryServices.Backup.Models.BackupManagementType.AzureStorage)
496572
{
497-
MinDurationCountInYears = PolicyConstants.AfsYearlyRetentionMin;
498-
MaxDurationCountInYears = PolicyConstants.AfsVaultYearlyRetentionMax;
573+
if (MinDuration != 0 && MaxDuration != 0)
574+
{
575+
MinDurationCountInYears = MinDuration;
576+
MaxDurationCountInYears = MaxDuration;
577+
}
578+
else
579+
{
580+
MinDurationCountInYears = PolicyConstants.AfsYearlyRetentionMin;
581+
MaxDurationCountInYears = PolicyConstants.AfsYearlyRetentionMax;
582+
}
499583
}
584+
500585
if (DurationCountInYears < MinDurationCountInYears || DurationCountInYears > MaxDurationCountInYears)
501586
{
502-
throw new ArgumentException(Resources.RetentionDurationCountInvalidException);
587+
throw new ArgumentException(string.Format(Resources.RetentionDurationCountInvalidException, "Years", MinDurationCountInYears, MaxDurationCountInYears));
503588
}
504589

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

src/RecoveryServices/RecoveryServices.Backup.Models/Properties/Resources.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RecoveryServices/RecoveryServices.Backup.Models/Properties/Resources.resx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@
226226
<value>In Monthly and Yearly retention schedules, RetentionFormatType cannot be 'Daily' if Weekly BackupSchedule is selected</value>
227227
</data>
228228
<data name="RetentionDurationCountInvalidException" xml:space="preserve">
229-
<value>RetentionDuration in Days/Weeks/Months/Years should be from 1 - 9999</value>
229+
<value>RetentionDuration in {0} should be from {1} - {2}</value>
230230
</data>
231231
<data name="WeeklyRetentionScheduleNullException" xml:space="preserve">
232232
<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>
@@ -860,7 +860,7 @@ Please contact Microsoft for further assistance.</value>
860860
<value>Unable to fetch CRR access token. Please retry the operation or contact Microsoft support if issue persists</value>
861861
</data>
862862
<data name="AFSPolicyUpdateNotAllowed" xml:space="preserve">
863-
<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>
863+
<value>Switching the backup tier from vaulted backup to snapshot is not possible. Please create a new policy for snapshot-only backups.</value>
864864
</data>
865865
<data name="AFSPolicyUpdateWarning" xml:space="preserve">
866866
<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>

src/RecoveryServices/RecoveryServices.Backup.Test/ScenarioTests/AzureFiles/ItemTests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ function Test-AzureFSVaultRestore
495495
-VaultId $vault.ID `
496496
-Policy $snapshotPolicy `
497497
-Item $item
498-
} "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."
498+
} "Switching the backup tier from vaulted backup to snapshot is not possible. Please create a new policy for snapshot-only backups."
499499

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

src/RecoveryServices/RecoveryServices.Backup.Test/ScenarioTests/AzureFiles/PolicyTests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function Test-AzureFSVaultPolicy
9595
-RetentionPolicy $retentionPolicy `
9696
-SchedulePolicy $schedulePolicy `
9797
-Policy $policy } `
98-
"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."
98+
"Switching the backup tier from vaulted backup to snapshot is not possible. Please create a new policy for snapshot-only backups."
9999

100100
# Delete policy
101101
Remove-AzRecoveryServicesBackupProtectionPolicy `

0 commit comments

Comments
 (0)