Skip to content

Commit

Permalink
provider/aws: Add support for name_prefix to aws_sqs_queue
Browse files Browse the repository at this point in the history
Fixes: #831
  • Loading branch information
stack72 committed Jun 14, 2017
1 parent 52d5bcb commit e4d6e5d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
20 changes: 18 additions & 2 deletions aws/resource_aws_sqs_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/hashicorp/terraform/helper/resource"
)

var AttributeMap = map[string]string{
Expand Down Expand Up @@ -43,8 +44,15 @@ func resourceAwsSqsQueue() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ConflictsWith: []string{"name_prefix"},
},
"name_prefix": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
},
"delay_seconds": {
Expand Down Expand Up @@ -110,7 +118,15 @@ func resourceAwsSqsQueue() *schema.Resource {
func resourceAwsSqsQueueCreate(d *schema.ResourceData, meta interface{}) error {
sqsconn := meta.(*AWSClient).sqsconn

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

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

Expand Down
47 changes: 46 additions & 1 deletion aws/resource_aws_sqs_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"fmt"
"testing"

"regexp"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/jen20/awspolicyequivalence"
"regexp"
)

func TestAccAWSSQSQueue_basic(t *testing.T) {
Expand Down Expand Up @@ -43,6 +45,24 @@ func TestAccAWSSQSQueue_basic(t *testing.T) {
})
}

func TestAccAWSSQSQueue_namePrefix(t *testing.T) {
prefix := "acctest-sqs-queue"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSQSQueueDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSQSConfigWithNamePrefix(prefix),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.queue"),
testAccCheckAWSSQSGeneratedNamePrefix("aws_sqs_queue.queue", "acctest-sqs-queue"),
),
},
},
})
}

func TestAccAWSSQSQueue_policy(t *testing.T) {
queueName := fmt.Sprintf("sqs-queue-%s", acctest.RandString(10))
topicName := fmt.Sprintf("sns-topic-%s", acctest.RandString(10))
Expand Down Expand Up @@ -299,6 +319,23 @@ func testAccCheckAWSSQSExistsWithDefaults(n string) resource.TestCheckFunc {
}
}

func testAccCheckAWSSQSGeneratedNamePrefix(resource, prefix string) resource.TestCheckFunc {
return func(s *terraform.State) error {
r, ok := s.RootModule().Resources[resource]
if !ok {
return fmt.Errorf("Resource not found")
}
name, ok := r.Primary.Attributes["name"]
if !ok {
return fmt.Errorf("Name attr not found: %#v", r.Primary.Attributes)
}
if !strings.HasPrefix(name, prefix) {
return fmt.Errorf("Name: %q, does not have prefix: %q", name, prefix)
}
return nil
}
}

func testAccCheckAWSSQSExistsWithOverrides(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -357,6 +394,14 @@ resource "aws_sqs_queue" "queue" {
`, r)
}

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

func testAccAWSSQSFifoConfigWithDefaults(r string) string {
return fmt.Sprintf(`
resource "aws_sqs_queue" "queue" {
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/sqs_queue.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ resource "aws_sqs_queue" "terraform_queue" {

The following arguments are supported:

* `name` - (Required) This is the human-readable name of the queue
* `name` - (Optional) This is the human-readable name of the queue. If omitted, Terraform will assign a random name.
* `name_prefix` - (Optional) Creates a unique name beginning with the specified prefix. Conflicts with `name`.
* `visibility_timeout_seconds` - (Optional) The visibility timeout for the queue. An integer from 0 to 43200 (12 hours). The default for this attribute is 30. For more information about visibility timeout, see [AWS docs](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/AboutVT.html).
* `message_retention_seconds` - (Optional) The number of seconds Amazon SQS retains a message. Integer representing seconds, from 60 (1 minute) to 1209600 (14 days). The default for this attribute is 345600 (4 days).
* `max_message_size` - (Optional) The limit of how many bytes a message can contain before Amazon SQS rejects it. An integer from 1024 bytes (1 KiB) up to 262144 bytes (256 KiB). The default for this attribute is 262144 (256 KiB).
Expand Down

0 comments on commit e4d6e5d

Please sign in to comment.