Skip to content

Commit d25c368

Browse files
committed
Added hydra adapter apis for policy and minor fixes
1 parent 109a74e commit d25c368

File tree

4 files changed

+143
-27
lines changed

4 files changed

+143
-27
lines changed

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Helpers/Validations/PolicyValidations.cs

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,55 @@ public static void ValidateLongTermRetentionPolicyWithSimpleRetentionPolicy(
5555
{
5656
ValidateRetentionAndBackupTimes(schPolicy.ScheduleRunTimes, ltrPolicy.WeeklySchedule.RetentionTimes);
5757

58-
// validate days of week - TBD
58+
// count of daysOfWeek should match for weekly schedule
59+
if(ltrPolicy.WeeklySchedule.DaysOfTheWeek.Count != schPolicy.ScheduleRunDays.Count)
60+
{
61+
throw new ArgumentException(
62+
"DaysOfTheWeek in WeeklySchedule of retention policy must be same as ScheduleRunDays in SchedulePolicy");
63+
}
64+
65+
// validate days of week
66+
ValidateRetentionAndScheduleDaysOfWeek(schPolicy.ScheduleRunDays, ltrPolicy.WeeklySchedule.DaysOfTheWeek);
5967
}
6068

6169
// validate monthly retention schedule with schPolicy
6270
if (ltrPolicy.MonthlySchedule != null)
6371
{
6472
ValidateRetentionAndBackupTimes(schPolicy.ScheduleRunTimes, ltrPolicy.MonthlySchedule.RetentionTimes);
6573

66-
// for monthly and weeklyFormat, validate days of week - - TBD
74+
// if backupSchedule is weekly, then user cannot choose 'Daily Retention format'
75+
if(schPolicy.ScheduleRunFrequency == ScheduleRunType.Weekly &&
76+
ltrPolicy.MonthlySchedule.RetentionScheduleFormatType == Cmdlets.Models.RetentionScheduleFormat.Daily)
77+
{
78+
throw new ArgumentException("DailyRetentionFormat cannot be chosen if Weekly BackupSchedule is selected");
79+
}
80+
81+
// for monthly and weeklyFormat, validate days of week
82+
if(ltrPolicy.MonthlySchedule.RetentionScheduleFormatType == Cmdlets.Models.RetentionScheduleFormat.Weekly)
83+
{
84+
ValidateRetentionAndScheduleDaysOfWeek(schPolicy.ScheduleRunDays,
85+
ltrPolicy.MonthlySchedule.RetentionScheduleWeekly.DaysOfTheWeek);
86+
}
6787
}
6888

6989
// validate yearly retention schedule with schPolicy
7090
if (ltrPolicy.YearlySchedule != null)
7191
{
7292
ValidateRetentionAndBackupTimes(schPolicy.ScheduleRunTimes, ltrPolicy.YearlySchedule.RetentionTimes);
7393

74-
// for yearly and weeklyFormat, validate days of week - TBD
94+
// if backupSchedule is weekly, then user cannot choose 'Daily Retention format'
95+
if (schPolicy.ScheduleRunFrequency == ScheduleRunType.Weekly &&
96+
ltrPolicy.YearlySchedule.RetentionScheduleFormatType == Cmdlets.Models.RetentionScheduleFormat.Daily)
97+
{
98+
throw new ArgumentException("DailyRetentionFormat cannot be chosen if Weekly BackupSchedule is selected");
99+
}
100+
101+
// for yearly and weeklyFormat, validate days of week
102+
if (ltrPolicy.YearlySchedule.RetentionScheduleFormatType == Cmdlets.Models.RetentionScheduleFormat.Weekly)
103+
{
104+
ValidateRetentionAndScheduleDaysOfWeek(schPolicy.ScheduleRunDays,
105+
ltrPolicy.YearlySchedule.RetentionScheduleWeekly.DaysOfTheWeek);
106+
}
75107
}
76108
}
77109

