Skip to content

Commit

Permalink
Merge pull request #2837 from sjauld/bugfix/SQS-name-validation
Browse files Browse the repository at this point in the history
r/aws_sqs_queue allow SQS name validation during terraform validate
  • Loading branch information
bflad authored Feb 14, 2018
2 parents dd6f95e + c54155e commit 05768df
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
3 changes: 2 additions & 1 deletion aws/resource_aws_sqs_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func resourceAwsSqsQueue() *schema.Resource {
ForceNew: true,
Computed: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: validateSQSQueueName,
},
"name_prefix": {
Type: schema.TypeString,
Expand Down Expand Up @@ -149,7 +150,7 @@ func resourceAwsSqsQueueCreate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error validating the FIFO queue name: %v", errors)
}
} else {
if errors := validateSQSQueueName(name, "name"); len(errors) > 0 {
if errors := validateSQSNonFifoQueueName(name, "name"); len(errors) > 0 {
return fmt.Errorf("Error validating SQS queue name: %v", errors)
}
}
Expand Down
14 changes: 13 additions & 1 deletion aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,19 @@ func validateApiGatewayIntegrationContentHandling(v interface{}, k string) (ws [
return
}

func validateSQSQueueName(v interface{}, k string) (errors []error) {
func validateSQSQueueName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 80 {
errors = append(errors, fmt.Errorf("%q cannot be longer than 80 characters", k))
}

if !regexp.MustCompile(`^[0-9A-Za-z-_]+(\.fifo)?$`).MatchString(value) {
errors = append(errors, fmt.Errorf("only alphanumeric characters and hyphens allowed in %q", k))
}
return
}

func validateSQSNonFifoQueueName(v interface{}, k string) (errors []error) {
value := v.(string)
if len(value) > 80 {
errors = append(errors, fmt.Errorf("%q cannot be longer than 80 characters", k))
Expand Down
20 changes: 18 additions & 2 deletions aws/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1061,9 +1061,13 @@ func TestValidateSQSQueueName(t *testing.T) {
strings.Repeat("W", 80),
}
for _, v := range validNames {
if errors := validateSQSQueueName(v, "name"); len(errors) > 0 {
if _, errors := validateSQSQueueName(v, "name"); len(errors) > 0 {
t.Fatalf("%q should be a valid SQS queue Name", v)
}

if errors := validateSQSNonFifoQueueName(v, "name"); len(errors) > 0 {
t.Fatalf("%q should be a valid SQS non-fifo queue Name", v)
}
}

invalidNames := []string{
Expand All @@ -1078,9 +1082,13 @@ func TestValidateSQSQueueName(t *testing.T) {
strings.Repeat("W", 81), // length > 80
}
for _, v := range invalidNames {
if errors := validateSQSQueueName(v, "name"); len(errors) == 0 {
if _, errors := validateSQSQueueName(v, "name"); len(errors) == 0 {
t.Fatalf("%q should be an invalid SQS queue Name", v)
}

if errors := validateSQSNonFifoQueueName(v, "name"); len(errors) == 0 {
t.Fatalf("%q should be an invalid SQS non-fifo queue Name", v)
}
}
}

Expand All @@ -1097,6 +1105,10 @@ func TestValidateSQSFifoQueueName(t *testing.T) {
fmt.Sprintf("%s.fifo", strings.Repeat("W", 75)),
}
for _, v := range validNames {
if _, errors := validateSQSQueueName(v, "name"); len(errors) > 0 {
t.Fatalf("%q should be a valid SQS queue Name", v)
}

if errors := validateSQSFifoQueueName(v, "name"); len(errors) > 0 {
t.Fatalf("%q should be a valid SQS FIFO queue Name: %v", v, errors)
}
Expand All @@ -1115,6 +1127,10 @@ func TestValidateSQSFifoQueueName(t *testing.T) {
strings.Repeat("W", 81), // length > 80
}
for _, v := range invalidNames {
if _, errors := validateSQSQueueName(v, "name"); len(errors) == 0 {
t.Fatalf("%q should be an invalid SQS queue Name", v)
}

if errors := validateSQSFifoQueueName(v, "name"); len(errors) == 0 {
t.Fatalf("%q should be an invalid SQS FIFO queue Name: %v", v, errors)
}
Expand Down

0 comments on commit 05768df

Please sign in to comment.