github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/builtin/providers/aws/validators.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "time" 7 ) 8 9 func validateRdsId(v interface{}, k string) (ws []string, errors []error) { 10 value := v.(string) 11 if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) { 12 errors = append(errors, fmt.Errorf( 13 "only lowercase alphanumeric characters and hyphens allowed in %q", k)) 14 } 15 if !regexp.MustCompile(`^[a-z]`).MatchString(value) { 16 errors = append(errors, fmt.Errorf( 17 "first character of %q must be a letter", k)) 18 } 19 if regexp.MustCompile(`--`).MatchString(value) { 20 errors = append(errors, fmt.Errorf( 21 "%q cannot contain two consecutive hyphens", k)) 22 } 23 if regexp.MustCompile(`-$`).MatchString(value) { 24 errors = append(errors, fmt.Errorf( 25 "%q cannot end with a hyphen", k)) 26 } 27 return 28 } 29 30 func validateASGScheduleTimestamp(v interface{}, k string) (ws []string, errors []error) { 31 value := v.(string) 32 _, err := time.Parse(awsAutoscalingScheduleTimeLayout, value) 33 if err != nil { 34 errors = append(errors, fmt.Errorf( 35 "%q cannot be parsed as iso8601 Timestamp Format", value)) 36 } 37 38 return 39 } 40 41 // validateTagFilters confirms the "value" component of a tag filter is one of 42 // AWS's three allowed types. 43 func validateTagFilters(v interface{}, k string) (ws []string, errors []error) { 44 value := v.(string) 45 if value != "KEY_ONLY" && value != "VALUE_ONLY" && value != "KEY_AND_VALUE" { 46 errors = append(errors, fmt.Errorf( 47 "%q must be one of \"KEY_ONLY\", \"VALUE_ONLY\", or \"KEY_AND_VALUE\"", k)) 48 } 49 return 50 } 51 52 func validateDbParamGroupName(v interface{}, k string) (ws []string, errors []error) { 53 value := v.(string) 54 if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) { 55 errors = append(errors, fmt.Errorf( 56 "only lowercase alphanumeric characters and hyphens allowed in %q", k)) 57 } 58 if !regexp.MustCompile(`^[a-z]`).MatchString(value) { 59 errors = append(errors, fmt.Errorf( 60 "first character of %q must be a letter", k)) 61 } 62 if regexp.MustCompile(`--`).MatchString(value) { 63 errors = append(errors, fmt.Errorf( 64 "%q cannot contain two consecutive hyphens", k)) 65 } 66 if regexp.MustCompile(`-$`).MatchString(value) { 67 errors = append(errors, fmt.Errorf( 68 "%q cannot end with a hyphen", k)) 69 } 70 if len(value) > 255 { 71 errors = append(errors, fmt.Errorf( 72 "%q cannot be greater than 255 characters", k)) 73 } 74 return 75 76 } 77 78 func validateStreamViewType(v interface{}, k string) (ws []string, errors []error) { 79 value := v.(string) 80 viewTypes := map[string]bool{ 81 "KEYS_ONLY": true, 82 "NEW_IMAGE": true, 83 "OLD_IMAGE": true, 84 "NEW_AND_OLD_IMAGES": true, 85 } 86 87 if !viewTypes[value] { 88 errors = append(errors, fmt.Errorf("%q be a valid DynamoDB StreamViewType", k)) 89 } 90 return 91 } 92 93 func validateElbName(v interface{}, k string) (ws []string, errors []error) { 94 value := v.(string) 95 if !regexp.MustCompile(`^[0-9A-Za-z-]+$`).MatchString(value) { 96 errors = append(errors, fmt.Errorf( 97 "only alphanumeric characters and hyphens allowed in %q: %q", 98 k, value)) 99 } 100 if len(value) > 32 { 101 errors = append(errors, fmt.Errorf( 102 "%q cannot be longer than 32 characters: %q", k, value)) 103 } 104 if regexp.MustCompile(`^-`).MatchString(value) { 105 errors = append(errors, fmt.Errorf( 106 "%q cannot begin with a hyphen: %q", k, value)) 107 } 108 if regexp.MustCompile(`-$`).MatchString(value) { 109 errors = append(errors, fmt.Errorf( 110 "%q cannot end with a hyphen: %q", k, value)) 111 } 112 return 113 114 } 115 116 func validateEcrRepositoryName(v interface{}, k string) (ws []string, errors []error) { 117 value := v.(string) 118 if len(value) < 2 { 119 errors = append(errors, fmt.Errorf( 120 "%q must be at least 2 characters long: %q", k, value)) 121 } 122 if len(value) > 256 { 123 errors = append(errors, fmt.Errorf( 124 "%q cannot be longer than 256 characters: %q", k, value)) 125 } 126 127 // http://docs.aws.amazon.com/AmazonECR/latest/APIReference/API_CreateRepository.html 128 pattern := `^(?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)*[a-z0-9]+(?:[._-][a-z0-9]+)*$` 129 if !regexp.MustCompile(pattern).MatchString(value) { 130 errors = append(errors, fmt.Errorf( 131 "%q doesn't comply with restrictions (%q): %q", 132 k, pattern, value)) 133 } 134 135 return 136 }