Skip to content

Commit

Permalink
resource/aws_service_discovery_http_namespace: Correct name validation (
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit authored Aug 22, 2020
1 parent af1dc7b commit 116a671
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 28 deletions.
2 changes: 1 addition & 1 deletion aws/resource_aws_service_discovery_http_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func resourceAwsServiceDiscoveryHttpNamespace() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateServiceDiscoveryHttpNamespaceName,
ValidateFunc: validateServiceDiscoveryNamespaceName,
},
"description": {
Type: schema.TypeString,
Expand Down
7 changes: 4 additions & 3 deletions aws/resource_aws_service_discovery_private_dns_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ func resourceAwsServiceDiscoveryPrivateDnsNamespace() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateServiceDiscoveryNamespaceName,
},
"description": {
Type: schema.TypeString,
Expand Down
7 changes: 4 additions & 3 deletions aws/resource_aws_service_discovery_public_dns_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ func resourceAwsServiceDiscoveryPublicDnsNamespace() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateServiceDiscoveryNamespaceName,
},
"description": {
Type: schema.TypeString,
Expand Down
26 changes: 5 additions & 21 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2354,27 +2354,6 @@ func validateCloudFrontPublicKeyNamePrefix(v interface{}, k string) (ws []string
return
}

func validateServiceDiscoveryHttpNamespaceName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[0-9A-Za-z_-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only alphanumeric characters, underscores and hyphens allowed in %q", k))
}
if !regexp.MustCompile(`^[a-zA-Z]`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"first character of %q must be a letter", k))
}
if !regexp.MustCompile(`[a-zA-Z]$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"last character of %q must be a letter", k))
}
if len(value) > 1024 {
errors = append(errors, fmt.Errorf(
"%q cannot be greater than 1024 characters", k))
}
return
}

func validateLbTargetGroupName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 32 {
Expand Down Expand Up @@ -2478,3 +2457,8 @@ func validateRoute53ResolverName(v interface{}, k string) (ws []string, errors [

return
}

var validateServiceDiscoveryNamespaceName = validation.All(
validation.StringLenBetween(1, 1024),
validation.StringMatch(regexp.MustCompile(`^[0-9A-Za-z._-]+$`), ""),
)
33 changes: 33 additions & 0 deletions aws/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3146,3 +3146,36 @@ func TestValidateRoute53ResolverName(t *testing.T) {
}
}
}

func TestValidateServiceDiscoveryNamespaceName(t *testing.T) {
validNames := []string{
"ValidName",
"V_-.dN01e",
"0",
".",
"-",
"_",
strings.Repeat("x", 1024),
}
for _, v := range validNames {
_, errors := validateServiceDiscoveryNamespaceName(v, "name")
if len(errors) != 0 {
t.Fatalf("%q should be a valid namespace name: %q", v, errors)
}
}

invalidNames := []string{
"Inval:dName",
"Invalid Name",
"*",
"",
// length > 512
strings.Repeat("x", 1025),
}
for _, v := range invalidNames {
_, errors := validateServiceDiscoveryNamespaceName(v, "name")
if len(errors) == 0 {
t.Fatalf("%q should be an invalid namespace name", v)
}
}
}

0 comments on commit 116a671

Please sign in to comment.