internal async Task CreateAffinityGroupAsync() { bool exists = false; try { // check if the affinity group already exists var affinityGroup = await _managementClient.AffinityGroups.GetAsync(_parameters.AffinityGroupName); exists = (affinityGroup.Name == _parameters.AffinityGroupName); } catch (Microsoft.WindowsAzure.CloudException e) { if (e.ErrorCode != "ResourceNotFound") { throw; } } catch (Exception e) { Console.WriteLine("\n Error: \n {0}", e.ToString()); } if (!exists) { // get default location LocationsListResponse.Location defaultLocation = _managementClient.Locations.List().Locations.FirstOrDefault <LocationsListResponse.Location>(); // create new affinity group await _managementClient.AffinityGroups.CreateAsync( new AffinityGroupCreateParameters() { Name = _parameters.AffinityGroupName, Description = _parameters.AffinityGroupName, Label = _parameters.AffinityGroupName, Location = defaultLocation.Name }); } }
/// <summary> /// The List Locations operation lists all of the data center locations /// that are valid for your subscription. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/gg441293.aspx /// for more information) /// </summary> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The List Locations operation response. /// </returns> public async Task <LocationsListResponse> ListAsync(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>(); TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters); } // Construct URL string url = ""; url = url + "/"; if (this.Client.Credentials.SubscriptionId != null) { url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId); } url = url + "/locations"; 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", "2014-10-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 LocationsListResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new LocationsListResponse(); XDocument responseDoc = XDocument.Parse(responseContent); XElement locationsSequenceElement = responseDoc.Element(XName.Get("Locations", "http://schemas.microsoft.com/windowsazure")); if (locationsSequenceElement != null) { foreach (XElement locationsElement in locationsSequenceElement.Elements(XName.Get("Location", "http://schemas.microsoft.com/windowsazure"))) { LocationsListResponse.Location locationInstance = new LocationsListResponse.Location(); result.Locations.Add(locationInstance); XElement nameElement = locationsElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure")); if (nameElement != null) { string nameInstance = nameElement.Value; locationInstance.Name = nameInstance; } XElement displayNameElement = locationsElement.Element(XName.Get("DisplayName", "http://schemas.microsoft.com/windowsazure")); if (displayNameElement != null) { string displayNameInstance = displayNameElement.Value; locationInstance.DisplayName = displayNameInstance; } XElement availableServicesSequenceElement = locationsElement.Element(XName.Get("AvailableServices", "http://schemas.microsoft.com/windowsazure")); if (availableServicesSequenceElement != null) { foreach (XElement availableServicesElement in availableServicesSequenceElement.Elements(XName.Get("AvailableService", "http://schemas.microsoft.com/windowsazure"))) { locationInstance.AvailableServices.Add(availableServicesElement.Value); } } XElement storageCapabilitiesElement = locationsElement.Element(XName.Get("StorageCapabilities", "http://schemas.microsoft.com/windowsazure")); if (storageCapabilitiesElement != null) { StorageCapabilities storageCapabilitiesInstance = new StorageCapabilities(); locationInstance.StorageCapabilities = storageCapabilitiesInstance; XElement storageAccountTypesSequenceElement = storageCapabilitiesElement.Element(XName.Get("StorageAccountTypes", "http://schemas.microsoft.com/windowsazure")); if (storageAccountTypesSequenceElement != null) { storageCapabilitiesInstance.StorageAccountTypes = new List <string>(); foreach (XElement storageAccountTypesElement in storageAccountTypesSequenceElement.Elements(XName.Get("StorageAccountType", "http://schemas.microsoft.com/windowsazure"))) { storageCapabilitiesInstance.StorageAccountTypes.Add(storageAccountTypesElement.Value); } } } XElement computeCapabilitiesElement = locationsElement.Element(XName.Get("ComputeCapabilities", "http://schemas.microsoft.com/windowsazure")); if (computeCapabilitiesElement != null) { ComputeCapabilities computeCapabilitiesInstance = new ComputeCapabilities(); locationInstance.ComputeCapabilities = computeCapabilitiesInstance; XElement virtualMachinesRoleSizesSequenceElement = computeCapabilitiesElement.Element(XName.Get("VirtualMachinesRoleSizes", "http://schemas.microsoft.com/windowsazure")); if (virtualMachinesRoleSizesSequenceElement != null) { foreach (XElement virtualMachinesRoleSizesElement in virtualMachinesRoleSizesSequenceElement.Elements(XName.Get("RoleSize", "http://schemas.microsoft.com/windowsazure"))) { computeCapabilitiesInstance.VirtualMachinesRoleSizes.Add(virtualMachinesRoleSizesElement.Value); } } XElement webWorkerRoleSizesSequenceElement = computeCapabilitiesElement.Element(XName.Get("WebWorkerRoleSizes", "http://schemas.microsoft.com/windowsazure")); if (webWorkerRoleSizesSequenceElement != null) { foreach (XElement webWorkerRoleSizesElement in webWorkerRoleSizesSequenceElement.Elements(XName.Get("RoleSize", "http://schemas.microsoft.com/windowsazure"))) { computeCapabilitiesInstance.WebWorkerRoleSizes.Add(webWorkerRoleSizesElement.Value); } } } } } } 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> /// The List Locations operation lists all of the data center locations /// that are valid for your subscription. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/gg441293.aspx /// for more information) /// </summary> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The List Locations operation response. /// </returns> public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Models.LocationsListResponse> ListAsync(CancellationToken cancellationToken) { // Validate // Tracing bool shouldTrace = CloudContext.Configuration.Tracing.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = Tracing.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); Tracing.Enter(invocationId, this, "ListAsync", tracingParameters); } // Construct URL string url = new Uri(this.Client.BaseUri, "/").AbsoluteUri + this.Client.Credentials.SubscriptionId + "/locations"; // 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) { Tracing.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { Tracing.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), CloudExceptionType.Xml); if (shouldTrace) { Tracing.Error(invocationId, ex); } throw ex; } // Create Result LocationsListResponse result = null; // Deserialize Response cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new LocationsListResponse(); XDocument responseDoc = XDocument.Parse(responseContent); XElement locationsSequenceElement = responseDoc.Element(XName.Get("Locations", "http://schemas.microsoft.com/windowsazure")); if (locationsSequenceElement != null) { foreach (XElement locationsElement in locationsSequenceElement.Elements(XName.Get("Location", "http://schemas.microsoft.com/windowsazure"))) { LocationsListResponse.Location locationInstance = new LocationsListResponse.Location(); result.Locations.Add(locationInstance); XElement nameElement = locationsElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure")); if (nameElement != null) { string nameInstance = nameElement.Value; locationInstance.Name = nameInstance; } XElement displayNameElement = locationsElement.Element(XName.Get("DisplayName", "http://schemas.microsoft.com/windowsazure")); if (displayNameElement != null) { string displayNameInstance = displayNameElement.Value; locationInstance.DisplayName = displayNameInstance; } XElement availableServicesSequenceElement = locationsElement.Element(XName.Get("AvailableServices", "http://schemas.microsoft.com/windowsazure")); if (availableServicesSequenceElement != null) { foreach (XElement availableServicesElement in availableServicesSequenceElement.Elements(XName.Get("AvailableService", "http://schemas.microsoft.com/windowsazure"))) { locationInstance.AvailableServices.Add(availableServicesElement.Value); } } } } result.StatusCode = statusCode; if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (shouldTrace) { Tracing.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }