github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/arukas/validators.go (about)

     1  package arukas
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashicorp/terraform/helper/schema"
     6  	"strings"
     7  )
     8  
     9  func validateMaxLength(minLength, maxLength int) schema.SchemaValidateFunc {
    10  	return func(v interface{}, k string) (ws []string, errors []error) {
    11  		value := v.(string)
    12  		if len(value) < minLength {
    13  			errors = append(errors, fmt.Errorf(
    14  				"%q cannot be shorter than %d characters: %q", k, minLength, value))
    15  		}
    16  		if len(value) > maxLength {
    17  			errors = append(errors, fmt.Errorf(
    18  				"%q cannot be longer than %d characters: %q", k, maxLength, value))
    19  		}
    20  		return
    21  	}
    22  }
    23  
    24  func validateIntegerInRange(min, max int) schema.SchemaValidateFunc {
    25  	return func(v interface{}, k string) (ws []string, errors []error) {
    26  		value := v.(int)
    27  		if value < min {
    28  			errors = append(errors, fmt.Errorf(
    29  				"%q cannot be lower than %d: %d", k, min, value))
    30  		}
    31  		if value > max {
    32  			errors = append(errors, fmt.Errorf(
    33  				"%q cannot be higher than %d: %d", k, max, value))
    34  		}
    35  		return
    36  	}
    37  }
    38  
    39  func validateStringInWord(allowWords []string) schema.SchemaValidateFunc {
    40  	return func(v interface{}, k string) (ws []string, errors []error) {
    41  		var found bool
    42  		for _, t := range allowWords {
    43  			if v.(string) == t {
    44  				found = true
    45  			}
    46  		}
    47  		if !found {
    48  			errors = append(errors, fmt.Errorf("%q must be one of [%s]", k, strings.Join(allowWords, "/")))
    49  
    50  		}
    51  		return
    52  	}
    53  }
    54  
    55  func validateIntInWord(allowWords []string) schema.SchemaValidateFunc {
    56  	return func(v interface{}, k string) (ws []string, errors []error) {
    57  		var found bool
    58  		for _, t := range allowWords {
    59  			if fmt.Sprintf("%d", v.(int)) == t {
    60  				found = true
    61  			}
    62  		}
    63  		if !found {
    64  			errors = append(errors, fmt.Errorf("%q must be one of [%s]", k, strings.Join(allowWords, "/")))
    65  
    66  		}
    67  		return
    68  	}
    69  }
    70  
    71  func validateDNSRecordValue() schema.SchemaValidateFunc {
    72  	return func(v interface{}, k string) (ws []string, errors []error) {
    73  		var rtype, value string
    74  
    75  		values := v.(map[string]interface{})
    76  		rtype = values["type"].(string)
    77  		value = values["value"].(string)
    78  		switch rtype {
    79  		case "MX", "NS", "CNAME":
    80  			if rtype == "MX" {
    81  				if values["priority"] == nil {
    82  					errors = append(errors, fmt.Errorf("%q required when TYPE was MX", k))
    83  				}
    84  			}
    85  			if !strings.HasSuffix(value, ".") {
    86  				errors = append(errors, fmt.Errorf("%q must be period at the end [%s]", k, value))
    87  			}
    88  		}
    89  		return
    90  	}
    91  
    92  }