Skip to content

Commit 09407d4

Browse files
committed
ProtectionPolicy Changes 3
1 parent 1b71ddb commit 09407d4

9 files changed

+105
-173
lines changed

src/ResourceManager/AzureBackup/Commands.AzureBackup/AzureBackupCmdletBase.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ public abstract class AzureBackupCmdletBase : AzurePSCmdlet
6060
/// Cancellation Token Source
6161
/// </summary>
6262
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
63-
protected CancellationToken CmdletCancellationToken;
63+
internal CancellationToken CmdletCancellationToken;
64+
65+
protected AzureBackupCmdletHelper azureBackupCmdletHelper;
6466

6567
/// <summary>
6668
/// Get Azure backup client.
6769
/// </summary>
68-
protected BackupServicesManagementClient AzureBackupClient
70+
internal BackupServicesManagementClient AzureBackupClient
6971
{
7072
get
7173
{
@@ -97,7 +99,7 @@ public void InitializeAzureBackupCmdlet(string rgName, string rName, string loca
9799
WriteDebug(string.Format("Initialized AzureBackup Cmdlet, ClientRequestId: {0}, ResourceGroupName: {1}, ResourceName : {2}", this.clientRequestId, resourceGroupName, resourceName));
98100

99101
CmdletCancellationToken = cancellationTokenSource.Token;
100-
AzureBackupCmdletHelper cmdHelper = new AzureBackupCmdletHelper(this);
102+
azureBackupCmdletHelper = new AzureBackupCmdletHelper(this);
101103
}
102104

103105
protected void ExecutionBlock(Action execAction)
@@ -170,7 +172,7 @@ private void HandleException(Exception exception)
170172
}
171173
}
172174

173-
protected CustomRequestHeaders GetCustomRequestHeaders()
175+
internal CustomRequestHeaders GetCustomRequestHeaders()
174176
{
175177
var hdrs = new CustomRequestHeaders()
176178
{

src/ResourceManager/AzureBackup/Commands.AzureBackup/AzureBackupCmdletHelpMessage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ internal static class AzureBackupCmdletHelpMessage
4141
public const string Comments = "Comments for for removing protection";
4242
public const string WorkloadType = "Workload type for which the policy is defined.";
4343
public const string BackupType = "Type of backup.";
44-
public const string ScheduleType = "Type of schedule.";
44+
public const string DailyScheduleType = "Switch parameter for daily backup schedule.";
45+
public const string WeeklyScheduleType = "Switch parameter for weekly backup schedule.";
4546
public const string ScheduleRunDays = "Days of week for running backup, required for weekly schedule.";
4647
public const string ScheduleRunTimes = "Times of day for running backup.";
4748
public const string RetentionType = "Unit of retention for the recovery point.";

src/ResourceManager/AzureBackup/Commands.AzureBackup/AzureBackupPolicyCmdletBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public override void ExecuteCmdlet()
3939
{
4040
base.ExecuteCmdlet();
4141

42-
WriteDebug(String.Format("Cmdlet called for ResourceGroupName: {0}, ResourceName: {1}, Location: {2}", ProtectionPolicy.ResourceGroupName, ProtectionPolicy.ResourceName, ProtectionPolicy.Location));
42+
WriteDebug(String.Format("Cmdlet called for ResourceGroupName: {0}, ResourceName: {1}, Location: {2}",
43+
ProtectionPolicy.ResourceGroupName, ProtectionPolicy.ResourceName, ProtectionPolicy.Location));
4344

4445
InitializeAzureBackupCmdlet(ProtectionPolicy.ResourceGroupName, ProtectionPolicy.ResourceName, ProtectionPolicy.Location);
4546
}

src/ResourceManager/AzureBackup/Commands.AzureBackup/Cmdlets/ProtectionPolicy/GetAzureBackupProtectionPolicy.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,23 @@ public override void ExecuteCmdlet()
3939
{
4040
WriteDebug("Making client call");
4141

42-
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
43-
44-
WriteDebug("Received policy response");
4542
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
4643
if (Name != null)
4744
{
48-
policyObjects = policyListResponse.ProtectionPolicies.Objects.Where(x => x.Name.Equals(Name, System.StringComparison.InvariantCultureIgnoreCase));
45+
AzureBackupProtectionPolicy policyInfo = azureBackupCmdletHelper.GetAzureBackupProtectionPolicyByName(Name, ResourceGroupName, ResourceName, Location);
46+
WriteDebug("Converting response");
47+
WriteObject(policyInfo);
4948
}
5049
else
5150
{
51+
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
52+
53+
WriteDebug("Received policy response");
5254
policyObjects = policyListResponse.ProtectionPolicies.Objects;
53-
}
5455

55-
WriteDebug("Converting response");
56-
AzureBackupCmdletHelper.WriteAzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, policyObjects);
57-
56+
WriteDebug("Converting response");
57+
azureBackupCmdletHelper.WriteAzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, policyObjects);
58+
}
5859
});
5960
}
6061
}

