Skip to content

Commit 533e4ba

Browse files
committed
Adding ProtectionPolicyCommandlets
1 parent d3cf619 commit 533e4ba

File tree

7 files changed

+447
-13
lines changed

7 files changed

+447
-13
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,13 @@ internal static class AzureBackupCmdletHelpMessage
2929
public const string ProtectionStatus = "Protection Status of the azure backup item.";
3030
public const string AzureBackUpItem = "Azure BackUp Item.";
3131
public const string RecoveryPointId = "Recovery Point Id.";
32+
public const string WorkloadType = "Workload type for which the policy is defined.";
33+
public const string BackupType = "Type of backup.";
34+
public const string ScheduleType = "Type of schedule.";
35+
public const string ScheduleRunDays = "Days of week for running backup, required for weekly schedule.";
36+
public const string ScheduleRunTimes = "Times of day for running backup.";
37+
public const string RetentionType = "Unit of retention for the recovery point.";
38+
public const string RententionDuration = "Duration of retention for the recovery point in units specified by RetentionType.";
39+
public const string PolicyInstanceId = "ProtectionPolicy InstanceId";
3240
}
3341
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public override void ExecuteCmdlet()
4242
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
4343

4444
WriteVerbose("Received policy response");
45-
WriteVerbose("Received policy response2");
4645
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
4746
if (Name != null)
4847
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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 : AzureBackupVaultCmdletBase
29+
{
30+
[Parameter(Position = 0, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName, ValueFromPipelineByPropertyName = true)]
31+
[ValidateNotNullOrEmpty]
32+
public string Name { get; set; }
33+
34+
[Parameter(Position = 1, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.WorkloadType, ValueFromPipelineByPropertyName = true)]
35+
[ValidateSet("VM")]
36+
public string WorkloadType { get; set; }
37+
38+
[Parameter(Position = 2, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.BackupType, ValueFromPipelineByPropertyName = true)]
39+
[ValidateSet("Full")]
40+
public string BackupType { get; set; }
41+
42+
[Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleType, ValueFromPipelineByPropertyName = true)]
43+
[ValidateSet("Daily","Weekly")]
44+
public string ScheduleType { get; set; }
45+
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]
52+
public DateTime ScheduleRunTimes { get; set; }
53+
54+
[Parameter(Position = 6, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RetentionType, ValueFromPipelineByPropertyName = true)]
55+
[ValidateSet("Days")]
56+
public string RetentionType { get; set; }
57+
58+
[Parameter(Position = 7, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RententionDuration, ValueFromPipelineByPropertyName = true)]
59+
[ValidateRange(1,30)]
60+
public int RetentionDuration { get; set; }
61+
62+
public override void ExecuteCmdlet()
63+
{
64+
base.ExecuteCmdlet();
65+
66+
ExecutionBlock(() =>
67+
{
68+
WriteVerbose("Making client call");
69+
70+
var backupSchedule = GetBackupSchedule();
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+
WriteVerbose("Received policy response");
84+
85+
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
86+
policyObjects = policyListResponse.ProtectionPolicies.Where(x => x.Name.Equals(Name, System.StringComparison.InvariantCultureIgnoreCase));
87+
88+
WriteVerbose("Converting response");
89+
WriteAzureBackupProtectionPolicy(policyObjects);
90+
});
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+
161+
}
162+
}
163+
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 : AzureBackupVaultCmdletBase
29+
{
30+
[Parameter(Position = 0, 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+
WriteVerbose("Making client call");
41+
42+
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
43+
44+
WriteVerbose("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+
WriteVerbose("Converting response");
60+
WriteVerbose("Successfully deleted policy");
61+
});
62+
}
63+
}
64+
}
65+

0 commit comments

Comments
 (0)