Skip to content

Commit 1b71ddb

Browse files
committed
ProtectionPolicyChanges2
1 parent 862bd35 commit 1b71ddb

File tree

6 files changed

+143
-134
lines changed

6 files changed

+143
-134
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public void InitializeAzureBackupCmdlet(string rgName, string rName, string loca
9797
WriteDebug(string.Format("Initialized AzureBackup Cmdlet, ClientRequestId: {0}, ResourceGroupName: {1}, ResourceName : {2}", this.clientRequestId, resourceGroupName, resourceName));
9898

9999
CmdletCancellationToken = cancellationTokenSource.Token;
100+
AzureBackupCmdletHelper cmdHelper = new AzureBackupCmdletHelper(this);
100101
}
101102

102103
protected void ExecutionBlock(Action execAction)

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

Lines changed: 10 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -27,96 +27,22 @@
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}", ProtectionPolicy.ResourceGroupName, ProtectionPolicy.ResourceName, ProtectionPolicy.Location));
7143

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;
44+
InitializeAzureBackupCmdlet(ProtectionPolicy.ResourceGroupName, ProtectionPolicy.ResourceName, ProtectionPolicy.Location);
12045
}
46+
12147
}
12248
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override void ExecuteCmdlet()
5353
}
5454

5555
WriteDebug("Converting response");
56-
ProtectionPolicyHelper.WriteAzureBackupProtectionPolicy(this, ResourceGroupName, ResourceName, Location, policyObjects);
56+
AzureBackupCmdletHelper.WriteAzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, policyObjects);
5757

5858
});
5959
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2424
/// <summary>
2525
/// Create new protection policy
2626
/// </summary>
27-
[Cmdlet(VerbsCommon.New, "AzureBackupProtectionPolicy", DefaultParameterSetName = DailyScheduleParamSet), OutputType(typeof(AzureBackupProtectionPolicy))]
27+
[Cmdlet(VerbsCommon.New, "AzureBackupProtectionPolicy", DefaultParameterSetName = NoScheduleParamSet), OutputType(typeof(AzureBackupProtectionPolicy))]
2828
public class NewAzureBackupProtectionPolicy : AzureBackupVaultCmdletBase
2929
{
3030
protected const string WeeklyScheduleParamSet = "WeeklyScheduleParamSet";
3131
protected const string DailyScheduleParamSet = "DailyScheduleParamSet";
32+
protected const string NoScheduleParamSet = "DailyScheduleParamSet";
3233

3334
[Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
3435
[ValidateNotNullOrEmpty]
@@ -59,6 +60,7 @@ public class NewAzureBackupProtectionPolicy : AzureBackupVaultCmdletBase
5960
public int RetentionDuration { get; set; }
6061

6162
[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)]
6264
[ValidateSet("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", IgnoreCase = true)]
6365
public string[] ScheduleRunDays { get; set; }
6466

@@ -72,7 +74,7 @@ public override void ExecuteCmdlet()
7274

7375
var ScheduleType = GetScheduelType(ScheduleRunDays);
7476

75-
var backupSchedule = ProtectionPolicyHelper.GetBackupSchedule(this,BackupType, ScheduleType, ScheduleRunTimes,
77+
var backupSchedule = AzureBackupCmdletHelper.FillBackupSchedule(BackupType, ScheduleType, ScheduleRunTimes,
7678
RetentionType, RetentionDuration, ScheduleRunDays);
7779

7880

@@ -96,21 +98,22 @@ public override void ExecuteCmdlet()
9698
policyObjects = policyListResponse.ProtectionPolicies.Where(x => x.Name.Equals(Name, System.StringComparison.InvariantCultureIgnoreCase));
9799

98100
WriteDebug("Converting response");
99-
ProtectionPolicyHelper.WriteAzureBackupProtectionPolicy(this, ResourceGroupName, ResourceName, Location, policyObjects);
101+
AzureBackupCmdletHelper.WriteAzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, policyObjects);
100102
});
101103
}
102104

