/// <summary>
        /// List all recommended indexes. If database name is null get recommendations for all databases on server.
        /// </summary>
        /// <param name="resourceGroupName">Resource group</param>
        /// <param name="serverName">Server name</param>
        /// <param name="databaseName">Database name</param>
        /// <param name="clientRequestId">Request id</param>
        /// <returns>List of all recommended indexes for specified server</returns>
        public List <IndexRecommendation> ListRecommendedIndexes(string resourceGroupName, string serverName, string databaseName, string clientRequestId)
        {
            var databases = new List <Management.Sql.LegacySdk.Models.Database>();

            var recommendedIndexes = new List <IndexRecommendation>();

            if (string.IsNullOrEmpty(databaseName))
            {
                var response = GetCurrentSqlClient(clientRequestId).Databases.ListExpanded(resourceGroupName, serverName, Expand);
                databases.AddRange(response.Databases);
            }
            else
            {
                var response = GetCurrentSqlClient(clientRequestId).Databases.GetExpanded(resourceGroupName, serverName, databaseName, Expand);
                databases.Add(response.Database);
            }

            foreach (var database in databases)
            {
                foreach (var schema in database.Properties.Schemas)
                {
                    foreach (var table in schema.Properties.Tables)
                    {
                        foreach (var recommended in table.Properties.RecommendedIndexes)
                        {
                            var recommendation = new IndexRecommendation(recommended.Properties);
                            recommendation.DatabaseName = database.Name;
                            recommendation.Name         = recommended.Name;
                            recommendedIndexes.Add(recommendation);
                        }
                    }
                }
            }
            return(recommendedIndexes);
        }
 /// <summary>
 /// Update model state
 /// </summary>
 /// <param name="recommendation">Update recommendation state</param>
 /// <returns>Recommendation with updated state</returns>
 protected override IndexRecommendation ApplyUserInputToModel(IndexRecommendation recommendation)
 {
     if (recommendation.State == IndexState.Active || recommendation.State == IndexState.Error)
     {
         recommendation.State = IndexState.Pending;
         return(recommendation);
     }
     else
     {
         throw new Exception("Index operation can only start if recommendation is currently in 'Active' or 'Error' state (retry operation index).");
     }
 }
 /// <summary>
 /// Update model state
 /// </summary>
 /// <param name="recommendation">Update recommendation state</param>
 /// <returns>Recommendation with updated state</returns>
 protected override IndexRecommendation ApplyUserInputToModel(IndexRecommendation recommendation)
 {
     if (recommendation.State == IndexState.Pending)
     {
         recommendation.State = IndexState.Active;
         return(recommendation);
     }
     else if (recommendation.State == IndexState.PendingRevert)
     {
         recommendation.State = IndexState.RevertCanceled;
         return(recommendation);
     }
     else
     {
         throw new Exception("Index operation can only be stopped if recommendation is currently in 'Pending' or 'Pending Revert' state");
     }
 }
 /// <summary>
 /// Update index recommendation state.
 /// </summary>
 /// <param name="resourceGroupName">Resource group name</param>
 /// <param name="serverName">Server name</param>
 /// <param name="indexRecommendation">Index recommendation</param>
 public void UpdateRecommendationState(string resourceGroupName, string serverName, IndexRecommendation indexRecommendation)
 {
     Communicator.UpdateRecommendedIndexState(resourceGroupName, serverName, indexRecommendation.DatabaseName, indexRecommendation.Schema, indexRecommendation.Table, indexRecommendation.Name, indexRecommendation.State);
 }