public void StorageAccountBeginCreateTest() { var handler = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK }; using (var context = UndoContext.Current) { context.Start(); var resourcesClient = StorageManagementTestUtilities.GetResourceManagementClient(handler); var storageMgmtClient = StorageManagementTestUtilities.GetStorageManagementClient(handler); // Create resource group var rgname = StorageManagementTestUtilities.CreateResourceGroup(resourcesClient); // Create storage account string accountName = TestUtilities.GenerateName("sto"); StorageAccountCreateParameters parameters = StorageManagementTestUtilities.GetDefaultStorageAccountParameters(); StorageAccountCreateResponse response = storageMgmtClient.StorageAccounts.BeginCreate(rgname, accountName, parameters); Assert.Equal(HttpStatusCode.Accepted, response.StatusCode); // Poll for creation while (response.StatusCode == HttpStatusCode.Accepted) { TestUtilities.Wait(response.RetryAfter * 1000); response = storageMgmtClient.GetCreateOperationStatus(response.OperationStatusLink); } // Verify create succeeded StorageAccount result = response.StorageAccount; Assert.Equal(result.Location, StorageManagementTestUtilities.DefaultLocation); Assert.Equal(result.AccountType, AccountType.StandardGRS); Assert.Equal(result.Tags.Count, 2); // Make sure a second create returns immediately response = storageMgmtClient.StorageAccounts.BeginCreate(rgname, accountName, parameters); Assert.Equal(HttpStatusCode.OK, response.StatusCode); // Verify create succeeded Assert.Equal(result.Location, StorageManagementTestUtilities.DefaultLocation); Assert.Equal(result.AccountType, AccountType.StandardGRS); Assert.Equal(result.Tags.Count, 2); } }
/// <summary> /// The Get Create Operation Status operation returns the status of the /// specified create operation. After calling the asynchronous Begin /// Create operation, you can call Get Create Operation Status to /// determine whether the operation has succeeded, failed, or is still /// in progress. /// </summary> /// <param name='operationStatusLink'> /// Required. The URL where the status of the long-running create /// operation can be checked. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The Create storage account operation response. /// </returns> public async Task <StorageAccountCreateResponse> GetCreateOperationStatusAsync(string operationStatusLink, CancellationToken cancellationToken) { // Validate if (operationStatusLink == null) { throw new ArgumentNullException("operationStatusLink"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("operationStatusLink", operationStatusLink); TracingAdapter.Enter(invocationId, this, "GetCreateOperationStatusAsync", tracingParameters); } // Construct URL string url = ""; url = url + operationStatusLink; 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-client-request-id", Guid.NewGuid().ToString()); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { TracingAdapter.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { TracingAdapter.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK && statusCode != HttpStatusCode.Accepted && statusCode != HttpStatusCode.InternalServerError) { 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 StorageAccountCreateResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.Accepted || statusCode == HttpStatusCode.InternalServerError) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new StorageAccountCreateResponse(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { StorageAccount storageAccountInstance = new StorageAccount(); result.StorageAccount = storageAccountInstance; JToken idValue = responseDoc["id"]; if (idValue != null && idValue.Type != JTokenType.Null) { string idInstance = ((string)idValue); storageAccountInstance.Id = idInstance; } JToken nameValue = responseDoc["name"]; if (nameValue != null && nameValue.Type != JTokenType.Null) { string nameInstance = ((string)nameValue); storageAccountInstance.Name = nameInstance; } JToken typeValue = responseDoc["type"]; if (typeValue != null && typeValue.Type != JTokenType.Null) { string typeInstance = ((string)typeValue); storageAccountInstance.Type = typeInstance; } JToken locationValue = responseDoc["location"]; if (locationValue != null && locationValue.Type != JTokenType.Null) { string locationInstance = ((string)locationValue); storageAccountInstance.Location = locationInstance; } JToken tagsSequenceElement = ((JToken)responseDoc["tags"]); if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null) { foreach (JProperty property in tagsSequenceElement) { string tagsKey = ((string)property.Name); string tagsValue = ((string)property.Value); storageAccountInstance.Tags.Add(tagsKey, tagsValue); } } JToken propertiesValue = responseDoc["properties"]; if (propertiesValue != null && propertiesValue.Type != JTokenType.Null) { JToken provisioningStateValue = propertiesValue["provisioningState"]; if (provisioningStateValue != null && provisioningStateValue.Type != JTokenType.Null) { ProvisioningState provisioningStateInstance = ((ProvisioningState)Enum.Parse(typeof(ProvisioningState), ((string)provisioningStateValue), true)); storageAccountInstance.ProvisioningState = provisioningStateInstance; } JToken accountTypeValue = propertiesValue["accountType"]; if (accountTypeValue != null && accountTypeValue.Type != JTokenType.Null) { AccountType accountTypeInstance = StorageManagementClient.ParseAccountType(((string)accountTypeValue)); storageAccountInstance.AccountType = accountTypeInstance; } JToken primaryEndpointsValue = propertiesValue["primaryEndpoints"]; if (primaryEndpointsValue != null && primaryEndpointsValue.Type != JTokenType.Null) { Endpoints primaryEndpointsInstance = new Endpoints(); storageAccountInstance.PrimaryEndpoints = primaryEndpointsInstance; JToken blobValue = primaryEndpointsValue["blob"]; if (blobValue != null && blobValue.Type != JTokenType.Null) { Uri blobInstance = TypeConversion.TryParseUri(((string)blobValue)); primaryEndpointsInstance.Blob = blobInstance; } JToken queueValue = primaryEndpointsValue["queue"]; if (queueValue != null && queueValue.Type != JTokenType.Null) { Uri queueInstance = TypeConversion.TryParseUri(((string)queueValue)); primaryEndpointsInstance.Queue = queueInstance; } JToken tableValue = primaryEndpointsValue["table"]; if (tableValue != null && tableValue.Type != JTokenType.Null) { Uri tableInstance = TypeConversion.TryParseUri(((string)tableValue)); primaryEndpointsInstance.Table = tableInstance; } } JToken primaryLocationValue = propertiesValue["primaryLocation"]; if (primaryLocationValue != null && primaryLocationValue.Type != JTokenType.Null) { string primaryLocationInstance = ((string)primaryLocationValue); storageAccountInstance.PrimaryLocation = primaryLocationInstance; } JToken statusOfPrimaryValue = propertiesValue["statusOfPrimary"]; if (statusOfPrimaryValue != null && statusOfPrimaryValue.Type != JTokenType.Null) { AccountStatus statusOfPrimaryInstance = ((AccountStatus)Enum.Parse(typeof(AccountStatus), ((string)statusOfPrimaryValue), true)); storageAccountInstance.StatusOfPrimary = statusOfPrimaryInstance; } JToken lastGeoFailoverTimeValue = propertiesValue["lastGeoFailoverTime"]; if (lastGeoFailoverTimeValue != null && lastGeoFailoverTimeValue.Type != JTokenType.Null) { DateTime lastGeoFailoverTimeInstance = ((DateTime)lastGeoFailoverTimeValue); storageAccountInstance.LastGeoFailoverTime = lastGeoFailoverTimeInstance; } JToken secondaryLocationValue = propertiesValue["secondaryLocation"]; if (secondaryLocationValue != null && secondaryLocationValue.Type != JTokenType.Null) { string secondaryLocationInstance = ((string)secondaryLocationValue); storageAccountInstance.SecondaryLocation = secondaryLocationInstance; } JToken statusOfSecondaryValue = propertiesValue["statusOfSecondary"]; if (statusOfSecondaryValue != null && statusOfSecondaryValue.Type != JTokenType.Null) { AccountStatus statusOfSecondaryInstance = ((AccountStatus)Enum.Parse(typeof(AccountStatus), ((string)statusOfSecondaryValue), true)); storageAccountInstance.StatusOfSecondary = statusOfSecondaryInstance; } JToken creationTimeValue = propertiesValue["creationTime"]; if (creationTimeValue != null && creationTimeValue.Type != JTokenType.Null) { DateTime creationTimeInstance = ((DateTime)creationTimeValue); storageAccountInstance.CreationTime = creationTimeInstance; } JToken customDomainValue = propertiesValue["customDomain"]; if (customDomainValue != null && customDomainValue.Type != JTokenType.Null) { CustomDomain customDomainInstance = new CustomDomain(); storageAccountInstance.CustomDomain = customDomainInstance; JToken nameValue2 = customDomainValue["name"]; if (nameValue2 != null && nameValue2.Type != JTokenType.Null) { string nameInstance2 = ((string)nameValue2); customDomainInstance.Name = nameInstance2; } JToken useSubDomainValue = customDomainValue["useSubDomain"]; if (useSubDomainValue != null && useSubDomainValue.Type != JTokenType.Null) { bool useSubDomainInstance = ((bool)useSubDomainValue); customDomainInstance.UseSubDomain = useSubDomainInstance; } } JToken secondaryEndpointsValue = propertiesValue["secondaryEndpoints"]; if (secondaryEndpointsValue != null && secondaryEndpointsValue.Type != JTokenType.Null) { Endpoints secondaryEndpointsInstance = new Endpoints(); storageAccountInstance.SecondaryEndpoints = secondaryEndpointsInstance; JToken blobValue2 = secondaryEndpointsValue["blob"]; if (blobValue2 != null && blobValue2.Type != JTokenType.Null) { Uri blobInstance2 = TypeConversion.TryParseUri(((string)blobValue2)); secondaryEndpointsInstance.Blob = blobInstance2; } JToken queueValue2 = secondaryEndpointsValue["queue"]; if (queueValue2 != null && queueValue2.Type != JTokenType.Null) { Uri queueInstance2 = TypeConversion.TryParseUri(((string)queueValue2)); secondaryEndpointsInstance.Queue = queueInstance2; } JToken tableValue2 = secondaryEndpointsValue["table"]; if (tableValue2 != null && tableValue2.Type != JTokenType.Null) { Uri tableInstance2 = TypeConversion.TryParseUri(((string)tableValue2)); secondaryEndpointsInstance.Table = tableInstance2; } } } } } result.StatusCode = statusCode; if (httpResponse.Headers.Contains("RetryAfter")) { result.RetryAfter = int.Parse(httpResponse.Headers.GetValues("RetryAfter").FirstOrDefault(), CultureInfo.InvariantCulture); } if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (statusCode == HttpStatusCode.Conflict) { result.Status = OperationStatus.Failed; } if (statusCode == HttpStatusCode.InternalServerError) { result.Status = OperationStatus.InProgress; } if (statusCode == HttpStatusCode.Accepted) { result.Status = OperationStatus.InProgress; } if (statusCode == HttpStatusCode.OK) { result.Status = OperationStatus.Succeeded; } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }