From 66d9cf231a8a47c72b9508c59ecf84672513790b Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Thu, 9 Nov 2017 15:23:21 +0000 Subject: [PATCH] Support for floating IP's. Fixes #273 --- azurerm/resource_arm_loadbalancer_nat_rule.go | 49 ++++++---- ...resource_arm_loadbalancer_nat_rule_test.go | 96 +++++++++++++++++++ .../r/loadbalancer_nat_rule.html.markdown | 1 + 3 files changed, 129 insertions(+), 17 deletions(-) diff --git a/azurerm/resource_arm_loadbalancer_nat_rule.go b/azurerm/resource_arm_loadbalancer_nat_rule.go index d56929b19c5c..748f99885a89 100644 --- a/azurerm/resource_arm_loadbalancer_nat_rule.go +++ b/azurerm/resource_arm_loadbalancer_nat_rule.go @@ -46,6 +46,12 @@ func resourceArmLoadBalancerNatRule() *schema.Resource { DiffSuppressFunc: ignoreCaseDiffSuppressFunc, }, + "enable_floating_ip": { + Type: schema.TypeBool, + Optional: true, + Computed: true, + }, + "frontend_port": { Type: schema.TypeInt, Required: true, @@ -113,8 +119,8 @@ func resourceArmLoadBalancerNatRuleCreate(d *schema.ResourceData, meta interface return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err) } - _, error := lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{})) - err = <-error + _, createError := lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{})) + err = <-createError if err != nil { return errwrap.Wrapf("Error Creating / Updating LoadBalancer {{err}}", err) } @@ -180,22 +186,26 @@ func resourceArmLoadBalancerNatRuleRead(d *schema.ResourceData, meta interface{} d.Set("name", config.Name) d.Set("resource_group_name", id.ResourceGroup) - d.Set("protocol", config.InboundNatRulePropertiesFormat.Protocol) - d.Set("frontend_port", config.InboundNatRulePropertiesFormat.FrontendPort) - d.Set("backend_port", config.InboundNatRulePropertiesFormat.BackendPort) - - if config.InboundNatRulePropertiesFormat.FrontendIPConfiguration != nil { - fipID, err := parseAzureResourceID(*config.InboundNatRulePropertiesFormat.FrontendIPConfiguration.ID) - if err != nil { - return err - } - d.Set("frontend_ip_configuration_name", fipID.Path["frontendIPConfigurations"]) - d.Set("frontend_ip_configuration_id", config.InboundNatRulePropertiesFormat.FrontendIPConfiguration.ID) - } + if props := config.InboundNatRulePropertiesFormat; props != nil { + d.Set("protocol", props.Protocol) + d.Set("frontend_port", props.FrontendPort) + d.Set("backend_port", props.BackendPort) + d.Set("enable_floating_ip", props.EnableFloatingIP) - if config.InboundNatRulePropertiesFormat.BackendIPConfiguration != nil { - d.Set("backend_ip_configuration_id", config.InboundNatRulePropertiesFormat.BackendIPConfiguration.ID) + if ipconfiguration := props.FrontendIPConfiguration; ipconfiguration != nil { + fipID, err := parseAzureResourceID(*ipconfiguration.ID) + if err != nil { + return err + } + + d.Set("frontend_ip_configuration_name", fipID.Path["frontendIPConfigurations"]) + d.Set("frontend_ip_configuration_id", ipconfiguration.ID) + } + + if ipconfiguration := props.BackendIPConfiguration; ipconfiguration != nil { + d.Set("backend_ip_configuration_id", ipconfiguration.ID) + } } return nil @@ -243,7 +253,7 @@ func resourceArmLoadBalancerNatRuleDelete(d *schema.ResourceData, meta interface return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err) } if read.ID == nil { - return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup) + return fmt.Errorf("Cannot read LoadBalancer %q (resource group %q) ID", loadBalancerName, resGroup) } return nil @@ -257,6 +267,11 @@ func expandAzureRmLoadBalancerNatRule(d *schema.ResourceData, lb *network.LoadBa BackendPort: utils.Int32(int32(d.Get("backend_port").(int))), } + if v, ok := d.GetOk("enable_floating_ip"); ok { + enableFloatingIP := v.(bool) + properties.EnableFloatingIP = utils.Bool(enableFloatingIP) + } + if v := d.Get("frontend_ip_configuration_name").(string); v != "" { rule, _, exists := findLoadBalancerFrontEndIpConfigurationByName(lb, v) if !exists { diff --git a/azurerm/resource_arm_loadbalancer_nat_rule_test.go b/azurerm/resource_arm_loadbalancer_nat_rule_test.go index 2f9933136fe9..03d6449559ff 100644 --- a/azurerm/resource_arm_loadbalancer_nat_rule_test.go +++ b/azurerm/resource_arm_loadbalancer_nat_rule_test.go @@ -159,6 +159,64 @@ func TestAccAzureRMLoadBalancerNatRule_disappears(t *testing.T) { }) } +func TestAccAzureRMLoadBalancerNatRule_enableFloatingIP(t *testing.T) { + var lb network.LoadBalancer + ri := acctest.RandInt() + natRuleName := fmt.Sprintf("NatRule-%d", ri) + location := testLocation() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMLoadBalancerDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMLoadBalancerNatRule_enableFloatingIP(ri, natRuleName, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), + testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb), + ), + }, + }, + }) +} + +func TestAccAzureRMLoadBalancerNatRule_disableFloatingIP(t *testing.T) { + var lb network.LoadBalancer + ri := acctest.RandInt() + natRuleName := fmt.Sprintf("NatRule-%d", ri) + location := testLocation() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMLoadBalancerDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), + testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb), + ), + }, + { + Config: testAccAzureRMLoadBalancerNatRule_enableFloatingIP(ri, natRuleName, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), + testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb), + ), + }, + { + Config: testAccAzureRMLoadBalancerNatRule_basic(ri, natRuleName, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), + testCheckAzureRMLoadBalancerNatRuleExists(natRuleName, &lb), + ), + }, + }, + }) +} + func testCheckAzureRMLoadBalancerNatRuleExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc { return func(s *terraform.State) error { _, _, exists := findLoadBalancerNatRuleByName(lb, natRuleName) @@ -372,3 +430,41 @@ resource "azurerm_lb_nat_rule" "test2" { } `, rInt, location, rInt, rInt, rInt, natRuleName, rInt, natRule2Name, rInt) } + +func testAccAzureRMLoadBalancerNatRule_enableFloatingIP(rInt int, natRuleName string, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestrg-%d" + location = "%s" +} + +resource "azurerm_public_ip" "test" { + name = "test-ip-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + public_ip_address_allocation = "static" +} + +resource "azurerm_lb" "test" { + name = "arm-test-loadbalancer-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + frontend_ip_configuration { + name = "one-%d" + public_ip_address_id = "${azurerm_public_ip.test.id}" + } +} + +resource "azurerm_lb_nat_rule" "test" { + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + loadbalancer_id = "${azurerm_lb.test.id}" + name = "%s" + protocol = "Tcp" + frontend_port = 3389 + backend_port = 3389 + frontend_ip_configuration_name = "one-%d" +} +`, rInt, location, rInt, rInt, rInt, natRuleName, rInt) +} diff --git a/website/docs/r/loadbalancer_nat_rule.html.markdown b/website/docs/r/loadbalancer_nat_rule.html.markdown index 4d8d7e4fb2b5..7bed9d2f90ca 100644 --- a/website/docs/r/loadbalancer_nat_rule.html.markdown +++ b/website/docs/r/loadbalancer_nat_rule.html.markdown @@ -60,6 +60,7 @@ The following arguments are supported: * `protocol` - (Required) The transport protocol for the external endpoint. Possible values are `Udp` or `Tcp`. * `frontend_port` - (Required) The port for the external endpoint. Port numbers for each Rule must be unique within the Load Balancer. Possible values range between 1 and 65534, inclusive. * `backend_port` - (Required) The port used for internal connections on the endpoint. Possible values range between 1 and 65535, inclusive. +* `enable_floating_ip` - (Optional) Enables the Floating IP Capacity, required to configure a SQL AlwaysOn Availability Group. ## Attributes Reference