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

Nat rule #22

Merged
merged 22 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
41ce0ff
Add lb nat rule to the provider
thetonymaster Aug 3, 2018
635bf10
Add lb nat rule resource
thetonymaster Aug 3, 2018
007065d
Add documentation for lb nat rule
thetonymaster Aug 3, 2018
bd9943c
Add nat rule to sidebar
thetonymaster Aug 3, 2018
001001d
Merge branch 'loadbalancer-backend-address-pool' into nat_rule
thetonymaster Aug 3, 2018
3a6c887
Merge branch 'loadbalancer-backend-address-pool' into nat_rule
thetonymaster Aug 3, 2018
a24adda
Merge branch 'loadbalancer-backend-address-pool' into nat_rule
thetonymaster Aug 6, 2018
b56658f
Remove location from nat rule
thetonymaster Aug 7, 2018
a70a34c
Remove location from nat_rule resource tests
thetonymaster Aug 7, 2018
a4e9d00
Move import test to basic test
thetonymaster Aug 7, 2018
62fb1e7
Merge branch 'master' into nat_rule
katbyte Aug 7, 2018
eb6d57e
Merge branch 'master' into nat_rule
thetonymaster Aug 8, 2018
4ddb554
Rename lb natrule create function Create->CreateUpdate
thetonymaster Aug 8, 2018
7b24fd6
Refactor the code to simplify some variables
thetonymaster Aug 8, 2018
0ee602a
Change Create->Manages on nat_rule documentation
thetonymaster Aug 8, 2018
9931a2b
Merge branch 'nat_rule' of github.com:thetonymaster/terraform-provide…
thetonymaster Aug 8, 2018
9d6baa7
Add validation to protocol argument
thetonymaster Aug 8, 2018
19c4393
Update validate package
thetonymaster Aug 9, 2018
67a8434
Validate ports & use constants instead of strings
thetonymaster Aug 9, 2018
bff1628
Merge branch 'master' into nat_rule
thetonymaster Aug 9, 2018
fbd8b0f
Update network.go
katbyte Aug 9, 2018
5fd8943
Update network_test.go
katbyte Aug 9, 2018
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
35 changes: 30 additions & 5 deletions azurestack/helpers/validate/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@ package validate
import (
"fmt"
"net"
"regexp"
)

