public void UpdateState() { using (UndoContext context = UndoContext.Current) { context.Start(); var handler = new BasicDelegatingHandler(); var sqlClient = Sql2ScenarioHelper.GetSqlClient(handler); var updateParams = new RecommendedIndexUpdateParameters { Properties = new RecommendedIndexUpdateProperties { State = "Pending" } }; var response = sqlClient.RecommendedIndexes.Update(ResourceGroupName, ServerName, DatabaseName, Schema, TableName, IndexName, updateParams); var index = response.RecommendedIndex; ValidateRecommendedIndex(index, "Pending"); updateParams.Properties.State = "Active"; sqlClient.RecommendedIndexes.Update(ResourceGroupName, ServerName, DatabaseName, Schema, TableName, IndexName, updateParams); } }
/// <summary> /// We execute or cancel index operations by updating index state. /// Allowed state transitions are :Active -> Pending /// - Start index creation processPending -> Active /// - Cancel index creationActive/Pending -> Ignored /// - Ignore index recommendation so it will no longer show /// in active recommendationsIgnored -> Active - /// Restore index recommendationSuccess -> Pending Revert - /// Revert index that has been createdPending Revert -> Revert /// Canceled - Cancel index revert operation /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.Azure.Management.Sql.IRecommendedIndexOperations. /// </param> /// <param name='resourceGroupName'> /// Required. The name of the Resource Group to which the Azure SQL /// Database Server belongs. /// </param> /// <param name='serverName'> /// Required. The name of the Azure SQL Database Server on which the /// database is hosted. /// </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='parameters'> /// Required. The required parameters for updating index state. /// </param> /// <returns> /// Represents the response to a get recommended index request. /// </returns> public static Task <RecommendedIndexUpdateResponse> UpdateAsync(this IRecommendedIndexOperations operations, string resourceGroupName, string serverName, string databaseName, string schemaName, string tableName, string indexName, RecommendedIndexUpdateParameters parameters) { return(operations.UpdateAsync(resourceGroupName, serverName, databaseName, schemaName, tableName, indexName, parameters, CancellationToken.None)); }
/// <summary> /// We execute or cancel index operations by updating index state. /// Allowed state transitions are :Active -> Pending /// - Start index creation processPending -> Active /// - Cancel index creationActive/Pending -> Ignored /// - Ignore index recommendation so it will no longer show /// in active recommendationsIgnored -> Active - /// Restore index recommendationSuccess -> Pending Revert - /// Revert index that has been createdPending Revert -> Revert /// Canceled - Cancel index revert operation /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.Azure.Management.Sql.IRecommendedIndexOperations. /// </param> /// <param name='resourceGroupName'> /// Required. The name of the Resource Group to which the Azure SQL /// Database Server belongs. /// </param> /// <param name='serverName'> /// Required. The name of the Azure SQL Database Server on which the /// database is hosted. /// </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='parameters'> /// Required. The required parameters for updating index state. /// </param> /// <returns> /// Represents the response to a get recommended index request. /// </returns> public static RecommendedIndexUpdateResponse Update(this IRecommendedIndexOperations operations, string resourceGroupName, string serverName, string databaseName, string schemaName, string tableName, string indexName, RecommendedIndexUpdateParameters parameters) { return(Task.Factory.StartNew((object s) => { return ((IRecommendedIndexOperations)s).UpdateAsync(resourceGroupName, serverName, databaseName, schemaName, tableName, indexName, parameters); } , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult()); }
/// <summary> /// We execute or cancel index operations by updating index state. /// Allowed state transitions are :Active -> Pending /// - Start index creation processPending -> Active /// - Cancel index creationActive/Pending -> Ignored /// - Ignore index recommendation so it will no longer show /// in active recommendationsIgnored -> Active - /// Restore index recommendationSuccess -> Pending Revert - /// Revert index that has been createdPending Revert -> Revert /// Canceled - Cancel index revert operation /// </summary> /// <param name='resourceGroupName'> /// Required. The name of the Resource Group to which the Azure SQL /// Database Server belongs. /// </param> /// <param name='serverName'> /// Required. The name of the Azure SQL Database Server on which the /// database is hosted. /// </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='parameters'> /// Required. The required parameters for updating index state. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// Represents the response to a get recommended index request. /// </returns> public async Task <RecommendedIndexUpdateResponse> UpdateAsync(string resourceGroupName, string serverName, string databaseName, string schemaName, string tableName, string indexName, RecommendedIndexUpdateParameters parameters, 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"); } if (parameters == null) { throw new ArgumentNullException("parameters"); } if (parameters.Properties == null) { throw new ArgumentNullException("parameters.Properties"); } // 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); tracingParameters.Add("parameters", parameters); TracingAdapter.Enter(invocationId, this, "UpdateAsync", 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.Put; httpRequest.RequestUri = new Uri(url); // Set Headers // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Serialize Request string requestContent = null; JToken requestDoc = null; JObject recommendedIndexUpdateParametersValue = new JObject(); requestDoc = recommendedIndexUpdateParametersValue; JObject propertiesValue = new JObject(); recommendedIndexUpdateParametersValue["properties"] = propertiesValue; if (parameters.Properties.State != null) { propertiesValue["state"] = parameters.Properties.State; } requestContent = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented); httpRequest.Content = new StringContent(requestContent, Encoding.UTF8); httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // 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, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result RecommendedIndexUpdateResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new RecommendedIndexUpdateResponse(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { ErrorResponse errorInstance = new ErrorResponse(); result.Error = errorInstance; JToken codeValue = responseDoc["code"]; if (codeValue != null && codeValue.Type != JTokenType.Null) { string codeInstance = ((string)codeValue); errorInstance.Code = codeInstance; } JToken messageValue = responseDoc["message"]; if (messageValue != null && messageValue.Type != JTokenType.Null) { string messageInstance = ((string)messageValue); errorInstance.Message = messageInstance; } JToken targetValue = responseDoc["target"]; if (targetValue != null && targetValue.Type != JTokenType.Null) { string targetInstance = ((string)targetValue); errorInstance.Target = targetInstance; } RecommendedIndex recommendedIndexInstance = new RecommendedIndex(); result.RecommendedIndex = recommendedIndexInstance; JToken propertiesValue2 = responseDoc["properties"]; if (propertiesValue2 != null && propertiesValue2.Type != JTokenType.Null) { RecommendedIndexProperties propertiesInstance = new RecommendedIndexProperties(); recommendedIndexInstance.Properties = propertiesInstance; JToken actionValue = propertiesValue2["action"]; if (actionValue != null && actionValue.Type != JTokenType.Null) { string actionInstance = ((string)actionValue); propertiesInstance.Action = actionInstance; } JToken stateValue = propertiesValue2["state"]; if (stateValue != null && stateValue.Type != JTokenType.Null) { string stateInstance = ((string)stateValue); propertiesInstance.State = stateInstance; } JToken createdValue = propertiesValue2["created"]; if (createdValue != null && createdValue.Type != JTokenType.Null) { DateTime createdInstance = ((DateTime)createdValue); propertiesInstance.Created = createdInstance; } JToken lastModifiedValue = propertiesValue2["lastModified"]; if (lastModifiedValue != null && lastModifiedValue.Type != JTokenType.Null) { DateTime lastModifiedInstance = ((DateTime)lastModifiedValue); propertiesInstance.LastModified = lastModifiedInstance; } JToken indexTypeValue = propertiesValue2["indexType"]; if (indexTypeValue != null && indexTypeValue.Type != JTokenType.Null) { string indexTypeInstance = ((string)indexTypeValue); propertiesInstance.IndexType = indexTypeInstance; } JToken schemaValue = propertiesValue2["schema"]; if (schemaValue != null && schemaValue.Type != JTokenType.Null) { string schemaInstance = ((string)schemaValue); propertiesInstance.Schema = schemaInstance; } JToken tableValue = propertiesValue2["table"]; if (tableValue != null && tableValue.Type != JTokenType.Null) { string tableInstance = ((string)tableValue); propertiesInstance.Table = tableInstance; } JToken columnsArray = propertiesValue2["columns"]; if (columnsArray != null && columnsArray.Type != JTokenType.Null) { foreach (JToken columnsValue in ((JArray)columnsArray)) { propertiesInstance.Columns.Add(((string)columnsValue)); } } JToken includedColumnsArray = propertiesValue2["includedColumns"]; if (includedColumnsArray != null && includedColumnsArray.Type != JTokenType.Null) { foreach (JToken includedColumnsValue in ((JArray)includedColumnsArray)) { propertiesInstance.IncludedColumns.Add(((string)includedColumnsValue)); } } JToken indexScriptValue = propertiesValue2["indexScript"]; if (indexScriptValue != null && indexScriptValue.Type != JTokenType.Null) { string indexScriptInstance = ((string)indexScriptValue); propertiesInstance.IndexScript = indexScriptInstance; } JToken estimatedImpactArray = propertiesValue2["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 = propertiesValue2["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("Location")) { result.OperationStatusLink = httpResponse.Headers.GetValues("Location").FirstOrDefault(); } if (httpResponse.Headers.Contains("Retry-After")) { result.RetryAfter = int.Parse(httpResponse.Headers.GetValues("Retry-After").FirstOrDefault(), CultureInfo.InvariantCulture); } if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (statusCode == HttpStatusCode.OK) { result.Status = OperationStatus.Succeeded; } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }