-
Notifications
You must be signed in to change notification settings - Fork 2
Adding ProtectionPolicyCommandlets #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
|---|---|---|
| @@ -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")] | ||
| 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)] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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 ...
There was a problem hiding this comment.
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?