func Ip4Address(i interface{}, k string) (_ []string, errors []error) {
func IPv4Address(i interface{}, k string) (_ []string, errors []error) {
return validateIpv4Address(i, k, false)
}

func IPv4AddressOrEmpty(i interface{}, k string) (_ []string, errors []error) {
return validateIpv4Address(i, k, true)
}

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

if v == "" && allowEmpty {
return
}

ip := net.ParseIP(v)
if four := ip.To4(); four == nil {
errors = append(errors, fmt.Errorf("%q is not a valid IP4 address: %q", k, v))
Expand All @@ -21,15 +32,29 @@ func Ip4Address(i interface{}, k string) (_ []string, errors []error) {
return
}

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

if matched := regexp.MustCompile(`^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$`).Match([]byte(v)); !matched {
errors = append(errors, fmt.Errorf("%q is not a valid MAC address: %q", k, i))
if _, err := net.ParseMAC(v); err != nil {
errors = append(errors, fmt.Errorf("%q is not a valid MAC address: %q (%v)", k, i, err))
}

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 < 1 || 65535 < v {
errors = append(errors, fmt.Errorf("%q is not a valid port number: %q", k, i))
}

return
Expand Down
159 changes: 131 additions & 28 deletions azurestack/helpers/validate/network_test.go
Original file line number Diff line number Diff line change
@@ -1,95 +1,198 @@
package validate

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

func TestHelper_Validate_Ip4Address(t *testing.T) {
func TestIPv4Address(t *testing.T) {
cases := []struct {
Ip string
IP string
Errors int
}{
{
Ip: "",
IP: "",
Errors: 1,
},
{
Ip: "0.0.0.0",
IP: "0.0.0.0",
Errors: 0,
},
{
Ip: "1.2.3.no",
IP: "1.2.3.no",
Errors: 1,
},
{
Ip: "text",
IP: "text",
Errors: 1,
},
{
Ip: "1.2.3.4",
IP: "1.2.3.4",
Errors: 0,
},
{
Ip: "12.34.43.21",
IP: "12.34.43.21",
Errors: 0,
},
{
Ip: "100.123.199.0",
IP: "100.123.199.0",
Errors: 0,
},
{
Ip: "255.255.255.255",
IP: "255.255.255.255",
Errors: 0,
},
}

for _, tc := range cases {
_, errors := Ip4Address(tc.Ip, "test")
t.Run(tc.IP, func(t *testing.T) {
_, errors := IPv4Address(tc.IP, "test")

if len(errors) < tc.Errors {
t.Fatalf("Expected Ip4Address to have an error for %q", tc.Ip)
}
if len(errors) != tc.Errors {
t.Fatalf("Expected IPv4Address to return %d error(s) not %d", len(errors), tc.Errors)
}
})
}
}

func TestHelper_Validate_MacAddress(t *testing.T) {
func TestIPv4AddressOrEmpty(t *testing.T) {
cases := []struct {
Ip string
IP string
Errors int
}{
{
Ip: "",
IP: "",
Errors: 0,
},
{
IP: "0.0.0.0",
Errors: 0,
},
{
IP: "1.2.3.no",
Errors: 1,
},
{
Ip: "text",
IP: "text",
Errors: 1,
},
{
Ip: "12:34:no",
IP: "1.2.3.4",
Errors: 0,
},
{
IP: "12.34.43.21",
Errors: 0,
},
{
IP: "100.123.199.0",
Errors: 0,
},
{
IP: "255.255.255.255",
Errors: 0,
},
}

for _, tc := range cases {
t.Run(tc.IP, func(t *testing.T) {
_, errors := IPv4AddressOrEmpty(tc.IP, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected IPv4AddressOrEmpty to return %d error(s) not %d", len(errors), tc.Errors)
}
})
}
}

func TestMACAddress(t *testing.T) {
cases := []struct {
MAC string
Errors int
}{
{
MAC: "",
Errors: 1,
},
{
MAC: "text d",
Errors: 1,
},
{
Ip: "123:34:56:78:90:ab",
MAC: "12:34:no",
Errors: 1,
},
{
Ip: "12:34:56:78:90:NO",
MAC: "123:34:56:78:90:ab",
Errors: 1,
},
{
Ip: "12:34:56:78:90:ab",
MAC: "12:34:56:78:90:NO",
Errors: 1,
},
{
MAC: "12:34:56:78:90:ab",
Errors: 0,
},
{
MAC: "ab:cd:ef:AB:CD:EF",
Errors: 0,
},
}

for _, tc := range cases {
t.Run(tc.MAC, func(t *testing.T) {
_, errors := MACAddress(tc.MAC, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected MACAddress to return %d error(s) not %d", len(errors), tc.Errors)
}
})
}
}

func TestPortNumber(t *testing.T) {
cases := []struct {
Port int
Errors int
}{
{
Port: -1,
Errors: 1,
},
{
Port: 0,
Errors: 1,
},
{
Port: 1,
Errors: 0,
},
{
Port: 8477,
Errors: 0,
},
{
Ip: "ab:cd:ef:AB:CD:EF",
Port: 65535,
Errors: 0,
},
{
Port: 65536,
Errors: 1,
},
{
Port: 7000000,
Errors: 1,
},
}

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

if len(errors) < tc.Errors {
t.Fatalf("Expected MacAddress to have an error for %q", tc.Ip)
}
if len(errors) != tc.Errors {
t.Fatalf("Expected PortNumber to return %d error(s) not %d", len(errors), tc.Errors)
}
})
}
}
1 change: 1 addition & 0 deletions azurestack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func Provider() terraform.ResourceProvider {
"azurestack_local_network_gateway": resourceArmLocalNetworkGateway(),
"azurestack_lb": resourceArmLoadBalancer(),
"azurestack_lb_backend_address_pool": resourceArmLoadBalancerBackendAddressPool(),
"azurestack_lb_nat_rule": resourceArmLoadBalancerNatRule(),
"azurestack_lb_probe": resourceArmLoadBalancerProbe(),
"azurestack_public_ip": resourceArmPublicIp(),
"azurestack_resource_group": resourceArmResourceGroup(),
Expand Down
2 changes: 1 addition & 1 deletion azurestack/resource_arm_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func resourceArmLoadBalancer() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.Ip4Address,
ValidateFunc: validate.IPv4Address,
},

"public_ip_address_id": {
Expand Down
Loading