/// <summary> /// Get the list of all storages for the subscription /// </summary> /// <param name='subscriptionId'> /// Optional. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The List Storage Accounts operation response. /// </returns> public async Task <StorageAccountListResponse> ListAzureStoragesAsync(string subscriptionId, CancellationToken cancellationToken) { // Validate // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("subscriptionId", subscriptionId); TracingAdapter.Enter(invocationId, this, "ListAzureStoragesAsync", tracingParameters); } // Construct URL string url = ""; url = url + "/"; if (subscriptionId != null) { url = url + Uri.EscapeDataString(subscriptionId); } url = url + "/services/storageservices"; string baseUrl = this.Client.BaseUri.AbsoluteUri; // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl[baseUrl.Length - 1] == '/') { baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } if (url[0] == '/') { url = url.Substring(1); } url = baseUrl + "/" + url; url = url.Replace(" ", "%20"); // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Get; httpRequest.RequestUri = new Uri(url); // Set Headers httpRequest.Headers.Add("x-ms-version", "2013-03-01"); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { TracingAdapter.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { TracingAdapter.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result StorageAccountListResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new StorageAccountListResponse(); XDocument responseDoc = XDocument.Parse(responseContent); XElement storageServicesSequenceElement = responseDoc.Element(XName.Get("StorageServices", "http://schemas.microsoft.com/windowsazure")); if (storageServicesSequenceElement != null) { foreach (XElement storageServicesElement in storageServicesSequenceElement.Elements(XName.Get("StorageService", "http://schemas.microsoft.com/windowsazure"))) { StorageAccountListResponse.StorageAccount storageServiceInstance = new StorageAccountListResponse.StorageAccount(); result.StorageAccounts.Add(storageServiceInstance); XElement urlElement = storageServicesElement.Element(XName.Get("Url", "http://schemas.microsoft.com/windowsazure")); if (urlElement != null) { Uri urlInstance = TypeConversion.TryParseUri(urlElement.Value); storageServiceInstance.Uri = urlInstance; } XElement serviceNameElement = storageServicesElement.Element(XName.Get("ServiceName", "http://schemas.microsoft.com/windowsazure")); if (serviceNameElement != null) { string serviceNameInstance = serviceNameElement.Value; storageServiceInstance.Name = serviceNameInstance; } XElement storageServicePropertiesElement = storageServicesElement.Element(XName.Get("StorageServiceProperties", "http://schemas.microsoft.com/windowsazure")); if (storageServicePropertiesElement != null) { StorageAccountListResponse.StorageAccountProperties storageServicePropertiesInstance = new StorageAccountListResponse.StorageAccountProperties(); storageServiceInstance.Properties = storageServicePropertiesInstance; XElement descriptionElement = storageServicePropertiesElement.Element(XName.Get("Description", "http://schemas.microsoft.com/windowsazure")); if (descriptionElement != null) { bool isNil = false; XAttribute nilAttribute = descriptionElement.Attribute(XName.Get("nil", "http://www.w3.org/2001/XMLSchema-instance")); if (nilAttribute != null) { isNil = nilAttribute.Value == "true"; } if (isNil == false) { string descriptionInstance = descriptionElement.Value; storageServicePropertiesInstance.Description = descriptionInstance; } } XElement affinityGroupElement = storageServicePropertiesElement.Element(XName.Get("AffinityGroup", "http://schemas.microsoft.com/windowsazure")); if (affinityGroupElement != null) { string affinityGroupInstance = affinityGroupElement.Value; storageServicePropertiesInstance.AffinityGroup = affinityGroupInstance; } XElement locationElement = storageServicePropertiesElement.Element(XName.Get("Location", "http://schemas.microsoft.com/windowsazure")); if (locationElement != null) { string locationInstance = locationElement.Value; storageServicePropertiesInstance.Location = locationInstance; } } } } } result.StatusCode = statusCode; if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }
/// <summary> /// Validates whether the storage belongs to the currently logged account or not. /// </summary> /// <param name="azureSubscription">Subscription ID</param> /// <param name="azureStorageAccount">Storage Account details</param> /// <param name="vaultLocation">Current Vault Location</param> /// <param name="validationSuccessful">Out variable to indicate if validation was successful</param> /// <param name="locationValid">Out variable to indicate if location of storage account is valid</param> public void ValidateStorageAccountAssociation( string azureSubscription, string azureStorageAccount, string vaultLocation, out bool validationSuccessful, out bool locationValid) { if (string.IsNullOrEmpty(azureSubscription)) { throw new InvalidOperationException( string.Format( Properties.Resources.SubscriptionIdIsNotValid)); } if (string.IsNullOrEmpty(azureStorageAccount)) { throw new InvalidOperationException( string.Format( Properties.Resources.StorageAccountNameIsNotValid)); } bool associatedAccount = false; StorageAccountListResponse azureStorageListResponse = null; StorageAccountListResponse.StorageAccount currentStorageAccount = null; try { azureStorageListResponse = this.GetSiteRecoveryClient().Storages.ListAzureStorages(azureSubscription); } catch (Exception) { validationSuccessful = false; locationValid = false; return; } foreach (var storage in azureStorageListResponse.StorageAccounts) { if (string.Compare( azureStorageAccount, storage.Name, StringComparison.OrdinalIgnoreCase) == 0) { associatedAccount = true; currentStorageAccount = storage; break; } } if (!associatedAccount) { validationSuccessful = false; locationValid = false; return; } // Validate that the Geo Location of the storage account is the same as that of the vault. if (string.IsNullOrEmpty(currentStorageAccount.Properties.Location)) { validationSuccessful = false; locationValid = false; return; } if (0 != string.Compare( currentStorageAccount.Properties.Location, vaultLocation, StringComparison.OrdinalIgnoreCase)) { validationSuccessful = true; locationValid = false; return; } validationSuccessful = true; locationValid = true; }