src/ResourceManager/AzureBackup/Commands.AzureBackup/Cmdlets/ProtectionPolicy/NewAzureBackupProtectionPolicy.cs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,32 @@ public class NewAzureBackupProtectionPolicy : AzureBackupVaultCmdletBase
3636
public string Name { get; set; }
3737

3838
[Parameter(Position = 4, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.WorkloadType, ValueFromPipelineByPropertyName = true)]
39-
[ValidateSet("VM")]
39+
[ValidateSet("VM", IgnoreCase = true)]
4040
public string WorkloadType { get; set; }
4141

4242
[Parameter(Position = 5, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.BackupType, ValueFromPipelineByPropertyName = true)]
43-
[ValidateSet("Full")]
43+
[ValidateSet("Full", IgnoreCase = true)]
4444
public string BackupType { get; set; }
4545

46-
[Parameter(ParameterSetName = DailyScheduleParamSet, Position = 7, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleType)]
46+
[Parameter(ParameterSetName = DailyScheduleParamSet, Position = 6, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.DailyScheduleType)]
4747
public SwitchParameter Daily { get; set; }
4848

49-
[Parameter(ParameterSetName = WeeklyScheduleParamSet, Position = 6, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleType)]
49+
[Parameter(ParameterSetName = WeeklyScheduleParamSet, Position = 7, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.WeeklyScheduleType)]
5050
public SwitchParameter Weekly { get; set; }
5151

52-
[Parameter(Position = 7, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunTimes, ValueFromPipelineByPropertyName = true)]
52+
[Parameter(Position = 8, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunTimes, ValueFromPipelineByPropertyName = true)]
5353
public DateTime ScheduleRunTimes { get; set; }
5454

55-
[Parameter(Position = 8, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RetentionType, ValueFromPipelineByPropertyName = true)]
55+
[Parameter(Position = 9, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RetentionType, ValueFromPipelineByPropertyName = true)]
5656
[ValidateSet("Days", IgnoreCase = true)]
5757
public string RetentionType { get; set; }
5858

59-
[Parameter(Position = 9, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RententionDuration, ValueFromPipelineByPropertyName = true)]
59+
[Parameter(Position = 10, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RententionDuration, ValueFromPipelineByPropertyName = true)]
6060
public int RetentionDuration { get; set; }
6161

62-
[Parameter(ParameterSetName = WeeklyScheduleParamSet, Position = 10, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunDays, ValueFromPipelineByPropertyName = true)]
63-
[Parameter(ParameterSetName = NoScheduleParamSet, Position = 10, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunDays, ValueFromPipelineByPropertyName = true)]
62+
[Parameter(ParameterSetName = WeeklyScheduleParamSet, Position = 11, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunDays, ValueFromPipelineByPropertyName = true)]
63+
[Parameter(ParameterSetName = NoScheduleParamSet, Position = 11, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunDays, ValueFromPipelineByPropertyName = true)]
64+
[AllowEmptyCollection]
6465
[ValidateSet("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", IgnoreCase = true)]
6566
public string[] ScheduleRunDays { get; set; }
6667

@@ -74,31 +75,20 @@ public override void ExecuteCmdlet()
7475

7576
var ScheduleType = GetScheduelType(ScheduleRunDays);
7677

77-
var backupSchedule = AzureBackupCmdletHelper.FillBackupSchedule(BackupType, ScheduleType, ScheduleRunTimes,
78+
var backupSchedule = azureBackupCmdletHelper.FillBackupSchedule(BackupType, ScheduleType, ScheduleRunTimes,
7879
RetentionType, RetentionDuration, ScheduleRunDays);
7980

80-
81-
//var backupSchedule = GetBackupSchedule(BackupType, ScheduleType, ScheduleRunTimes,
82-
// RetentionType, RetentionDuration, ScheduleRunDays);
83-
8481
var addProtectionPolicyRequest = new AddProtectionPolicyRequest();
8582
addProtectionPolicyRequest.PolicyName = this.Name;
8683
addProtectionPolicyRequest.Schedule = backupSchedule;
87-
addProtectionPolicyRequest.WorkloadType = this.WorkloadType;
84+
addProtectionPolicyRequest.WorkloadType = Enum.Parse(typeof(WorkloadType), this.WorkloadType, true).ToString();
8885

8986
var operationId = AzureBackupClient.ProtectionPolicy.AddAsync(addProtectionPolicyRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
90-
9187
WriteDebug("Protection policy created successfully");
92-
93-
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
94-
95-
WriteDebug("Received policy response");
96-
97-
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
98-
policyObjects = policyListResponse.ProtectionPolicies.Where(x => x.Name.Equals(Name, System.StringComparison.InvariantCultureIgnoreCase));
9988

89+
AzureBackupProtectionPolicy policyInfo = azureBackupCmdletHelper.GetAzureBackupProtectionPolicyByName(Name, ResourceGroupName, ResourceName, Location);
10090
WriteDebug("Converting response");
101-
AzureBackupCmdletHelper.WriteAzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, policyObjects);
91+
WriteObject(policyInfo);
10292
});
10393
}
10494

