Skip to content

Commit 502d827

Browse files
committed
ProtectionPolicy Commandlets
1 parent 08dd728 commit 502d827

File tree

7 files changed

+196
-214
lines changed

7 files changed

+196
-214
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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.Management.Automation;
17+
using System.Collections.Generic;
18+
using System.Xml;
19+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
20+
using Microsoft.Azure.Common.Authentication;
21+
using Microsoft.Azure.Common.Authentication.Models;
22+
using System.Threading;
23+
using Hyak.Common;
24+
using Microsoft.Azure.Commands.AzureBackup.Properties;
25+
using System.Net;
26+
using Microsoft.Azure.Management.BackupServices.Models;
27+
28+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
29+
{
30+
public abstract class AzureBackupPolicyCmdletBase : AzureBackupVaultCmdletBase
31+
{
32+
public override void ExecuteCmdlet()
33+
{
34+
base.ExecuteCmdlet();
35+
36+
WriteDebug(String.Format("Cmdlet called for ResourceGroupName: {0}, ResourceName: {1}, Location: {2}", ResourceGroupName, ResourceName, Location));
37+
}
38+
39+
public void WriteAzureBackupProtectionPolicy(ProtectionPolicyInfo sourcePolicy)
40+
{
41+
this.WriteObject(new AzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, sourcePolicy));
42+
}
43+
44+
public void WriteAzureBackupProtectionPolicy(IEnumerable<ProtectionPolicyInfo> sourcePolicyList)
45+
{
46+
List<AzureBackupProtectionPolicy> targetList = new List<AzureBackupProtectionPolicy>();
47+
48+
foreach (var sourcePolicy in sourcePolicyList)
49+
{
50+
targetList.Add(new AzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, sourcePolicy));
51+
}
52+
53+
this.WriteObject(targetList, true);
54+
}
55+
56+
public BackupSchedule GetBackupSchedule(string backupType, string scheduleType, DateTime scheduleStartTime,
57+
string retentionType, int retentionDuration, string[] scheduleRunDays = null)
58+
{
59+
var backupSchedule = new BackupSchedule();
60+
61+
backupSchedule.BackupType = backupType;
62+
backupSchedule.RetentionPolicy = GetRetentionPolicy(retentionType, retentionDuration);
63+
//Enum.Parse(ScheduleRunType, this.ScheduleType),
64+
backupSchedule.ScheduleRun = scheduleType;
65+
if (string.Compare(scheduleType, "Weekly", true) == 0)
66+
{
67+
backupSchedule.ScheduleRunDays = GetScheduleRunDays(scheduleRunDays);
68+
}
69+
70+
DateTime scheduleRunTime = GetScheduleRunTime(scheduleStartTime);
71+
72+
backupSchedule.ScheduleRunTimes = new List<DateTime> { scheduleRunTime };
73+
74+
WriteDebug("Exiting GetBackupSchedule");
75+
return backupSchedule;
76+
}
77+
78+
private RetentionPolicy GetRetentionPolicy(string retentionType, int retentionDuration)
79+
{
80+
var retentionPolicy = new RetentionPolicy
81+
{
82+
RetentionType = (RetentionDurationType)Enum.Parse(typeof(RetentionDurationType), retentionType, true),
83+
RetentionDuration = retentionDuration
84+
};
85+
86+
return retentionPolicy;
87+
}
88+
89+
private IList<DayOfWeek> GetScheduleRunDays(string[] scheduleRunDays)
90+
{
91+
if (scheduleRunDays == null || scheduleRunDays.Length <= 0)
92+
{
93+
var exception = new Exception("For weekly scheduletype , ScheduleRunDays param is required.");
94+
var errorRecord = new ErrorRecord(exception, string.Empty, ErrorCategory.InvalidData, null);
95+
WriteError(errorRecord);
96+
}
97+
98+
IList<DayOfWeek> ListofWeekDays = new List<DayOfWeek>();
99+
100+
foreach (var dayOfWeek in scheduleRunDays)
101+
{
102+
WriteDebug("dayOfWeek" + dayOfWeek.ToString());
103+
DayOfWeek item = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), dayOfWeek, true);
104+
WriteDebug("Item" + item.ToString());
105+
if (!ListofWeekDays.Contains(item))
106+
{
107+
ListofWeekDays.Add(item);
108+
}
109+
}
110+
111+
return ListofWeekDays;
112+
}
113+
114+
private DateTime GetScheduleRunTime(DateTime scheduleStartTime)
115+
{
116+
scheduleStartTime = scheduleStartTime.ToUniversalTime();
117+
DateTime scheduleRunTime = new DateTime(scheduleStartTime.Year, scheduleStartTime.Month,
118+
scheduleStartTime.Day, scheduleStartTime.Hour, scheduleStartTime.Minute - (scheduleStartTime.Minute % 30), 0);
119+
return scheduleRunTime;
120+
}
121+
}
122+
}

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

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2525
/// Get list of protection policies
2626
/// </summary>
2727
[Cmdlet(VerbsCommon.Get, "AzureBackupProtectionPolicy"), OutputType(typeof(AzureBackupProtectionPolicy), typeof(List<AzureBackupProtectionPolicy>))]
28-
public class GetAzureBackupProtectionPolicy : AzureBackupVaultCmdletBase
28+
public class GetAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
2929
{
3030
[Parameter(Position = 3, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
3131
[ValidateNotNullOrEmpty]
@@ -37,11 +37,11 @@ public override void ExecuteCmdlet()
3737

3838
ExecutionBlock(() =>
3939
{
40-
WriteVerbose("Making client call");
40+
WriteDebug("Making client call");
4141

4242
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
4343

44-
WriteVerbose("Received policy response");
44+
WriteDebug("Received policy response");
4545
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
4646
if (Name != null)
4747
{
@@ -52,27 +52,10 @@ public override void ExecuteCmdlet()
5252
policyObjects = policyListResponse.ProtectionPolicies.Objects;
5353
}
5454

55-
WriteVerbose("Converting response");
55+
WriteDebug("Converting response");
5656
WriteAzureBackupProtectionPolicy(policyObjects);
5757
});
5858
}
59-
60-
public void WriteAzureBackupProtectionPolicy(ProtectionPolicyInfo sourcePolicy)
61-
{
62-
this.WriteObject(new AzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, sourcePolicy));
63-
}
64-
65-
public void WriteAzureBackupProtectionPolicy(IEnumerable<ProtectionPolicyInfo> sourcePolicyList)
66-
{
67-
List<AzureBackupProtectionPolicy> targetList = new List<AzureBackupProtectionPolicy>();
68-
69-
foreach (var sourcePolicy in sourcePolicyList)
70-
{
71-
targetList.Add(new AzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, sourcePolicy));
72-
}
73-
74-
this.WriteObject(targetList, true);
75-
}
7659
}
7760
}
7861

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

