Skip to content

Commit

Permalink
Merge pull request #1987 from ewbankkit/issue-1983
Browse files Browse the repository at this point in the history
Add support for SQS queue tags
  • Loading branch information
radeksimko authored Oct 27, 2017
2 parents 59afb0a + ecee8f9 commit 39611c5
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 25 deletions.
84 changes: 67 additions & 17 deletions aws/resource_aws_sqs_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ import (
"github.com/hashicorp/terraform/helper/resource"
)

var AttributeMap = map[string]string{
"delay_seconds": "DelaySeconds",
"max_message_size": "MaximumMessageSize",
"message_retention_seconds": "MessageRetentionPeriod",
"receive_wait_time_seconds": "ReceiveMessageWaitTimeSeconds",
"visibility_timeout_seconds": "VisibilityTimeout",
"policy": "Policy",
"redrive_policy": "RedrivePolicy",
"arn": "QueueArn",
"fifo_queue": "FifoQueue",
"content_based_deduplication": "ContentBasedDeduplication",
"kms_master_key_id": "KmsMasterKeyId",
"kms_data_key_reuse_period_seconds": "KmsDataKeyReusePeriodSeconds",
var sqsQueueAttributeMap = map[string]string{
"delay_seconds": sqs.QueueAttributeNameDelaySeconds,
"max_message_size": sqs.QueueAttributeNameMaximumMessageSize,
"message_retention_seconds": sqs.QueueAttributeNameMessageRetentionPeriod,
"receive_wait_time_seconds": sqs.QueueAttributeNameReceiveMessageWaitTimeSeconds,
"visibility_timeout_seconds": sqs.QueueAttributeNameVisibilityTimeout,
"policy": sqs.QueueAttributeNamePolicy,
"redrive_policy": sqs.QueueAttributeNameRedrivePolicy,
"arn": sqs.QueueAttributeNameQueueArn,
"fifo_queue": sqs.QueueAttributeNameFifoQueue,
"content_based_deduplication": sqs.QueueAttributeNameContentBasedDeduplication,
"kms_master_key_id": sqs.QueueAttributeNameKmsMasterKeyId,
"kms_data_key_reuse_period_seconds": sqs.QueueAttributeNameKmsDataKeyReusePeriodSeconds,
}

// A number of these are marked as computed because if you don't
Expand Down Expand Up @@ -122,6 +122,7 @@ func resourceAwsSqsQueue() *schema.Resource {
Computed: true,
Optional: true,
},
"tags": tagsSchema(),
},
}
}
Expand Down Expand Up @@ -166,7 +167,7 @@ func resourceAwsSqsQueueCreate(d *schema.ResourceData, meta interface{}) error {
resource := *resourceAwsSqsQueue()

for k, s := range resource.Schema {
if attrKey, ok := AttributeMap[k]; ok {
if attrKey, ok := sqsQueueAttributeMap[k]; ok {
if value, ok := d.GetOk(k); ok {
switch s.Type {
case schema.TypeInt:
Expand All @@ -190,19 +191,24 @@ func resourceAwsSqsQueueCreate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error creating SQS queue: %s", err)
}

d.SetId(*output.QueueUrl)
d.SetId(aws.StringValue(output.QueueUrl))

return resourceAwsSqsQueueUpdate(d, meta)
}

func resourceAwsSqsQueueUpdate(d *schema.ResourceData, meta interface{}) error {
sqsconn := meta.(*AWSClient).sqsconn

if err := setTagsSQS(sqsconn, d); err != nil {
return err
}

attributes := make(map[string]*string)

resource := *resourceAwsSqsQueue()

for k, s := range resource.Schema {
if attrKey, ok := AttributeMap[k]; ok {
if attrKey, ok := sqsQueueAttributeMap[k]; ok {
if d.HasChange(k) {
log.Printf("[DEBUG] Updating %s", attrKey)
_, n := d.GetChange(k)
Expand Down Expand Up @@ -261,7 +267,7 @@ func resourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error {
attrmap := attributeOutput.Attributes
resource := *resourceAwsSqsQueue()
// iKey = internal struct key, oKey = AWS Attribute Map key
for iKey, oKey := range AttributeMap {
for iKey, oKey := range sqsQueueAttributeMap {
if attrmap[oKey] != nil {
switch resource.Schema[iKey].Type {
case schema.TypeInt:
Expand Down Expand Up @@ -292,6 +298,14 @@ func resourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error {
d.Set("fifo_queue", d.Get("fifo_queue").(bool))
d.Set("content_based_deduplication", d.Get("content_based_deduplication").(bool))

listTagsOutput, err := sqsconn.ListQueueTags(&sqs.ListQueueTagsInput{
QueueUrl: aws.String(d.Id()),
})
if err != nil {
return err
}
d.Set("tags", tagsToMapGeneric(listTagsOutput.Tags))

return nil
}

Expand Down Expand Up @@ -322,3 +336,39 @@ func extractNameFromSqsQueueUrl(queue string) (string, error) {
return segments[2], nil

}

func setTagsSQS(conn *sqs.SQS, d *schema.ResourceData) error {
if d.HasChange("tags") {
oraw, nraw := d.GetChange("tags")
create, remove := diffTagsGeneric(oraw.(map[string]interface{}), nraw.(map[string]interface{}))

if len(remove) > 0 {
log.Printf("[DEBUG] Removing tags: %#v", remove)
keys := make([]*string, 0, len(remove))
for k, _ := range remove {
keys = append(keys, aws.String(k))
}

_, err := conn.UntagQueue(&sqs.UntagQueueInput{
QueueUrl: aws.String(d.Id()),
TagKeys: keys,
})
if err != nil {
return err
}
}
if len(create) > 0 {
log.Printf("[DEBUG] Creating tags: %#v", create)

_, err := conn.TagQueue(&sqs.TagQueueInput{
QueueUrl: aws.String(d.Id()),
Tags: create,
})
if err != nil {
return err
}
}
}

return nil
}
73 changes: 66 additions & 7 deletions aws/resource_aws_sqs_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,40 @@ func TestAccAWSSQSQueue_basic(t *testing.T) {
})
}

func TestAccAWSSQSQueue_tags(t *testing.T) {
queueName := fmt.Sprintf("sqs-queue-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSQSQueueDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSQSConfigWithTags(queueName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.queue"),
resource.TestCheckResourceAttr("aws_sqs_queue.queue", "tags.%", "2"),
resource.TestCheckResourceAttr("aws_sqs_queue.queue", "tags.Usage", "original"),
),
},
{
Config: testAccAWSSQSConfigWithTagsChanged(queueName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.queue"),
resource.TestCheckResourceAttr("aws_sqs_queue.queue", "tags.%", "1"),
resource.TestCheckResourceAttr("aws_sqs_queue.queue", "tags.Usage", "changed"),
),
},
{
Config: testAccAWSSQSConfigWithDefaults(queueName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.queue"),
resource.TestCheckNoResourceAttr("aws_sqs_queue.queue", "tags"),
),
},
},
})
}

func TestAccAWSSQSQueue_namePrefix(t *testing.T) {
prefix := "acctest-sqs-queue"
resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -407,24 +441,24 @@ func TestAccAWSSQSQueue_Encryption(t *testing.T) {
func testAccAWSSQSConfigWithDefaults(r string) string {
return fmt.Sprintf(`
resource "aws_sqs_queue" "queue" {
name = "%s"
name = "%s"
}
`, r)
}

func testAccAWSSQSConfigWithNamePrefix(r string) string {
return fmt.Sprintf(`
resource "aws_sqs_queue" "queue" {
name_prefix = "%s"
name_prefix = "%s"
}
`, r)
}

func testAccAWSSQSFifoConfigWithDefaults(r string) string {
return fmt.Sprintf(`
resource "aws_sqs_queue" "queue" {
name = "%s.fifo"
fifo_queue = true
name = "%s.fifo"
fifo_queue = true
}
`, r)
}
Expand All @@ -450,8 +484,8 @@ resource "aws_sqs_queue" "my_queue" {
redrive_policy = <<EOF
{
"maxReceiveCount": 3,
"deadLetterTargetArn": "${aws_sqs_queue.my_dead_letter_queue.arn}"
"maxReceiveCount": 3,
"deadLetterTargetArn": "${aws_sqs_queue.my_dead_letter_queue.arn}"
}
EOF
}
Expand Down Expand Up @@ -557,7 +591,32 @@ func testAccAWSSQSConfigWithEncryption(queue string) string {
resource "aws_sqs_queue" "queue" {
name = "%s"
kms_master_key_id = "alias/aws/sqs"
kms_data_key_reuse_period_seconds = 300
kms_data_key_reuse_period_seconds = 300
}
`, queue)
}

func testAccAWSSQSConfigWithTags(r string) string {
return fmt.Sprintf(`
resource "aws_sqs_queue" "queue" {
name = "%s"
tags {
Environment = "production"
Usage = "original"
}
}
`, r)
}

func testAccAWSSQSConfigWithTagsChanged(r string) string {
return fmt.Sprintf(`
resource "aws_sqs_queue" "queue" {
name = "%s"
tags {
Usage = "changed"
}
}
`, r)
}
7 changes: 6 additions & 1 deletion website/docs/r/sqs_queue.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: |-
Provides a SQS resource.
---

# aws\_sqs\_queue
# aws_sqs_queue

## Example Usage

Expand All @@ -18,6 +18,10 @@ resource "aws_sqs_queue" "terraform_queue" {
message_retention_seconds = 86400
receive_wait_time_seconds = 10
redrive_policy = "{\"deadLetterTargetArn\":\"${aws_sqs_queue.terraform_queue_deadletter.arn}\",\"maxReceiveCount\":4}"
tags {
Environment = "production"
}
}
```

Expand Down Expand Up @@ -58,6 +62,7 @@ The following arguments are supported:
* `content_based_deduplication` - (Optional) Enables content-based deduplication for FIFO queues. For more information, see the [related documentation](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/FIFO-queues.html#FIFO-queues-exactly-once-processing)
* `kms_master_key_id` - (Optional) The ID of an AWS-managed customer master key (CMK) for Amazon SQS or a custom CMK. For more information, see [Key Terms](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-server-side-encryption.html#sqs-sse-key-terms).
* `kms_data_key_reuse_period_seconds` - (Optional) The length of time, in seconds, for which Amazon SQS can reuse a data key to encrypt or decrypt messages before calling AWS KMS again. An integer representing seconds, between 60 seconds (1 minute) and 86,400 seconds (24 hours). The default is 300 (5 minutes).
* `tags` - (Optional) A mapping of tags to assign to the queue.

## Attributes Reference

Expand Down

0 comments on commit 39611c5

Please sign in to comment.