Merge pull request #8103 from BedeGaming/validation-helper

helper: create validation package to provide common validation functions
This commit is contained in:
Paul Stack 2016-10-07 13:42:29 +01:00 committed by GitHub
commit 4ac7d0a6bb
4 changed files with 150 additions and 82 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/trafficmanager"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceArmTrafficManagerEndpoint() *schema.Resource {
@ -31,7 +32,7 @@ func resourceArmTrafficManagerEndpoint() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAzureRMTrafficManagerEndpointType,
ValidateFunc: validation.StringInSlice([]string{"azureEndpoints", "nestedEndpoints", "externalEndpoints"}, false),
},
"profile_name": {
@ -62,14 +63,14 @@ func resourceArmTrafficManagerEndpoint() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validateAzureRMTrafficManagerEndpointWeight,
ValidateFunc: validation.IntBetween(1, 1000),
},
"priority": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validateAzureRMTrafficManagerEndpointPriority,
ValidateFunc: validation.IntBetween(1, 1000),
},
"endpoint_location": {
@ -234,32 +235,3 @@ func getArmTrafficManagerEndpointProperties(d *schema.ResourceData) *trafficmana
return &endpointProps
}
func validateAzureRMTrafficManagerEndpointType(i interface{}, k string) (s []string, errors []error) {
valid := map[string]struct{}{
"azureEndpoints": struct{}{},
"externalEndpoints": struct{}{},
"nestedEndpoints": struct{}{},
}
if _, ok := valid[i.(string)]; !ok {
errors = append(errors, fmt.Errorf("endpoint type invalid, got %s", i.(string)))
}
return
}
func validateAzureRMTrafficManagerEndpointWeight(i interface{}, k string) (s []string, errors []error) {
w := i.(int)
if w < 1 || w > 1000 {
errors = append(errors, fmt.Errorf("endpoint weight must be between 1-1000 inclusive"))
}
return
}
func validateAzureRMTrafficManagerEndpointPriority(i interface{}, k string) (s []string, errors []error) {
p := i.(int)
if p < 1 || p > 1000 {
errors = append(errors, fmt.Errorf("endpoint priority must be between 1-1000 inclusive"))
}
return
}

View File