@@ -96,23 +128,14 @@ private static void ValidateRetentionAndBackupTimes(List<DateTime> schPolicyTime
96128

97129
private static void ValidateRetentionAndScheduleDaysOfWeek(List<DayOfWeek> schList, List<DayOfWeek> retList)
98130
{
99-
// TBD
100-
101-
/* if (backupDOWList.Count != DaysOfTheWeek.Count)
131+
foreach (var day in retList)
102132
{
103-
throw new ArgumentException("DaysOfTheWeek of retention schedule must be same of backup schedule DaysOfTheWeek");
104-
} */
105-
106-
// each day in retList must be present in
107-
108-
/* foreach (var day in DaysOfTheWeek)
109-
{
110-
if (!backupDOWList.Contains(day))
133+
if (!schList.Contains(day))
111134
{
112-
throw new ArgumentException("DaysOfTheWeek of retention schedule must be same of backup schedule DaysOfTheWeek");
135+
throw new ArgumentException(
136+
"DaysOfTheWeek in retention schedule should be subset of ScheduleRunDays of SchedulePolicy");
113137
}
114-
115-
}*/
138+
}
116139
}
117140

118141
#endregion

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.HydraAdapter/BMSAPIs/PolicyAPIs.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,57 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16-
using System.Collections.Generic;
1716
using System.Linq;
1817
using System.Text;
1918
using System.Threading.Tasks;
19+
using System.Collections.Generic;
20+
using Microsoft.Azure.Management.RecoveryServices.Backup.Models;
2021

