From 6f2fb0db384cd87933e6abb61005168ae8f85f2b Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Thu, 25 Jun 2015 11:00:15 +0100 Subject: [PATCH] provider/aws: Add validation for aws_elb.name --- builtin/providers/aws/resource_aws_elb.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index ebc45c941183..08fde29a263f 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "log" + "regexp" "strings" "github.com/aws/aws-sdk-go/aws" @@ -25,6 +26,26 @@ func resourceAwsElb() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, + ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + if !regexp.MustCompile(`^[0-9a-z-]$`).MatchString(value) { + errors = append(errors, fmt.Errorf( + "only lowercase alphanumeric characters and hyphens allowed in %q", k)) + } + if len(value) > 32 { + errors = append(errors, fmt.Errorf( + "%q cannot be longer than 32 characters", k)) + } + if regexp.MustCompile(`^-`).MatchString(value) { + errors = append(errors, fmt.Errorf( + "%q cannot begin with a hyphen", k)) + } + if regexp.MustCompile(`-$`).MatchString(value) { + errors = append(errors, fmt.Errorf( + "%q cannot end with a hyphen", k)) + } + return + }, }, "internal": &schema.Schema{