Skip to content

Commit 80ce066

Browse files
committed
Minor bug fixes in policy, added testcase of policy
1 parent 6e266b7 commit 80ce066

File tree

6 files changed

+84
-15
lines changed

6 files changed

+84
-15
lines changed

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Helpers/Conversions/SchedulePolicyConversions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@ public static AzureRmRecoveryServicesSimpleSchedulePolicy GetPSSimpleSchedulePol
5151

5252
#region PStoHydraObject conversions
5353

54+
public static List<DateTime> ParseScheduleRunTimesToUTC(List<DateTime> localTimes)
55+
{
56+
if (localTimes == null || localTimes.Count == 0)
57+
{
58+
return null;
59+
}
60+
61+
List<DateTime> utcTimes = new List<DateTime>();
62+
DateTime temp;
63+
64+
foreach (DateTime localTime in localTimes)
65+
{
66+
temp = localTime;
67+
if (localTime.Kind == DateTimeKind.Local)
68+
{
69+
temp = localTime.ToUniversalTime();
70+
temp = new DateTime(temp.Year, temp.Month,
71+
temp.Day, temp.Hour, temp.Minute - (temp.Minute % 30), 0);
72+
}
73+
utcTimes.Add(temp);
74+
}
75+
76+
return utcTimes;
77+
}
78+
5479
public static SimpleSchedulePolicy GetHydraSimpleSchedulePolicy(
5580
AzureRmRecoveryServicesSimpleSchedulePolicy psPolicy)
5681
{
@@ -65,6 +90,7 @@ public static SimpleSchedulePolicy GetHydraSimpleSchedulePolicy(
6590
{
6691
hydraPolicy.ScheduleRunDays = HelperUtils.GetStringListFromEnumList<DayOfWeek>(psPolicy.ScheduleRunDays);
6792
}
93+
hydraPolicy.ScheduleRunTimes = psPolicy.ScheduleRunTimes;
6894

6995
return hydraPolicy;
7096
}

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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@
250250
<value>In Schedule, if ScheduleRunFrequency is Weekly then ScheduleRunDays should not be empty and not contain duplicate entries</value>
251251
</data>
252252
<data name="InvalidScheduleTimeInScheduleException" xml:space="preserve">
253-
<value>ScheduleTimes in Schedule should have ONE time and must be of multiples of 30 Mins with seconds and milliseconds set to 0</value>
253+
<value>ScheduleTimes in Schedule should be in UTC Timezone, have ONE time and must be of multiples of 30 Mins with seconds and milliseconds set to 0</value>
254254
</data>
255255
<data name="InvalidWeeksOfMonthInMonthlyYearlyRetentionPolicyException" xml:space="preserve">
256256
<value>In Monthly/Yearly retention schedule, WeeksOfMonth is NULL or empty or contains duplicate entires</value>

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Providers/Providers/IaasVmPsBackupProvider.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ public ProtectionPolicyResponse CreatePolicy()
203203

204204
// do validations
205205
ValidateAzureVMWorkloadType(workloadType);
206+
ValidateAzureVMSchedulePolicy(schedulePolicy);
207+
208+
// update the retention times from backupSchedule to retentionPolicy after converting to UTC
209+
CopyScheduleTimeToRetentionTimes((AzureRmRecoveryServicesLongTermRetentionPolicy)retentionPolicy,
210+
(AzureRmRecoveryServicesSimpleSchedulePolicy)schedulePolicy);
206211

207212
// validate both RetentionPolicy and SchedulePolicy
208213
ValidateAzureVMRetentionPolicy(retentionPolicy);
@@ -263,6 +268,11 @@ public List<AzureRmRecoveryServicesJobBase> ModifyPolicy()
263268
((AzureRmRecoveryServicesIaasVmPolicy)policy).RetentionPolicy = retentionPolicy;
264269
}
265270

271+
// copy the backupSchedule time to retentionPolicy after converting to UTC
272+
CopyScheduleTimeToRetentionTimes(
273+
(AzureRmRecoveryServicesLongTermRetentionPolicy)((AzureRmRecoveryServicesIaasVmPolicy)policy).RetentionPolicy,
274+
(AzureRmRecoveryServicesSimpleSchedulePolicy)((AzureRmRecoveryServicesIaasVmPolicy)policy).SchedulePolicy);
275+
266276
// Now validate both RetentionPolicy and SchedulePolicy matches or not
267277
PolicyHelpers.ValidateLongTermRetentionPolicyWithSimpleRetentionPolicy(
268278
(AzureRmRecoveryServicesLongTermRetentionPolicy)((AzureRmRecoveryServicesIaasVmPolicy)policy).RetentionPolicy,
@@ -437,7 +447,7 @@ private static DateTime GenerateRandomTime()
437447
Random rand = new Random();
438448
int hour = rand.Next(0, 24);
439449
int minute = (rand.Next(0, 2) == 0) ? 0 : 30;
440-
return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minute, 00);
450+
return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minute, 00, DateTimeKind.Utc);
441451
}
442452

443453

