Skip to content

Commit

Permalink
Merge pull request #45 from MabOneSdk/pragrawa
Browse files Browse the repository at this point in the history
ProtectionPolicy changes
  • Loading branch information
pragrawa committed Jun 17, 2015
2 parents ef35853 + fa9ece2 commit fb29a17
Show file tree
Hide file tree
Showing 11 changed files with 386 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ public abstract class AzureBackupCmdletBase : AzurePSCmdlet
/// Cancellation Token Source
/// </summary>
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
protected CancellationToken CmdletCancellationToken;
internal CancellationToken CmdletCancellationToken;

protected AzureBackupCmdletHelper azureBackupCmdletHelper;

/// <summary>
/// Get Azure backup client.
/// </summary>
protected BackupServicesManagementClient AzureBackupClient
internal BackupServicesManagementClient AzureBackupClient
{
get
{
Expand Down Expand Up @@ -97,6 +99,7 @@ public void InitializeAzureBackupCmdlet(string rgName, string rName, string loca
WriteDebug(string.Format("Initialized AzureBackup Cmdlet, ClientRequestId: {0}, ResourceGroupName: {1}, ResourceName : {2}", this.clientRequestId, resourceGroupName, resourceName));

CmdletCancellationToken = cancellationTokenSource.Token;
azureBackupCmdletHelper = new AzureBackupCmdletHelper(this);
}

protected void ExecutionBlock(Action execAction)
Expand Down Expand Up @@ -169,7 +172,7 @@ private void HandleException(Exception exception)
}
}

protected CustomRequestHeaders GetCustomRequestHeaders()
internal CustomRequestHeaders GetCustomRequestHeaders()
{
var hdrs = new CustomRequestHeaders()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ internal static class AzureBackupCmdletHelpMessage
public const string Comments = "Comments for for removing protection";
public const string WorkloadType = "Workload type for which the policy is defined.";
public const string BackupType = "Type of backup.";
public const string ScheduleType = "Type of schedule.";
public const string DailyScheduleType = "Switch parameter for daily backup schedule.";
public const string WeeklyScheduleType = "Switch parameter for weekly backup schedule.";
public const string ScheduleRunDays = "Days of week for running backup, required for weekly schedule.";
public const string ScheduleRunTimes = "Times of day for running backup.";
public const string RetentionType = "Unit of retention for the recovery point.";
public const string RententionDuration = "Duration of retention for the recovery point in units specified by RetentionType.";
public const string PolicyInstanceId = "ProtectionPolicy InstanceId";
public const string PolicyNewName = "Policy new name";
public const string AzureBackupPolicy = "Azure Backup protection policy";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,96 +27,23 @@

namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
{
public abstract class AzureBackupPolicyCmdletBase : AzureBackupVaultCmdletBase
public abstract class AzureBackupPolicyCmdletBase : AzureBackupCmdletBase
{
// ToDO:
// Correct Help message and other attributes related to paameters
[Parameter(Position = 0, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.AzureBackupPolicy, ValueFromPipeline = true)]
[ValidateNotNullOrEmpty]
public AzureBackupProtectionPolicy ProtectionPolicy { get; set; }

public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();

WriteDebug(String.Format("Cmdlet called for ResourceGroupName: {0}, ResourceName: {1}, Location: {2}", ResourceGroupName, ResourceName, Location));
}

public void WriteAzureBackupProtectionPolicy(ProtectionPolicyInfo sourcePolicy)
{
this.WriteObject(new AzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, sourcePolicy));
}

public void WriteAzureBackupProtectionPolicy(IEnumerable<ProtectionPolicyInfo> sourcePolicyList)
{
List<AzureBackupProtectionPolicy> targetList = new List<AzureBackupProtectionPolicy>();

foreach (var sourcePolicy in sourcePolicyList)
{
targetList.Add(new AzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, sourcePolicy));
}

this.WriteObject(targetList, true);
}

public BackupSchedule GetBackupSchedule(string backupType, string scheduleType, DateTime scheduleStartTime,
string retentionType, int retentionDuration, string[] scheduleRunDays = null)
{
var backupSchedule = new BackupSchedule();

backupSchedule.BackupType = backupType;
backupSchedule.RetentionPolicy = GetRetentionPolicy(retentionType, retentionDuration);
//Enum.Parse(ScheduleRunType, this.ScheduleType),
backupSchedule.ScheduleRun = scheduleType;
if (string.Compare(scheduleType, "Weekly", true) == 0)
{
backupSchedule.ScheduleRunDays = GetScheduleRunDays(scheduleRunDays);
}

DateTime scheduleRunTime = GetScheduleRunTime(scheduleStartTime);
WriteDebug(String.Format("Cmdlet called for ResourceGroupName: {0}, ResourceName: {1}, Location: {2}",
ProtectionPolicy.ResourceGroupName, ProtectionPolicy.ResourceName, ProtectionPolicy.Location));

backupSchedule.ScheduleRunTimes = new List<DateTime> { scheduleRunTime };

WriteDebug("Exiting GetBackupSchedule");
return backupSchedule;
}

