/// <summary> /// Get affinity group /// </summary> /// <param name="name"></param> /// <returns></returns> public AffinityGroupListResponse.AffinityGroup GetAffinityGroup(string name) { TestEasyLog.Instance.Info(string.Format("Getting affinity group '{0}'", name)); try { var result = ManagementClient.AffinityGroups.GetAsync(name, new CancellationToken()).Result; var affinityGroup = new AffinityGroupListResponse.AffinityGroup { Capabilities = result.Capabilities, Description = result.Description, Label = result.Label, Location = result.Location, Name = result.Name, }; TestEasyLog.Instance.LogObject(affinityGroup); return(affinityGroup); } catch (Exception) { TestEasyLog.Instance.Warning(string.Format("Affinity group '{0}' not found", name)); } return(null); }
internal static void LogObject(this TestEasyLog log, AffinityGroupListResponse.AffinityGroup affinityGroup) { if (affinityGroup == null) { return; } log.Info(string.Format("AffinityGroup Name:{0}", affinityGroup.Name)); if (!string.IsNullOrEmpty(affinityGroup.Label)) { log.Info(string.Format("AffinityGroup Label:{0}", affinityGroup.Label)); } log.Info(string.Format("AffinityGroup Description:{0}", affinityGroup.Description)); log.Info(string.Format("AffinityGroup Location:{0}", affinityGroup.Location)); }
/// <summary> /// Get affinity group /// </summary> /// <param name="name"></param> /// <returns></returns> public AffinityGroupListResponse.AffinityGroup GetAffinityGroup(string name) { TestEasyLog.Instance.Info(string.Format("Getting affinity group '{0}'", name)); try { var result = ManagementClient.AffinityGroups.GetAsync(name, new CancellationToken()).Result; var affinityGroup = new AffinityGroupListResponse.AffinityGroup { Capabilities = result.Capabilities, Description = result.Description, Label = result.Label, Location = result.Location, Name = result.Name, }; TestEasyLog.Instance.LogObject(affinityGroup); return affinityGroup; } catch (Exception) { TestEasyLog.Instance.Warning(string.Format("Affinity group '{0}' not found", name)); } return null; }
/// <summary> /// The List Affinity Groups operation lists the affinity groups /// associated with the specified subscription. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460797.aspx /// for more information) /// </summary> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The List Affinity Groups operation response. /// </returns> public async Task <AffinityGroupListResponse> 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, "/").ToString() + this.Client.Credentials.SubscriptionId + "/affinitygroups"; // 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 AffinityGroupListResponse result = null; // Deserialize Response cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new AffinityGroupListResponse(); XDocument responseDoc = XDocument.Parse(responseContent); XElement affinityGroupsSequenceElement = responseDoc.Element(XName.Get("AffinityGroups", "http://schemas.microsoft.com/windowsazure")); if (affinityGroupsSequenceElement != null) { foreach (XElement affinityGroupsElement in affinityGroupsSequenceElement.Elements(XName.Get("AffinityGroup", "http://schemas.microsoft.com/windowsazure"))) { AffinityGroupListResponse.AffinityGroup affinityGroupInstance = new AffinityGroupListResponse.AffinityGroup(); result.AffinityGroups.Add(affinityGroupInstance); XElement nameElement = affinityGroupsElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure")); if (nameElement != null) { string nameInstance = nameElement.Value; affinityGroupInstance.Name = nameInstance; } XElement labelElement = affinityGroupsElement.Element(XName.Get("Label", "http://schemas.microsoft.com/windowsazure")); if (labelElement != null) { string labelInstance = TypeConversion.FromBase64String(labelElement.Value); affinityGroupInstance.Label = labelInstance; } XElement descriptionElement = affinityGroupsElement.Element(XName.Get("Description", "http://schemas.microsoft.com/windowsazure")); if (descriptionElement != null) { string descriptionInstance = descriptionElement.Value; affinityGroupInstance.Description = descriptionInstance; } XElement locationElement = affinityGroupsElement.Element(XName.Get("Location", "http://schemas.microsoft.com/windowsazure")); if (locationElement != null) { string locationInstance = locationElement.Value; affinityGroupInstance.Location = locationInstance; } XElement capabilitiesSequenceElement = affinityGroupsElement.Element(XName.Get("Capabilities", "http://schemas.microsoft.com/windowsazure")); if (capabilitiesSequenceElement != null) { foreach (XElement capabilitiesElement in capabilitiesSequenceElement.Elements(XName.Get("Capability", "http://schemas.microsoft.com/windowsazure"))) { affinityGroupInstance.Capabilities.Add(capabilitiesElement.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(); } } }
/// <summary> /// The List Affinity Groups operation lists the affinity groups /// associated with the specified subscription. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460797.aspx /// for more information) /// </summary> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The List Affinity Groups operation response. /// </returns> public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Models.AffinityGroupListResponse> 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 = "/" + (this.Client.Credentials.SubscriptionId != null ? this.Client.Credentials.SubscriptionId.Trim() : "") + "/affinitygroups"; 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-05-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)); if (shouldTrace) { Tracing.Error(invocationId, ex); } throw ex; } // Create Result AffinityGroupListResponse result = null; // Deserialize Response cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new AffinityGroupListResponse(); XDocument responseDoc = XDocument.Parse(responseContent); XElement affinityGroupsSequenceElement = responseDoc.Element(XName.Get("AffinityGroups", "http://schemas.microsoft.com/windowsazure")); if (affinityGroupsSequenceElement != null) { result.AffinityGroups = new List <AffinityGroupListResponse.AffinityGroup>(); foreach (XElement affinityGroupsElement in affinityGroupsSequenceElement.Elements(XName.Get("AffinityGroup", "http://schemas.microsoft.com/windowsazure"))) { AffinityGroupListResponse.AffinityGroup affinityGroupInstance = new AffinityGroupListResponse.AffinityGroup(); result.AffinityGroups.Add(affinityGroupInstance); XElement nameElement = affinityGroupsElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure")); if (nameElement != null) { string nameInstance = nameElement.Value; affinityGroupInstance.Name = nameInstance; } XElement labelElement = affinityGroupsElement.Element(XName.Get("Label", "http://schemas.microsoft.com/windowsazure")); if (labelElement != null) { string labelInstance = TypeConversion.FromBase64String(labelElement.Value); affinityGroupInstance.Label = labelInstance; } XElement descriptionElement = affinityGroupsElement.Element(XName.Get("Description", "http://schemas.microsoft.com/windowsazure")); if (descriptionElement != null) { string descriptionInstance = descriptionElement.Value; affinityGroupInstance.Description = descriptionInstance; } XElement locationElement = affinityGroupsElement.Element(XName.Get("Location", "http://schemas.microsoft.com/windowsazure")); if (locationElement != null) { string locationInstance = locationElement.Value; affinityGroupInstance.Location = locationInstance; } XElement capabilitiesSequenceElement = affinityGroupsElement.Element(XName.Get("Capabilities", "http://schemas.microsoft.com/windowsazure")); if (capabilitiesSequenceElement != null) { affinityGroupInstance.Capabilities = new List <string>(); foreach (XElement capabilitiesElement in capabilitiesSequenceElement.Elements(XName.Get("Capability", "http://schemas.microsoft.com/windowsazure"))) { affinityGroupInstance.Capabilities.Add(capabilitiesElement.Value); } } XElement createdTimeElement = affinityGroupsElement.Element(XName.Get("CreatedTime", "http://schemas.microsoft.com/windowsazure")); if (createdTimeElement != null && string.IsNullOrEmpty(createdTimeElement.Value) == false) { DateTime createdTimeInstance = DateTime.Parse(createdTimeElement.Value, CultureInfo.InvariantCulture); affinityGroupInstance.CreatedTime = createdTimeInstance; } XElement computeCapabilitiesElement = affinityGroupsElement.Element(XName.Get("ComputeCapabilities", "http://schemas.microsoft.com/windowsazure")); if (computeCapabilitiesElement != null) { ComputeCapabilities computeCapabilitiesInstance = new ComputeCapabilities(); affinityGroupInstance.ComputeCapabilities = computeCapabilitiesInstance; XElement virtualMachinesRoleSizesSequenceElement = computeCapabilitiesElement.Element(XName.Get("VirtualMachinesRoleSizes", "http://schemas.microsoft.com/windowsazure")); if (virtualMachinesRoleSizesSequenceElement != null) { computeCapabilitiesInstance.VirtualMachinesRoleSizes = new System.Collections.Generic.List <string>(); 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) { computeCapabilitiesInstance.WebWorkerRoleSizes = new System.Collections.Generic.List <string>(); 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) { Tracing.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }