示例#1
0
 private ServiceObjective CreateServiceObjectiveFromResponse(ServiceObjectiveListResponse.ServiceObjective response)
 {
     return(new ServiceObjective()
     {
         Name = response.Name,
         Id = Guid.Parse(response.Id),
         IsDefault = response.IsDefault,
         IsSystem = response.IsSystem,
         Enabled = response.Enabled,
         Description = response.Description,
         Context = this,
         DimensionSettings = CreateDimensionSettingsFromResponse(response.DimensionSettings)
     });
 }
示例#2
0
        /// <summary>
        /// Returns information about all Service Objectives on a database
        /// server.
        /// </summary>
        /// <param name='serverName'>
        /// The name of the database server to be queried.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Response containing the list of service objective for a given
        /// server.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Sql.Models.ServiceObjectiveListResponse> ListAsync(string serverName, CancellationToken cancellationToken)
        {
            // Validate
            if (serverName == null)
            {
                throw new ArgumentNullException("serverName");
            }

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = Tracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("serverName", serverName);
                Tracing.Enter(invocationId, this, "ListAsync", tracingParameters);
            }

            // Construct URL
            string url = new Uri(this.Client.BaseUri, this.Client.Credentials.SubscriptionId).AbsoluteUri + "/services/sqlservers/servers/" + serverName + "/serviceobjectives";

            // 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", "2012-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
                    ServiceObjectiveListResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new ServiceObjectiveListResponse();
                    XDocument responseDoc = XDocument.Parse(responseContent);

                    XElement serviceResourcesSequenceElement = responseDoc.Element(XName.Get("ServiceResources", "http://schemas.microsoft.com/windowsazure"));
                    if (serviceResourcesSequenceElement != null)
                    {
                        foreach (XElement serviceResourcesElement in serviceResourcesSequenceElement.Elements(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure")))
                        {
                            ServiceObjectiveListResponse.ServiceObjective serviceResourceInstance = new ServiceObjectiveListResponse.ServiceObjective();
                            result.ServiceObjectives.Add(serviceResourceInstance);

                            XElement nameElement = serviceResourcesElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                            if (nameElement != null)
                            {
                                string nameInstance = nameElement.Value;
                                serviceResourceInstance.Name = nameInstance;
                            }

                            XElement typeElement = serviceResourcesElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure"));
                            if (typeElement != null)
                            {
                                string typeInstance = typeElement.Value;
                                serviceResourceInstance.Type = typeInstance;
                            }

                            XElement stateElement = serviceResourcesElement.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure"));
                            if (stateElement != null)
                            {
                                string stateInstance = stateElement.Value;
                                serviceResourceInstance.State = stateInstance;
                            }

                            XElement selfLinkElement = serviceResourcesElement.Element(XName.Get("SelfLink", "http://schemas.microsoft.com/windowsazure"));
                            if (selfLinkElement != null)
                            {
                                string selfLinkInstance = selfLinkElement.Value;
                                serviceResourceInstance.SelfLink = selfLinkInstance;
                            }

                            XElement parentLinkElement = serviceResourcesElement.Element(XName.Get("ParentLink", "http://schemas.microsoft.com/windowsazure"));
                            if (parentLinkElement != null)
                            {
                                string parentLinkInstance = parentLinkElement.Value;
                                serviceResourceInstance.ParentLink = parentLinkInstance;
                            }

                            XElement idElement = serviceResourcesElement.Element(XName.Get("Id", "http://schemas.microsoft.com/windowsazure"));
                            if (idElement != null)
                            {
                                string idInstance = idElement.Value;
                                serviceResourceInstance.Id = idInstance;
                            }

                            XElement isDefaultElement = serviceResourcesElement.Element(XName.Get("IsDefault", "http://schemas.microsoft.com/windowsazure"));
                            if (isDefaultElement != null)
                            {
                                bool isDefaultInstance = bool.Parse(isDefaultElement.Value);
                                serviceResourceInstance.IsDefault = isDefaultInstance;
                            }

                            XElement isSystemElement = serviceResourcesElement.Element(XName.Get("IsSystem", "http://schemas.microsoft.com/windowsazure"));
                            if (isSystemElement != null)
                            {
                                bool isSystemInstance = bool.Parse(isSystemElement.Value);
                                serviceResourceInstance.IsSystem = isSystemInstance;
                            }

                            XElement descriptionElement = serviceResourcesElement.Element(XName.Get("Description", "http://schemas.microsoft.com/windowsazure"));
                            if (descriptionElement != null)
                            {
                                string descriptionInstance = descriptionElement.Value;
                                serviceResourceInstance.Description = descriptionInstance;
                            }

                            XElement enabledElement = serviceResourcesElement.Element(XName.Get("Enabled", "http://schemas.microsoft.com/windowsazure"));
                            if (enabledElement != null)
                            {
                                bool enabledInstance = bool.Parse(enabledElement.Value);
                                serviceResourceInstance.Enabled = enabledInstance;
                            }

                            XElement dimensionSettingsSequenceElement = serviceResourcesElement.Element(XName.Get("DimensionSettings", "http://schemas.microsoft.com/windowsazure"));
                            if (dimensionSettingsSequenceElement != null)
                            {
                                foreach (XElement dimensionSettingsElement in dimensionSettingsSequenceElement.Elements(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure")))
                                {
                                    ServiceObjectiveListResponse.ServiceObjective.DimensionSettingResponse serviceResourceInstance2 = new ServiceObjectiveListResponse.ServiceObjective.DimensionSettingResponse();
                                    serviceResourceInstance.DimensionSettings.Add(serviceResourceInstance2);

                                    XElement nameElement2 = dimensionSettingsElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                                    if (nameElement2 != null)
                                    {
                                        string nameInstance2 = nameElement2.Value;
                                        serviceResourceInstance2.Name = nameInstance2;
                                    }

                                    XElement typeElement2 = dimensionSettingsElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure"));
                                    if (typeElement2 != null)
                                    {
                                        string typeInstance2 = typeElement2.Value;
                                        serviceResourceInstance2.Type = typeInstance2;
                                    }

                                    XElement stateElement2 = dimensionSettingsElement.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure"));
                                    if (stateElement2 != null)
                                    {
                                        string stateInstance2 = stateElement2.Value;
                                        serviceResourceInstance2.State = stateInstance2;
                                    }

                                    XElement selfLinkElement2 = dimensionSettingsElement.Element(XName.Get("SelfLink", "http://schemas.microsoft.com/windowsazure"));
                                    if (selfLinkElement2 != null)
                                    {
                                        string selfLinkInstance2 = selfLinkElement2.Value;
                                        serviceResourceInstance2.SelfLink = selfLinkInstance2;
                                    }

                                    XElement parentLinkElement2 = dimensionSettingsElement.Element(XName.Get("ParentLink", "http://schemas.microsoft.com/windowsazure"));
                                    if (parentLinkElement2 != null)
                                    {
                                        string parentLinkInstance2 = parentLinkElement2.Value;
                                        serviceResourceInstance2.ParentLink = parentLinkInstance2;
                                    }

                                    XElement idElement2 = dimensionSettingsElement.Element(XName.Get("Id", "http://schemas.microsoft.com/windowsazure"));
                                    if (idElement2 != null)
                                    {
                                        string idInstance2 = idElement2.Value;
                                        serviceResourceInstance2.Id = idInstance2;
                                    }

                                    XElement descriptionElement2 = dimensionSettingsElement.Element(XName.Get("Description", "http://schemas.microsoft.com/windowsazure"));
                                    if (descriptionElement2 != null)
                                    {
                                        string descriptionInstance2 = descriptionElement2.Value;
                                        serviceResourceInstance2.Description = descriptionInstance2;
                                    }

                                    XElement ordinalElement = dimensionSettingsElement.Element(XName.Get("Ordinal", "http://schemas.microsoft.com/windowsazure"));
                                    if (ordinalElement != null)
                                    {
                                        byte ordinalInstance = byte.Parse(ordinalElement.Value, CultureInfo.InvariantCulture);
                                        serviceResourceInstance2.Ordinal = ordinalInstance;
                                    }

                                    XElement isDefaultElement2 = dimensionSettingsElement.Element(XName.Get("IsDefault", "http://schemas.microsoft.com/windowsazure"));
                                    if (isDefaultElement2 != null)
                                    {
                                        bool isDefaultInstance2 = bool.Parse(isDefaultElement2.Value);
                                        serviceResourceInstance2.IsDefault = isDefaultInstance2;
                                    }
                                }
                            }
                        }
                    }

                    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();
                }
            }
        }