Skip to content

Commit 2d96f73

Browse files
committed
Merge pull request #197 from MabOneSdk/dev1-sudreddy
Policy cmdlets implementation and bug fixes
2 parents 210d834 + 3fc8388 commit 2d96f73

File tree

11 files changed

+298
-42
lines changed

11 files changed

+298
-42
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.HydraAdapter/BMSAPIs/PolicyAPIs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public ProtectionPolicyResponse GetProtectionPolicy(
5151
BmsAdapter.CmdletCancellationToken).Result;
5252
}
5353

54-
public ProtectionPolicyListResponse GetProtectionPolicy(
54+
public ProtectionPolicyListResponse ListProtectionPolicy(
5555
string resourceGroupName,
5656
string resourceName,
5757
ProtectionPolicyQueryParameters queryFilter)

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
}

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

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

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

2223
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ProviderModel
2324
{
@@ -39,10 +40,6 @@ public interface IPsBackupProvider
3940

4041
ProtectionPolicyResponse CreatePolicy();
4142

42-
ProtectionPolicyResponse ModifyPolicy();
43-
44-
ProtectionPolicyResponse GetPolicy();
45-
46-
void DeletePolicy();
43+
List<AzureRmRecoveryServicesJobBase> ModifyPolicy();
4744
}
4845
}

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Linq;
1818
using System.Text;
1919
using System.Threading.Tasks;
20+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
2021

2122
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ProviderModel
2223
{
@@ -62,17 +63,7 @@ public Management.RecoveryServices.Backup.Models.ProtectionPolicyResponse Create
6263
throw new NotImplementedException();
6364
}
6465

65-
public Management.RecoveryServices.Backup.Models.ProtectionPolicyResponse ModifyPolicy()
66-
{
67-
throw new NotImplementedException();
68-
}
69-
70-
public Management.RecoveryServices.Backup.Models.ProtectionPolicyResponse GetPolicy()
71-
{
72-
throw new NotImplementedException();
73-
}
74-
75-
public void DeletePolicy()
66+
public List<AzureRmRecoveryServicesJobBase> ModifyPolicy()
7667
{
7768
throw new NotImplementedException();
7869
}

0 commit comments

Comments
 (0)