private void ValidateRecommendedIndex(RecommendedIndex index, string state) { Assert.Equal(index.Name, "nci_wi_Person_25F9812A-26A3-4ECA-B078-B2982817D12C"); Assert.Equal(index.Properties.Action, "Create"); Assert.Equal(index.Properties.State, state); Assert.Equal(index.Properties.Created, DateTime.Parse("2015-06-22T13:18:00")); Assert.Equal(index.Properties.LastModified, DateTime.Parse("2015-06-22T13:18:00")); Assert.Equal(index.Properties.IndexType, "NONCLUSTERED"); Assert.Equal(index.Properties.Schema, "[dbo]"); Assert.Equal(index.Properties.Table, "[Person]"); Assert.Equal(index.Properties.Columns[0], "[FirstName]"); Assert.Equal(index.Properties.Columns[1], "[LastName]"); Assert.Equal(index.Properties.IncludedColumns[0], "[MiddleName]"); Assert.Equal(index.Properties.IncludedColumns[1], "[Suffix]"); Assert.Equal(index.Properties.IndexScript, "create nonclustered index [nci_wi_Person_25F9812A-26A3-4ECA-B078-B2982817D12C] on [dbo].[Person] ([FirstName], [LastName]) include ([MiddleName], [Suffix]) with (online = on)"); Assert.Equal(index.Properties.EstimatedImpact[0].Name, "benefit"); Assert.Equal(index.Properties.EstimatedImpact[0].Unit, "unitless"); Assert.Equal(index.Properties.EstimatedImpact[0].ChangeValueAbsolute, 3); Assert.Equal(index.Properties.EstimatedImpact[0].ChangeValueRelative, null); Assert.Equal(index.Properties.ReportedImpact[0].Name, "space_change"); Assert.Equal(index.Properties.ReportedImpact[0].Unit, "megabytes"); Assert.Equal(index.Properties.ReportedImpact[0].ChangeValueAbsolute, null); Assert.Equal(index.Properties.ReportedImpact[0].ChangeValueRelative, null); }
/// <summary> /// Returns details on recommended index. /// </summary> /// <param name='resourceGroupName'> /// Required. The name of the Resource Group. /// </param> /// <param name='serverName'> /// Required. The name of the Azure SQL server. /// </param> /// <param name='databaseName'> /// Required. The name of the Azure SQL database. /// </param> /// <param name='schemaName'> /// Required. The name of the Azure SQL database schema. /// </param> /// <param name='tableName'> /// Required. The name of the Azure SQL database table. /// </param> /// <param name='indexName'> /// Required. The name of the Azure SQL database recommended index. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// Represents the response to a get recommended index request. /// </returns> public async Task <RecommendedIndexGetResponse> GetAsync(string resourceGroupName, string serverName, string databaseName, string schemaName, string tableName, string indexName, CancellationToken cancellationToken) { // Validate if (resourceGroupName == null) { throw new ArgumentNullException("resourceGroupName"); } if (serverName == null) { throw new ArgumentNullException("serverName"); } if (databaseName == null) { throw new ArgumentNullException("databaseName"); } if (schemaName == null) { throw new ArgumentNullException("schemaName"); } if (tableName == null) { throw new ArgumentNullException("tableName"); } if (indexName == null) { throw new ArgumentNullException("indexName"); } // 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("databaseName", databaseName); tracingParameters.Add("schemaName", schemaName); tracingParameters.Add("tableName", tableName); tracingParameters.Add("indexName", indexName); 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 + "/databases/"; url = url + Uri.EscapeDataString(databaseName); url = url + "/schemas/"; url = url + Uri.EscapeDataString(schemaName); url = url + "/tables/"; url = url + Uri.EscapeDataString(tableName); url = url + "/recommendedIndexes/"; url = url + Uri.EscapeDataString(indexName); 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 RecommendedIndexGetResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new RecommendedIndexGetResponse(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { RecommendedIndex recommendedIndexInstance = new RecommendedIndex(); result.RecommendedIndex = recommendedIndexInstance; JToken propertiesValue = responseDoc["properties"]; if (propertiesValue != null && propertiesValue.Type != JTokenType.Null) { RecommendedIndexProperties propertiesInstance = new RecommendedIndexProperties(); recommendedIndexInstance.Properties = propertiesInstance; JToken actionValue = propertiesValue["action"]; if (actionValue != null && actionValue.Type != JTokenType.Null) { string actionInstance = ((string)actionValue); propertiesInstance.Action = actionInstance; } JToken stateValue = propertiesValue["state"]; if (stateValue != null && stateValue.Type != JTokenType.Null) { string stateInstance = ((string)stateValue); propertiesInstance.State = stateInstance; } JToken createdValue = propertiesValue["created"]; if (createdValue != null && createdValue.Type != JTokenType.Null) { DateTime createdInstance = ((DateTime)createdValue); propertiesInstance.Created = createdInstance; } JToken lastModifiedValue = propertiesValue["lastModified"]; if (lastModifiedValue != null && lastModifiedValue.Type != JTokenType.Null) { DateTime lastModifiedInstance = ((DateTime)lastModifiedValue); propertiesInstance.LastModified = lastModifiedInstance; } JToken indexTypeValue = propertiesValue["indexType"]; if (indexTypeValue != null && indexTypeValue.Type != JTokenType.Null) { string indexTypeInstance = ((string)indexTypeValue); propertiesInstance.IndexType = indexTypeInstance; } JToken schemaValue = propertiesValue["schema"]; if (schemaValue != null && schemaValue.Type != JTokenType.Null) { string schemaInstance = ((string)schemaValue); propertiesInstance.Schema = schemaInstance; } JToken tableValue = propertiesValue["table"]; if (tableValue != null && tableValue.Type != JTokenType.Null) { string tableInstance = ((string)tableValue); propertiesInstance.Table = tableInstance; } JToken columnsArray = propertiesValue["columns"]; if (columnsArray != null && columnsArray.Type != JTokenType.Null) { foreach (JToken columnsValue in ((JArray)columnsArray)) { propertiesInstance.Columns.Add(((string)columnsValue)); } } JToken includedColumnsArray = propertiesValue["includedColumns"]; if (includedColumnsArray != null && includedColumnsArray.Type != JTokenType.Null) { foreach (JToken includedColumnsValue in ((JArray)includedColumnsArray)) { propertiesInstance.IncludedColumns.Add(((string)includedColumnsValue)); } } JToken indexScriptValue = propertiesValue["indexScript"]; if (indexScriptValue != null && indexScriptValue.Type != JTokenType.Null) { string indexScriptInstance = ((string)indexScriptValue); propertiesInstance.IndexScript = indexScriptInstance; } JToken estimatedImpactArray = propertiesValue["estimatedImpact"]; if (estimatedImpactArray != null && estimatedImpactArray.Type != JTokenType.Null) { foreach (JToken estimatedImpactValue in ((JArray)estimatedImpactArray)) { OperationImpact operationImpactInstance = new OperationImpact(); propertiesInstance.EstimatedImpact.Add(operationImpactInstance); JToken nameValue = estimatedImpactValue["name"]; if (nameValue != null && nameValue.Type != JTokenType.Null) { string nameInstance = ((string)nameValue); operationImpactInstance.Name = nameInstance; } JToken unitValue = estimatedImpactValue["unit"]; if (unitValue != null && unitValue.Type != JTokenType.Null) { string unitInstance = ((string)unitValue); operationImpactInstance.Unit = unitInstance; } JToken changeValueAbsoluteValue = estimatedImpactValue["changeValueAbsolute"]; if (changeValueAbsoluteValue != null && changeValueAbsoluteValue.Type != JTokenType.Null) { double changeValueAbsoluteInstance = ((double)changeValueAbsoluteValue); operationImpactInstance.ChangeValueAbsolute = changeValueAbsoluteInstance; } JToken changeValueRelativeValue = estimatedImpactValue["changeValueRelative"]; if (changeValueRelativeValue != null && changeValueRelativeValue.Type != JTokenType.Null) { double changeValueRelativeInstance = ((double)changeValueRelativeValue); operationImpactInstance.ChangeValueRelative = changeValueRelativeInstance; } } } JToken reportedImpactArray = propertiesValue["reportedImpact"]; if (reportedImpactArray != null && reportedImpactArray.Type != JTokenType.Null) { foreach (JToken reportedImpactValue in ((JArray)reportedImpactArray)) { OperationImpact operationImpactInstance2 = new OperationImpact(); propertiesInstance.ReportedImpact.Add(operationImpactInstance2); JToken nameValue2 = reportedImpactValue["name"]; if (nameValue2 != null && nameValue2.Type != JTokenType.Null) { string nameInstance2 = ((string)nameValue2); operationImpactInstance2.Name = nameInstance2; } JToken unitValue2 = reportedImpactValue["unit"]; if (unitValue2 != null && unitValue2.Type != JTokenType.Null) { string unitInstance2 = ((string)unitValue2); operationImpactInstance2.Unit = unitInstance2; } JToken changeValueAbsoluteValue2 = reportedImpactValue["changeValueAbsolute"]; if (changeValueAbsoluteValue2 != null && changeValueAbsoluteValue2.Type != JTokenType.Null) { double changeValueAbsoluteInstance2 = ((double)changeValueAbsoluteValue2); operationImpactInstance2.ChangeValueAbsolute = changeValueAbsoluteInstance2; } JToken changeValueRelativeValue2 = reportedImpactValue["changeValueRelative"]; if (changeValueRelativeValue2 != null && changeValueRelativeValue2.Type != JTokenType.Null) { double changeValueRelativeInstance2 = ((double)changeValueRelativeValue2); operationImpactInstance2.ChangeValueRelative = changeValueRelativeInstance2; } } } } JToken idValue = responseDoc["id"]; if (idValue != null && idValue.Type != JTokenType.Null) { string idInstance = ((string)idValue); recommendedIndexInstance.Id = idInstance; } JToken nameValue3 = responseDoc["name"]; if (nameValue3 != null && nameValue3.Type != JTokenType.Null) { string nameInstance3 = ((string)nameValue3); recommendedIndexInstance.Name = nameInstance3; } JToken typeValue = responseDoc["type"]; if (typeValue != null && typeValue.Type != JTokenType.Null) { string typeInstance = ((string)typeValue); recommendedIndexInstance.Type = typeInstance; } JToken locationValue = responseDoc["location"]; if (locationValue != null && locationValue.Type != JTokenType.Null) { string locationInstance = ((string)locationValue); recommendedIndexInstance.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); recommendedIndexInstance.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(); } } }