103105
private string GetScheduelType(string[] ScheduleRunDays)
104106
{
105107
WriteDebug("ParameterSetName = " + this.ParameterSetName.ToString());
106-
if (this.ParameterSetName == WeeklyScheduleParamSet || (ScheduleRunDays != null && ScheduleRunDays.Length > 0))
108+
109+
if (ScheduleRunDays != null && ScheduleRunDays.Length > 0)
107110
{
108111
return ScheduleType.Weekly.ToString();
109112
}
110-
else
113+
else
111114
{
112115
return ScheduleType.Daily.ToString();
113-
}
116+
}
114117
}
115118
}
116119
}

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

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,57 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2424
/// <summary>
2525
/// Update existing protection policy
2626
/// </summary>
27-
[Cmdlet(VerbsCommon.Set, "AzureBackupProtectionPolicy"), OutputType(typeof(AzureBackupProtectionPolicy))]
27+
[Cmdlet(VerbsCommon.Set, "AzureBackupProtectionPolicy", DefaultParameterSetName = NoScheduleParamSet), OutputType(typeof(AzureBackupProtectionPolicy))]
2828
public class SetAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
2929
{
30-
30+
protected const string WeeklyScheduleParamSet = "WeeklyScheduleParamSet";
31+
protected const string DailyScheduleParamSet = "DailyScheduleParamSet";
32+
protected const string NoScheduleParamSet = "NoScheduleParamSet";
33+
3134
[Parameter(Position = 3, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.PolicyNewName, ValueFromPipelineByPropertyName = true)]
3235
[ValidateNotNullOrEmpty]
3336
public string NewName { get; set; }
3437

38+
[Parameter(Position = 5, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.BackupType, ValueFromPipelineByPropertyName = true)]
39+
[ValidateSet("Full")]
40+
public string BackupType { get; set; }
41+
42+
[Parameter(ParameterSetName = DailyScheduleParamSet, Position = 7, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleType)]
43+
public SwitchParameter Daily { get; set; }
44+
45+
[Parameter(ParameterSetName = WeeklyScheduleParamSet, Position = 6, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleType)]
46+
public SwitchParameter Weekly { get; set; }
47+
48+
[Parameter(Position = 7, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunTimes, ValueFromPipelineByPropertyName = true)]
49+
public DateTime ScheduleRunTimes { get; set; }
50+
51+
[Parameter(Position = 8, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.RetentionType, ValueFromPipelineByPropertyName = true)]
52+
[ValidateSet("Days", IgnoreCase = true)]
53+
public string RetentionType { get; set; }
54+
55+
[Parameter(Position = 9, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.RententionDuration, ValueFromPipelineByPropertyName = true)]
56+
public int RetentionDuration { get; set; }
57+
58+
[Parameter(ParameterSetName = WeeklyScheduleParamSet, Position = 10, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunDays, ValueFromPipelineByPropertyName = true)]
59+
[ValidateSet("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", IgnoreCase = true)]
60+
public string[] ScheduleRunDays { get; set; }
61+
3562
public override void ExecuteCmdlet()
3663
{
3764
base.ExecuteCmdlet();
3865

3966
ExecutionBlock(() =>
4067
{
4168
WriteDebug("Making client call");
69+
4270
AzureBackupProtectionPolicy policy = ProtectionPolicy;
43-
ProtectionPolicyHelper.ValidateAzureBackupPolicyRequest(this, policy);
4471

45-
var backupSchedule = ProtectionPolicyHelper.GetBackupSchedule(this, policy.BackupType, policy.ScheduleType, policy.ScheduleRunTimes,
46-
policy.RetentionType, policy.RetentionDuration, policy.ScheduleRunDays.ToArray<string>());
72+
FillRemainingValuesForSetPolicyRequest(policy);
73+
74+
AzureBackupCmdletHelper.ValidateAzureBackupPolicyRequest(policy);
75+
76+
var backupSchedule = AzureBackupCmdletHelper.FillBackupSchedule(BackupType, policy.ScheduleType, ScheduleRunTimes,
77+
RetentionType, RetentionDuration, policy.ScheduleRunDays.ToArray<string>());
4778

4879

4980
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
@@ -81,10 +112,52 @@ public override void ExecuteCmdlet()
81112
policyObjects = policyListResponse_afterUpdate.ProtectionPolicies.Where(x => x.Name.Equals(NewName, System.StringComparison.InvariantCultureIgnoreCase));
82113

83114
WriteDebug("Converting response");
84-
ProtectionPolicyHelper.WriteAzureBackupProtectionPolicy(this, policy.ResourceGroupName, policy.ResourceName, policy.Location, policyObjects);
115+
AzureBackupCmdletHelper.WriteAzureBackupProtectionPolicy(policy.ResourceGroupName, policy.ResourceName, policy.Location, policyObjects);
85116

86117
});
87118
}
119+
120+
private void FillRemainingValuesForSetPolicyRequest(AzureBackupProtectionPolicy policy)
121+
{
122+
if(string.IsNullOrEmpty(BackupType))
123+
{
124+
BackupType = policy.BackupType;
125+
}
126+
127+
if (ScheduleRunTimes == null)
128+
{
129+
ScheduleRunTimes = policy.ScheduleRunTimes;
130+
}
131+
132+
if (string.IsNullOrEmpty(RetentionType))
133+
{
134+
RetentionType = policy.RetentionType;
135+
}
136+
137+
if (RetentionDuration == 0)
138+
{
139+
RetentionDuration = policy.RetentionDuration;
140+
}
141+
142+
if (string.IsNullOrEmpty(BackupType))
143+
{
144+
BackupType = policy.BackupType;
145+
}
146+
147+
if (this.ParameterSetName != NoScheduleParamSet )
148+
{
149+
if (ScheduleRunDays != null && ScheduleRunDays.Length > 0)
150+
{
151+
policy.ScheduleType = ScheduleType.Weekly.ToString();
152+
policy.ScheduleRunDays = ScheduleRunDays.ToList<string>();
153+
}
154+
else
155+
{
156+
policy.ScheduleType = ScheduleType.Daily.ToString();
157+
}
158+
159+
}
160+
}
88161
}
89162
}
90163

0 commit comments

Comments
 (0)