private RetentionPolicy GetRetentionPolicy(string retentionType, int retentionDuration)
{
var retentionPolicy = new RetentionPolicy
{
RetentionType = (RetentionDurationType)Enum.Parse(typeof(RetentionDurationType), retentionType, true),
RetentionDuration = retentionDuration
};

return retentionPolicy;
}

private IList<DayOfWeek> GetScheduleRunDays(string[] scheduleRunDays)
{
if (scheduleRunDays == null || scheduleRunDays.Length <= 0)
{
var exception = new Exception("For weekly scheduletype , ScheduleRunDays param is required.");
var errorRecord = new ErrorRecord(exception, string.Empty, ErrorCategory.InvalidData, null);
WriteError(errorRecord);
}

IList<DayOfWeek> ListofWeekDays = new List<DayOfWeek>();

foreach (var dayOfWeek in scheduleRunDays)
{
WriteDebug("dayOfWeek" + dayOfWeek.ToString());
DayOfWeek item = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), dayOfWeek, true);
WriteDebug("Item" + item.ToString());
if (!ListofWeekDays.Contains(item))
{
ListofWeekDays.Add(item);
}
}

return ListofWeekDays;
}

private DateTime GetScheduleRunTime(DateTime scheduleStartTime)
{
scheduleStartTime = scheduleStartTime.ToUniversalTime();
DateTime scheduleRunTime = new DateTime(scheduleStartTime.Year, scheduleStartTime.Month,
scheduleStartTime.Day, scheduleStartTime.Hour, scheduleStartTime.Minute - (scheduleStartTime.Minute % 30), 0);
return scheduleRunTime;
InitializeAzureBackupCmdlet(ProtectionPolicy.ResourceGroupName, ProtectionPolicy.ResourceName, ProtectionPolicy.Location);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
/// Get list of protection policies
/// </summary>
[Cmdlet(VerbsCommon.Get, "AzureBackupProtectionPolicy"), OutputType(typeof(AzureBackupProtectionPolicy), typeof(List<AzureBackupProtectionPolicy>))]
public class GetAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
public class GetAzureBackupProtectionPolicy : AzureBackupVaultCmdletBase
{
[Parameter(Position = 3, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
[ValidateNotNullOrEmpty]
Expand All @@ -39,21 +39,23 @@ public override void ExecuteCmdlet()
{
WriteDebug("Making client call");

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

WriteDebug("Received policy response");
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
if (Name != null)
{
policyObjects = policyListResponse.ProtectionPolicies.Objects.Where(x => x.Name.Equals(Name, System.StringComparison.InvariantCultureIgnoreCase));
AzureBackupProtectionPolicy policyInfo = azureBackupCmdletHelper.GetAzureBackupProtectionPolicyByName(Name, ResourceGroupName, ResourceName, Location);
WriteDebug("Converting response");
WriteObject(policyInfo);
}
else
{
var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;

WriteDebug("Received policy response");
policyObjects = policyListResponse.ProtectionPolicies.Objects;
}

WriteDebug("Converting response");
WriteAzureBackupProtectionPolicy(policyObjects);
WriteDebug("Converting response");
azureBackupCmdletHelper.WriteAzureBackupProtectionPolicy(ResourceGroupName, ResourceName, Location, policyObjects);
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,44 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
/// <summary>
/// Create new protection policy
/// </summary>
[Cmdlet(VerbsCommon.Add, "AzureBackupProtectionPolicy"), OutputType(typeof(AzureBackupProtectionPolicy))]
public class NewAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
{
[Cmdlet(VerbsCommon.New, "AzureBackupProtectionPolicy", DefaultParameterSetName = NoScheduleParamSet), OutputType(typeof(AzureBackupProtectionPolicy))]
public class NewAzureBackupProtectionPolicy : AzureBackupVaultCmdletBase
{
protected const string WeeklyScheduleParamSet = "WeeklyScheduleParamSet";
protected const string DailyScheduleParamSet = "DailyScheduleParamSet";
protected const string NoScheduleParamSet = "DailyScheduleParamSet";

[Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

[Parameter(Position = 4, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.WorkloadType, ValueFromPipelineByPropertyName = true)]
[ValidateSet("VM")]
[ValidateSet("VM", IgnoreCase = true)]
public string WorkloadType { get; set; }

[Parameter(Position = 5, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.BackupType, ValueFromPipelineByPropertyName = true)]
[ValidateSet("Full")]
[ValidateSet("Full", IgnoreCase = true)]
public string BackupType { get; set; }

[Parameter(Position = 6, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleType, ValueFromPipelineByPropertyName = true)]
[ValidateSet("Daily", "Weekly")]
public string ScheduleType { get; set; }
[Parameter(ParameterSetName = DailyScheduleParamSet, Position = 6, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.DailyScheduleType)]
public SwitchParameter Daily { get; set; }

[Parameter(ParameterSetName = WeeklyScheduleParamSet, Position = 7, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.WeeklyScheduleType)]
public SwitchParameter Weekly { get; set; }

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

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

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

[Parameter(Position = 10, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunDays, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = WeeklyScheduleParamSet, Position = 11, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunDays, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = NoScheduleParamSet, Position = 11, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunDays, ValueFromPipelineByPropertyName = true)]
[AllowEmptyCollection]
[ValidateSet("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", IgnoreCase = true)]
public string[] ScheduleRunDays { get; set; }

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

var backupSchedule = GetBackupSchedule(BackupType, ScheduleType, ScheduleRunTimes,
RetentionType, RetentionDuration, ScheduleRunDays);
var ScheduleType = GetScheduelType(ScheduleRunDays);

var backupSchedule = azureBackupCmdletHelper.FillBackupSchedule(BackupType, ScheduleType, ScheduleRunTimes,
RetentionType, RetentionDuration, ScheduleRunDays);

var addProtectionPolicyRequest = new AddProtectionPolicyRequest();
addProtectionPolicyRequest.PolicyName = this.Name;
addProtectionPolicyRequest.Schedule = backupSchedule;
addProtectionPolicyRequest.WorkloadType = this.WorkloadType;
addProtectionPolicyRequest.WorkloadType = Enum.Parse(typeof(WorkloadType), this.WorkloadType, true).ToString();

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

WriteVerbose("Protection policy created successfully");

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

WriteDebug("Received policy response");

IEnumerable<ProtectionPolicyInfo> policyObjects = null;
policyObjects = policyListResponse.ProtectionPolicies.Where(x => x.Name.Equals(Name, System.StringComparison.InvariantCultureIgnoreCase));

AzureBackupProtectionPolicy policyInfo = azureBackupCmdletHelper.GetAzureBackupProtectionPolicyByName(Name, ResourceGroupName, ResourceName, Location);
WriteDebug("Converting response");
WriteAzureBackupProtectionPolicy(policyObjects);
WriteObject(policyInfo);
});
}
}

private string GetScheduelType(string[] ScheduleRunDays)
{
WriteDebug("ParameterSetName = " + this.ParameterSetName.ToString());

if (ScheduleRunDays != null && ScheduleRunDays.Length > 0)
{
return ScheduleType.Weekly.ToString();
}
else
{
return ScheduleType.Daily.ToString();
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
[Cmdlet(VerbsCommon.Remove, "AzureBackupProtectionPolicy")]
public class RemoveAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
{
[Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();
Expand All @@ -39,25 +35,20 @@ public override void ExecuteCmdlet()
{
WriteDebug("Making client call");

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

WriteDebug("Received policy response");
IEnumerable<ProtectionPolicyInfo> policyObjects = null;

policyObjects = policyListResponse.ProtectionPolicies.Objects.Where(x => x.Name.Equals(Name, System.StringComparison.InvariantCultureIgnoreCase));

if (policyObjects.Count<ProtectionPolicyInfo>() != 0)
AzureBackupProtectionPolicy policyInfo = azureBackupCmdletHelper.GetAzureBackupProtectionPolicyByName(ProtectionPolicy.Name,
ProtectionPolicy.ResourceGroupName, ProtectionPolicy.ResourceName, ProtectionPolicy.Location);

if (policyInfo != null)
{
ProtectionPolicyInfo protectionPolicyInfo = policyObjects.ElementAt<ProtectionPolicyInfo>(0);
var policyRemoveResponse = AzureBackupClient.ProtectionPolicy.DeleteAsync(protectionPolicyInfo.InstanceId, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
var policyRemoveResponse = AzureBackupClient.ProtectionPolicy.DeleteAsync(policyInfo.InstanceId, GetCustomRequestHeaders(), CmdletCancellationToken).Result;

WriteDebug("Converting response");
WriteVerbose("Successfully deleted policy");
}
else
{
WriteVerbose("Policy Not Found");
}

WriteDebug("Converting response");
WriteVerbose("Successfully deleted policy");
});
}
}
Expand Down
Loading

0 comments on commit fb29a17

Please sign in to comment.