Lines changed: 23 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -25,139 +25,70 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2525
/// Create new protection policy
2626
/// </summary>
2727
[Cmdlet(VerbsCommon.Add, "AzureBackupProtectionPolicy"), OutputType(typeof(AzureBackupProtectionPolicy))]
28-
public class NewAzureBackupProtectionPolicy : AzureBackupVaultCmdletBase
29-
{
30-
[Parameter(Position = 0, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName, ValueFromPipelineByPropertyName = true)]
28+
public class NewAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
29+
{
30+
[Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
3131
[ValidateNotNullOrEmpty]
3232
public string Name { get; set; }
3333

34-
[Parameter(Position = 1, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.WorkloadType, ValueFromPipelineByPropertyName = true)]
34+
[Parameter(Position = 4, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.WorkloadType, ValueFromPipelineByPropertyName = true)]
3535
[ValidateSet("VM")]
3636
public string WorkloadType { get; set; }
3737

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

42-
[Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleType, ValueFromPipelineByPropertyName = true)]
43-
[ValidateSet("Daily","Weekly")]
42+
[Parameter(Position = 6, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleType, ValueFromPipelineByPropertyName = true)]
43+
[ValidateSet("Daily", "Weekly")]
4444
public string ScheduleType { get; set; }
4545

46-
[Parameter(Position = 4, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunDays, ValueFromPipelineByPropertyName = true)]
47-
[AllowEmptyCollection]
48-
public string[] ScheduleRunDays { get; set; }
49-
50-
[Parameter(Position = 5, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunTimes, ValueFromPipelineByPropertyName = true)]
51-
[ValidateNotNullOrEmpty]
46+
[Parameter(Position = 7, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunTimes, ValueFromPipelineByPropertyName = true)]
5247
public DateTime ScheduleRunTimes { get; set; }
5348

54-
[Parameter(Position = 6, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RetentionType, ValueFromPipelineByPropertyName = true)]
55-
[ValidateSet("Days")]
49+
[Parameter(Position = 8, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RetentionType, ValueFromPipelineByPropertyName = true)]
50+
[ValidateSet("Days", IgnoreCase = true)]
5651
public string RetentionType { get; set; }
5752

58-
[Parameter(Position = 7, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RententionDuration, ValueFromPipelineByPropertyName = true)]
53+
[Parameter(Position = 9, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RententionDuration, ValueFromPipelineByPropertyName = true)]
5954
[ValidateRange(1,30)]
6055
public int RetentionDuration { get; set; }
6156

57+
[Parameter(Position = 10, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunDays, ValueFromPipelineByPropertyName = true)]
58+
[ValidateSet("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", IgnoreCase = true)]
59+
public string[] ScheduleRunDays { get; set; }
60+
6261
public override void ExecuteCmdlet()
6362
{
6463
base.ExecuteCmdlet();
6564

6665
ExecutionBlock(() =>
6766
{
68-
WriteVerbose("Making client call");
67+
WriteDebug("Making client call");
6968

70-
var backupSchedule = GetBackupSchedule();
69+
var backupSchedule = GetBackupSchedule(BackupType, ScheduleType, ScheduleRunTimes,
70+
RetentionType, RetentionDuration, ScheduleRunDays);
7171

7272
var addProtectionPolicyRequest = new AddProtectionPolicyRequest();
7373
addProtectionPolicyRequest.PolicyName = this.Name;
7474
addProtectionPolicyRequest.Schedule = backupSchedule;
7575
addProtectionPolicyRequest.WorkloadType = this.WorkloadType;
7676

77-
var OperationId = AzureBackupClient.ProtectionPolicy.AddAsync(addProtectionPolicyRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
77+
var operationId = AzureBackupClient.ProtectionPolicy.AddAsync(addProtectionPolicyRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
7878

7979
WriteVerbose("Protection policy created successfully");
8080

8181
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
8282

83-
WriteVerbose("Received policy response");
83+
WriteDebug("Received policy response");
8484

8585
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
8686
policyObjects = policyListResponse.ProtectionPolicies.Where(x => x.Name.Equals(Name, System.StringComparison.InvariantCultureIgnoreCase));
87-
88-
WriteVerbose("Converting response");
87+
88+
WriteDebug("Converting response");
8989
WriteAzureBackupProtectionPolicy(policyObjects);
9090
});
91-
}
92-
93-
public void WriteAzureBackupProtectionPolicy(IEnumerable<ProtectionPolicyInfo> sourcePolicyList)
94-
{
95-
List<AzureBackupProtectionPolicy> targetList = new List<AzureBackupProtectionPolicy>();
96-
97-
foreach (var sourcePolicy in sourcePolicyList)
98-
{
99-
targetList.Add(new AzureBackupProtectionPolicy(ResourceGroupName, ResourceName, sourcePolicy));
100-
}
101-
102-
this.WriteObject(targetList, true);
103-
}
104-
105-
private BackupSchedule GetBackupSchedule()
106-
{
107-
WriteVerbose("Entering GetBackupSchedule");
108-
109-
var backupSchedule = new BackupSchedule();
110-
111-
backupSchedule.BackupType = this.BackupType;
112-
backupSchedule.RetentionPolicy = GetRetentionPolicy();
113-
//Enum.Parse(ScheduleRunType, this.ScheduleType),
114-
backupSchedule.ScheduleRun = this.ScheduleType;
115-
if (this.ScheduleType == "Weekly")
116-
{
117-
backupSchedule.ScheduleRunDays = GetScheduleRunDays();
118-
}
119-
backupSchedule.ScheduleRunTimes = new List<DateTime> {this.ScheduleRunTimes};
120-
backupSchedule.ScheduleStartTime = this.ScheduleRunTimes;
121-
122-
WriteVerbose("Exiting GetBackupSchedule");
123-
return backupSchedule;
124-
}
125-
126-
private RetentionPolicy GetRetentionPolicy()
127-
{
128-
WriteVerbose("Entering RetentionPolicy");
129-
var retentionPolicy = new RetentionPolicy
130-
{
131-
RetentionType = (RetentionDurationType)Enum.Parse(typeof(RetentionDurationType), this.RetentionType),
132-
RetentionDuration = this.RetentionDuration
133-
};
134-
135-
return retentionPolicy;
136-
}
137-
138-
private IList<DayOfWeek> GetScheduleRunDays()
139-
{
140-
IList<DayOfWeek> ListofWeekDays = new List<DayOfWeek>();
141-
142-
foreach(var dayOfWeek in this.ScheduleRunDays)
143-
{
144-
WriteVerbose("dayOfWeek" + dayOfWeek.ToString());
145-
DayOfWeek item = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), dayOfWeek, true);
146-
WriteVerbose("Item" + item.ToString());
147-
if(!ListofWeekDays.Contains(item))
148-
{
149-
ListofWeekDays.Add(item);
150-
}
151-
152-
else
153-
{
154-
throw new ArgumentException(string.Format("Repeated Days in ScheduleRunDays"));
155-
}
156-
}
157-
158-
return ListofWeekDays;
159-
}
160-
91+
}
16192
}
16293
}
16394

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2525
/// Remove a protection policy
2626
/// </summary>
2727
[Cmdlet(VerbsCommon.Remove, "AzureBackupProtectionPolicy")]
28-
public class RemoveAzureBackupProtectionPolicy : AzureBackupVaultCmdletBase
28+
public class RemoveAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
2929
{
30-
[Parameter(Position = 0, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
30+
[Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
3131
[ValidateNotNullOrEmpty]
3232
public string Name { get; set; }
3333

@@ -37,11 +37,11 @@ public override void ExecuteCmdlet()
3737

3838
ExecutionBlock(() =>
3939
{
40-
WriteVerbose("Making client call");
40+
WriteDebug("Making client call");
4141

4242
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
4343

44-
WriteVerbose("Received policy response");
44+
WriteDebug("Received policy response");
4545
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
4646

4747
policyObjects = policyListResponse.ProtectionPolicies.Objects.Where(x => x.Name.Equals(Name, System.StringComparison.InvariantCultureIgnoreCase));
@@ -56,7 +56,7 @@ public override void ExecuteCmdlet()
5656
WriteVerbose("Policy Not Found");
5757
}
5858

59-
WriteVerbose("Converting response");
59+
WriteDebug("Converting response");
6060
WriteVerbose("Successfully deleted policy");
6161
});
6262
}

0 commit comments

Comments
 (0)