/// <summary>
 /// Create or update a storage insight.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.OperationalInsights.IStorageInsightOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The resource group name of the storage insight.
 /// </param>
 /// <param name='workspaceName'>
 /// Required. The name of the parent workspace that will contain the
 /// storage insight
 /// </param>
 /// <param name='parameters'>
 /// Required. The parameters required to create or update a storage
 /// insight.
 /// </param>
 /// <returns>
 /// The create or update storage insight operation response.
 /// </returns>
 public static StorageInsightCreateOrUpdateResponse CreateOrUpdate(this IStorageInsightOperations operations, string resourceGroupName, string workspaceName, StorageInsightCreateOrUpdateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IStorageInsightOperations)s).CreateOrUpdateAsync(resourceGroupName, workspaceName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        public void CanCreateUpdateDeleteStorageInsight()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

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

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetOperationalInsightsManagementClient(handler);

                string resourceGroupName = TestUtilities.GenerateName("OIHyak");
                var resourceGroup = TestHelper.CreateResourceGroup(resourceGroupName, resourceClient);

                // Create a workspace that will house the storage insights
                string workspaceName = TestUtilities.GenerateName("AzTest");
                var workspaceCreateParameters = new WorkspaceCreateOrUpdateParameters
                {
                    Workspace =
                        new Workspace
                        {
                            Name = workspaceName,
                            Location = resourceGroup.Location
                        }
                };

                var workspaceCreateResponse = client.Workspaces.CreateOrUpdate(resourceGroupName, workspaceCreateParameters);
                Assert.True(HttpStatusCode.Created == workspaceCreateResponse.StatusCode || HttpStatusCode.OK == workspaceCreateResponse.StatusCode);

                // Create a storage insight
                string storageInsightName = TestUtilities.GenerateName("AzTestSI");
                string storageAccountName = TestUtilities.GenerateName("AzTestFakeSA");
                var createParameters = new StorageInsightCreateOrUpdateParameters
                {
                    StorageInsight = new StorageInsight
                    {
                        Location = resourceGroup.Location,
                        Name = storageInsightName,
                        Properties = new StorageInsightProperties
                        {
                            Tables = new[] { "WADWindowsEventLogsTable", "LinuxSyslogVer2v0" },
                            Containers = new[] { "wad-iis-logfiles" },
                            StorageAccount =
                                new StorageAccount
                                {
                                    Id = string.Format(StorageAccountIdFormat, client.Credentials.SubscriptionId, resourceGroupName, storageAccountName),
                                    Key = "1234"
                                }
                        }
                    }
                };
                var createResponse = client.StorageInsights.CreateOrUpdate(resourceGroupName, workspaceName, createParameters);
                Assert.True(HttpStatusCode.Created == createResponse.StatusCode || HttpStatusCode.OK == createResponse.StatusCode);
                TestHelper.ValidateStorageInsight(createParameters.StorageInsight, createResponse.StorageInsight);

                // Get the storage insight
                var getResponse = client.StorageInsights.Get(resourceGroupName, workspaceName, storageInsightName);
                Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
                TestHelper.ValidateStorageInsight(createParameters.StorageInsight, getResponse.StorageInsight);

                // Create a second storage insight for list testing
                var storageInsightNameTwo = TestUtilities.GenerateName("AzTestSI");
                createParameters.StorageInsight.Name = storageInsightNameTwo;
                createParameters.StorageInsight.Properties.Containers = null;
                createResponse = client.StorageInsights.CreateOrUpdate(resourceGroupName, workspaceName, createParameters);
                Assert.True(HttpStatusCode.Created == createResponse.StatusCode || HttpStatusCode.OK == createResponse.StatusCode);
                TestHelper.ValidateStorageInsight(createParameters.StorageInsight, createResponse.StorageInsight);

                // List the storage insights in the workspace
                var listResponse = client.StorageInsights.ListInWorkspace(resourceGroupName, workspaceName);
                Assert.True(HttpStatusCode.OK == listResponse.StatusCode);
                Assert.Equal(2, listResponse.StorageInsights.Count);
                Assert.Null(listResponse.NextLink);
                Assert.Single(listResponse.StorageInsights.Where(w => w.Name.Equals(storageInsightName, StringComparison.OrdinalIgnoreCase)));
                Assert.Single(listResponse.StorageInsights.Where(w => w.Name.Equals(storageInsightNameTwo, StringComparison.OrdinalIgnoreCase)));

                // Perform an update on one of the storage insights
                createResponse.StorageInsight.Properties.StorageAccount.Key = "9876";
                createResponse.StorageInsight.Properties.Tables = new[] { "WADWindowsEventLogsTable" };
                createResponse.StorageInsight.Properties.Containers = new[] { "wad-iis-logfiles" };
                var updateParameters = new StorageInsightCreateOrUpdateParameters { StorageInsight= createResponse.StorageInsight };
                var updateResponse = client.StorageInsights.CreateOrUpdate(resourceGroupName, workspaceName, updateParameters);
                Assert.True(HttpStatusCode.OK == updateResponse.StatusCode);
                TestHelper.ValidateStorageInsight(updateParameters.StorageInsight, updateResponse.StorageInsight);

                // Delete a storage insight
                var deleteResponse = client.StorageInsights.Delete(resourceGroupName, workspaceName, storageInsightName);
                Assert.Equal(HttpStatusCode.OK, deleteResponse.StatusCode);

                // Verify the storageinsight is gone
                TestHelper.VerifyCloudException(HttpStatusCode.NotFound, () => client.StorageInsights.Get(resourceGroupName, workspaceName, storageInsightName));
            }
        }
        public void StorageInsightCreateFailsWithoutWorkspace()
        {
            BasicDelegatingHandler handler = new BasicDelegatingHandler();

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

                var resourceClient = TestHelper.GetResourceClient(handler);
                var client = TestHelper.GetOperationalInsightsManagementClient(handler);

                string resourceGroupName = TestUtilities.GenerateName("OIHyak");
                var resourceGroup = TestHelper.CreateResourceGroup(resourceGroupName, resourceClient);

                // Create a storage insight on a non-existent workspace
                string storageInsightName = TestUtilities.GenerateName("AzTestSI");
                string storageAccountName = TestUtilities.GenerateName("AzTestFakeSA");
                string workspaceName = TestUtilities.GenerateName("AzTest");
                var createParameters = new StorageInsightCreateOrUpdateParameters
                {
                    StorageInsight = new StorageInsight
                    {
                        Location = resourceGroup.Location,
                        Name = storageInsightName,
                        Properties = new StorageInsightProperties
                        {
                            StorageAccount =
                                new StorageAccount
                                {
                                    Id = string.Format(StorageAccountIdFormat, client.Credentials.SubscriptionId, resourceGroupName, storageAccountName),
                                    Key = "1234"
                                }
                        }
                    }
                };

                TestHelper.VerifyCloudException(HttpStatusCode.NotFound, () => client.StorageInsights.CreateOrUpdate(resourceGroupName, workspaceName, createParameters));
            }
        }
 /// <summary>
 /// Create or update a storage insight.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.OperationalInsights.IStorageInsightOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The resource group name of the storage insight.
 /// </param>
 /// <param name='workspaceName'>
 /// Required. The name of the parent workspace that will contain the
 /// storage insight
 /// </param>
 /// <param name='parameters'>
 /// Required. The parameters required to create or update a storage
 /// insight.
 /// </param>
 /// <returns>
 /// The create or update storage insight operation response.
 /// </returns>
 public static Task<StorageInsightCreateOrUpdateResponse> CreateOrUpdateAsync(this IStorageInsightOperations operations, string resourceGroupName, string workspaceName, StorageInsightCreateOrUpdateParameters parameters)
 {
     return operations.CreateOrUpdateAsync(resourceGroupName, workspaceName, parameters, CancellationToken.None);
 }