Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_lb_nat_rule: support for floating IP's #542

Merged
merged 1 commit into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions azurerm/resource_arm_loadbalancer_nat_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
96 changes: 96 additions & 0 deletions azurerm/resource_arm_loadbalancer_nat_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions website/docs/r/loadbalancer_nat_rule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down