Skip to content

Commit 3c86ee5

Browse files
committed
Merge pull request #24 from MabOneSdk/pragrawa
Adding ProtectionPolicyCommandlets
2 parents 5094f2c + 22fcb9f commit 3c86ee5

File tree

8 files changed

+433
-26
lines changed

8 files changed

+433
-26
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,13 @@ internal static class AzureBackupCmdletHelpMessage
3939
public const string RemoveProtectionOption = "Remove Protection Option";
4040
public const string Reason = "Reson for removing protection";
4141
public const string Comments = "Comments for for removing protection";
42+
public const string WorkloadType = "Workload type for which the policy is defined.";
43+
public const string BackupType = "Type of backup.";
44+
public const string ScheduleType = "Type of schedule.";
45+
public const string ScheduleRunDays = "Days of week for running backup, required for weekly schedule.";
46+
public const string ScheduleRunTimes = "Times of day for running backup.";
47+
public const string RetentionType = "Unit of retention for the recovery point.";
48+
public const string RententionDuration = "Duration of retention for the recovery point in units specified by RetentionType.";
49+
public const string PolicyInstanceId = "ProtectionPolicy InstanceId";
4250
}
4351
}
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 & 22 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,12 +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");
45-
WriteVerbose("Received policy response2");
44+
WriteDebug("Received policy response");
4645
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
4746
if (Name != null)
4847
{
@@ -53,27 +52,10 @@ public override void ExecuteCmdlet()
5352
policyObjects = policyListResponse.ProtectionPolicies.Objects;
5453
}
5554

56-
WriteVerbose("Converting response");
55+
WriteDebug("Converting response");
5756
WriteAzureBackupProtectionPolicy(policyObjects);
5857
});
5958
}
60-
61-
public void WriteAzureBackupProtectionPolicy(ProtectionPolicyInfo sourcePolicy)
62-
{
63-
this.WriteObject(new AzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, sourcePolicy));
64-
}
65-
66-
public void WriteAzureBackupProtectionPolicy(IEnumerable<ProtectionPolicyInfo> sourcePolicyList)
67-
{
68-
List<AzureBackupProtectionPolicy> targetList = new List<AzureBackupProtectionPolicy>();
69-
70-
foreach (var sourcePolicy in sourcePolicyList)
71-
{
72-
targetList.Add(new AzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, sourcePolicy));
73-
}
74-
75-
this.WriteObject(targetList, true);
76-
}
7759
}
7860
}
7961

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 System.Linq;
20+
using Microsoft.Azure.Management.BackupServices.Models;
21+
22+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
23+
{
24+
/// <summary>
25+
/// Create new protection policy
26+
/// </summary>
27+
[Cmdlet(VerbsCommon.Add, "AzureBackupProtectionPolicy"), OutputType(typeof(AzureBackupProtectionPolicy))]
28+
public class NewAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
29+
{
30+
[Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
31+
[ValidateNotNullOrEmpty]
32+
public string Name { get; set; }
33+
34+
[Parameter(Position = 4, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.WorkloadType, ValueFromPipelineByPropertyName = true)]
35+
[ValidateSet("VM")]
36+
public string WorkloadType { get; set; }
37+
38+
[Parameter(Position = 5, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.BackupType, ValueFromPipelineByPropertyName = true)]
39+
[ValidateSet("Full")]
40+
public string BackupType { get; set; }
41+
42+
[Parameter(Position = 6, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleType, ValueFromPipelineByPropertyName = true)]
43+
[ValidateSet("Daily", "Weekly")]
44+
public string ScheduleType { get; set; }
45+
46+
[Parameter(Position = 7, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunTimes, ValueFromPipelineByPropertyName = true)]
47+
public DateTime ScheduleRunTimes { get; set; }
48+
49+
[Parameter(Position = 8, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RetentionType, ValueFromPipelineByPropertyName = true)]
50+
[ValidateSet("Days", IgnoreCase = true)]
51+
public string RetentionType { get; set; }
52+
53+
[Parameter(Position = 9, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RententionDuration, ValueFromPipelineByPropertyName = true)]
54+
[ValidateRange(1,30)]
55+
public int RetentionDuration { get; set; }
56+
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+
61+
public override void ExecuteCmdlet()
62+
{
63+
base.ExecuteCmdlet();
64+
65+
ExecutionBlock(() =>
66+
{
67+
WriteDebug("Making client call");
68+
69+
var backupSchedule = GetBackupSchedule(BackupType, ScheduleType, ScheduleRunTimes,
70+
RetentionType, RetentionDuration, ScheduleRunDays);
71+
72+
var addProtectionPolicyRequest = new AddProtectionPolicyRequest();
73+
addProtectionPolicyRequest.PolicyName = this.Name;
74+
addProtectionPolicyRequest.Schedule = backupSchedule;
75+
addProtectionPolicyRequest.WorkloadType = this.WorkloadType;
76+
77+
var operationId = AzureBackupClient.ProtectionPolicy.AddAsync(addProtectionPolicyRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
78+
79+
WriteVerbose("Protection policy created successfully");
80+
81+
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
82+
83+
WriteDebug("Received policy response");
84+
85+
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
86+
policyObjects = policyListResponse.ProtectionPolicies.Where(x => x.Name.Equals(Name, System.StringComparison.InvariantCultureIgnoreCase));
87+
88+
WriteDebug("Converting response");
89+
WriteAzureBackupProtectionPolicy(policyObjects);
90+
});
91+
}
92+
}
93+
}
94+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 System.Linq;
20+
using Microsoft.Azure.Management.BackupServices.Models;
21+
22+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
23+
{
24+
/// <summary>
25+
/// Remove a protection policy
26+
/// </summary>
27+
[Cmdlet(VerbsCommon.Remove, "AzureBackupProtectionPolicy")]
28+
public class RemoveAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
29+
{
30+
[Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
31+
[ValidateNotNullOrEmpty]
32+
public string Name { get; set; }
33+
34+
public override void ExecuteCmdlet()
35+
{
36+
base.ExecuteCmdlet();
37+
38+
ExecutionBlock(() =>
39+
{
40+
WriteDebug("Making client call");
41+
42+
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
43+
44+
WriteDebug("Received policy response");
45+
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
46+
47+
policyObjects = policyListResponse.ProtectionPolicies.Objects.Where(x => x.Name.Equals(Name, System.StringComparison.InvariantCultureIgnoreCase));
48+
49+
if (policyObjects.Count<ProtectionPolicyInfo>() != 0)
50+
{
51+
ProtectionPolicyInfo protectionPolicyInfo = policyObjects.ElementAt<ProtectionPolicyInfo>(0);
52+
var policyRemoveResponse = AzureBackupClient.ProtectionPolicy.DeleteAsync(protectionPolicyInfo.InstanceId, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
53+
}
54+
else
55+
{
56+
WriteVerbose("Policy Not Found");
57+
}
58+
59+
WriteDebug("Converting response");
60+
WriteVerbose("Successfully deleted policy");
61+
});
62+
}
63+
}
64+
}
65+

0 commit comments

Comments
 (0)