Skip to content

Commit 6e77249

Browse files
author
dragonfly91
committed
Fixed merge conflicts
2 parents 4d1b5fc + 2d96f73 commit 6e77249

File tree

14 files changed

+447
-59
lines changed

14 files changed

+447
-59
lines changed

src/ResourceManager/RecoveryServices.Backup/Cmdlets/ProtectionPolicy/GetAzureRmRecoveryServicesPolicy.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
using System.Management.Automation;
2121
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
2222
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ProviderModel;
23+
using Microsoft.Azure.Management.RecoveryServices.Backup.Models;
24+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers;
2325

2426
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
2527
{
2628
/// <summary>
2729
/// Get list of protection policies
2830
/// </summary>
29-
[Cmdlet(VerbsCommon.Get, "AzureRmRecoveryServicesProtectionPolicy", DefaultParameterSetName = NoParamSet), OutputType(typeof(AzureRmRecoveryServicesPolicyBase), typeof(List<AzureRmRecoveryServicesPolicyBase>))]
31+
[Cmdlet(VerbsCommon.Get, "AzureRmRecoveryServicesProtectionPolicy", DefaultParameterSetName = NoParamSet), OutputType(typeof(List<AzureRmRecoveryServicesPolicyBase>))]
3032
public class GetAzureRmRecoveryServicesProtectionPolicy : RecoveryServicesBackupCmdletBase
3133
{
3234
protected const string PolicyNameParamSet = "PolicyNameParamSet";
@@ -49,15 +51,28 @@ public class GetAzureRmRecoveryServicesProtectionPolicy : RecoveryServicesBackup
4951

5052
public override void ExecuteCmdlet()
5153
{
52-
// TBD section below
53-
5454
base.ExecuteCmdlet();
5555

56+
string rgName = ""; // TBD
57+
string resourceName = ""; // TBD
58+
List<AzureRmRecoveryServicesPolicyBase> respList = new List<AzureRmRecoveryServicesPolicyBase>();
59+
5660
switch(this.ParameterSetName)
5761
{
5862
case PolicyNameParamSet:
63+
// validate policyName
64+
PolicyCmdletHelpers.ValidateProtectionPolicyName(Name);
65+
66+
// query service
67+
ProtectionPolicyResponse policy = PolicyCmdletHelpers.GetProtectionPolicyByName(
68+
Name,
69+
HydraAdapter,
70+
rgName,
71+
resourceName);
72+
respList.Add(ConversionHelpers.GetPolicyModel(policy.Item));
5973
break;
6074

75+
// below cases TBD
6176
case WorkloadParamSet:
6277
break;
6378

@@ -69,9 +84,9 @@ public override void ExecuteCmdlet()
6984

7085
default:
7186
break;
72-
}
87+
}
7388

74-
// TBD
89+
WriteObject(respList);
7590
}
7691
}
7792
}

