Skip to content

Commit

Permalink
r/aws_sqs_queue: append .fifo suffix for FIFO queue if name unspecifi…
Browse files Browse the repository at this point in the history
…ed (#17164)

* fix: append .fifo suffix for FIFO queue if name unspecified

* test: add test for unnamed fifo queues

* r/aws_sqs_queue: Make name generation and validation consistent with aws_sns_topic (#15828).

Acceptance test output:

% make testacc TEST=./aws TESTARGS='-run=TestAccAWSSQSQueue_' ACCTEST_PARALLELISM=4
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 4 -run=TestAccAWSSQSQueue_ -timeout 180m
=== RUN   TestAccAWSSQSQueue_basic
=== PAUSE TestAccAWSSQSQueue_basic
=== RUN   TestAccAWSSQSQueue_tags
=== PAUSE TestAccAWSSQSQueue_tags
=== RUN   TestAccAWSSQSQueue_namePrefix
=== PAUSE TestAccAWSSQSQueue_namePrefix
=== RUN   TestAccAWSSQSQueue_namePrefix_fifo
=== PAUSE TestAccAWSSQSQueue_namePrefix_fifo
=== RUN   TestAccAWSSQSQueue_policy
=== PAUSE TestAccAWSSQSQueue_policy
=== RUN   TestAccAWSSQSQueue_queueDeletedRecently
=== PAUSE TestAccAWSSQSQueue_queueDeletedRecently
=== RUN   TestAccAWSSQSQueue_redrivePolicy
=== PAUSE TestAccAWSSQSQueue_redrivePolicy
=== RUN   TestAccAWSSQSQueue_Policybasic
=== PAUSE TestAccAWSSQSQueue_Policybasic
=== RUN   TestAccAWSSQSQueue_FIFO
=== PAUSE TestAccAWSSQSQueue_FIFO
=== RUN   TestAccAWSSQSQueue_FIFOExpectNameError
=== PAUSE TestAccAWSSQSQueue_FIFOExpectNameError
=== RUN   TestAccAWSSQSQueue_FIFOWithContentBasedDeduplication
=== PAUSE TestAccAWSSQSQueue_FIFOWithContentBasedDeduplication
=== RUN   TestAccAWSSQSQueue_ExpectContentBasedDeduplicationError
=== PAUSE TestAccAWSSQSQueue_ExpectContentBasedDeduplicationError
=== RUN   TestAccAWSSQSQueue_Encryption
=== PAUSE TestAccAWSSQSQueue_Encryption
=== RUN   TestAccAWSSQSQueue_FIFO_MinusName
=== PAUSE TestAccAWSSQSQueue_FIFO_MinusName
=== CONT  TestAccAWSSQSQueue_basic
=== CONT  TestAccAWSSQSQueue_FIFO
=== CONT  TestAccAWSSQSQueue_Encryption
=== CONT  TestAccAWSSQSQueue_policy
--- PASS: TestAccAWSSQSQueue_FIFO (14.10s)
=== CONT  TestAccAWSSQSQueue_Policybasic
--- PASS: TestAccAWSSQSQueue_Encryption (14.34s)
=== CONT  TestAccAWSSQSQueue_redrivePolicy
--- PASS: TestAccAWSSQSQueue_redrivePolicy (14.96s)
=== CONT  TestAccAWSSQSQueue_queueDeletedRecently
--- PASS: TestAccAWSSQSQueue_basic (33.02s)
=== CONT  TestAccAWSSQSQueue_FIFOWithContentBasedDeduplication
--- PASS: TestAccAWSSQSQueue_FIFOWithContentBasedDeduplication (11.85s)
=== CONT  TestAccAWSSQSQueue_ExpectContentBasedDeduplicationError
--- PASS: TestAccAWSSQSQueue_ExpectContentBasedDeduplicationError (1.64s)
=== CONT  TestAccAWSSQSQueue_namePrefix_fifo
--- PASS: TestAccAWSSQSQueue_queueDeletedRecently (17.96s)
=== CONT  TestAccAWSSQSQueue_FIFOExpectNameError
--- PASS: TestAccAWSSQSQueue_FIFOExpectNameError (1.59s)
=== CONT  TestAccAWSSQSQueue_FIFO_MinusName
--- PASS: TestAccAWSSQSQueue_namePrefix_fifo (13.39s)
=== CONT  TestAccAWSSQSQueue_tags
--- PASS: TestAccAWSSQSQueue_FIFO_MinusName (13.12s)
=== CONT  TestAccAWSSQSQueue_namePrefix
--- PASS: TestAccAWSSQSQueue_policy (66.36s)
--- PASS: TestAccAWSSQSQueue_namePrefix (11.90s)
--- PASS: TestAccAWSSQSQueue_Policybasic (64.42s)
--- PASS: TestAccAWSSQSQueue_tags (30.27s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	94.367s

* r/aws_sqs_queue: Additional name generation tests.

Acceptance test output:

% make testacc TEST=./aws TESTARGS='-run=TestAccAWSSQSQueue_' ACCTEST_PARALLELISM=4
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 4 -run=TestAccAWSSQSQueue_ -timeout 180m
=== RUN   TestAccAWSSQSQueue_basic
=== PAUSE TestAccAWSSQSQueue_basic
=== RUN   TestAccAWSSQSQueue_tags
=== PAUSE TestAccAWSSQSQueue_tags
=== RUN   TestAccAWSSQSQueue_Name_Generated
=== PAUSE TestAccAWSSQSQueue_Name_Generated
=== RUN   TestAccAWSSQSQueue_Name_Generated_FIFOQueue
=== PAUSE TestAccAWSSQSQueue_Name_Generated_FIFOQueue
=== RUN   TestAccAWSSQSQueue_NamePrefix
=== PAUSE TestAccAWSSQSQueue_NamePrefix
=== RUN   TestAccAWSSQSQueue_NamePrefix_FIFOQueue
=== PAUSE TestAccAWSSQSQueue_NamePrefix_FIFOQueue
=== RUN   TestAccAWSSQSQueue_policy
=== PAUSE TestAccAWSSQSQueue_policy
=== RUN   TestAccAWSSQSQueue_queueDeletedRecently
=== PAUSE TestAccAWSSQSQueue_queueDeletedRecently
=== RUN   TestAccAWSSQSQueue_redrivePolicy
=== PAUSE TestAccAWSSQSQueue_redrivePolicy
=== RUN   TestAccAWSSQSQueue_Policybasic
=== PAUSE TestAccAWSSQSQueue_Policybasic
=== RUN   TestAccAWSSQSQueue_FIFO
=== PAUSE TestAccAWSSQSQueue_FIFO
=== RUN   TestAccAWSSQSQueue_FIFOExpectNameError
=== PAUSE TestAccAWSSQSQueue_FIFOExpectNameError
=== RUN   TestAccAWSSQSQueue_FIFOWithContentBasedDeduplication
=== PAUSE TestAccAWSSQSQueue_FIFOWithContentBasedDeduplication
=== RUN   TestAccAWSSQSQueue_ExpectContentBasedDeduplicationError
=== PAUSE TestAccAWSSQSQueue_ExpectContentBasedDeduplicationError
=== RUN   TestAccAWSSQSQueue_Encryption
=== PAUSE TestAccAWSSQSQueue_Encryption
=== CONT  TestAccAWSSQSQueue_basic
=== CONT  TestAccAWSSQSQueue_redrivePolicy
=== CONT  TestAccAWSSQSQueue_NamePrefix
=== CONT  TestAccAWSSQSQueue_queueDeletedRecently
--- PASS: TestAccAWSSQSQueue_NamePrefix (14.27s)
=== CONT  TestAccAWSSQSQueue_policy
--- PASS: TestAccAWSSQSQueue_redrivePolicy (16.99s)
=== CONT  TestAccAWSSQSQueue_Name_Generated
--- PASS: TestAccAWSSQSQueue_queueDeletedRecently (20.58s)
=== CONT  TestAccAWSSQSQueue_Name_Generated_FIFOQueue
--- PASS: TestAccAWSSQSQueue_Name_Generated (12.20s)
=== CONT  TestAccAWSSQSQueue_FIFOWithContentBasedDeduplication
--- PASS: TestAccAWSSQSQueue_Name_Generated_FIFOQueue (12.84s)
=== CONT  TestAccAWSSQSQueue_NamePrefix_FIFOQueue
--- PASS: TestAccAWSSQSQueue_basic (34.10s)
=== CONT  TestAccAWSSQSQueue_tags
--- PASS: TestAccAWSSQSQueue_FIFOWithContentBasedDeduplication (13.36s)
=== CONT  TestAccAWSSQSQueue_ExpectContentBasedDeduplicationError
--- PASS: TestAccAWSSQSQueue_ExpectContentBasedDeduplicationError (1.61s)
=== CONT  TestAccAWSSQSQueue_Encryption
--- PASS: TestAccAWSSQSQueue_NamePrefix_FIFOQueue (12.78s)
=== CONT  TestAccAWSSQSQueue_FIFO
--- PASS: TestAccAWSSQSQueue_Encryption (11.97s)
=== CONT  TestAccAWSSQSQueue_FIFOExpectNameError
--- PASS: TestAccAWSSQSQueue_FIFOExpectNameError (1.62s)
=== CONT  TestAccAWSSQSQueue_Policybasic
--- PASS: TestAccAWSSQSQueue_FIFO (11.88s)
--- PASS: TestAccAWSSQSQueue_tags (30.67s)
--- PASS: TestAccAWSSQSQueue_policy (64.88s)
--- PASS: TestAccAWSSQSQueue_Policybasic (63.94s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	124.792s

* r/aws_sns_topic: Remove 'ImportStateVerifyIgnore' for 'name_prefix' in acceptance tests.

Acceptance test output:

% make testacc TEST=./aws TESTARGS='-run=TestAccAWSSNSTopic_' ACCTEST_PARALLELISM=4
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 4 -run=TestAccAWSSNSTopic_ -timeout 180m
=== RUN   TestAccAWSSNSTopic_basic
=== PAUSE TestAccAWSSNSTopic_basic
=== RUN   TestAccAWSSNSTopic_Name
=== PAUSE TestAccAWSSNSTopic_Name
=== RUN   TestAccAWSSNSTopic_NamePrefix
=== PAUSE TestAccAWSSNSTopic_NamePrefix
=== RUN   TestAccAWSSNSTopic_policy
=== PAUSE TestAccAWSSNSTopic_policy
=== RUN   TestAccAWSSNSTopic_withIAMRole
=== PAUSE TestAccAWSSNSTopic_withIAMRole
=== RUN   TestAccAWSSNSTopic_withFakeIAMRole
=== PAUSE TestAccAWSSNSTopic_withFakeIAMRole
=== RUN   TestAccAWSSNSTopic_withDeliveryPolicy
=== PAUSE TestAccAWSSNSTopic_withDeliveryPolicy
=== RUN   TestAccAWSSNSTopic_deliveryStatus
=== PAUSE TestAccAWSSNSTopic_deliveryStatus
=== RUN   TestAccAWSSNSTopic_Name_Generated_FIFOTopic
=== PAUSE TestAccAWSSNSTopic_Name_Generated_FIFOTopic
=== RUN   TestAccAWSSNSTopic_Name_FIFOTopic
=== PAUSE TestAccAWSSNSTopic_Name_FIFOTopic
=== RUN   TestAccAWSSNSTopic_NamePrefix_FIFOTopic
=== PAUSE TestAccAWSSNSTopic_NamePrefix_FIFOTopic
=== RUN   TestAccAWSSNSTopic_FIFOWithContentBasedDeduplication
=== PAUSE TestAccAWSSNSTopic_FIFOWithContentBasedDeduplication
=== RUN   TestAccAWSSNSTopic_FIFOExpectContentBasedDeduplicationError
=== PAUSE TestAccAWSSNSTopic_FIFOExpectContentBasedDeduplicationError
=== RUN   TestAccAWSSNSTopic_encryption
=== PAUSE TestAccAWSSNSTopic_encryption
=== RUN   TestAccAWSSNSTopic_tags
=== PAUSE TestAccAWSSNSTopic_tags
=== CONT  TestAccAWSSNSTopic_basic
=== CONT  TestAccAWSSNSTopic_Name_Generated_FIFOTopic
=== CONT  TestAccAWSSNSTopic_FIFOWithContentBasedDeduplication
=== CONT  TestAccAWSSNSTopic_FIFOExpectContentBasedDeduplicationError
--- PASS: TestAccAWSSNSTopic_FIFOExpectContentBasedDeduplicationError (3.24s)
=== CONT  TestAccAWSSNSTopic_NamePrefix_FIFOTopic
--- PASS: TestAccAWSSNSTopic_basic (17.44s)
=== CONT  TestAccAWSSNSTopic_Name_FIFOTopic
--- PASS: TestAccAWSSNSTopic_Name_Generated_FIFOTopic (17.45s)
=== CONT  TestAccAWSSNSTopic_withIAMRole
--- PASS: TestAccAWSSNSTopic_NamePrefix_FIFOTopic (16.26s)
=== CONT  TestAccAWSSNSTopic_deliveryStatus
--- PASS: TestAccAWSSNSTopic_FIFOWithContentBasedDeduplication (29.43s)
=== CONT  TestAccAWSSNSTopic_withDeliveryPolicy
--- PASS: TestAccAWSSNSTopic_Name_FIFOTopic (13.79s)
=== CONT  TestAccAWSSNSTopic_withFakeIAMRole
--- PASS: TestAccAWSSNSTopic_withIAMRole (23.62s)
=== CONT  TestAccAWSSNSTopic_Name
--- PASS: TestAccAWSSNSTopic_withDeliveryPolicy (14.49s)
=== CONT  TestAccAWSSNSTopic_policy
--- PASS: TestAccAWSSNSTopic_deliveryStatus (34.30s)
=== CONT  TestAccAWSSNSTopic_tags
--- PASS: TestAccAWSSNSTopic_Name (13.55s)
=== CONT  TestAccAWSSNSTopic_NamePrefix
--- PASS: TestAccAWSSNSTopic_policy (15.40s)
=== CONT  TestAccAWSSNSTopic_encryption
--- PASS: TestAccAWSSNSTopic_NamePrefix (13.83s)
--- PASS: TestAccAWSSNSTopic_encryption (26.86s)
--- PASS: TestAccAWSSNSTopic_tags (38.04s)
--- PASS: TestAccAWSSNSTopic_withFakeIAMRole (125.75s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	160.023s

Co-authored-by: Kit Ewbank <Kit_Ewbank@hotmail.com>
  • Loading branch information
drexler and ewbankkit authored Apr 15, 2021
1 parent 94b6bcc commit f47d5cb
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 226 deletions.
5 changes: 5 additions & 0 deletions aws/internal/service/sqs/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sqs

const (
FifoQueueNameSuffix = ".fifo"
)
14 changes: 6 additions & 8 deletions aws/resource_aws_sns_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,9 @@ func TestAccAWSSNSTopic_NamePrefix(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name_prefix"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down Expand Up @@ -390,10 +389,9 @@ func TestAccAWSSNSTopic_NamePrefix_FIFOTopic(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name_prefix"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down
89 changes: 58 additions & 31 deletions aws/resource_aws_sqs_queue.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package aws

import (
"context"
"fmt"
"log"
"net/url"
"regexp"
"strconv"
"strings"
"time"
Expand All @@ -17,6 +19,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/naming"
tfsqs "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/sqs"
)

var sqsQueueAttributeMap = map[string]string{
Expand Down Expand Up @@ -46,19 +50,20 @@ func resourceAwsSqsQueue() *schema.Resource {
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
CustomizeDiff: resourceAwsSqsQueueCustomizeDiff,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: validateSQSQueueName,
},
"name_prefix": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name"},
},
Expand Down Expand Up @@ -136,34 +141,12 @@ func resourceAwsSqsQueueCreate(d *schema.ResourceData, meta interface{}) error {
sqsconn := meta.(*AWSClient).sqsconn

var name string
fifoQueue := d.Get("fifo_queue").(bool)

fq := d.Get("fifo_queue").(bool)

if v, ok := d.GetOk("name"); ok {
name = v.(string)
} else if v, ok := d.GetOk("name_prefix"); ok {
name = resource.PrefixedUniqueId(v.(string))
if fq {
name += ".fifo"
}
} else {
name = resource.UniqueId()
}

cbd := d.Get("content_based_deduplication").(bool)

if fq {
if errors := validateSQSFifoQueueName(name); len(errors) > 0 {
return fmt.Errorf("Error validating the FIFO queue name: %v", errors)
}
if fifoQueue {
name = naming.GenerateWithSuffix(d.Get("name").(string), d.Get("name_prefix").(string), tfsqs.FifoQueueNameSuffix)
} else {
if errors := validateSQSNonFifoQueueName(name); len(errors) > 0 {
return fmt.Errorf("Error validating SQS queue name: %v", errors)
}
}

if !fq && cbd {
return fmt.Errorf("Content based deduplication can only be set with FIFO queues")
name = naming.Generate(d.Get("name").(string), d.Get("name_prefix").(string))
}

log.Printf("[DEBUG] SQS queue create: %s", name)
Expand Down Expand Up @@ -301,16 +284,16 @@ func resourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error {
return err
}

fifoQueue := false

// Always set attribute defaults
d.Set("arn", "")
d.Set("content_based_deduplication", false)
d.Set("delay_seconds", 0)
d.Set("fifo_queue", false)
d.Set("kms_data_key_reuse_period_seconds", 300)
d.Set("kms_master_key_id", "")
d.Set("max_message_size", 262144)
d.Set("message_retention_seconds", 345600)
d.Set("name", name)
d.Set("policy", "")
d.Set("receive_wait_time_seconds", 0)
d.Set("redrive_policy", "")
Expand Down Expand Up @@ -350,7 +333,7 @@ func resourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error parsing fifo_queue value (%s) into boolean: %s", v, err)
}

d.Set("fifo_queue", vBool)
fifoQueue = vBool
}

if v, ok := queueAttributes[sqs.QueueAttributeNameKmsDataKeyReusePeriodSeconds]; ok && v != "" {
Expand Down Expand Up @@ -416,6 +399,14 @@ func resourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error {
}
}

d.Set("fifo_queue", fifoQueue)
d.Set("name", name)
if fifoQueue {
d.Set("name_prefix", naming.NamePrefixFromNameWithSuffix(name, tfsqs.FifoQueueNameSuffix))
} else {
d.Set("name_prefix", naming.NamePrefixFromName(name))
}

tags, err := keyvaluetags.SqsListTags(sqsconn, d.Id())

if err != nil {
Expand Down Expand Up @@ -444,6 +435,42 @@ func resourceAwsSqsQueueDelete(d *schema.ResourceData, meta interface{}) error {
return err
}

func resourceAwsSqsQueueCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
fifoQueue := diff.Get("fifo_queue").(bool)
contentBasedDeduplication := diff.Get("content_based_deduplication").(bool)

if diff.Id() == "" {
// Create.

var name string

if fifoQueue {
name = naming.GenerateWithSuffix(diff.Get("name").(string), diff.Get("name_prefix").(string), tfsqs.FifoQueueNameSuffix)
} else {
name = naming.Generate(diff.Get("name").(string), diff.Get("name_prefix").(string))
}

var re *regexp.Regexp

if fifoQueue {
re = regexp.MustCompile(`^[a-zA-Z0-9_-]{1,75}\.fifo$`)
} else {
re = regexp.MustCompile(`^[a-zA-Z0-9_-]{1,80}$`)
}

if !re.MatchString(name) {
return fmt.Errorf("invalid queue name: %s", name)
}

}

if !fifoQueue && contentBasedDeduplication {
return fmt.Errorf("content-based deduplication can only be set for FIFO queue")
}

return nil
}

func extractNameFromSqsQueueUrl(queue string) (string, error) {
//http://sqs.us-west-2.amazonaws.com/123456789012/queueName
u, err := url.Parse(queue)
Expand Down
Loading

0 comments on commit f47d5cb

Please sign in to comment.