Added docs, tests, and updated SQS code

This commit is contained in:
John Ewart 2015-05-15 13:09:20 -07:00
parent 8dd479dbe0
commit d538194f59
4 changed files with 237 additions and 7 deletions

View File

@ -22,6 +22,9 @@ var AttributeMap = map[string]string{
} }
// A number of these are marked as computed because if you don't
// provide a value, SQS will provide you with defaults (which are the
// default values specified below)
func resourceAwsSqsQueue() *schema.Resource { func resourceAwsSqsQueue() *schema.Resource {
return &schema.Resource{ return &schema.Resource{
Create: resourceAwsSqsQueueCreate, Create: resourceAwsSqsQueueCreate,
@ -30,7 +33,7 @@ func resourceAwsSqsQueue() *schema.Resource {
Delete: resourceAwsSqsQueueDelete, Delete: resourceAwsSqsQueueDelete,
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"queue": &schema.Schema{ "name": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
ForceNew: true, ForceNew: true,
@ -38,22 +41,27 @@ func resourceAwsSqsQueue() *schema.Resource {
"delay_seconds": &schema.Schema{ "delay_seconds": &schema.Schema{
Type: schema.TypeInt, Type: schema.TypeInt,
Optional: true, Optional: true,
Computed: true,
}, },
"max_message_size": &schema.Schema{ "max_message_size": &schema.Schema{
Type: schema.TypeInt, Type: schema.TypeInt,
Optional: true, Optional: true,
Computed: true,
}, },
"message_retention_seconds": &schema.Schema{ "message_retention_seconds": &schema.Schema{
Type: schema.TypeInt, Type: schema.TypeInt,
Optional: true, Optional: true,
Computed: true,
}, },
"receive_wait_time_seconds": &schema.Schema{ "receive_wait_time_seconds": &schema.Schema{
Type: schema.TypeInt, Type: schema.TypeInt,
Optional: true, Optional: true,
Computed: true,
}, },
"visibility_timeout_seconds": &schema.Schema{ "visibility_timeout_seconds": &schema.Schema{
Type: schema.TypeInt, Type: schema.TypeInt,
Optional: true, Optional: true,
Computed: true,
}, },
"policy": &schema.Schema{ "policy": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
@ -70,12 +78,12 @@ func resourceAwsSqsQueue() *schema.Resource {
func resourceAwsSqsQueueCreate(d *schema.ResourceData, meta interface{}) error { func resourceAwsSqsQueueCreate(d *schema.ResourceData, meta interface{}) error {
sqsconn := meta.(*AWSClient).sqsconn sqsconn := meta.(*AWSClient).sqsconn
queue := d.Get("queue").(string) name := d.Get("name").(string)
log.Printf("[DEBUG] SQS queue create: %s", queue) log.Printf("[DEBUG] SQS queue create: %s", name)
req := &sqs.CreateQueueInput{ req := &sqs.CreateQueueInput{
QueueName: aws.String(queue), QueueName: aws.String(name),
} }
attributes := make(map[string]*string) attributes := make(map[string]*string)
@ -145,6 +153,7 @@ func resourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error {
attributeOutput, err := sqsconn.GetQueueAttributes(&sqs.GetQueueAttributesInput{ attributeOutput, err := sqsconn.GetQueueAttributes(&sqs.GetQueueAttributesInput{
QueueURL: aws.String(d.Id()), QueueURL: aws.String(d.Id()),
AttributeNames: []*string{aws.String("All")},
}) })
if err != nil { if err != nil {
return err return err

View File

@ -0,0 +1,179 @@
package aws
import (
"fmt"
"testing"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/service/sqs"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSSQSQueue(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSQSQueueDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSSQSConfigWithDefaults,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.queue-with-defaults"),
),
},
resource.TestStep{
Config: testAccAWSSQSConfigWithOverrides,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSQSExistsWithOverrides("aws_sqs_queue.queue-with-overrides"),
),
},
},
})
}
func testAccCheckAWSSQSQueueDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).sqsconn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_sqs_queue" {
continue
}
// Check if queue exists by checking for its attributes
params := &sqs.GetQueueAttributesInput{
QueueURL: aws.String(rs.Primary.ID),
}
_, err := conn.GetQueueAttributes(params)
if err == nil {
return fmt.Errorf("Queue %s still exists. Failing!", rs.Primary.ID)
}
// Verify the error is what we want
_, ok := err.(aws.APIError)
if !ok {
return err
}
}
return nil
}
func testAccCheckAWSSQSExistsWithDefaults(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Queue URL specified!")
}
conn := testAccProvider.Meta().(*AWSClient).sqsconn
params := &sqs.GetQueueAttributesInput{
QueueURL: aws.String(rs.Primary.ID),
AttributeNames: []*string{aws.String("All")},
}
resp, err := conn.GetQueueAttributes(params)
if err != nil {
return err
}
// checking if attributes are defaults
for k, v := range *resp.Attributes {
if k == "VisibilityTimeout" && *v != "30" {
return fmt.Errorf("VisibilityTimeout (%s) was not set to 30", *v)
}
if k == "MessageRetentionPeriod" && *v != "345600" {
return fmt.Errorf("MessageRetentionPeriod (%s) was not set to 345600", *v)
}
if k == "MaximumMessageSize" && *v != "262144" {
return fmt.Errorf("MaximumMessageSize (%s) was not set to 262144", *v)
}
if k == "DelaySeconds" && *v != "0" {
return fmt.Errorf("DelaySeconds (%s) was not set to 0", *v)
}
if k == "ReceiveMessageWaitTimeSeconds" && *v != "0" {
return fmt.Errorf("ReceiveMessageWaitTimeSeconds (%s) was not set to 0", *v)
}
}
return nil
}
}
func testAccCheckAWSSQSExistsWithOverrides(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Queue URL specified!")
}
conn := testAccProvider.Meta().(*AWSClient).sqsconn
params := &sqs.GetQueueAttributesInput{
QueueURL: aws.String(rs.Primary.ID),
AttributeNames: []*string{aws.String("All")},
}
resp, err := conn.GetQueueAttributes(params)
if err != nil {
return err
}
// checking if attributes match our overrides
for k, v := range *resp.Attributes {
if k == "VisibilityTimeout" && *v != "60" {
return fmt.Errorf("VisibilityTimeout (%s) was not set to 60", *v)
}
if k == "MessageRetentionPeriod" && *v != "86400" {
return fmt.Errorf("MessageRetentionPeriod (%s) was not set to 86400", *v)
}
if k == "MaximumMessageSize" && *v != "2048" {
return fmt.Errorf("MaximumMessageSize (%s) was not set to 2048", *v)
}
if k == "DelaySeconds" && *v != "90" {
return fmt.Errorf("DelaySeconds (%s) was not set to 90", *v)
}
if k == "ReceiveMessageWaitTimeSeconds" && *v != "10" {
return fmt.Errorf("ReceiveMessageWaitTimeSeconds (%s) was not set to 10", *v)
}
}
return nil
}
}
const testAccAWSSQSConfigWithDefaults = `
resource "aws_sqs_queue" "queue-with-defaults" {
name = "test-sqs-queue-with-defaults"
}
`
const testAccAWSSQSConfigWithOverrides = `
resource "aws_sqs_queue" "queue-with-overrides" {
name = "test-sqs-queue-with-overrides"
delay_seconds = 90
max_message_size = 2048
message_retention_seconds = 86400
receive_wait_time_seconds = 10
visibility_timeout_seconds = 60
}
`

