|
| 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 Microsoft.WindowsAzure.Commands.Utilities.Common; |
| 20 | +using Microsoft.Azure.Common.Authentication; |
| 21 | +using Microsoft.Azure.Common.Authentication.Models; |
| 22 | +using System.Threading; |
| 23 | +using Hyak.Common; |
| 24 | +using Microsoft.Azure.Commands.AzureBackup.Properties; |
| 25 | +using System.Net; |
| 26 | +using Microsoft.WindowsAzure.Management.Scheduler; |
| 27 | +using Microsoft.Azure.Management.BackupServices; |
| 28 | +using Microsoft.Azure.Management.BackupServices.Models; |
| 29 | + |
| 30 | +namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets |
| 31 | +{ |
| 32 | + public abstract class AzureBackupCmdletBase : AzurePSCmdlet |
| 33 | + { |
| 34 | + /// <summary> |
| 35 | + /// ResourceGroup context for the operation |
| 36 | + /// </summary> |
| 37 | + private string resourceGroupName { get; set; } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Resource context for the operation |
| 41 | + /// </summary> |
| 42 | + private string resourceName { get; set; } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Client request id. |
| 46 | + /// </summary> |
| 47 | + private string clientRequestId; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Azure backup client. |
| 51 | + /// </summary> |
| 52 | + private BackupServicesManagementClient azureBackupClient; |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Cancellation Token Source |
| 56 | + /// </summary> |
| 57 | + private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); |
| 58 | + protected CancellationToken CmdletCancellationToken; |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Get Azure backup client. |
| 62 | + /// </summary> |
| 63 | + protected BackupServicesManagementClient AzureBackupClient |
| 64 | + { |
| 65 | + get |
| 66 | + { |
| 67 | + if (this.azureBackupClient == null) |
| 68 | + { |
| 69 | + // Temp code to be able to test internal env. |
| 70 | + ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; |
| 71 | + |
| 72 | + var cloudServicesClient = AzureSession.ClientFactory.CreateClient<CloudServiceManagementClient>(Profile, Profile.Context.Subscription, AzureEnvironment.Endpoint.ResourceManager); |
| 73 | + this.azureBackupClient = AzureSession.ClientFactory.CreateCustomClient<BackupServicesManagementClient>(resourceName, resourceGroupName, cloudServicesClient.Credentials, cloudServicesClient.BaseUri); |
| 74 | + } |
| 75 | + |
| 76 | + return this.azureBackupClient; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public void InitializeAzureBackupCmdlet(string rgName, string rName) |
| 81 | + { |
| 82 | + resourceGroupName = rgName; |
| 83 | + resourceName = rName; |
| 84 | + |
| 85 | + clientRequestId = Guid.NewGuid().ToString() + "-" + DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ssZ") + "-PS"; |
| 86 | + |
| 87 | + WriteDebug(string.Format("Initialized AzureBackup Cmdlet, ClientRequestId: {0}, ResourceGroupName: {1}, ResourceName : {2}", this.clientRequestId, resourceGroupName, resourceName)); |
| 88 | + |
| 89 | + CmdletCancellationToken = cancellationTokenSource.Token; |
| 90 | + } |
| 91 | + |
| 92 | + protected void ExecutionBlock(Action execAction) |
| 93 | + { |
| 94 | + try |
| 95 | + { |
| 96 | + execAction(); |
| 97 | + } |
| 98 | + catch (Exception exception) |
| 99 | + { |
| 100 | + WriteDebug(String.Format("Caught exception, type: {0}", exception.GetType())); |
| 101 | + HandleException(exception); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Handles set of exceptions thrown by client |
| 107 | + /// </summary> |
| 108 | + /// <param name="ex"></param> |
| 109 | + private void HandleException(Exception exception) |
| 110 | + { |
| 111 | + if (exception is AggregateException && ((AggregateException)exception).InnerExceptions != null |
| 112 | + && ((AggregateException)exception).InnerExceptions.Count != 0) |
| 113 | + { |
| 114 | + WriteDebug("Handling aggregate exception"); |
| 115 | + foreach (var innerEx in ((AggregateException)exception).InnerExceptions) |
| 116 | + { |
| 117 | + HandleException(innerEx); |
| 118 | + } |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + Exception targetEx = exception; |
| 123 | + string targetErrorId = String.Empty; |
| 124 | + ErrorCategory targetErrorCategory = ErrorCategory.NotSpecified; |
| 125 | + |
| 126 | + if (exception is CloudException) |
| 127 | + { |
| 128 | + var cloudEx = exception as CloudException; |
| 129 | + if (cloudEx.Response != null && cloudEx.Response.StatusCode == HttpStatusCode.NotFound) |
| 130 | + { |
| 131 | + WriteDebug(String.Format("Received CloudException, StatusCode: {0}", cloudEx.Response.StatusCode)); |
| 132 | + |
| 133 | + targetEx = new Exception(Resources.ResourceNotFoundMessage); |
| 134 | + targetErrorCategory = ErrorCategory.InvalidArgument; |
| 135 | + } |
| 136 | + else if (cloudEx.Error != null) |
| 137 | + { |
| 138 | + WriteDebug(String.Format("Received CloudException, ErrorCode: {0}, Message: {1}", cloudEx.Error.Code, cloudEx.Error.Message)); |
| 139 | + |
| 140 | + targetErrorId = cloudEx.Error.Code; |
| 141 | + targetErrorCategory = ErrorCategory.InvalidOperation; |
| 142 | + } |
| 143 | + } |
| 144 | + else if (exception is WebException) |
| 145 | + { |
| 146 | + var webEx = exception as WebException; |
| 147 | + WriteDebug(string.Format("Received WebException, Response: {0}, Status: {1}", webEx.Response, webEx.Status)); |
| 148 | + |
| 149 | + targetErrorCategory = ErrorCategory.ConnectionError; |
| 150 | + } |
| 151 | + else if (exception is ArgumentException || exception is ArgumentNullException) |
| 152 | + { |
| 153 | + WriteDebug(string.Format("Received ArgumentException")); |
| 154 | + targetErrorCategory = ErrorCategory.InvalidArgument; |
| 155 | + } |
| 156 | + |
| 157 | + var errorRecord = new ErrorRecord(targetEx, targetErrorId, targetErrorCategory, null); |
| 158 | + WriteError(errorRecord); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + protected CustomRequestHeaders GetCustomRequestHeaders() |
| 163 | + { |
| 164 | + var hdrs = new CustomRequestHeaders() |
| 165 | + { |
| 166 | + // ClientRequestId is a unique ID for every request to backend service. |
| 167 | + ClientRequestId = this.clientRequestId, |
| 168 | + }; |
| 169 | + |
| 170 | + return hdrs; |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
0 commit comments