@@ -519,6 +529,7 @@ private AzureIaaSVMProtectableItem GetAzureVMProtectableObject(string azureVMNam
519529
return protectableObject;
520530

521531
}
532+
522533
private bool IsDiscoveryNeeded(string vmName, string rgName, bool isComputeAzureVM,
523534
out AzureIaaSVMProtectableItem protectableObject)
524535
{
@@ -641,6 +652,34 @@ private HttpStatusCode TrackRefreshContainerOperation(string operationResultLink
641652
return status;
642653
}
643654

655+
private void CopyScheduleTimeToRetentionTimes(AzureRmRecoveryServicesLongTermRetentionPolicy retPolicy,
656+
AzureRmRecoveryServicesSimpleSchedulePolicy schPolicy)
657+
{
658+
// first convert schedule run times to UTC
659+
schPolicy.ScheduleRunTimes = PolicyHelpers.ParseScheduleRunTimesToUTC(schPolicy.ScheduleRunTimes);
660+
661+
// now copy times from schedule to retention policy
662+
if(retPolicy.IsDailyScheduleEnabled && retPolicy.DailySchedule != null)
663+
{
664+
retPolicy.DailySchedule.RetentionTimes = schPolicy.ScheduleRunTimes;
665+
}
666+
667+
if (retPolicy.IsWeeklyScheduleEnabled && retPolicy.WeeklySchedule != null)
668+
{
669+
retPolicy.DailySchedule.RetentionTimes = schPolicy.ScheduleRunTimes;
670+
}
671+
672+
if (retPolicy.IsMonthlyScheduleEnabled && retPolicy.MonthlySchedule != null)
673+
{
674+
retPolicy.DailySchedule.RetentionTimes = schPolicy.ScheduleRunTimes;
675+
}
676+
677+
if (retPolicy.IsYearlyScheduleEnabled && retPolicy.YearlySchedule != null)
678+
{
679+
retPolicy.DailySchedule.RetentionTimes = schPolicy.ScheduleRunTimes;
680+
}
681+
}
682+
644683
#endregion
645684
}
646685
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Test/ScenarioTests/IaasVm/PolicyTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
1615
using System;
1716
using System.Collections.Generic;
1817
using System.Linq;
1918
using System.Text;
2019
using System.Threading.Tasks;
2120
using Xunit;
21+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
2222

2323
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Test.ScenarioTests
2424
{
25-
public partial class ContainerTests : RecoveryServicesBackupTestsBase
25+
public partial class PolicyTests : RecoveryServicesBackupTestsBase
2626
{
2727
[Fact]
2828
public void TestPolicyScenario()

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Test/ScenarioTests/IaasVm/PolicyTests.ps1

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@
1515
function Test-PolicyScenario
1616
{
1717
$vault = Get-AzureRmRecoveryServicesVault -ResourceGroupName "phaniktRSV" -Name "phaniktRs1";
18-
$containers = Get-AzureRmRecoveryServicesContainer -Vault $vault -ContainerType "AzureVM" -Status "Registered";
19-
foreach ($container in $containers)
20-
{
21-
echo $container.Name $container.ResourceGroupName;
22-
}
23-
Assert-AreEqual $containers[0].Name "mylinux1";
18+
Set-AzureRmRecoveryServicesVaultContext -Vault $vault;
2419

25-
$namedContainer = Get-AzureRmRecoveryServicesContainer -Vault $vault -ContainerType "AzureVM" -Status "Registered" -Name "mylinux1";
26-
Assert-AreEqual $namedContainer.Name "mylinux1";
20+
# get default objects
21+
$schedulePolicy = Get-AzureRmRecoveryServicesSchedulePolicyObject -WorkloadType "AzureVM"
22+
$retPolicy = Get-AzureRmRecoveryServicesBackupRetentionPolicyObject -WorkloadType "AzureVM"
2723

28-
$rgFilteredContainer = Get-AzureRmRecoveryServicesContainer -Vault $vault -ContainerType "AzureVM" -Status "Registered" -Name "mylinux1" -ResourceGroupName "00prjai12";
29-
echo $rgFilteredContainer.Name $rgFilteredContainer.ResourceGroupName;
24+
# now create new policy
25+
$policy = New-AzureRmRecoveryServicesProtectionPolicy -Name "pwtest1" -WorkloadType "AzureVM" -RetentionPolicy $retPolicy -SchedulePolicy $schedulePolicy
26+
27+
# now get policy and update it with new schedule/retention
28+
$schedulePolicy = Get-AzureRmRecoveryServicesSchedulePolicyObject -WorkloadType "AzureVM"
29+
$retPolicy = Get-AzureRmRecoveryServicesBackupRetentionPolicyObject -WorkloadType "AzureVM"
30+
31+
Get-AzureRmRecoveryServicesProtectionPolicy -Name "pwtest1" | Set-AzureRmRecoveryServicesProtectionPolicy -RetentionPolicy $retPolicy -SchedulePolicy $schedulePolicy
32+
33+
echo $schedulePolicy
3034
}

0 commit comments

Comments
 (0)