src/ResourceManager/AzureBackup/Commands.AzureBackup/Cmdlets/ProtectionPolicy/RemoveAzureBackupProtectionPolicy.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,20 @@ public override void ExecuteCmdlet()
3535
{
3636
WriteDebug("Making client call");
3737

38-
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
39-
40-
WriteDebug("Received policy response");
41-
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
42-
43-
policyObjects = policyListResponse.ProtectionPolicies.Objects.Where(x => x.Name.Equals(ProtectionPolicy.Name, System.StringComparison.InvariantCultureIgnoreCase));
44-
45-
if (policyObjects.Count<ProtectionPolicyInfo>() != 0)
38+
AzureBackupProtectionPolicy policyInfo = azureBackupCmdletHelper.GetAzureBackupProtectionPolicyByName(ProtectionPolicy.Name,
39+
ProtectionPolicy.ResourceGroupName, ProtectionPolicy.ResourceName, ProtectionPolicy.Location);
40+
41+
if (policyInfo != null)
4642
{
47-
ProtectionPolicyInfo protectionPolicyInfo = policyObjects.ElementAt<ProtectionPolicyInfo>(0);
48-
var policyRemoveResponse = AzureBackupClient.ProtectionPolicy.DeleteAsync(protectionPolicyInfo.InstanceId, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
43+
var policyRemoveResponse = AzureBackupClient.ProtectionPolicy.DeleteAsync(policyInfo.InstanceId, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
44+
45+
WriteDebug("Converting response");
46+
WriteVerbose("Successfully deleted policy");
4947
}
5048
else
5149
{
5250
WriteVerbose("Policy Not Found");
5351
}
54-
55-
WriteDebug("Converting response");
56-
WriteVerbose("Successfully deleted policy");
5752
});
5853
}
5954
}

src/ResourceManager/AzureBackup/Commands.AzureBackup/Cmdlets/ProtectionPolicy/SetAzureBackupProtectionPolicy.cs

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,31 @@ public class SetAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
3131
protected const string DailyScheduleParamSet = "DailyScheduleParamSet";
3232
protected const string NoScheduleParamSet = "NoScheduleParamSet";
3333

34-
[Parameter(Position = 3, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.PolicyNewName, ValueFromPipelineByPropertyName = true)]
34+
[Parameter(Position = 1, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.PolicyNewName)]
3535
[ValidateNotNullOrEmpty]
3636
public string NewName { get; set; }
3737

38-
[Parameter(Position = 5, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.BackupType, ValueFromPipelineByPropertyName = true)]
39-
[ValidateSet("Full")]
38+
[Parameter(Position = 2, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.BackupType)]
39+
[ValidateSet("Full", IgnoreCase = true)]
4040
public string BackupType { get; set; }
4141

42-
[Parameter(ParameterSetName = DailyScheduleParamSet, Position = 7, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleType)]
42+
[Parameter(ParameterSetName = DailyScheduleParamSet, Position = 3, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.DailyScheduleType)]
4343
public SwitchParameter Daily { get; set; }
4444

45-
[Parameter(ParameterSetName = WeeklyScheduleParamSet, Position = 6, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleType)]
45+
[Parameter(ParameterSetName = WeeklyScheduleParamSet, Position = 4, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.WeeklyScheduleType)]
4646
public SwitchParameter Weekly { get; set; }
4747

48-
[Parameter(Position = 7, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunTimes, ValueFromPipelineByPropertyName = true)]
48+
[Parameter(Position = 5, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunTimes)]
4949
public DateTime ScheduleRunTimes { get; set; }
5050

51-
[Parameter(Position = 8, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.RetentionType, ValueFromPipelineByPropertyName = true)]
51+
[Parameter(Position = 6, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.RetentionType)]
5252
[ValidateSet("Days", IgnoreCase = true)]
5353
public string RetentionType { get; set; }
5454

55-
[Parameter(Position = 9, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.RententionDuration, ValueFromPipelineByPropertyName = true)]
55+
[Parameter(Position = 7, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.RententionDuration)]
5656
public int RetentionDuration { get; set; }
5757