src/ResourceManager/RecoveryServices.Backup/Cmdlets/ProtectionPolicy/NewAzureRmRecoveryServicesPolicy.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ public override void ExecuteCmdlet()
5555
{
5656
base.ExecuteCmdlet();
5757

58+
// validate policy name
59+
PolicyCmdletHelpers.ValidateProtectionPolicyName(Name);
60+
61+
// Validate if policy already exists
62+
string rgName = ""; // TBD
63+
string resourceName = ""; // TBD
64+
if(PolicyCmdletHelpers.GetProtectionPolicyByName(Name, HydraAdapter, rgName, resourceName) != null)
65+
{
66+
throw new ArgumentException("Policy already exists with this name:" + Name);
67+
}
68+
5869
PsBackupProviderManager providerManager = new PsBackupProviderManager(new Dictionary<System.Enum, object>()
5970
{
6071
{PolicyParams.PolicyName, Name},
@@ -68,8 +79,11 @@ public override void ExecuteCmdlet()
6879
psBackupProvider.CreatePolicy();
6980

7081
// now get the created policy and return
71-
ProtectionPolicyResponse policy = psBackupProvider.GetPolicy();
72-
82+
ProtectionPolicyResponse policy = PolicyCmdletHelpers.GetProtectionPolicyByName(
83+
Name,
84+
HydraAdapter,
85+
rgName,
86+
resourceName);
7387
// now convert hydraPolicy to PSObject
7488
WriteObject(ConversionHelpers.GetPolicyModel(policy.Item));
7589
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Linq;
17+
using System.Text;
18+
using System.Threading.Tasks;
19+
using System.Collections.Generic;
20+
using System.Management.Automation;
21+
using System.Text.RegularExpressions;
22+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers;
23+
using Microsoft.Azure.Management.RecoveryServices.Backup.Models;
24+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
25+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ProviderModel;
26+
27+
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
28+
{
29+
public class PolicyCmdletHelpers
30+
{
31+
public static Regex rgx = new Regex(@"^[A-Za-z][-A-Za-z0-9]*[A-Za-z0-9]$");
32+
33+
public static void ValidateProtectionPolicyName(string policyName)
34+
{
35+
if (policyName.Length < PolicyConstants.MinPolicyNameLength ||
36+
policyName.Length > PolicyConstants.MaxPolicyNameLength)
37+
{
38+
throw new ArgumentException(string.Format(
39+
"PolicyName length exception - should be within {0} and {1}",
40+
PolicyConstants.MinPolicyNameLength,
41+
PolicyConstants.MaxPolicyNameLength));
42+
}
43+
44+
if (!rgx.IsMatch(policyName))
45+
{
46+
var exception = new ArgumentException(
47+
"The protection policy name should contain alphanumeric characters " +
48+
"and cannot start with a number");
49+
throw exception;
50+
}
51+
}
52+
53+
public static ProtectionPolicyResponse GetProtectionPolicyByName(string policyName,
54+
HydraAdapter.HydraAdapter hydraAdapter,
55+
string resourceGroupName,
56+
string resourceName)
57+
{
58+
ProtectionPolicyResponse response = null;
59+
60+
try
61+
{
62+
response = hydraAdapter.GetProtectionPolicy(resourceGroupName, resourceName, policyName);
63+
}
64+
catch
65+
{
66+
// if http response is NotFound - then ignore
67+
// else rethrow the exception - TBD
68+
}
69+
70+
return response;
71+
}
72+
}
73+
}

src/ResourceManager/RecoveryServices.Backup/Cmdlets/ProtectionPolicy/SetAzureRmRecoveryServicesPolicy.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
2828
/// <summary>
2929
/// Update existing protection policy
3030
/// </summary>
31-
[Cmdlet(VerbsCommon.Set, "AzureRmRecoveryServicesProtectionPolicy")]
31+
[Cmdlet(VerbsCommon.Set, "AzureRmRecoveryServicesProtectionPolicy"), OutputType(typeof(List<AzureRmRecoveryServicesJobBase>))]
3232
public class SetAzureRmRecoveryServicesProtectionPolicy : RecoveryServicesBackupCmdletBase
3333
{
3434
[Parameter(Mandatory = false, HelpMessage = ParamHelpMsg.Policy.RetentionPolicy)]
@@ -47,6 +47,19 @@ public override void ExecuteCmdlet()
4747
{
4848
base.ExecuteCmdlet();
4949

50+
// Validate policy name
51+
PolicyCmdletHelpers.ValidateProtectionPolicyName(Policy.Name);
52+
53+
// Validate if policy already exists
54+
string rgName = ""; // TBD
55+
string resourceName = ""; // TBD
56+
ProtectionPolicyResponse servicePolicy = PolicyCmdletHelpers.GetProtectionPolicyByName(
57+
Policy.Name, HydraAdapter, rgName, resourceName);
58+
if (servicePolicy == null)
59+
{
60+
throw new ArgumentException("Policy doesn't exist with this name:" + Policy.Name);
61+
}
62+
5063
PsBackupProviderManager providerManager = new PsBackupProviderManager(new Dictionary<System.Enum, object>()
5164
{
5265
{PolicyParams.ProtectionPolicy, Policy},
@@ -55,14 +68,9 @@ public override void ExecuteCmdlet()
5568
}, HydraAdapter);
5669

5770
IPsBackupProvider psBackupProvider = providerManager.GetProviderInstance(Policy.WorkloadType,
58-
Policy.BackupManagementType);
59-
psBackupProvider.ModifyPolicy();
60-
61-
// now get the created policy and return
62-
ProtectionPolicyResponse policy = psBackupProvider.GetPolicy();
63-
71+
Policy.BackupManagementType);
6472
// now convert hydraPolicy to PSObject
65-
WriteObject(ConversionHelpers.GetPolicyModel(policy.Item));
73+
WriteObject(psBackupProvider.ModifyPolicy());
6674
}
6775
}
6876
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Cmdlets.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<Compile Include="Cmdlets\ProtectionPolicy\GetAzureRmRecoveryServicesPolicy.cs" />
7676
<Compile Include="Cmdlets\ProtectionPolicy\GetAzureRMBackupSchedulePolicyObject.cs" />
7777
<Compile Include="Cmdlets\ProtectionPolicy\GetAzureRMBackupRetentionPolicyObject.cs" />
78+
<Compile Include="Cmdlets\ProtectionPolicy\PolicyCmdletHelpers.cs" />
7879
<Compile Include="Cmdlets\ProtectionPolicy\SetAzureRmRecoveryServicesPolicy.cs" />
7980
<Compile Include="Cmdlets\ProtectionPolicy\NewAzureRmRecoveryServicesPolicy.cs" />
8081
<Compile Include="CmdletWarningAndErrorMessages.cs" />

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 ListProtectionPolicy(
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/CmdletParamEnums.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public enum PolicyParams
3838
PolicyName,
3939
SchedulePolicy,
4040
RetentionPolicy,
41-
ProtectionPolicy
41+
ProtectionPolicy,
42+
ResourceGroupName,
43+
ResourceName
4244
}
4345
}

0 commit comments

Comments
 (0)