private ServiceObjective CreateServiceObjectiveFromResponse(ServiceObjectiveGetResponse 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) }); }
/// <summary> /// Retrieve information on latest service objective with service objective /// </summary> /// <param name="serviceObjective">The service objective to refresh.</param> /// <returns> /// An object containing the information about the specific service objective. /// </returns> public ServiceObjective GetServiceObjective(ServiceObjective serviceObjectiveToRefresh) { this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId(); // Get the SQL management client SqlManagementClient sqlManagementClient = this.subscription.CreateClient <SqlManagementClient>(); this.AddTracingHeaders(sqlManagementClient); // Retrieve the specified database ServiceObjectiveGetResponse response = sqlManagementClient.ServiceObjectives.Get( this.serverName, serviceObjectiveToRefresh.Id.ToString()); // Construct the resulting Database object ServiceObjective serviceObjective = CreateServiceObjectiveFromResponse(response); return(serviceObjective); }
/// <summary> /// Returns information about a certain Service Objective with a /// specific Id. /// </summary> /// <param name='serverName'> /// Required. The name of the Azure SQL Database Server to be queried. /// </param> /// <param name='serviceObjectiveId'> /// Required. The Id of the Service Objective to be obtained. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// Response containing the service objective for a given Azure SQL /// Database Server with matching service objective Id. /// </returns> public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Sql.Models.ServiceObjectiveGetResponse> GetAsync(string serverName, string serviceObjectiveId, CancellationToken cancellationToken) { // Validate if (serverName == null) { throw new ArgumentNullException("serverName"); } if (serviceObjectiveId == null) { throw new ArgumentNullException("serviceObjectiveId"); } // 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); tracingParameters.Add("serviceObjectiveId", serviceObjectiveId); Tracing.Enter(invocationId, this, "GetAsync", tracingParameters); } // Construct URL string url = (this.Client.Credentials.SubscriptionId != null ? this.Client.Credentials.SubscriptionId.Trim() : "") + "/services/sqlservers/servers/" + serverName.Trim() + "/serviceobjectives/" + serviceObjectiveId.Trim(); 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", "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)); if (shouldTrace) { Tracing.Error(invocationId, ex); } throw ex; } // Create Result ServiceObjectiveGetResponse result = null; // Deserialize Response cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new ServiceObjectiveGetResponse(); XDocument responseDoc = XDocument.Parse(responseContent); XElement serviceResourceElement = responseDoc.Element(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure")); if (serviceResourceElement != null) { ServiceObjective serviceResourceInstance = new ServiceObjective(); result.ServiceObjective = serviceResourceInstance; XElement idElement = serviceResourceElement.Element(XName.Get("Id", "http://schemas.microsoft.com/windowsazure")); if (idElement != null) { string idInstance = idElement.Value; serviceResourceInstance.Id = idInstance; } XElement isDefaultElement = serviceResourceElement.Element(XName.Get("IsDefault", "http://schemas.microsoft.com/windowsazure")); if (isDefaultElement != null) { bool isDefaultInstance = bool.Parse(isDefaultElement.Value); serviceResourceInstance.IsDefault = isDefaultInstance; } XElement isSystemElement = serviceResourceElement.Element(XName.Get("IsSystem", "http://schemas.microsoft.com/windowsazure")); if (isSystemElement != null) { bool isSystemInstance = bool.Parse(isSystemElement.Value); serviceResourceInstance.IsSystem = isSystemInstance; } XElement descriptionElement = serviceResourceElement.Element(XName.Get("Description", "http://schemas.microsoft.com/windowsazure")); if (descriptionElement != null) { string descriptionInstance = descriptionElement.Value; serviceResourceInstance.Description = descriptionInstance; } XElement enabledElement = serviceResourceElement.Element(XName.Get("Enabled", "http://schemas.microsoft.com/windowsazure")); if (enabledElement != null) { bool enabledInstance = bool.Parse(enabledElement.Value); serviceResourceInstance.Enabled = enabledInstance; } XElement dimensionSettingsSequenceElement = serviceResourceElement.Element(XName.Get("DimensionSettings", "http://schemas.microsoft.com/windowsazure")); if (dimensionSettingsSequenceElement != null) { serviceResourceInstance.DimensionSettings = new List <ServiceObjective.DimensionSettingResponse>(); foreach (XElement dimensionSettingsElement in dimensionSettingsSequenceElement.Elements(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure"))) { ServiceObjective.DimensionSettingResponse serviceResourceInstance2 = new ServiceObjective.DimensionSettingResponse(); serviceResourceInstance.DimensionSettings.Add(serviceResourceInstance2); 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; } XElement nameElement = dimensionSettingsElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure")); if (nameElement != null) { string nameInstance = nameElement.Value; serviceResourceInstance2.Name = nameInstance; } XElement typeElement = dimensionSettingsElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure")); if (typeElement != null) { string typeInstance = typeElement.Value; serviceResourceInstance2.Type = typeInstance; } XElement stateElement = dimensionSettingsElement.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure")); if (stateElement != null) { string stateInstance = stateElement.Value; serviceResourceInstance2.State = stateInstance; } } } XElement nameElement2 = serviceResourceElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure")); if (nameElement2 != null) { string nameInstance2 = nameElement2.Value; serviceResourceInstance.Name = nameInstance2; } XElement typeElement2 = serviceResourceElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure")); if (typeElement2 != null) { string typeInstance2 = typeElement2.Value; serviceResourceInstance.Type = typeInstance2; } XElement stateElement2 = serviceResourceElement.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure")); if (stateElement2 != null) { string stateInstance2 = stateElement2.Value; serviceResourceInstance.State = stateInstance2; } } 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> /// Returns information about an Azure SQL Database Server Service /// Objectives. /// </summary> /// <param name='resourceGroupName'> /// Required. The name of the Resource Group to which the server /// belongs. /// </param> /// <param name='serverName'> /// Required. The name of the Server. /// </param> /// <param name='serviceObjectiveName'> /// Required. The name of the service objective to retrieve. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// Represents the response to a Get Azure Sql Database Service /// Objective request. /// </returns> public async Task <ServiceObjectiveGetResponse> GetAsync(string resourceGroupName, string serverName, string serviceObjectiveName, CancellationToken cancellationToken) { // Validate if (resourceGroupName == null) { throw new ArgumentNullException("resourceGroupName"); } if (serverName == null) { throw new ArgumentNullException("serverName"); } if (serviceObjectiveName == null) { throw new ArgumentNullException("serviceObjectiveName"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("resourceGroupName", resourceGroupName); tracingParameters.Add("serverName", serverName); tracingParameters.Add("serviceObjectiveName", serviceObjectiveName); 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 + "/resourceGroups/"; url = url + Uri.EscapeDataString(resourceGroupName); url = url + "/providers/"; url = url + "Microsoft.Sql"; url = url + "/servers/"; url = url + Uri.EscapeDataString(serverName); url = url + "/serviceObjectives/"; url = url + Uri.EscapeDataString(serviceObjectiveName); 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 ServiceObjectiveGetResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new ServiceObjectiveGetResponse(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { ServiceObjective serviceObjectiveInstance = new ServiceObjective(); result.ServiceObjective = serviceObjectiveInstance; JToken propertiesValue = responseDoc["properties"]; if (propertiesValue != null && propertiesValue.Type != JTokenType.Null) { ServiceObjectiveProperties propertiesInstance = new ServiceObjectiveProperties(); serviceObjectiveInstance.Properties = propertiesInstance; JToken serviceObjectiveNameValue = propertiesValue["serviceObjectiveName"]; if (serviceObjectiveNameValue != null && serviceObjectiveNameValue.Type != JTokenType.Null) { string serviceObjectiveNameInstance = ((string)serviceObjectiveNameValue); propertiesInstance.ServiceObjectiveName = serviceObjectiveNameInstance; } JToken isDefaultValue = propertiesValue["isDefault"]; if (isDefaultValue != null && isDefaultValue.Type != JTokenType.Null) { bool isDefaultInstance = ((bool)isDefaultValue); propertiesInstance.IsDefault = isDefaultInstance; } JToken isSystemValue = propertiesValue["isSystem"]; if (isSystemValue != null && isSystemValue.Type != JTokenType.Null) { bool isSystemInstance = ((bool)isSystemValue); propertiesInstance.IsSystem = isSystemInstance; } JToken descriptionValue = propertiesValue["description"]; if (descriptionValue != null && descriptionValue.Type != JTokenType.Null) { string descriptionInstance = ((string)descriptionValue); propertiesInstance.Description = descriptionInstance; } JToken enabledValue = propertiesValue["enabled"]; if (enabledValue != null && enabledValue.Type != JTokenType.Null) { bool enabledInstance = ((bool)enabledValue); propertiesInstance.Enabled = enabledInstance; } } JToken idValue = responseDoc["id"]; if (idValue != null && idValue.Type != JTokenType.Null) { string idInstance = ((string)idValue); serviceObjectiveInstance.Id = idInstance; } JToken nameValue = responseDoc["name"]; if (nameValue != null && nameValue.Type != JTokenType.Null) { string nameInstance = ((string)nameValue); serviceObjectiveInstance.Name = nameInstance; } JToken typeValue = responseDoc["type"]; if (typeValue != null && typeValue.Type != JTokenType.Null) { string typeInstance = ((string)typeValue); serviceObjectiveInstance.Type = typeInstance; } JToken locationValue = responseDoc["location"]; if (locationValue != null && locationValue.Type != JTokenType.Null) { string locationInstance = ((string)locationValue); serviceObjectiveInstance.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); serviceObjectiveInstance.Tags.Add(tagsKey, tagsValue); } } } } 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(); } } }