Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,13 @@ internal static class AzureBackupCmdletHelpMessage
public const string RemoveProtectionOption = "Remove Protection Option";
public const string Reason = "Reson for removing protection";
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 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";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Management.Automation;
using System.Collections.Generic;
using System.Xml;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Common.Authentication.Models;
using System.Threading;
using Hyak.Common;
using Microsoft.Azure.Commands.AzureBackup.Properties;
using System.Net;
using Microsoft.Azure.Management.BackupServices.Models;

namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
{
public abstract class AzureBackupPolicyCmdletBase : AzureBackupVaultCmdletBase
{
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);

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;
}
}
}
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 : AzureBackupVaultCmdletBase
public class GetAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
{
[Parameter(Position = 3, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
[ValidateNotNullOrEmpty]
Expand All @@ -37,12 +37,11 @@ public override void ExecuteCmdlet()

ExecutionBlock(() =>
{
WriteVerbose("Making client call");
WriteDebug("Making client call");

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

WriteVerbose("Received policy response");
WriteVerbose("Received policy response2");
WriteDebug("Received policy response");
IEnumerable<ProtectionPolicyInfo> policyObjects = null;
if (Name != null)
{
Expand All @@ -53,27 +52,10 @@ public override void ExecuteCmdlet()
policyObjects = policyListResponse.ProtectionPolicies.Objects;
}

WriteVerbose("Converting response");
WriteDebug("Converting response");
WriteAzureBackupProtectionPolicy(policyObjects);
});
}

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);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Management.Automation;
using System.Collections.Generic;
using System.Xml;
using System.Linq;
using Microsoft.Azure.Management.BackupServices.Models;

namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
{
/// <summary>
/// Create new protection policy
/// </summary>
[Cmdlet(VerbsCommon.Add, "AzureBackupProtectionPolicy"), OutputType(typeof(AzureBackupProtectionPolicy))]
public class NewAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
{
[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")]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming it will take care of NullOrEmpty check as well ...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does user get to know somehow that what are valid values ("VM", etc) for this parameter?

public string WorkloadType { get; set; }

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

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

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

[Parameter(Position = 8, 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)]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He can't specify 90 days? Is this validation defined in PM spec ?

public int RetentionDuration { get; set; }

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

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

ExecutionBlock(() =>
{
WriteDebug("Making client call");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming you don't need any more validations, than what are already defined in parameter tags.?

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

var addProtectionPolicyRequest = new AddProtectionPolicyRequest();
addProtectionPolicyRequest.PolicyName = this.Name;
addProtectionPolicyRequest.Schedule = backupSchedule;
addProtectionPolicyRequest.WorkloadType = this.WorkloadType;

var operationId = AzureBackupClient.ProtectionPolicy.AddAsync(addProtectionPolicyRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result;

WriteVerbose("Protection policy created successfully");

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can make this code common - betn Get, Add, Update.


WriteDebug("Received policy response");

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

WriteDebug("Converting response");
WriteAzureBackupProtectionPolicy(policyObjects);
});
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Management.Automation;
using System.Collections.Generic;
using System.Xml;
using System.Linq;
using Microsoft.Azure.Management.BackupServices.Models;

namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
{
/// <summary>
/// Remove a protection policy
/// </summary>
[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();

ExecutionBlock(() =>
{
WriteDebug("Making client call");

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As suggested earlier, make a common helper to GetProtectionPolicyByName()


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)
{
ProtectionPolicyInfo protectionPolicyInfo = policyObjects.ElementAt<ProtectionPolicyInfo>(0);
var policyRemoveResponse = AzureBackupClient.ProtectionPolicy.DeleteAsync(protectionPolicyInfo.InstanceId, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
}
else
{
WriteVerbose("Policy Not Found");
}

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

Loading