-
Notifications
You must be signed in to change notification settings - Fork 233
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
validation: add additional Int, Network, Time and Web validators #296
Changes from all commits
3f3dde2
9a86338
ee4fe2a
c1005d6
af70e5e
197bcb9
2504e75
43d0e8f
d00d319
c4e0f07
03bfef2
f86cf8d
e481cb9
4770c6a
cc4d279
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,81 +9,172 @@ import ( | |
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
// CIDRNetwork returns a SchemaValidateFunc which tests if the provided value | ||
// is of type string, is in valid CIDR network notation, and has significant bits between min and max (inclusive) | ||
func CIDRNetwork(min, max int) schema.SchemaValidateFunc { | ||
// SingleIP returns a SchemaValidateFunc which tests if the provided value | ||
// is of type string, and in valid single Value notation | ||
func SingleIP() schema.SchemaValidateFunc { | ||
return func(i interface{}, k string) (s []string, es []error) { | ||
v, ok := i.(string) | ||
if !ok { | ||
es = append(es, fmt.Errorf("expected type of %s to be string", k)) | ||
return | ||
} | ||
|
||
_, ipnet, err := net.ParseCIDR(v) | ||
if err != nil { | ||
es = append(es, fmt.Errorf( | ||
"expected %s to contain a valid CIDR, got: %s with err: %s", k, v, err)) | ||
return | ||
ip := net.ParseIP(v) | ||
if ip == nil { | ||
es = append(es, fmt.Errorf("expected %s to contain a valid IP, got: %s", k, v)) | ||
} | ||
return | ||
} | ||
} | ||
|
||
if ipnet == nil || v != ipnet.String() { | ||
es = append(es, fmt.Errorf( | ||
"expected %s to contain a valid network CIDR, expected %s, got %s", | ||
k, ipnet, v)) | ||
} | ||
// IsIPv6Address is a SchemaValidateFunc which tests if the provided value is of type string and a valid IPv6 address | ||
func IsIPv6Address(i interface{}, k string) (warnings []string, errors []error) { | ||
v, ok := i.(string) | ||
if !ok { | ||
errors = append(errors, fmt.Errorf("expected type of %q to be string", k)) | ||
return | ||
} | ||
|
||
sigbits, _ := ipnet.Mask.Size() | ||
if sigbits < min || sigbits > max { | ||
es = append(es, fmt.Errorf( | ||
"expected %q to contain a network CIDR with between %d and %d significant bits, got: %d", | ||
k, min, max, sigbits)) | ||
} | ||
ip := net.ParseIP(v) | ||
if six := ip.To16(); six == nil { | ||
errors = append(errors, fmt.Errorf("expected %s to contain a valid IPv6 address, got: %s", k, v)) | ||
} | ||
|
||
return warnings, errors | ||
} | ||
|
||
// IsIPv4Address is a SchemaValidateFunc which tests if the provided value is of type string and a valid IPv4 address | ||
func IsIPv4Address(i interface{}, k string) (warnings []string, errors []error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs doc/comment |
||
v, ok := i.(string) | ||
if !ok { | ||
errors = append(errors, fmt.Errorf("expected type of %q to be string", k)) | ||
return | ||
} | ||
|
||
ip := net.ParseIP(v) | ||
if four := ip.To4(); four == nil { | ||
errors = append(errors, fmt.Errorf("expected %s to contain a valid IPv4 address, got: %s", k, v)) | ||
} | ||
|
||
return warnings, errors | ||
} | ||
|
||
// SingleIP returns a SchemaValidateFunc which tests if the provided value | ||
// is of type string, and in valid single IP notation | ||
func SingleIP() schema.SchemaValidateFunc { | ||
// IPRange returns a SchemaValidateFunc which tests if the provided value is of type string, and in valid IP range | ||
func IPRange() schema.SchemaValidateFunc { | ||
return func(i interface{}, k string) (s []string, es []error) { | ||
v, ok := i.(string) | ||
if !ok { | ||
es = append(es, fmt.Errorf("expected type of %s to be string", k)) | ||
return | ||
} | ||
|
||
ip := net.ParseIP(v) | ||
if ip == nil { | ||
ips := strings.Split(v, "-") | ||
if len(ips) != 2 { | ||
es = append(es, fmt.Errorf( | ||
"expected %s to contain a valid IP range, got: %s", k, v)) | ||
return | ||
} | ||
ip1 := net.ParseIP(ips[0]) | ||
ip2 := net.ParseIP(ips[1]) | ||
if ip1 == nil || ip2 == nil || bytes.Compare(ip1, ip2) > 0 { | ||
es = append(es, fmt.Errorf( | ||
"expected %s to contain a valid IP, got: %s", k, v)) | ||
"expected %s to contain a valid IP range, got: %s", k, v)) | ||
} | ||
return | ||
} | ||
} | ||
|
||
// IPRange returns a SchemaValidateFunc which tests if the provided value | ||
// is of type string, and in valid IP range notation | ||
func IPRange() schema.SchemaValidateFunc { | ||
// IsCIDR is a SchemaValidateFunc which tests if the provided value is of type string and a valid CIDR | ||
func IsCIDR(i interface{}, k string) (warnings []string, errors []error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs doc/comment |
||
v, ok := i.(string) | ||
if !ok { | ||
errors = append(errors, fmt.Errorf("expected type of %s to be string", k)) | ||
return | ||
} | ||
|
||
_, _, err := net.ParseCIDR(v) | ||
if err != nil { | ||
errors = append(errors, fmt.Errorf("expected %q to be a valid IPv4 Value, got %v: %v", k, i, err)) | ||
} | ||
|
||
return warnings, errors | ||
} | ||
|
||
// CIDRNetwork returns a SchemaValidateFunc which tests if the provided value | ||
// is of type string, is in valid Value network notation, and has significant bits between min and max (inclusive) | ||
func CIDRNetwork(min, max int) schema.SchemaValidateFunc { | ||
return func(i interface{}, k string) (s []string, es []error) { | ||
v, ok := i.(string) | ||
if !ok { | ||
es = append(es, fmt.Errorf("expected type of %s to be string", k)) | ||
return | ||
} | ||
|
||
ips := strings.Split(v, "-") | ||
if len(ips) != 2 { | ||
_, ipnet, err := net.ParseCIDR(v) | ||
if err != nil { | ||
es = append(es, fmt.Errorf( | ||
"expected %s to contain a valid IP range, got: %s", k, v)) | ||
"expected %s to contain a valid Value, got: %s with err: %s", k, v, err)) | ||
return | ||
} | ||
ip1 := net.ParseIP(ips[0]) | ||
ip2 := net.ParseIP(ips[1]) | ||
if ip1 == nil || ip2 == nil || bytes.Compare(ip1, ip2) > 0 { | ||
|
||
if ipnet == nil || v != ipnet.String() { | ||
es = append(es, fmt.Errorf( | ||
"expected %s to contain a valid IP range, got: %s", k, v)) | ||
"expected %s to contain a valid network Value, expected %s, got %s", | ||
k, ipnet, v)) | ||
} | ||
|
||
sigbits, _ := ipnet.Mask.Size() | ||
if sigbits < min || sigbits > max { | ||
es = append(es, fmt.Errorf( | ||
"expected %q to contain a network Value with between %d and %d significant bits, got: %d", | ||
k, min, max, sigbits)) | ||
} | ||
|
||
return | ||
} | ||
} | ||
|
||
// IsMACAddress is a SchemaValidateFunc which tests if the provided value is of type string and a valid MAC address | ||
func IsMACAddress(i interface{}, k string) (warnings []string, errors []error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs doc/comment |
||
v, ok := i.(string) | ||
if !ok { | ||
errors = append(errors, fmt.Errorf("expected type of %q to be string", k)) | ||
return | ||
} | ||
|
||
if _, err := net.ParseMAC(v); err != nil { | ||
errors = append(errors, fmt.Errorf("expected %q to be a valid MAC address, got %v: %v", k, i, err)) | ||
} | ||
|
||
return warnings, errors | ||
} | ||
|
||
// IsPortNumber is a SchemaValidateFunc which tests if the provided value is of type string and a valid TCP Port Number | ||
func IsPortNumber(i interface{}, k string) (warnings []string, errors []error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs doc/comment |
||
v, ok := i.(int) | ||
if !ok { | ||
errors = append(errors, fmt.Errorf("expected type of %q to be int", k)) | ||
return | ||
} | ||
|
||
if 1 > v || v > 65535 { | ||
errors = append(errors, fmt.Errorf("expected %q to be a valid port number, got: %v", k, v)) | ||
} | ||
|
||
return warnings, errors | ||
} | ||
|
||
// IsPortNumberOrZero is a SchemaValidateFunc which tests if the provided value is of type string and a valid TCP Port Number or zero | ||
func IsPortNumberOrZero(i interface{}, k string) (warnings []string, errors []error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs doc/comment |
||
v, ok := i.(int) | ||
if !ok { | ||
errors = append(errors, fmt.Errorf("expected type of %q to be int", k)) | ||
return | ||
} | ||
|
||
if 0 > v || v > 65535 { | ||
errors = append(errors, fmt.Errorf("expected %q to be a valid port number or 0, got: %v", k, v)) | ||
} | ||
|
||
return warnings, errors | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs doc/comment