Skip to content

Commit fb29a17

Browse files
committed
Merge pull request #45 from MabOneSdk/pragrawa
ProtectionPolicy changes
2 parents ef35853 + fa9ece2 commit fb29a17

11 files changed

+386
-185
lines changed

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

Lines changed: 6 additions & 3 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,6 +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;
102+
azureBackupCmdletHelper = new AzureBackupCmdletHelper(this);
100103
}
101104

102105
protected void ExecutionBlock(Action execAction)
@@ -169,7 +172,7 @@ private void HandleException(Exception exception)
169172
}
170173
}
171174

172-
protected CustomRequestHeaders GetCustomRequestHeaders()
175+
internal CustomRequestHeaders GetCustomRequestHeaders()
173176
{
174177
var hdrs = new CustomRequestHeaders()
175178
{

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ internal static class AzureBackupCmdletHelpMessage
6464
public const string Comments = "Comments for for removing protection";
6565
public const string WorkloadType = "Workload type for which the policy is defined.";
6666
public const string BackupType = "Type of backup.";
67-
public const string ScheduleType = "Type of schedule.";
67+
public const string DailyScheduleType = "Switch parameter for daily backup schedule.";
68+
public const string WeeklyScheduleType = "Switch parameter for weekly backup schedule.";
6869
public const string ScheduleRunDays = "Days of week for running backup, required for weekly schedule.";
6970
public const string ScheduleRunTimes = "Times of day for running backup.";
7071
public const string RetentionType = "Unit of retention for the recovery point.";
7172
public const string RententionDuration = "Duration of retention for the recovery point in units specified by RetentionType.";
72-
public const string PolicyInstanceId = "ProtectionPolicy InstanceId";
73+
public const string PolicyNewName = "Policy new name";
74+
public const string AzureBackupPolicy = "Azure Backup protection policy";
7375
}
7476
}

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

Lines changed: 11 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -27,96 +27,23 @@
2727

2828
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2929
{
30-
public abstract class AzureBackupPolicyCmdletBase : AzureBackupVaultCmdletBase
30+
public abstract class AzureBackupPolicyCmdletBase : AzureBackupCmdletBase
3131
{
32+
// ToDO:
33+
// Correct Help message and other attributes related to paameters
34+
[Parameter(Position = 0, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.AzureBackupPolicy, ValueFromPipeline = true)]
35+
[ValidateNotNullOrEmpty]
36+
public AzureBackupProtectionPolicy ProtectionPolicy { get; set; }
37+
3238
public override void ExecuteCmdlet()
3339
{
3440
base.ExecuteCmdlet();
3541

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);
42+
WriteDebug(String.Format("Cmdlet called for ResourceGroupName: {0}, ResourceName: {1}, Location: {2}",
43+
ProtectionPolicy.ResourceGroupName, ProtectionPolicy.ResourceName, ProtectionPolicy.Location));
7144

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;
45+
InitializeAzureBackupCmdlet(ProtectionPolicy.ResourceGroupName, ProtectionPolicy.ResourceName, ProtectionPolicy.Location);
12046
}
47+
12148
}
12249
}

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