@ -10,6 +10,7 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/trafficmanager"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceArmTrafficManagerProfile() *schema.Resource {
@ -33,13 +34,13 @@ func resourceArmTrafficManagerProfile() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validateAzureRMTrafficManagerStatus,
ValidateFunc: validation.StringInSlice([]string{"Enabled", "Disabled"}, true),
},
"traffic_routing_method": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAzureRMTrafficManagerRoutingMethod,
ValidateFunc: validation.StringInSlice([]string{"Performance", "Weighted", "Priority"}, false),
},
"dns_config": {
@ -55,7 +56,7 @@ func resourceArmTrafficManagerProfile() *schema.Resource {
"ttl": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateAzureRMTrafficManagerTTL,
ValidateFunc: validation.IntBetween(30, 999999),
},
},
},
@ -76,12 +77,12 @@ func resourceArmTrafficManagerProfile() *schema.Resource {
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAzureRMTrafficManagerMonitorProtocol,
ValidateFunc: validation.StringInSlice([]string{"http", "https"}, false),
},
"port": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateAzureRMTrafficManagerMonitorPort,
ValidateFunc: validation.IntBetween(1, 65535),
},
"path": {
Type: schema.TypeString,
@ -279,48 +280,3 @@ func resourceAzureRMTrafficManagerMonitorConfigHash(v interface{}) int {
return hashcode.String(buf.String())
}
func validateAzureRMTrafficManagerStatus(i interface{}, k string) (s []string, errors []error) {
status := strings.ToLower(i.(string))
if status != "enabled" && status != "disabled" {
errors = append(errors, fmt.Errorf("%s must be one of: Enabled, Disabled", k))
}
return
}
func validateAzureRMTrafficManagerRoutingMethod(i interface{}, k string) (s []string, errors []error) {
valid := map[string]struct{}{
"Performance": struct{}{},
"Weighted": struct{}{},
"Priority": struct{}{},
}
if _, ok := valid[i.(string)]; !ok {
errors = append(errors, fmt.Errorf("traffic_routing_method must be one of (Performance, Weighted, Priority), got %s", i.(string)))
}
return
}
func validateAzureRMTrafficManagerTTL(i interface{}, k string) (s []string, errors []error) {
ttl := i.(int)
if ttl < 30 || ttl > 999999 {
errors = append(errors, fmt.Errorf("ttl must be between 30 and 999,999 inclusive"))
}
return
}
func validateAzureRMTrafficManagerMonitorProtocol(i interface{}, k string) (s []string, errors []error) {
p := i.(string)
if p != "http" && p != "https" {
errors = append(errors, fmt.Errorf("monitor_config.protocol must be one of: http, https"))
}
return
}
func validateAzureRMTrafficManagerMonitorPort(i interface{}, k string) (s []string, errors []error) {
p := i.(int)
if p < 1 || p > 65535 {
errors = append(errors, fmt.Errorf("monitor_config.port must be between 1 - 65535 inclusive"))
}
return
}

View File

@ -0,0 +1,49 @@
package validation
import (
"fmt"
"strings"
"github.com/hashicorp/terraform/helper/schema"
)
// IntBetween returns a SchemaValidateFunc which tests if the provided value
// is of type int and is between min and max (inclusive)
func IntBetween(min, max int) schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(int)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be int", k))
return
}
if v < min || v > max {
es = append(es, fmt.Errorf("expected %s to be in the range (%d - %d), got %d", k, min, max, v))
return
}
return
}
}
// StringInSlice returns a SchemaValidateFunc which tests if the provided value
// is of type string and matches the value of an element in the valid slice
// will test with in lower case if ignoreCase is true
func StringInSlice(valid []string, ignoreCase bool) 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
}
for _, str := range valid {
if v == str || (ignoreCase && strings.ToLower(v) == strings.ToLower(str)) {
return
}
}
es = append(es, fmt.Errorf("expected %s to be one of %v, got %s", k, valid, v))
return
}
}

View File

@ -0,0 +1,91 @@
package validation
import (
"regexp"
"testing"
"github.com/hashicorp/terraform/helper/schema"
)
type testCase struct {
val interface{}
f schema.SchemaValidateFunc
expectedErr *regexp.Regexp
}
func TestValidationIntBetween(t *testing.T) {
runTestCases(t, []testCase{
{
val: 1,
f: IntBetween(1, 1),
},
{
val: 1,
f: IntBetween(0, 2),
},
{
val: 1,
f: IntBetween(2, 3),
expectedErr: regexp.MustCompile("expected [\\w]+ to be in the range \\(2 - 3\\), got 1"),
},
{
val: "1",
f: IntBetween(2, 3),
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be int"),
},
})
}
func TestValidationSringInSlice(t *testing.T) {
runTestCases(t, []testCase{
{
val: "ValidValue",
f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false),
},
// ignore case
{
val: "VALIDVALUE",
f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, true),
},
{
val: "VALIDVALUE",
f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false),
expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[ValidValue AnotherValidValue\\], got VALIDVALUE"),
},
{
val: "InvalidValue",
f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false),
expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[ValidValue AnotherValidValue\\], got InvalidValue"),
},
{
val: 1,
f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false),
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be string"),
},
})
}
func runTestCases(t *testing.T, cases []testCase) {
matchErr := func(errs []error, r *regexp.Regexp) bool {
// err must match one provided
for _, err := range errs {
if r.MatchString(err.Error()) {
return true
}
}
return false
}
for i, tc := range cases {
_, errs := tc.f(tc.val, "test_property")
if len(errs) == 0 && tc.expectedErr == nil {
continue
}
if !matchErr(errs, tc.expectedErr) {
t.Fatalf("expected test case %d to produce error matching \"%s\", got %v", i, tc.expectedErr, errs)
}
}
}