2122
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.HydraAdapter
2223
{
2324
public partial class HydraAdapter
2425
{
26+
public ProtectionPolicyResponse CreateOrUpdateProtectionPolicy(
27+
string resourceGroupName,
28+
string resourceName,
29+
string policyName,
30+
ProtectionPolicyRequest request)
31+
{
32+
return BmsAdapter.Client.ProtectionPolicy.CreateOrUpdateAsync(
33+
resourceGroupName,
34+
resourceName,
35+
policyName,
36+
request,
37+
BmsAdapter.GetCustomRequestHeaders(),
38+
BmsAdapter.CmdletCancellationToken).Result;
39+
}
40+
41+
public ProtectionPolicyResponse GetProtectionPolicy(
42+
string resourceGroupName,
43+
string resourceName,
44+
string policyName)
45+
{
46+
return BmsAdapter.Client.ProtectionPolicy.GetAsync(
47+
resourceGroupName,
48+
resourceName,
49+
policyName,
50+
BmsAdapter.GetCustomRequestHeaders(),
51+
BmsAdapter.CmdletCancellationToken).Result;
52+
}
53+
54+
public ProtectionPolicyListResponse GetProtectionPolicy(
55+
string resourceGroupName,
56+
string resourceName,
57+
ProtectionPolicyQueryParameters queryFilter)
58+
{
59+
return BmsAdapter.Client.ProtectionPolicy.ListAsync(
60+
resourceGroupName,
61+
resourceName,
62+
queryFilter,
63+
BmsAdapter.GetCustomRequestHeaders(),
64+
BmsAdapter.CmdletCancellationToken).Result;
65+
}
66+
67+
// TBD for other operations
2568
}
2669
}

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

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public override void Validate()
6262
{
6363
if (WeeklySchedule == null)
6464
{
65-
throw new ArgumentException("IsDailyScheduleEnabled=true but DailySchedule is NULL");
65+
throw new ArgumentException("IsWeeklyScheduleEnabled=true but WeeklySchedule is NULL");
6666
}
6767
else
6868
{
@@ -74,7 +74,7 @@ public override void Validate()
7474
{
7575
if (MonthlySchedule == null)
7676
{
77-
throw new ArgumentException("IsDailyScheduleEnabled=true but DailySchedule is NULL");
77+
throw new ArgumentException("IsMonthlyScheduleEnabled=true but MonthlySchedule is NULL");
7878
}
7979
else
8080
{
@@ -86,7 +86,7 @@ public override void Validate()
8686
{
8787
if (YearlySchedule == null)
8888
{
89-
throw new ArgumentException("IsDailyScheduleEnabled=true but DailySchedule is NULL");
89+
throw new ArgumentException("IsYearlyScheduleEnabled=true but YearlySchedule is NULL");
9090
}
9191
else
9292
{
@@ -171,7 +171,12 @@ public override void Validate()
171171
if (DaysOfTheWeek == null || DaysOfTheWeek.Count == 0)
172172
{
173173
throw new ArgumentException("DaysOfTheWeek is NULL/Empty");
174-
}
174+
}
175+
176+
if(DaysOfTheWeek.Count != DaysOfTheWeek.Distinct().Count())
177+
{
178+
throw new ArgumentException("DaysOfTheWeek list in WeeklyRetentionSchdule contains duplicate entries");
179+
}
175180

176181
base.Validate();
177182
}
@@ -191,12 +196,12 @@ public class MonthlyRetentionSchedule : RetentionScheduleBase
191196
public RetentionScheduleFormat RetentionScheduleFormatType { get; set; }
192197

193198
public DailyRetentionFormat RetentionScheduleDaily { get; set; }
199+
194200
public WeeklyRetentionFormat RetentionScheduleWeekly { get; set; }
195201

196202
public MonthlyRetentionSchedule()
197203
: base()
198-
{
199-
204+
{
200205
}
201206

202207
public override void Validate()
@@ -270,6 +275,11 @@ public override void Validate()
270275
{
271276
throw new ArgumentException("MonthsOfYear is set to NULL/Empty");
272277
}
278+
279+
if (MonthsOfYear.Count != MonthsOfYear.Distinct().Count())
280+
{
281+
throw new ArgumentException("MonthsOfYear list in YearlyRetentionSchdule contains duplicate entries");
282+
}
273283

274284
if (RetentionScheduleFormatType == RetentionScheduleFormat.Daily)
275285
{
@@ -314,6 +324,29 @@ public void Validate()
314324
{
315325
throw new ArgumentException("DaysOfThe Month is set to NULL/Empty");
316326
}
327+
328+
// check if all the days are unique or not
329+
List<Day> distinctDays = DaysOfTheMonth.GroupBy(x => new { x.Date, x.IsLast }).Select(g => g.First()).ToList();
330+
if (DaysOfTheMonth.Count != distinctDays.Count)
331+
{
332+
throw new ArgumentException("DaysOfTheMonth list in DailyRetentionFormat contains duplicate entries");
333+
}
334+
335+
// also check if there exists more than one 'IsLast=true'
336+
int countOfIsLast = 0;
337+
foreach (Day day in DaysOfTheMonth)
338+
{
339+
day.Validate();
340+
if(day.IsLast)
341+
{
342+
countOfIsLast++;
343+
}
344+
}
345+
346+
if(countOfIsLast > 1)
347+
{
348+
throw new ArgumentException("Only ONE day can have IsLast=true in DaysOfTheMonth list");
349+
}
317350
}
318351

319352
public override string ToString()
@@ -338,7 +371,17 @@ public void Validate()
338371
if (WeeksOfTheMonth == null || WeeksOfTheMonth.Count == 0)
339372
{
340373
throw new ArgumentException("WeeksOfTheMonth is set to NULL/Empty");
341-
}
374+
}
375+
376+
if (DaysOfTheWeek.Count != DaysOfTheWeek.Distinct().Count())
377+
{
378+
throw new ArgumentException("DaysOfTheWeek list in WeeklyRetentionFormat contains duplicate entries");
379+
}
380+
381+
if (WeeksOfTheMonth.Count != WeeksOfTheMonth.Distinct().Count())
382+
{
383+
throw new ArgumentException("WeeksOfTheMonth list in WeeklyRetentionFormat contains duplicate entries");
384+
}
342385
}
343386

344387
public override string ToString()

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Models/CommonModels/PolicyScheduleObjects.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,21 @@ public override void Validate()
4848
if (ScheduleRunDays == null || ScheduleRunDays.Count == 0)
4949
{
5050
throw new ArgumentException("", "scheduleRunDays");
51-
}
51+
}
52+
53+
if (ScheduleRunDays.Count != ScheduleRunDays.Distinct().Count())
54+
{
55+
throw new ArgumentException("ScheduleRunDays list in BackupSchedule contains duplicate entries");
56+
}
5257
}
5358
}
5459

5560
public override string ToString()
5661
{
5762
return String.Format("scheduleRunType:{0}, ScheduleRunDays:{1}, ScheduleRunTimes:{2}",
58-
ScheduleRunFrequency, ScheduleRunDays, ScheduleRunTimes);
63+
ScheduleRunFrequency,
64+
TraceUtils.GetString(ScheduleRunDays),
65+
TraceUtils.GetString(ScheduleRunTimes));
5966
}
6067
}
6168
}

0 commit comments

Comments
 (0)