Lines changed: 10 additions & 8 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 : AzureBackupPolicyCmdletBase
28+
public class GetAzureBackupProtectionPolicy : AzureBackupVaultCmdletBase
2929
{
3030
[Parameter(Position = 3, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
3131
[ValidateNotNullOrEmpty]
@@ -39,21 +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-
WriteAzureBackupProtectionPolicy(policyObjects);
56+
WriteDebug("Converting response");
57+
azureBackupCmdletHelper.WriteAzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, policyObjects);
58+
}
5759
});
5860
}
5961
}

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

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,44 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2424
/// <summary>
2525
/// Create new protection policy
2626
/// </summary>
27-
[Cmdlet(VerbsCommon.Add, "AzureBackupProtectionPolicy"), OutputType(typeof(AzureBackupProtectionPolicy))]
28-
public class NewAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
29-
{
27+
[Cmdlet(VerbsCommon.New, "AzureBackupProtectionPolicy", DefaultParameterSetName = NoScheduleParamSet), OutputType(typeof(AzureBackupProtectionPolicy))]
28+
public class NewAzureBackupProtectionPolicy : AzureBackupVaultCmdletBase
29+
{
30+
protected const string WeeklyScheduleParamSet = "WeeklyScheduleParamSet";
31+
protected const string DailyScheduleParamSet = "DailyScheduleParamSet";
32+
protected const string NoScheduleParamSet = "DailyScheduleParamSet";
33+
3034
[Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
3135
[ValidateNotNullOrEmpty]
3236
public string Name { get; set; }
3337

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

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

42-
[Parameter(Position = 6, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleType, ValueFromPipelineByPropertyName = true)]
43-
[ValidateSet("Daily", "Weekly")]
44-
public string ScheduleType { get; set; }
46+
[Parameter(ParameterSetName = DailyScheduleParamSet, Position = 6, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.DailyScheduleType)]
47+
public SwitchParameter Daily { get; set; }
48+
49+
[Parameter(ParameterSetName = WeeklyScheduleParamSet, Position = 7, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.WeeklyScheduleType)]
50+
public SwitchParameter Weekly { get; set; }
4551

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

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

53-
[Parameter(Position = 9, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RententionDuration, ValueFromPipelineByPropertyName = true)]
54-
[ValidateRange(1,30)]
59+
[Parameter(Position = 10, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RententionDuration, ValueFromPipelineByPropertyName = true)]
5560
public int RetentionDuration { get; set; }
5661

57-
[Parameter(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]
5865
[ValidateSet("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", IgnoreCase = true)]
5966
public string[] ScheduleRunDays { get; set; }
6067

@@ -66,29 +73,38 @@ public override void ExecuteCmdlet()
6673
{
6774
WriteDebug("Making client call");
6875

69-
var backupSchedule = GetBackupSchedule(BackupType, ScheduleType, ScheduleRunTimes,
70-
RetentionType, RetentionDuration, ScheduleRunDays);
76+
var ScheduleType = GetScheduelType(ScheduleRunDays);
77+
78+
var backupSchedule = azureBackupCmdletHelper.FillBackupSchedule(BackupType, ScheduleType, ScheduleRunTimes,
79+
RetentionType, RetentionDuration, ScheduleRunDays);
7180

7281
var addProtectionPolicyRequest = new AddProtectionPolicyRequest();
7382
addProtectionPolicyRequest.PolicyName = this.Name;
7483
addProtectionPolicyRequest.Schedule = backupSchedule;
75-
addProtectionPolicyRequest.WorkloadType = this.WorkloadType;
84+
addProtectionPolicyRequest.WorkloadType = Enum.Parse(typeof(WorkloadType), this.WorkloadType, true).ToString();
7685

7786
var operationId = AzureBackupClient.ProtectionPolicy.AddAsync(addProtectionPolicyRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
87+
WriteDebug("Protection policy created successfully");
7888

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-
89+
AzureBackupProtectionPolicy policyInfo = azureBackupCmdletHelper.GetAzureBackupProtectionPolicyByName(Name, ResourceGroupName, ResourceName, Location);
8890
WriteDebug("Converting response");
89-
WriteAzureBackupProtectionPolicy(policyObjects);
91+
WriteObject(policyInfo);
9092
});
91-
}
93+
}
94+
95+
private string GetScheduelType(string[] ScheduleRunDays)
96+
{
97+
WriteDebug("ParameterSetName = " + this.ParameterSetName.ToString());
98+
99+
if (ScheduleRunDays != null && ScheduleRunDays.Length > 0)
100+
{
101+
return ScheduleType.Weekly.ToString();
102+
}
103+
else
104+
{
105+
return ScheduleType.Daily.ToString();
106+
}
107+
}
92108
}
93109
}
94110

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2727
[Cmdlet(VerbsCommon.Remove, "AzureBackupProtectionPolicy")]
2828
public class RemoveAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
2929
{
30-
[Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
31-
[ValidateNotNullOrEmpty]
32-
public string Name { get; set; }
33-
3430
public override void ExecuteCmdlet()
3531
{
3632
base.ExecuteCmdlet();
@@ -39,25 +35,20 @@ public override void ExecuteCmdlet()
3935
{
4036
WriteDebug("Making client call");
4137

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)
38+
AzureBackupProtectionPolicy policyInfo = azureBackupCmdletHelper.GetAzureBackupProtectionPolicyByName(ProtectionPolicy.Name,
39+
ProtectionPolicy.ResourceGroupName, ProtectionPolicy.ResourceName, ProtectionPolicy.Location);
40+
41+
if (policyInfo != null)
5042
{
51-
ProtectionPolicyInfo protectionPolicyInfo = policyObjects.ElementAt<ProtectionPolicyInfo>(0);
52-
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");
5347
}
5448
else
5549
{
5650
WriteVerbose("Policy Not Found");
5751
}
58-
59-
WriteDebug("Converting response");
60-
WriteVerbose("Successfully deleted policy");
6152
});
6253
}
6354
}

0 commit comments

Comments
 (0)