/// <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>
 /// <returns>The communication model object</returns>
 private ServerAuditingPolicyCreateOrUpdateParameters PolicizeServerAuditingModel(ServerAuditingPolicyModel model)
 {
     ServerAuditingPolicyCreateOrUpdateParameters updateParameters = new ServerAuditingPolicyCreateOrUpdateParameters();
     ServerAuditingPolicyProperties properties = new ServerAuditingPolicyProperties();
     updateParameters.Properties = properties;
     properties.AuditingState = PolicizeAuditState(model.AuditState);
     properties.StorageAccountName = ExtractStorageAccountName(model);
     properties.StorageAccountResourceGroupName = ExtractStorageAccountResourceGroup(properties.StorageAccountName);
     properties.StorageAccountSubscriptionId = ExtractStorageAccountSubscriptionId(properties.StorageAccountName);
     properties.StorageTableEndpoint = ExtractStorageAccountTableEndpoint(properties.StorageAccountName);
     properties.StorageAccountKey = ExtractStorageAccountKey(properties.StorageAccountName, model, properties.StorageAccountResourceGroupName, StorageKeyKind.Primary);
     properties.StorageAccountSecondaryKey = ExtractStorageAccountKey(properties.StorageAccountName, model, properties.StorageAccountResourceGroupName, StorageKeyKind.Secondary);
     properties.EventTypesToAudit = ExtractEventTypes(model);
     properties.RetentionDays = model.RetentionInDays.ToString();
     properties.AuditLogsTableName = model.TableIdentifier;
     return updateParameters;
 }
 /// <summary>
 /// Creates or updates an Azure SQL Database Server auditing policy.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Sql.IAuditingPolicyOperations.
 /// </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 auditing policy.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static Task<AzureOperationResponse> CreateOrUpdateServerPolicyAsync(this IAuditingPolicyOperations operations, string resourceGroupName, string serverName, ServerAuditingPolicyCreateOrUpdateParameters parameters)
 {
     return operations.CreateOrUpdateServerPolicyAsync(resourceGroupName, serverName, parameters, CancellationToken.None);
 }
 /// <summary>
 /// Sets the database server auditing policy of the given database server in the given resource group
 /// </summary>
 public void SetServerAuditingPolicy(string resourceGroupName, string serverName,  string clientRequestId, ServerAuditingPolicyCreateOrUpdateParameters parameters)
 {
     IAuditingPolicyOperations operations = GetCurrentSqlClient(clientRequestId).AuditingPolicy;
     operations.CreateOrUpdateServerPolicy(resourceGroupName, serverName, parameters);
 }
 /// <summary>
 /// Creates or updates an Azure SQL Database Server auditing policy.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Sql.IAuditingPolicyOperations.
 /// </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 auditing policy.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static AzureOperationResponse CreateOrUpdateServerPolicy(this IAuditingPolicyOperations operations, string resourceGroupName, string serverName, ServerAuditingPolicyCreateOrUpdateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IAuditingPolicyOperations)s).CreateOrUpdateServerPolicyAsync(resourceGroupName, serverName, 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 server's 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 void TestServerAuditingAPIs(SqlManagementClient sqlClient, string resourceGroupName, Server server)
        {
            ServerAuditingPolicyGetResponse getDefaultServerPolicyResponse = sqlClient.AuditingPolicy.GetServerPolicy(resourceGroupName, server.Name);
            ServerAuditingPolicyProperties properties = getDefaultServerPolicyResponse.AuditingPolicy.Properties;

            // Verify that the initial Get request contains the default policy.
            TestUtilities.ValidateOperationResponse(getDefaultServerPolicyResponse, HttpStatusCode.OK);
            VerifyServerAuditingPolicyInformation(GetDefaultServerAuditProperties(), properties);

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

            var updateResponse = sqlClient.AuditingPolicy.CreateOrUpdateServerPolicy(resourceGroupName, server.Name, updateParams);

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

            ServerAuditingPolicyGetResponse getUpdatedPolicyResponse = sqlClient.AuditingPolicy.GetServerPolicy(resourceGroupName, server.Name);
            ServerAuditingPolicyProperties updatedProperties = getUpdatedPolicyResponse.AuditingPolicy.Properties;

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