/// <summary>
 /// Creates or updates a saved search for a given workspace.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.OperationalInsights.ISearchOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The resource group name of the workspace.
 /// </param>
 /// <param name='workspaceName'>
 /// Required. A unique workspace instance name.
 /// </param>
 /// <param name='savedSearchId'>
 /// Required. The id of the saved search.
 /// </param>
 /// <param name='parameters'>
 /// Required. The parameters required to save a search.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static AzureOperationResponse CreateOrUpdateSavedSearch(this ISearchOperations operations, string resourceGroupName, string workspaceName, string savedSearchId, SearchCreateOrUpdateSavedSearchParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((ISearchOperations)s).CreateOrUpdateSavedSearchAsync(resourceGroupName, workspaceName, savedSearchId, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        public void CanCreateOrUpdateAndDeleteSavedSearches()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();

                var client = TestHelper.GetOperationalInsightsManagementClient(handler);

                string resourceGroupName = "mms-eus";
                string workspaceName = "workspace-861bd466-5400-44be-9552-5ba40823c3aa";
                string newSavedSearchId = "test-new-saved-search-id-2015";

                SearchCreateOrUpdateSavedSearchParameters parameters = new SearchCreateOrUpdateSavedSearchParameters();
                parameters.Properties = new SavedSearchProperties();
                parameters.Properties.Version = 1;
                parameters.Properties.Query = "* | measure Count() by Computer";
                parameters.Properties.DisplayName = "Create or Update Saved Search Test";
                parameters.Properties.Category = " Saved Search Test Category";
                parameters.Properties.Tags = new List<Tag>() { new Tag() { Name = "Group", Value = "Computer" } };

                var result = client.Search.ListSavedSearches(resourceGroupName, workspaceName);

                var createSavedSearchResults = client.Search.CreateOrUpdateSavedSearch(
                    resourceGroupName,
                    workspaceName,
                    newSavedSearchId,
                    parameters);
                Assert.NotNull(createSavedSearchResults);

                // Verify that the saved search was saved
                var savedSearchesResult = client.Search.ListSavedSearches(resourceGroupName, workspaceName);
                Assert.NotNull(savedSearchesResult);
                Assert.NotNull(savedSearchesResult.Value);
                Assert.NotEqual(savedSearchesResult.Value.Count, 0);
                Assert.NotNull(savedSearchesResult.Value[0].Id);
                Assert.NotNull(savedSearchesResult.Value[0].Properties);
                bool foundSavedSearch = false;
                for (int i = 0; i < savedSearchesResult.Value.Count; i++)
                {
                    SavedSearchProperties properties = savedSearchesResult.Value[i].Properties;
                    if (properties.Category.Equals(parameters.Properties.Category)
                        && properties.Version == parameters.Properties.Version
                        && properties.Query.Equals(parameters.Properties.Query)
                        && properties.DisplayName.Equals(parameters.Properties.DisplayName)
                        && properties.Tags[0].Name.Equals(parameters.Properties.Tags[0].Name)
                        && properties.Tags[0].Value.Equals(parameters.Properties.Tags[0].Value))
                    {
                        foundSavedSearch = true;
                        parameters.ETag = savedSearchesResult.Value[i].ETag;
                    }
                }
                Assert.True(foundSavedSearch);

                // Test updating a saved search
                parameters.Properties.Query = "*";
                parameters.Properties.Tags = new List<Tag>() { new Tag() { Name = "Source", Value = "Test2" } };
                var updateSavedSearchResults = client.Search.CreateOrUpdateSavedSearch(
                    resourceGroupName,
                    workspaceName,
                    newSavedSearchId,
                    parameters);
                Assert.NotNull(updateSavedSearchResults);
                // Verify that the saved search was saved
                savedSearchesResult = client.Search.ListSavedSearches(resourceGroupName, workspaceName);
                Assert.NotNull(savedSearchesResult);
                Assert.NotNull(savedSearchesResult.Value);
                Assert.NotEqual(savedSearchesResult.Value.Count, 0);
                Assert.NotNull(savedSearchesResult.Value[0].Id);
                Assert.NotNull(savedSearchesResult.Value[0].Properties);
                foundSavedSearch = false;
                for (int i = 0; i < savedSearchesResult.Value.Count; i++)
                {
                    SavedSearchProperties properties = savedSearchesResult.Value[i].Properties;
                    if (properties.Category.Equals(parameters.Properties.Category)
                        && properties.Version == parameters.Properties.Version
                        && properties.Query.Equals(parameters.Properties.Query)
                        && properties.DisplayName.Equals(parameters.Properties.DisplayName)
                        && properties.Tags[0].Name.Equals(parameters.Properties.Tags[0].Name)
                        && properties.Tags[0].Value.Equals(parameters.Properties.Tags[0].Value))
                    {
                        foundSavedSearch = true;
                    }
                }
                Assert.True(foundSavedSearch);



                // Test the function to delete a saved search
                client.Search.DeleteSavedSearch(resourceGroupName, workspaceName, newSavedSearchId);
                savedSearchesResult = client.Search.ListSavedSearches(resourceGroupName, workspaceName);

                foundSavedSearch = false;
                for (int i = 0; i < savedSearchesResult.Value.Count; i++)
                {
                    SavedSearchProperties properties = savedSearchesResult.Value[i].Properties;
                    if (properties.Category.Equals(parameters.Properties.Category)
                        && properties.Version == parameters.Properties.Version
                        && properties.Query.Equals(parameters.Properties.Query)
                        && properties.DisplayName.Equals(parameters.Properties.DisplayName))
                    {
                        foundSavedSearch = true;
                    }
                }
                Assert.False(foundSavedSearch);
            }
        }
        public virtual HttpStatusCode CreateOrUpdateSavedSearch(string resourceGroupName, string workspaceName, string savedSearchId, SavedSearchProperties properties, bool force, Action<bool, string, string, string, Action> ConfirmAction, string ETag = null)
        {
            HttpStatusCode status = HttpStatusCode.Ambiguous;
            Action createSavedSearch = () =>
                {
                    SearchCreateOrUpdateSavedSearchParameters searchParameters = new SearchCreateOrUpdateSavedSearchParameters();
                    if (ETag != null && ETag != "")
                    {
                        searchParameters.ETag = ETag;
                    }
                    searchParameters.Properties = properties;
                    AzureOperationResponse response = OperationalInsightsManagementClient.Search.CreateOrUpdateSavedSearch(resourceGroupName, workspaceName, savedSearchId, searchParameters);
                    status = response.StatusCode;
                };
            if (force)
            {
                createSavedSearch();
            }
            else
            {
                bool savedSearchExists = CheckSavedSearchExists(resourceGroupName, workspaceName, savedSearchId);

                ConfirmAction(
                    !savedSearchExists,    // prompt only if the saved search exists
                    string.Format(
                        CultureInfo.InvariantCulture,
                        Resources.SavedSearchExists,
                        savedSearchId,
                        workspaceName),
                    string.Format(
                        CultureInfo.InvariantCulture,
                        Resources.SavedSearchCreating,
                        savedSearchId,
                        workspaceName),
                    savedSearchId,
                    createSavedSearch);
            }
            return status;
        }
 /// <summary>
 /// Creates or updates a saved search for a given workspace.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.OperationalInsights.ISearchOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The resource group name of the workspace.
 /// </param>
 /// <param name='workspaceName'>
 /// Required. A unique workspace instance name.
 /// </param>
 /// <param name='savedSearchId'>
 /// Required. The id of the saved search.
 /// </param>
 /// <param name='parameters'>
 /// Required. The parameters required to save a search.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static Task<AzureOperationResponse> CreateOrUpdateSavedSearchAsync(this ISearchOperations operations, string resourceGroupName, string workspaceName, string savedSearchId, SearchCreateOrUpdateSavedSearchParameters parameters)
 {
     return operations.CreateOrUpdateSavedSearchAsync(resourceGroupName, workspaceName, savedSearchId, parameters, CancellationToken.None);
 }