View File

@ -0,0 +1,38 @@
---
layout: "aws"
page_title: "AWS: aws_sqs_queue"
sidebar_current: "docs-aws-resource-sqs"
description: |-
Provides a SQS resource.
---
# aws\_sqs\_queue
## Example Usage
```
resource "aws_sqs_queue" "terrform_queue" {
name = "terraform-example-queue"
delay_seconds = 90
max_message_size = 2048
message_retention_seconds = 86400
receive_wait_time_seconds = 10
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) This is the human-readable name of the queue
* `visibility_timeout_seconds` - (Optional) The time in seconds that the delivery of all messages in the queue will be delayed. An integer from 0 to 900 (15 minutes). The default for this attribute is 30 seconds
* `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).
* `delay_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.
* `receive_wait_time_seconds` - (Optional) The time for which a ReceiveMessage call will wait for a message to arrive (long polling) before returning. An integer from 0 to 20 (seconds). The default for this attribute is 0, meaning that the call will return immediately.
* `policy` - (Optional) The JSON policy for the SQS queue
## Attributes Reference
The following attributes are exported:
* `id` - The URL for the created Amazon SQS queue.

View File

@ -152,9 +152,13 @@
<a href="/docs/providers/aws/r/security_group.html">aws_security_group</a> <a href="/docs/providers/aws/r/security_group.html">aws_security_group</a>
</li> </li>
<li<%= sidebar_current("docs-aws-resource-security-group-rule") %>> <li<%= sidebar_current("docs-aws-resource-security-group-rule") %>>
<a href="/docs/providers/aws/r/security_group_rule.html">aws_security_group_rule</a> <a href="/docs/providers/aws/r/security_group_rule.html">aws_security_group_rule</a>
</li> </li>
<li<%= sidebar_current("docs-aws-resource-sqs-queue") %>>
<a href="/docs/providers/aws/r/sqs_queue.html">aws_sqs_queue</a>
</li>
<li<%= sidebar_current("docs-aws-resource-subnet") %>> <li<%= sidebar_current("docs-aws-resource-subnet") %>>
<a href="/docs/providers/aws/r/subnet.html">aws_subnet</a> <a href="/docs/providers/aws/r/subnet.html">aws_subnet</a>