|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Management.Automation; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Xml; |
| 19 | +using System.Linq; |
| 20 | +using Microsoft.Azure.Management.BackupServices.Models; |
| 21 | + |
| 22 | +namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// Create new protection policy |
| 26 | + /// </summary> |
| 27 | + [Cmdlet(VerbsCommon.Add, "AzureBackupProtectionPolicy"), OutputType(typeof(AzureBackupProtectionPolicy))] |
| 28 | + public class NewAzureBackupProtectionPolicy : AzureBackupVaultCmdletBase |
| 29 | + { |
| 30 | + [Parameter(Position = 0, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName, ValueFromPipelineByPropertyName = true)] |
| 31 | + [ValidateNotNullOrEmpty] |
| 32 | + public string Name { get; set; } |
| 33 | + |
| 34 | + [Parameter(Position = 1, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.WorkloadType, ValueFromPipelineByPropertyName = true)] |
| 35 | + [ValidateSet("VM")] |
| 36 | + public string WorkloadType { get; set; } |
| 37 | + |
| 38 | + [Parameter(Position = 2, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.BackupType, ValueFromPipelineByPropertyName = true)] |
| 39 | + [ValidateSet("Full")] |
| 40 | + public string BackupType { get; set; } |
| 41 | + |
| 42 | + [Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleType, ValueFromPipelineByPropertyName = true)] |
| 43 | + [ValidateSet("Daily","Weekly")] |
| 44 | + public string ScheduleType { get; set; } |
| 45 | + |
| 46 | + [Parameter(Position = 4, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunDays, ValueFromPipelineByPropertyName = true)] |
| 47 | + [AllowEmptyCollection] |
| 48 | + public string[] ScheduleRunDays { get; set; } |
| 49 | + |
| 50 | + [Parameter(Position = 5, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ScheduleRunTimes, ValueFromPipelineByPropertyName = true)] |
| 51 | + [ValidateNotNullOrEmpty] |
| 52 | + public DateTime ScheduleRunTimes { get; set; } |
| 53 | + |
| 54 | + [Parameter(Position = 6, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RetentionType, ValueFromPipelineByPropertyName = true)] |
| 55 | + [ValidateSet("Days")] |
| 56 | + public string RetentionType { get; set; } |
| 57 | + |
| 58 | + [Parameter(Position = 7, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.RententionDuration, ValueFromPipelineByPropertyName = true)] |
| 59 | + [ValidateRange(1,30)] |
| 60 | + public int RetentionDuration { get; set; } |
| 61 | + |
| 62 | + public override void ExecuteCmdlet() |
| 63 | + { |
| 64 | + base.ExecuteCmdlet(); |
| 65 | + |
| 66 | + ExecutionBlock(() => |
| 67 | + { |
| 68 | + WriteVerbose("Making client call"); |
| 69 | + |
| 70 | + var backupSchedule = GetBackupSchedule(); |
| 71 | + |
| 72 | + var addProtectionPolicyRequest = new AddProtectionPolicyRequest(); |
| 73 | + addProtectionPolicyRequest.PolicyName = this.Name; |
| 74 | + addProtectionPolicyRequest.Schedule = backupSchedule; |
| 75 | + addProtectionPolicyRequest.WorkloadType = this.WorkloadType; |
| 76 | + |
| 77 | + var OperationId = AzureBackupClient.ProtectionPolicy.AddAsync(addProtectionPolicyRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result; |
| 78 | + |
| 79 | + WriteVerbose("Protection policy created successfully"); |
| 80 | + |
| 81 | + var policyListResponse = AzureBackupClient.ProtectionPolicy.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result; |
| 82 | + |
| 83 | + WriteVerbose("Received policy response"); |
| 84 | + |
| 85 | + IEnumerable<ProtectionPolicyInfo> policyObjects = null; |
| 86 | + policyObjects = policyListResponse.ProtectionPolicies.Where(x => x.Name.Equals(Name, System.StringComparison.InvariantCultureIgnoreCase)); |
| 87 | + |
| 88 | + WriteVerbose("Converting response"); |
| 89 | + WriteAzureBackupProtectionPolicy(policyObjects); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + public void WriteAzureBackupProtectionPolicy(IEnumerable<ProtectionPolicyInfo> sourcePolicyList) |
| 94 | + { |
| 95 | + List<AzureBackupProtectionPolicy> targetList = new List<AzureBackupProtectionPolicy>(); |
| 96 | + |
| 97 | + foreach (var sourcePolicy in sourcePolicyList) |
| 98 | + { |
| 99 | + targetList.Add(new AzureBackupProtectionPolicy(ResourceGroupName, ResourceName, sourcePolicy)); |
| 100 | + } |
| 101 | + |
| 102 | + this.WriteObject(targetList, true); |
| 103 | + } |
| 104 | + |
| 105 | + private BackupSchedule GetBackupSchedule() |
| 106 | + { |
| 107 | + WriteVerbose("Entering GetBackupSchedule"); |
| 108 | + |
| 109 | + var backupSchedule = new BackupSchedule(); |
| 110 | + |
| 111 | + backupSchedule.BackupType = this.BackupType; |
| 112 | + backupSchedule.RetentionPolicy = GetRetentionPolicy(); |
| 113 | + //Enum.Parse(ScheduleRunType, this.ScheduleType), |
| 114 | + backupSchedule.ScheduleRun = this.ScheduleType; |
| 115 | + if (this.ScheduleType == "Weekly") |
| 116 | + { |
| 117 | + backupSchedule.ScheduleRunDays = GetScheduleRunDays(); |
| 118 | + } |
| 119 | + backupSchedule.ScheduleRunTimes = new List<DateTime> {this.ScheduleRunTimes}; |
| 120 | + backupSchedule.ScheduleStartTime = this.ScheduleRunTimes; |
| 121 | + |
| 122 | + WriteVerbose("Exiting GetBackupSchedule"); |
| 123 | + return backupSchedule; |
| 124 | + } |
| 125 | + |
| 126 | + private RetentionPolicy GetRetentionPolicy() |
| 127 | + { |
| 128 | + WriteVerbose("Entering RetentionPolicy"); |
| 129 | + var retentionPolicy = new RetentionPolicy |
| 130 | + { |
| 131 | + RetentionType = (RetentionDurationType)Enum.Parse(typeof(RetentionDurationType), this.RetentionType), |
| 132 | + RetentionDuration = this.RetentionDuration |
| 133 | + }; |
| 134 | + |
| 135 | + return retentionPolicy; |
| 136 | + } |
| 137 | + |
| 138 | + private IList<DayOfWeek> GetScheduleRunDays() |
| 139 | + { |
| 140 | + IList<DayOfWeek> ListofWeekDays = new List<DayOfWeek>(); |
| 141 | + |
| 142 | + foreach(var dayOfWeek in this.ScheduleRunDays) |
| 143 | + { |
| 144 | + WriteVerbose("dayOfWeek" + dayOfWeek.ToString()); |
| 145 | + DayOfWeek item = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), dayOfWeek, true); |
| 146 | + WriteVerbose("Item" + item.ToString()); |
| 147 | + if(!ListofWeekDays.Contains(item)) |
| 148 | + { |
| 149 | + ListofWeekDays.Add(item); |
| 150 | + } |
| 151 | + |
| 152 | + else |
| 153 | + { |
| 154 | + throw new ArgumentException(string.Format("Repeated Days in ScheduleRunDays")); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return ListofWeekDays; |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | +} |
| 163 | + |
0 commit comments