Ejemplo n.º 1
0
        public async Task <FirewallRuleResponse> CreateFirewallRuleAsync(
            IAzureResourceManagementSession azureResourceManagementSession,
            IAzureSqlServerResource azureSqlServer,
            FirewallRuleRequest firewallRuleRequest)
        {
            CommonUtil.CheckForNull(azureResourceManagementSession, "azureResourceManagerSession");
            CommonUtil.CheckForNull(firewallRuleRequest, "firewallRuleRequest");
            CommonUtil.CheckForNull(azureSqlServer, "azureSqlServer");

            try
            {
                AzureResourceManagementSession vsAzureResourceManagementSession = azureResourceManagementSession as AzureResourceManagementSession;

                if (vsAzureResourceManagementSession != null)
                {
                    var firewallRule = new RestFirewallRule()
                    {
                        EndIpAddress   = firewallRuleRequest.EndIpAddress.ToString(),
                        StartIpAddress = firewallRuleRequest.StartIpAddress.ToString()
                    };
                    IFirewallRulesOperations firewallRuleOperations = vsAzureResourceManagementSession.SqlManagementClient.FirewallRules;
                    var firewallRuleResponse = await ExecuteCloudRequest(
                        () => firewallRuleOperations.CreateOrUpdateWithHttpMessagesAsync(
                            azureSqlServer.ResourceGroupName ?? string.Empty,
                            azureSqlServer.Name,
                            firewallRuleRequest.FirewallRuleName,
                            firewallRule,
                            GetCustomHeaders()),
                        SR.FirewallRuleCreationFailedWithError);

                    var response = firewallRuleResponse.Body;
                    return(new FirewallRuleResponse()
                    {
                        StartIpAddress = response.StartIpAddress,
                        EndIpAddress = response.EndIpAddress,
                        Created = true
                    });
                }
                // else respond with failure case
                return(new FirewallRuleResponse()
                {
                    Created = false
                });
            }
            catch (Exception ex)
            {
                TraceException(TraceEventType.Error, (int)TraceId.AzureResource, ex, "Failed to create firewall rule");
                throw;
            }
        }
 /// <summary>
 /// Verifies that the FirewallRule values match the expected values
 /// </summary>
 /// <param name="firewallRuleName">The expected rule name</param>
 /// <param name="startIp1">The expected start IP address</param>
 /// <param name="endIp1">The expected end IP address</param>
 /// <param name="rule">The actual rule information</param>
 private static void VerifyFirewallRuleInformation(string firewallRuleName, string startIp1, string endIp1, FirewallRule rule)
 {
     Assert.Equal(firewallRuleName, rule.Name);
     Assert.Equal(startIp1, rule.Properties.StartIpAddress);
     Assert.Equal(endIp1, rule.Properties.EndIpAddress);
 }