github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/builtin/providers/kubernetes/validators.go (about)

     1  package kubernetes
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  
    10  	"k8s.io/apimachinery/pkg/api/resource"
    11  	apiValidation "k8s.io/apimachinery/pkg/api/validation"
    12  	utilValidation "k8s.io/apimachinery/pkg/util/validation"
    13  )
    14  
    15  func validateAnnotations(value interface{}, key string) (ws []string, es []error) {
    16  	m := value.(map[string]interface{})
    17  	for k, _ := range m {
    18  		errors := utilValidation.IsQualifiedName(strings.ToLower(k))
    19  		if len(errors) > 0 {
    20  			for _, e := range errors {
    21  				es = append(es, fmt.Errorf("%s (%q) %s", key, k, e))
    22  			}
    23  		}
    24  	}
    25  	return
    26  }
    27  
    28  func validateName(value interface{}, key string) (ws []string, es []error) {
    29  	v := value.(string)
    30  
    31  	errors := apiValidation.NameIsDNSLabel(v, false)
    32  	if len(errors) > 0 {
    33  		for _, err := range errors {
    34  			es = append(es, fmt.Errorf("%s %s", key, err))
    35  		}
    36  	}
    37  	return
    38  }
    39  
    40  func validateGenerateName(value interface{}, key string) (ws []string, es []error) {
    41  	v := value.(string)
    42  
    43  	errors := apiValidation.NameIsDNSLabel(v, true)
    44  	if len(errors) > 0 {
    45  		for _, err := range errors {
    46  			es = append(es, fmt.Errorf("%s %s", key, err))
    47  		}
    48  	}
    49  	return
    50  }
    51  
    52  func validateLabels(value interface{}, key string) (ws []string, es []error) {
    53  	m := value.(map[string]interface{})
    54  	for k, v := range m {
    55  		for _, msg := range utilValidation.IsQualifiedName(k) {
    56  			es = append(es, fmt.Errorf("%s (%q) %s", key, k, msg))
    57  		}
    58  		val := v.(string)
    59  		for _, msg := range utilValidation.IsValidLabelValue(val) {
    60  			es = append(es, fmt.Errorf("%s (%q) %s", key, val, msg))
    61  		}
    62  	}
    63  	return
    64  }
    65  
    66  func validatePortNum(value interface{}, key string) (ws []string, es []error) {
    67  	errors := utilValidation.IsValidPortNum(value.(int))
    68  	if len(errors) > 0 {
    69  		for _, err := range errors {
    70  			es = append(es, fmt.Errorf("%s %s", key, err))
    71  		}
    72  	}
    73  	return
    74  }
    75  
    76  func validatePortName(value interface{}, key string) (ws []string, es []error) {
    77  	errors := utilValidation.IsValidPortName(value.(string))
    78  	if len(errors) > 0 {
    79  		for _, err := range errors {
    80  			es = append(es, fmt.Errorf("%s %s", key, err))
    81  		}
    82  	}
    83  	return
    84  }
    85  func validatePortNumOrName(value interface{}, key string) (ws []string, es []error) {
    86  	switch value.(type) {
    87  	case string:
    88  		intVal, err := strconv.Atoi(value.(string))
    89  		if err != nil {
    90  			return validatePortName(value, key)
    91  		}
    92  		return validatePortNum(intVal, key)
    93  	case int:
    94  		return validatePortNum(value, key)
    95  
    96  	default:
    97  		es = append(es, fmt.Errorf("%s must be defined of type string or int on the schema", key))
    98  		return
    99  	}
   100  }
   101  
   102  func validateResourceList(value interface{}, key string) (ws []string, es []error) {
   103  	m := value.(map[string]interface{})
   104  	for k, value := range m {
   105  		if _, ok := value.(int); ok {
   106  			continue
   107  		}
   108  
   109  		if v, ok := value.(string); ok {
   110  			_, err := resource.ParseQuantity(v)
   111  			if err != nil {
   112  				es = append(es, fmt.Errorf("%s.%s (%q): %s", key, k, v, err))
   113  			}
   114  			continue
   115  		}
   116  
   117  		err := "Value can be either string or int"
   118  		es = append(es, fmt.Errorf("%s.%s (%#v): %s", key, k, value, err))
   119  	}
   120  	return
   121  }
   122  
   123  func validateResourceQuantity(value interface{}, key string) (ws []string, es []error) {
   124  	if v, ok := value.(string); ok {
   125  		_, err := resource.ParseQuantity(v)
   126  		if err != nil {
   127  			es = append(es, fmt.Errorf("%s.%s : %s", key, v, err))
   128  		}
   129  	}
   130  	return
   131  }
   132  
   133  func validatePositiveInteger(value interface{}, key string) (ws []string, es []error) {
   134  	v := value.(int)
   135  	if v <= 0 {
   136  		es = append(es, fmt.Errorf("%s must be greater than 0", key))
   137  	}
   138  	return
   139  }
   140  
   141  func validateDNSPolicy(value interface{}, key string) (ws []string, es []error) {
   142  	v := value.(string)
   143  	if v != "ClusterFirst" && v != "Default" {
   144  		es = append(es, fmt.Errorf("%s must be either ClusterFirst or Default", key))
   145  	}
   146  	return
   147  }
   148  
   149  func validateRestartPolicy(value interface{}, key string) (ws []string, es []error) {
   150  	v := value.(string)
   151  	switch v {
   152  	case "Always", "OnFailure", "Never":
   153  		return
   154  	default:
   155  		es = append(es, fmt.Errorf("%s must be one of Always, OnFailure or Never ", key))
   156  	}
   157  	return
   158  }
   159  
   160  func validateTerminationGracePeriodSeconds(value interface{}, key string) (ws []string, es []error) {
   161  	v := value.(int)
   162  	if v < 0 {
   163  		es = append(es, fmt.Errorf("%s must be greater than or equal to 0", key))
   164  	}
   165  	return
   166  }
   167  
   168  func validateAttributeValueDoesNotContain(searchString string) schema.SchemaValidateFunc {
   169  	return func(v interface{}, k string) (ws []string, errors []error) {
   170  		input := v.(string)
   171  		if !strings.Contains(input, searchString) {
   172  			errors = append(errors, fmt.Errorf(
   173  				"%q must not contain %q",
   174  				k, searchString))
   175  		}
   176  		return
   177  	}
   178  }
   179  
   180  func validateAttributeValueIsIn(validValues []string) schema.SchemaValidateFunc {
   181  	return func(v interface{}, k string) (ws []string, errors []error) {
   182  		input := v.(string)
   183  		isValid := false
   184  		for _, s := range validValues {
   185  			if s == input {
   186  				isValid = true
   187  				break
   188  			}
   189  		}
   190  		if !isValid {
   191  			errors = append(errors, fmt.Errorf(
   192  				"%q must contain a value from %#v, got %q",
   193  				k, validValues, input))
   194  		}
   195  		return
   196  
   197  	}
   198  }