|
| 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.Web; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Collections.Specialized; |
| 19 | +using System.Linq; |
| 20 | +using System.Management.Automation; |
| 21 | +using System.Text; |
| 22 | +using System.Threading.Tasks; |
| 23 | +using Microsoft.Azure.Commands.Compute; |
| 24 | +using Microsoft.Azure.Management.BackupServices.Models; |
| 25 | +using MBS = Microsoft.Azure.Management.BackupServices; |
| 26 | +using Microsoft.Azure.Commands.Compute.Models; |
| 27 | + |
| 28 | +namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets |
| 29 | +{ |
| 30 | + /// <summary> |
| 31 | + /// Get list of containers |
| 32 | + /// </summary> |
| 33 | + [Cmdlet(VerbsLifecycle.Register, "AzureBackupContainer"), OutputType(typeof(Guid))] |
| 34 | + public class RegisterAzureBackupContainer : AzureBackupVaultCmdletBase |
| 35 | + { |
| 36 | + //[Parameter(Position = 2, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.VirtualMachine)] |
| 37 | + //[ValidateNotNullOrEmpty] |
| 38 | + //public PSVirtualMachineInstanceView VirtualMachine { get; set; } |
| 39 | + [Parameter(Position = 2, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.VirtualMachine)] |
| 40 | + [ValidateNotNullOrEmpty] |
| 41 | + public string VirtualMachineName { get; set; } |
| 42 | + |
| 43 | + [Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.VirtualMachine)] |
| 44 | + [ValidateNotNullOrEmpty] |
| 45 | + public string VirtualMachineRGName { get; set; } |
| 46 | + |
| 47 | + public override void ExecuteCmdlet() |
| 48 | + { |
| 49 | + base.ExecuteCmdlet(); |
| 50 | + |
| 51 | + ExecutionBlock(() => |
| 52 | + { |
| 53 | + //string vmName = VirtualMachine.Name; |
| 54 | + //string rgName = VirtualMachine.ResourceGroupName; |
| 55 | + string vmName = VirtualMachineName; |
| 56 | + string rgName = VirtualMachineRGName; |
| 57 | + Guid jobId = Guid.Empty; |
| 58 | + bool isDiscoveryNeed = false; |
| 59 | + MBS.OperationResponse operationResponse; |
| 60 | + |
| 61 | + ContainerInfo container = null; |
| 62 | + isDiscoveryNeed = IsDiscoveryNeeded(vmName, rgName, out container); |
| 63 | + if(isDiscoveryNeed) |
| 64 | + { |
| 65 | + RefreshContainer(); |
| 66 | + isDiscoveryNeed = IsDiscoveryNeeded(vmName, rgName, out container); |
| 67 | + if ((isDiscoveryNeed == true) || (container == null)) |
| 68 | + { |
| 69 | + //Container is not discovered. Throw exception |
| 70 | + throw new NotImplementedException(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + //Container is discovered. Register the container |
| 75 | + List<string> containerNameList = new List<string>(); |
| 76 | + containerNameList.Add(container.Name); |
| 77 | + RegisterContainerRequestInput registrationRequest = new RegisterContainerRequestInput(containerNameList, AzureBackupContainerType.IaasVMContainer.ToString()); |
| 78 | + operationResponse = AzureBackupClient.Container.RegisterAsync(registrationRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result; |
| 79 | + |
| 80 | + //TODO fix the OperationResponse to JobID conversion |
| 81 | + jobId = operationResponse.OperationId; |
| 82 | + WriteObject(jobId); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + private void RefreshContainer() |
| 87 | + { |
| 88 | + bool isRetyNeeded = true; |
| 89 | + int retryCount = 1; |
| 90 | + bool isDiscoverySuccessful = false; |
| 91 | + while (isRetyNeeded && retryCount <= 3) |
| 92 | + { |
| 93 | + MBS.OperationResponse opResponse = |
| 94 | + AzureBackupClient.Container.RefreshAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result; |
| 95 | + |
| 96 | + //Now wait for the operation to Complete |
| 97 | + isRetyNeeded = WaitForDiscoveryToCOmplete(opResponse.OperationId.ToString(), out isDiscoverySuccessful); |
| 98 | + retryCount++; |
| 99 | + } |
| 100 | + |
| 101 | + if (!isDiscoverySuccessful) |
| 102 | + { |
| 103 | + //Discovery failed |
| 104 | + throw new Exception(); //TODO: |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private bool WaitForDiscoveryToCOmplete(string operationId, out bool isDiscoverySuccessful) |
| 109 | + { |
| 110 | + bool isRetryNeeded = false; |
| 111 | + |
| 112 | + |
| 113 | + BMSOperationStatusResponse status = new BMSOperationStatusResponse() |
| 114 | + { |
| 115 | + OperationStatus = AzureBackupOperationStatus.InProgress.ToString() |
| 116 | + }; |
| 117 | + |
| 118 | + while (status.OperationStatus != AzureBackupOperationStatus.Completed.ToString()) |
| 119 | + { |
| 120 | + status = AzureBackupClient.OperationStatus.GetAsync(operationId, GetCustomRequestHeaders(), CmdletCancellationToken).Result; |
| 121 | + System.Threading.Thread.Sleep(TimeSpan.FromSeconds(15)); |
| 122 | + } |
| 123 | + |
| 124 | + isDiscoverySuccessful = true; |
| 125 | + //If operation fails check if retry is needed or not |
| 126 | + if (status.OperationResult != AzureBackupOperationResult.Succeeded.ToString()) |
| 127 | + { |
| 128 | + isDiscoverySuccessful = false; |
| 129 | + if ((status.ErrorCode == AzureBackupOperationErrorCode.DiscoveryInProgress.ToString() || |
| 130 | + (status.ErrorCode == AzureBackupOperationErrorCode.BMSUserErrorObjectLocked.ToString()))) |
| 131 | + { |
| 132 | + //Need to retry for this errors |
| 133 | + isRetryNeeded = true; |
| 134 | + } |
| 135 | + } |
| 136 | + return isRetryNeeded; |
| 137 | + } |
| 138 | + |
| 139 | + private bool IsDiscoveryNeeded(string vmName, string rgName, out ContainerInfo container) |
| 140 | + { |
| 141 | + bool isDiscoveryNeed = false; |
| 142 | + //First check if container is discoverd or not |
| 143 | + ListContainerQueryParameter queryParams = new ListContainerQueryParameter(); |
| 144 | + queryParams.ContainerTypeField = AzureBackupContainerType.IaasVMContainer.ToString(); |
| 145 | + queryParams.ContainerStatusField = String.Empty; |
| 146 | + queryParams.ContainerFriendlyNameField = vmName; |
| 147 | + string queryString = GetQueryFileter(queryParams); |
| 148 | + |
| 149 | + ListContainerResponse containers = AzureBackupClient.Container.ListAsync(queryString, |
| 150 | + GetCustomRequestHeaders(), CmdletCancellationToken).Result; |
| 151 | + if (containers.Objects.Count() == 0) |
| 152 | + { |
| 153 | + //Container is not discover |
| 154 | + WriteVerbose("Container is not discovered"); |
| 155 | + container = null; |
| 156 | + isDiscoveryNeed = true; |
| 157 | + } |
| 158 | + |
| 159 | + else |
| 160 | + { |
| 161 | + //We can have multiple container with same friendly name. |
| 162 | + //Look for resourceGroup name in the container unoque name |
| 163 | + container = containers.Objects.Where(c => c.ParentContainerFriendlyName.ToLower().Equals(rgName.ToLower())).FirstOrDefault(); |
| 164 | + if (container == null) |
| 165 | + { |
| 166 | + //Container is not in list of registered container |
| 167 | + isDiscoveryNeed = true; |
| 168 | + } |
| 169 | + } |
| 170 | + return isDiscoveryNeed; |
| 171 | + } |
| 172 | + |
| 173 | + private string GetQueryFileter(ListContainerQueryParameter queryParams) |
| 174 | + { |
| 175 | + NameValueCollection collection = new NameValueCollection(); |
| 176 | + if (!String.IsNullOrEmpty(queryParams.ContainerTypeField)) |
| 177 | + { |
| 178 | + collection.Add("ContainerType", queryParams.ContainerTypeField); |
| 179 | + } |
| 180 | + |
| 181 | + if (!String.IsNullOrEmpty(queryParams.ContainerStatusField)) |
| 182 | + { |
| 183 | + collection.Add("ContainerStatus", queryParams.ContainerStatusField); |
| 184 | + } |
| 185 | + |
| 186 | + if (!String.IsNullOrEmpty(queryParams.ContainerFriendlyNameField)) |
| 187 | + { |
| 188 | + collection.Add("FriendlyName", queryParams.ContainerFriendlyNameField); |
| 189 | + } |
| 190 | + |
| 191 | + if (collection == null || collection.Count == 0) |
| 192 | + { |
| 193 | + return String.Empty; |
| 194 | + } |
| 195 | + |
| 196 | + var httpValueCollection = HttpUtility.ParseQueryString(String.Empty); |
| 197 | + httpValueCollection.Add(collection); |
| 198 | + |
| 199 | + return "&" + httpValueCollection.ToString(); |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments