/// <summary>
 /// Creates or updates an Azure SQL Database blob auditing policy.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Sql.IBlobAuditingOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the Resource Group to which the 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 for which the auditing
 /// policy applies.
 /// </param>
 /// <param name='parameters'>
 /// Required. The required parameters for createing or updating a Azure
 /// SQL Database auditing policy.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static AzureOperationResponse CreateOrUpdateDatabasePolicy(this IBlobAuditingOperations operations, string resourceGroupName, string serverName, string databaseName, BlobAuditingCreateOrUpdateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IBlobAuditingOperations)s).CreateOrUpdateDatabasePolicyAsync(resourceGroupName, serverName, databaseName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        /// <summary>
        /// The non-boilerplated test code of the APIs for managing the lifecycle of a given database's blob auditing policy. It is meant to be called with a name of an already existing database (and therefore already existing 
        /// server and resource group). This test does not create these resources and does not remove them.
        /// </summary>
        private void TestDatabaseAuditingApis(SqlManagementClient sqlClient, string resourceGroupName, Server server, Database database)
        {
            BlobAuditingGetResponse getDefaultDatabasePolicyResponse = sqlClient.BlobAuditing.GetDatabaseBlobAuditingPolicy(resourceGroupName, server.Name, database.Name);
            var properties = getDefaultDatabasePolicyResponse.AuditingPolicy.Properties;

            // Verify that the initial Get request contains the default policy.
            TestUtilities.ValidateOperationResponse(getDefaultDatabasePolicyResponse);
            VerifyAuditingPolicyInformation(GetDefaultBlobAuditProperties(), properties);
            // Modify the policy properties, send and receive, see it its still ok
            var updateParams = new BlobAuditingCreateOrUpdateParameters { Properties = properties };

            var updateResponse = sqlClient.BlobAuditing.CreateOrUpdateDatabasePolicy(resourceGroupName, server.Name, database.Name, updateParams);

            // Verify that the initial Get request contains the default policy.
            TestUtilities.ValidateOperationResponse(updateResponse);

            var getUpdatedPolicyResponse = sqlClient.BlobAuditing.GetDatabaseBlobAuditingPolicy(resourceGroupName, server.Name, database.Name);
            var updatedProperties = getUpdatedPolicyResponse.AuditingPolicy.Properties;

            // Verify that the Get request contains the updated policy.
            TestUtilities.ValidateOperationResponse(getUpdatedPolicyResponse);
            VerifyAuditingPolicyInformation(properties, updatedProperties);
        }
 /// <summary>
 /// Sets the database server blob auditing policy of the given database server in the given resource group
 /// </summary>
 public void SetServerAuditingPolicy(string resourceGroupName, string serverName, string clientRequestId, BlobAuditingCreateOrUpdateParameters parameters)
 {
     var operations = GetCurrentSqlClient(clientRequestId).BlobAuditing;
     var statusLink =  operations.CreateOrUpdateServerPolicy(resourceGroupName, serverName, parameters).OperationStatusLink;
     for (var iterationCount = 0; iterationCount < 1800; iterationCount++) // wait for at most an hour
     {
         var status = GetServerCreateOrUpdateOperationStatus(statusLink, clientRequestId);
         if (status == OperationStatus.Succeeded)
         {
             break;
         }
         Task.Delay(2000); // wait 2 seconds between each poll
     }
 }
 /// <summary>
 /// Calls the set blob audit APIs for the database auditing policy for the given database in the given database server in the given resource group
 /// </summary>
 public void SetDatabaseAuditingPolicy(string resourceGroupName, string serverName, string databaseName, string clientRequestId, BlobAuditingCreateOrUpdateParameters parameters)
 {
     var operations = GetCurrentSqlClient(clientRequestId).BlobAuditing;
     operations.CreateOrUpdateDatabasePolicy(resourceGroupName, serverName, databaseName, parameters);
 }
 /// <summary>
 /// Creates or updates an Azure SQL Database Server blob auditing
 /// policy.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Sql.IBlobAuditingOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the Resource Group to which the server
 /// belongs.
 /// </param>
 /// <param name='serverName'>
 /// Required. The name of the Azure SQL Database Server on which the
 /// database is hosted.
 /// </param>
 /// <param name='parameters'>
 /// Required. The required parameters for createing or updating a Azure
 /// SQL Database Server blob auditing policy.
 /// </param>
 /// <returns>
 /// Response Azure Sql Server blob auditing operation.
 /// </returns>
 public static Task<ServerBlobAuditingResponse> CreateOrUpdateServerPolicyAsync(this IBlobAuditingOperations operations, string resourceGroupName, string serverName, BlobAuditingCreateOrUpdateParameters parameters)
 {
     return operations.CreateOrUpdateServerPolicyAsync(resourceGroupName, serverName, parameters, CancellationToken.None);
 }
        /// <summary>
        /// Takes the cmdlets model object and transform it to the policy as expected by the endpoint
        /// </summary>
        /// <param name="model">The AuditingPolicy model object</param>
        /// <param name="storageEndpointSuffix">The suffix of the storage endpoint</param>
        /// <returns>The communication model object</returns>
        private BlobAuditingCreateOrUpdateParameters PolicizeBlobAuditingModel(BaseBlobAuditingPolicyModel model, string storageEndpointSuffix)
        {
            var updateParameters = new BlobAuditingCreateOrUpdateParameters();
            var properties = new BlobAuditingProperties();
            updateParameters.Properties = properties;
            properties.State = model.AuditState.ToString();
            if (!IgnoreStorage)
            {
                properties.StorageEndpoint = ExtractStorageAccountName(model, storageEndpointSuffix);
                properties.StorageAccountAccessKey = ExtractStorageAccountKey(model.StorageAccountName);
            }
            properties.AuditActionsAndGroups = ExtractAuditActionsAndGroups(model);
            properties.RetentionDays = (int) model.RetentionInDays;

            return updateParameters;
        }
        /// <summary>
        /// The non-boilerplated test code of the APIs for managing the lifecycle of a given server's blob auditing policy. It is meant to be called with a name of an already existing server (and therefore already existing 
        /// resource group). This test does not create these resources and does not remove them.
        /// </summary>
        private async void TestServerAuditingApis(SqlManagementClient sqlClient, string resourceGroupName, Server server)
        {
            var getDefaultServerPolicyResponse = sqlClient.BlobAuditing.GetServerPolicy(resourceGroupName, server.Name);
            var properties = getDefaultServerPolicyResponse.AuditingPolicy.Properties;

            // Verify that the initial Get request contains the default policy.
            TestUtilities.ValidateOperationResponse(getDefaultServerPolicyResponse);
            VerifyAuditingPolicyInformation(GetDefaultBlobAuditProperties(), properties);

            // Modify the policy properties, send and receive, see it its still ok
            ChangeBlobAuditPolicy(properties);
            var updateParams = new BlobAuditingCreateOrUpdateParameters { Properties = properties };

            var updateResponse = sqlClient.BlobAuditing.CreateOrUpdateServerPolicy(resourceGroupName, server.Name, updateParams);
            var succeededInUpated = false;
            // Verify that the initial Get request of contains the default policy.
            TestUtilities.ValidateOperationResponse(updateResponse, HttpStatusCode.Accepted);
            for (var iterationCount = 0; iterationCount < 10; iterationCount++) // at most 10 iterations, each means wait of 30 seconds
            {
                var blobAuditStatusResponse = sqlClient.BlobAuditing.GetOperationStatus(updateResponse.OperationStatusLink);
                var blobAuditingOperationResult = blobAuditStatusResponse.OperationResult.Properties;
                if (blobAuditingOperationResult.State == OperationStatus.Succeeded)
                {
                    succeededInUpated = true;
                    break;
                }
                if (HttpMockServer.Mode != HttpRecorderMode.Playback)
                {
                    await Task.Delay(30000);    
                }                                  
            }

            Assert.True(succeededInUpated, "Failed in updating the server's policy");

            var getUpdatedPolicyResponse = sqlClient.BlobAuditing.GetServerPolicy(resourceGroupName, server.Name);
            var updatedProperties = getUpdatedPolicyResponse.AuditingPolicy.Properties;

            // Verify that the Get request contains the updated policy.
            TestUtilities.ValidateOperationResponse(getUpdatedPolicyResponse);
            VerifyAuditingPolicyInformation(properties, updatedProperties);
        }