/// <summary>
 /// Adds a new server-level Firewall Rule for an Azure SQL Database
 /// Server.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Sql.IFirewallRuleOperations.
 /// </param>
 /// <param name='serverName'>
 /// Required. The name of the Azure SQL Database Server to which this
 /// rule will be applied.
 /// </param>
 /// <param name='parameters'>
 /// Required. The parameters for the Create Firewall Rule operation.
 /// </param>
 /// <returns>
 /// Contains the response to a Create Firewall Rule operation.
 /// </returns>
 public static FirewallRuleCreateResponse Create(this IFirewallRuleOperations operations, string serverName, FirewallRuleCreateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IFirewallRuleOperations)s).CreateAsync(serverName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Adds a new server-level firewall rule for a SQL Database server
 /// that belongs to a subscription.  (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/gg715280.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Sql.IFirewallRuleOperations.
 /// </param>
 /// <param name='serverName'>
 /// The name of the SQL database server to which this rule will be
 /// applied.
 /// </param>
 /// <param name='parameters'>
 /// Parameters for the Create Firewall Rule operation.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static FirewallRuleCreateResponse Create(this IFirewallRuleOperations operations, string serverName, FirewallRuleCreateParameters parameters)
 {
     try
     {
         return operations.CreateAsync(serverName, parameters).Result;
     }
     catch (AggregateException ex)
     {
         if (ex.InnerExceptions.Count > 1)
         {
             throw;
         }
         else
         {
             throw ex.InnerException;
         }
     }
 }
 /// <summary>
 /// Adds a new server-level Firewall Rule for an Azure SQL Database
 /// Server.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Sql.IFirewallRuleOperations.
 /// </param>
 /// <param name='serverName'>
 /// Required. The name of the Azure SQL Database Server to which this
 /// rule will be applied.
 /// </param>
 /// <param name='parameters'>
 /// Required. The parameters for the Create Firewall Rule operation.
 /// </param>
 /// <returns>
 /// Contains the response to a Create Firewall Rule operation.
 /// </returns>
 public static Task<FirewallRuleCreateResponse> CreateAsync(this IFirewallRuleOperations operations, string serverName, FirewallRuleCreateParameters parameters)
 {
     return operations.CreateAsync(serverName, parameters, CancellationToken.None);
 }
        /// <summary>
        /// Checks for the existence of a specific Azure Sql Firewall rule, if it doesn't exist it will create it.
        /// </summary>
        /// <param name="client">The <see cref="SqlManagementClient"/> that is performing the operation.</param>
        /// <param name="serverName">The name of the server that we want to use to create the database.</param>
        /// <param name="parameters">The <see cref="FirewallRuleCreateParameters"/> set of parameters for the rule.</param>
        /// <returns>The async <see cref="Task"/> wrapper.</returns>
        public static async Task CreateFirewallRuleIfNotExistsAsync(this SqlManagementClient client, string serverName, FirewallRuleCreateParameters parameters)
        {
            Contract.Requires(client != null);
            Contract.Requires(!string.IsNullOrWhiteSpace(serverName));
            Contract.Requires(parameters != null);
            Contract.Requires(!string.IsNullOrWhiteSpace(parameters.Name));
            Contract.Requires(!string.IsNullOrWhiteSpace(parameters.StartIPAddress));
            Contract.Requires(!string.IsNullOrWhiteSpace(parameters.EndIPAddress));

            FirewallRuleGetResponse rule = null;
            FlexStreams.BuildEventsObserver.OnNext(new CheckIfExistsEvent(AzureResource.FirewallRule, parameters.Name));

            try
            {
                rule = await client.FirewallRules.GetAsync(serverName, parameters.Name);
            }
            catch (CloudException cex)
            {
                if (!cex.Error.Message.Contains($"Resource with the name '{parameters.Name}' does not exist")) throw;
            }

            if (rule != null)
            {
                FlexStreams.BuildEventsObserver.OnNext(new FoundExistingEvent(AzureResource.FirewallRule, parameters.Name));
                return;
            }

            await client.FirewallRules.CreateAsync(serverName, parameters);
            FlexStreams.BuildEventsObserver.OnNext(new ProvisionEvent(AzureResource.FirewallRule, parameters.Name));
        }