58-
[Parameter(ParameterSetName = WeeklyScheduleParamSet, Position = 10, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunDays, ValueFromPipelineByPropertyName = true)]
58+
[Parameter(ParameterSetName = WeeklyScheduleParamSet, Position = 8, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunDays)]
5959
[ValidateSet("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", IgnoreCase = true)]
6060
public string[] ScheduleRunDays { get; set; }
6161

@@ -66,53 +66,38 @@ public override void ExecuteCmdlet()
6666
ExecutionBlock(() =>
6767
{
6868
WriteDebug("Making client call");
69-
70-
AzureBackupProtectionPolicy policy = ProtectionPolicy;
71-
72-
FillRemainingValuesForSetPolicyRequest(policy);
7369

74-
AzureBackupCmdletHelper.ValidateAzureBackupPolicyRequest(policy);
70+
AzureBackupProtectionPolicy policyInfo = azureBackupCmdletHelper.GetAzureBackupProtectionPolicyByName(ProtectionPolicy.Name,
71+
ProtectionPolicy.ResourceGroupName, ProtectionPolicy.ResourceName, ProtectionPolicy.Location);
72+
73+
FillRemainingValuesForSetPolicyRequest(policyInfo);
7574

76-
var backupSchedule = AzureBackupCmdletHelper.FillBackupSchedule(BackupType, policy.ScheduleType, ScheduleRunTimes,
77-
RetentionType, RetentionDuration, policy.ScheduleRunDays.ToArray<string>());
78-
79-
80-
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
75+
var backupSchedule = azureBackupCmdletHelper.FillBackupSchedule(BackupType, policyInfo.ScheduleType, ScheduleRunTimes,
76+
RetentionType, RetentionDuration, policyInfo.ScheduleRunDays.ToArray<string>());
8177

82-
NewName = (string.IsNullOrEmpty(NewName) ? policy.Name: NewName);
78+
NewName = (string.IsNullOrEmpty(NewName) ? policyInfo.Name : NewName);
8379
var updateProtectionPolicyRequest = new UpdateProtectionPolicyRequest();
8480
updateProtectionPolicyRequest.PolicyName = this.NewName;
8581
updateProtectionPolicyRequest.Schedule = backupSchedule;
8682

87-
WriteDebug("Got the protectionPolicy List");
88-
89-
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
90-
91-
policyObjects = policyListResponse.ProtectionPolicies.Objects.Where(x => x.Name.Equals(policy.Name, System.StringComparison.InvariantCultureIgnoreCase));
92-
93-
WriteDebug("Got the protectionPolicy with Name" + this.ProtectionPolicy.Name);
94-
95-
if (policyObjects.Count<ProtectionPolicyInfo>() != 0)
83+
if (policyInfo != null)
9684
{
97-
var operationId = AzureBackupClient.ProtectionPolicy.UpdateAsync(ProtectionPolicy.InstanceId, updateProtectionPolicyRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
85+
var operationId = AzureBackupClient.ProtectionPolicy.UpdateAsync(policyInfo.InstanceId, updateProtectionPolicyRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
9886
}
9987
else
10088
{
10189
var exception = new Exception("Protection Policy Not Found with Name" + ProtectionPolicy.Name);
10290
var errorRecord = new ErrorRecord(exception, string.Empty, ErrorCategory.InvalidData, null);
103-
WriteError(errorRecord);
91+
WriteError(errorRecord);
92+
return;
10493
}
10594

10695
WriteDebug("Protection Policy successfully updated");
10796

108-
var policyListResponse_afterUpdate = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
109-
110-
WriteDebug("Received policy response");
111-
112-
policyObjects = policyListResponse_afterUpdate.ProtectionPolicies.Where(x => x.Name.Equals(NewName, System.StringComparison.InvariantCultureIgnoreCase));
113-
97+
AzureBackupProtectionPolicy updatedPolicyInfo = azureBackupCmdletHelper.GetAzureBackupProtectionPolicyByName(NewName,
98+
ProtectionPolicy.ResourceGroupName, ProtectionPolicy.ResourceName, ProtectionPolicy.Location);
11499
WriteDebug("Converting response");
115-
AzureBackupCmdletHelper.WriteAzureBackupProtectionPolicy(policy.ResourceGroupName, policy.ResourceName, policy.Location, policyObjects);
100+
WriteObject(updatedPolicyInfo);
116101

117102
});
118103
}
@@ -154,6 +139,7 @@ private void FillRemainingValuesForSetPolicyRequest(AzureBackupProtectionPolicy
154139
else
155140
{
156141
policy.ScheduleType = ScheduleType.Daily.ToString();
142+
policy.ScheduleRunDays = new List<string>();
157143
}
158144

159145
}

0 commit comments

Comments
 (0)