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

Update azurerm_lb_nat_rule with azurestasck PR review requests #1736

Merged
merged 1 commit into from
Aug 8, 2018
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
14 changes: 14 additions & 0 deletions azurerm/helpers/validate/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,17 @@ func MACAddress(i interface{}, k string) (_ []string, errors []error) {

return
}

func PortNumber(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(int)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be int", k))
return
}

if v < 0 || 65535 < v {
errors = append(errors, fmt.Errorf("%q is not a valid port number: %q", k, i))
}

return
}
51 changes: 50 additions & 1 deletion azurerm/helpers/validate/network_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package validate

import "testing"
import (
"strconv"
"testing"
)

func TestIPv4Address(t *testing.T) {
cases := []struct {
Expand Down Expand Up @@ -147,3 +150,49 @@ func TestMACAddress(t *testing.T) {
})
}
}

func TestPortNumber(t *testing.T) {
cases := []struct {
Port int
Errors int
}{
{
Port: -1,
Errors: 1,
},
{
Port: 0,
Errors: 0,
},
{
Port: 1,
Errors: 0,
},
{
Port: 8477,
Errors: 0,
},
{
Port: 65535,
Errors: 0,
},
{
Port: 65536,
Errors: 1,
},
{
Port: 7000000,
Errors: 1,
},
}

for _, tc := range cases {
t.Run(strconv.Itoa(tc.Port), func(t *testing.T) {
_, errors := PortNumber(tc.Port, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected PortNumber to return %d error(s) not %d", len(errors), tc.Errors)
}
})
}
}
53 changes: 32 additions & 21 deletions azurerm/resource_arm_loadbalancer_nat_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,52 @@ import (
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmLoadBalancerNatRule() *schema.Resource {
return &schema.Resource{
Create: resourceArmLoadBalancerNatRuleCreate,
Create: resourceArmLoadBalancerNatRuleCreateUpdate,
Read: resourceArmLoadBalancerNatRuleRead,
Update: resourceArmLoadBalancerNatRuleCreate,
Update: resourceArmLoadBalancerNatRuleCreateUpdate,
Delete: resourceArmLoadBalancerNatRuleDelete,

Importer: &schema.ResourceImporter{
State: loadBalancerSubResourceStateImporter,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},

"location": deprecatedLocationSchema(),

"resource_group_name": resourceGroupNameSchema(),

"loadbalancer_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},

"protocol": {
Type: schema.TypeString,
Required: true,
StateFunc: ignoreCaseStateFunc,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validation.StringInSlice([]string{
string(network.TransportProtocolAll),
string(network.TransportProtocolTCP),
string(network.TransportProtocolUDP),
}, true),
},

"enable_floating_ip": {
Expand All @@ -53,18 +64,21 @@ func resourceArmLoadBalancerNatRule() *schema.Resource {
},

"frontend_port": {
Type: schema.TypeInt,
Required: true,
Type: schema.TypeInt,
Required: true,
ValidateFunc: validate.PortNumber,
},

"backend_port": {
Type: schema.TypeInt,
Required: true,
Type: schema.TypeInt,
Required: true,
ValidateFunc: validate.PortNumber,
},

"frontend_ip_configuration_name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},

"frontend_ip_configuration_id": {
Expand All @@ -80,7 +94,7 @@ func resourceArmLoadBalancerNatRule() *schema.Resource {
}
}

func resourceArmLoadBalancerNatRuleCreate(d *schema.ResourceData, meta interface{}) error {
func resourceArmLoadBalancerNatRuleCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).loadBalancerClient
ctx := meta.(*ArmClient).StopContext

Expand Down Expand Up @@ -108,7 +122,7 @@ func resourceArmLoadBalancerNatRuleCreate(d *schema.ResourceData, meta interface
existingNatRule, existingNatRuleIndex, exists := findLoadBalancerNatRuleByName(loadBalancer, d.Get("name").(string))
if exists {
if d.Get("name").(string) == *existingNatRule.Name {
// this probe is being updated/reapplied remove old copy from the slice
// this nat rule is being updated/reapplied remove old copy from the slice
natRules = append(natRules[:existingNatRuleIndex], natRules[existingNatRuleIndex+1:]...)
}
}
Expand Down Expand Up @@ -278,8 +292,7 @@ func expandAzureRmLoadBalancerNatRule(d *schema.ResourceData, lb *network.LoadBa
}

if v, ok := d.GetOk("enable_floating_ip"); ok {
enableFloatingIP := v.(bool)
properties.EnableFloatingIP = utils.Bool(enableFloatingIP)
properties.EnableFloatingIP = utils.Bool(v.(bool))
}

if v := d.Get("frontend_ip_configuration_name").(string); v != "" {
Expand All @@ -288,11 +301,9 @@ func expandAzureRmLoadBalancerNatRule(d *schema.ResourceData, lb *network.LoadBa
return nil, fmt.Errorf("[ERROR] Cannot find FrontEnd IP Configuration with the name %s", v)
}

feip := network.SubResource{
properties.FrontendIPConfiguration = &network.SubResource{
ID: rule.ID,
}

properties.FrontendIPConfiguration = &feip
}

natRule := network.InboundNatRule{
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/loadbalancer_nat_rule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description: |-

# azurerm_lb_nat_rule

Create a LoadBalancer NAT Rule.
Manages a LoadBalancer NAT Rule.

~> **NOTE** When using this resource, the LoadBalancer needs to have a FrontEnd IP Configuration Attached

Expand Down Expand Up @@ -57,7 +57,7 @@ The following arguments are supported:
* `resource_group_name` - (Required) The name of the resource group in which to create the resource.
* `loadbalancer_id` - (Required) The ID of the LoadBalancer in which to create the NAT Rule.
* `frontend_ip_configuration_name` - (Required) The name of the frontend IP configuration exposing this rule.
* `protocol` - (Required) The transport protocol for the external endpoint. Possible values are `Udp` or `Tcp`.
* `protocol` - (Required) The transport protocol for the external endpoint. Possible values are `Udp`, `Tcp` or `All`.
* `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.
Expand Down