github.com/dougneal/terraform@v0.6.15-0.20170330092735-b6a3840768a4/helper/validation/validation.go (about) 1 package validation 2 3 import ( 4 "fmt" 5 "net" 6 "strings" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 // IntBetween returns a SchemaValidateFunc which tests if the provided value 12 // is of type int and is between min and max (inclusive) 13 func IntBetween(min, max int) schema.SchemaValidateFunc { 14 return func(i interface{}, k string) (s []string, es []error) { 15 v, ok := i.(int) 16 if !ok { 17 es = append(es, fmt.Errorf("expected type of %s to be int", k)) 18 return 19 } 20 21 if v < min || v > max { 22 es = append(es, fmt.Errorf("expected %s to be in the range (%d - %d), got %d", k, min, max, v)) 23 return 24 } 25 26 return 27 } 28 } 29 30 // StringInSlice returns a SchemaValidateFunc which tests if the provided value 31 // is of type string and matches the value of an element in the valid slice 32 // will test with in lower case if ignoreCase is true 33 func StringInSlice(valid []string, ignoreCase bool) schema.SchemaValidateFunc { 34 return func(i interface{}, k string) (s []string, es []error) { 35 v, ok := i.(string) 36 if !ok { 37 es = append(es, fmt.Errorf("expected type of %s to be string", k)) 38 return 39 } 40 41 for _, str := range valid { 42 if v == str || (ignoreCase && strings.ToLower(v) == strings.ToLower(str)) { 43 return 44 } 45 } 46 47 es = append(es, fmt.Errorf("expected %s to be one of %v, got %s", k, valid, v)) 48 return 49 } 50 } 51 52 // StringLenBetween returns a SchemaValidateFunc which tests if the provided value 53 // is of type string and has length between min and max (inclusive) 54 func StringLenBetween(min, max int) schema.SchemaValidateFunc { 55 return func(i interface{}, k string) (s []string, es []error) { 56 v, ok := i.(string) 57 if !ok { 58 es = append(es, fmt.Errorf("expected type of %s to be string", k)) 59 return 60 } 61 if len(v) < min || len(v) > max { 62 es = append(es, fmt.Errorf("expected length of %s to be in the range (%d - %d), got %s", k, min, max, v)) 63 } 64 return 65 } 66 } 67 68 // CIDRNetwork returns a SchemaValidateFunc which tests if the provided value 69 // is of type string, is in valid CIDR network notation, and has significant bits between min and max (inclusive) 70 func CIDRNetwork(min, max int) schema.SchemaValidateFunc { 71 return func(i interface{}, k string) (s []string, es []error) { 72 v, ok := i.(string) 73 if !ok { 74 es = append(es, fmt.Errorf("expected type of %s to be string", k)) 75 return 76 } 77 78 _, ipnet, err := net.ParseCIDR(v) 79 if err != nil { 80 es = append(es, fmt.Errorf( 81 "expected %s to contain a valid CIDR, got: %s with err: %s", k, v, err)) 82 return 83 } 84 85 if ipnet == nil || v != ipnet.String() { 86 es = append(es, fmt.Errorf( 87 "expected %s to contain a valid network CIDR, expected %s, got %s", 88 k, ipnet, v)) 89 } 90 91 sigbits, _ := ipnet.Mask.Size() 92 if sigbits < min || sigbits > max { 93 es = append(es, fmt.Errorf( 94 "expected %q to contain a network CIDR with between %d and %d significant bits, got: %d", 95 k, min, max, sigbits)) 96 } 97 98 return 99 } 100 }