/// <summary>
 /// Convert a SLO capability to AzureSqlDatabaseServerServiceObjectiveModel
 /// </summary>
 /// <param name="edition">The edition</param>
 /// <param name="slo">The service objective</param>
 /// <param name="resourceGroupName">The resource group name</param>
 /// /// <param name="serverName">The server name</param>
 /// <returns>The converted ServiceObjective model</returns>
 private static AzureSqlServerServiceObjectiveModel CreateServiceObjectiveModelFromResponse(
     EditionCapability edition,
     ServiceObjectiveCapability slo,
     string resourceGroupName = null,
     string serverName        = null)
 {
     return(new AzureSqlServerServiceObjectiveModel()
     {
         ResourceGroupName = resourceGroupName,
         ServerName = serverName,
         ServiceObjectiveName = slo.Name,
         IsDefault = slo.Status == CapabilityStatus.Default,
         IsSystem = string.Equals(edition.Name, "System", StringComparison.OrdinalIgnoreCase),
         Description = string.Empty,
         Enabled = IsEnabled(slo.Status),
         Edition = edition.Name,
         SkuName = slo.Sku.Name,
         Family = slo.Sku.Family,
         Capacity = slo.Sku.Capacity,
         CapacityUnit = slo.PerformanceLevel.Unit
     });
 }
        /// <summary>
        /// Returns information about the Azure SQL capabilities available for
        /// the specified region.
        /// </summary>
        /// <param name='locationName'>
        /// Required. The name of the region for which the Azure SQL
        /// capabilities are retrieved.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Represents the response to a Get Azure Sql capabilities request
        /// </returns>
        public async Task <LocationCapabilitiesGetResponse> GetAsync(string locationName, CancellationToken cancellationToken)
        {
            // Validate
            if (locationName == null)
            {
                throw new ArgumentNullException("locationName");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

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

            // Construct URL
            string url = "";

            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/providers/";
            url = url + "Microsoft.Sql";
            url = url + "/locations/";
            url = url + Uri.EscapeDataString(locationName);
            url = url + "/capabilities";
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2014-04-01");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            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

                // 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
                    LocationCapabilitiesGetResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new LocationCapabilitiesGetResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            LocationCapability capabilitiesInstance = new LocationCapability();
                            result.Capabilities = capabilitiesInstance;

                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                string nameInstance = ((string)nameValue);
                                capabilitiesInstance.Name = nameInstance;
                            }

                            JToken statusValue = responseDoc["status"];
                            if (statusValue != null && statusValue.Type != JTokenType.Null)
                            {
                                string statusInstance = ((string)statusValue);
                                capabilitiesInstance.Status = statusInstance;
                            }

                            JToken supportedServerVersionsArray = responseDoc["supportedServerVersions"];
                            if (supportedServerVersionsArray != null && supportedServerVersionsArray.Type != JTokenType.Null)
                            {
                                foreach (JToken supportedServerVersionsValue in ((JArray)supportedServerVersionsArray))
                                {
                                    ServerVersionCapability serverVersionCapabilityInstance = new ServerVersionCapability();
                                    capabilitiesInstance.SupportedServerVersions.Add(serverVersionCapabilityInstance);

                                    JToken nameValue2 = supportedServerVersionsValue["name"];
                                    if (nameValue2 != null && nameValue2.Type != JTokenType.Null)
                                    {
                                        string nameInstance2 = ((string)nameValue2);
                                        serverVersionCapabilityInstance.Name = nameInstance2;
                                    }

                                    JToken statusValue2 = supportedServerVersionsValue["status"];
                                    if (statusValue2 != null && statusValue2.Type != JTokenType.Null)
                                    {
                                        string statusInstance2 = ((string)statusValue2);
                                        serverVersionCapabilityInstance.Status = statusInstance2;
                                    }

                                    JToken supportedEditionsArray = supportedServerVersionsValue["supportedEditions"];
                                    if (supportedEditionsArray != null && supportedEditionsArray.Type != JTokenType.Null)
                                    {
                                        foreach (JToken supportedEditionsValue in ((JArray)supportedEditionsArray))
                                        {
                                            EditionCapability editionCapabilityInstance = new EditionCapability();
                                            serverVersionCapabilityInstance.SupportedEditions.Add(editionCapabilityInstance);

                                            JToken nameValue3 = supportedEditionsValue["name"];
                                            if (nameValue3 != null && nameValue3.Type != JTokenType.Null)
                                            {
                                                string nameInstance3 = ((string)nameValue3);
                                                editionCapabilityInstance.Name = nameInstance3;
                                            }

                                            JToken statusValue3 = supportedEditionsValue["status"];
                                            if (statusValue3 != null && statusValue3.Type != JTokenType.Null)
                                            {
                                                string statusInstance3 = ((string)statusValue3);
                                                editionCapabilityInstance.Status = statusInstance3;
                                            }

                                            JToken supportedServiceLevelObjectivesArray = supportedEditionsValue["supportedServiceLevelObjectives"];
                                            if (supportedServiceLevelObjectivesArray != null && supportedServiceLevelObjectivesArray.Type != JTokenType.Null)
                                            {
                                                foreach (JToken supportedServiceLevelObjectivesValue in ((JArray)supportedServiceLevelObjectivesArray))
                                                {
                                                    ServiceObjectiveCapability serviceObjectiveCapabilityInstance = new ServiceObjectiveCapability();
                                                    editionCapabilityInstance.SupportedServiceObjectives.Add(serviceObjectiveCapabilityInstance);

                                                    JToken nameValue4 = supportedServiceLevelObjectivesValue["name"];
                                                    if (nameValue4 != null && nameValue4.Type != JTokenType.Null)
                                                    {
                                                        string nameInstance4 = ((string)nameValue4);
                                                        serviceObjectiveCapabilityInstance.Name = nameInstance4;
                                                    }

                                                    JToken statusValue4 = supportedServiceLevelObjectivesValue["status"];
                                                    if (statusValue4 != null && statusValue4.Type != JTokenType.Null)
                                                    {
                                                        string statusInstance4 = ((string)statusValue4);
                                                        serviceObjectiveCapabilityInstance.Status = statusInstance4;
                                                    }

                                                    JToken idValue = supportedServiceLevelObjectivesValue["id"];
                                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                                    {
                                                        Guid idInstance = Guid.Parse(((string)idValue));
                                                        serviceObjectiveCapabilityInstance.Id = idInstance;
                                                    }

                                                    JToken supportedMaxSizesArray = supportedServiceLevelObjectivesValue["supportedMaxSizes"];
                                                    if (supportedMaxSizesArray != null && supportedMaxSizesArray.Type != JTokenType.Null)
                                                    {
                                                        foreach (JToken supportedMaxSizesValue in ((JArray)supportedMaxSizesArray))
                                                        {
                                                            MaxSizeCapability maxSizeCapabilityInstance = new MaxSizeCapability();
                                                            serviceObjectiveCapabilityInstance.SupportedMaxSizes.Add(maxSizeCapabilityInstance);

                                                            JToken limitValue = supportedMaxSizesValue["limit"];
                                                            if (limitValue != null && limitValue.Type != JTokenType.Null)
                                                            {
                                                                int limitInstance = ((int)limitValue);
                                                                maxSizeCapabilityInstance.Limit = limitInstance;
                                                            }

                                                            JToken unitValue = supportedMaxSizesValue["unit"];
                                                            if (unitValue != null && unitValue.Type != JTokenType.Null)
                                                            {
                                                                string unitInstance = ((string)unitValue);
                                                                maxSizeCapabilityInstance.Unit = unitInstance;
                                                            }

                                                            JToken statusValue5 = supportedMaxSizesValue["status"];
                                                            if (statusValue5 != null && statusValue5.Type != JTokenType.Null)
                                                            {
                                                                string statusInstance5 = ((string)statusValue5);
                                                                maxSizeCapabilityInstance.Status = statusInstance5;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    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();
                }
            }
        }