provider/aws: Normalize and compact SQS Redrive, Policy JSON

* provider/aws: Nomralize SQS Redrive Policy JSON

* provider/aws: Fix typo in log statements

* compact the Policy on SNS Queue

* add acceptance test for policy formatting
This commit is contained in:
Clint 2016-04-04 11:41:36 -05:00
parent a2695b4652
commit b351a72e4c
4 changed files with 135 additions and 11 deletions

View File

@ -177,14 +177,14 @@ func resourceAwsSnsTopicRead(d *schema.ResourceData, meta interface{}) error {
resource := *resourceAwsSnsTopic() resource := *resourceAwsSnsTopic()
// iKey = internal struct key, oKey = AWS Attribute Map key // iKey = internal struct key, oKey = AWS Attribute Map key
for iKey, oKey := range SNSAttributeMap { for iKey, oKey := range SNSAttributeMap {
log.Printf("[DEBUG] Updating %s => %s", iKey, oKey) log.Printf("[DEBUG] Reading %s => %s", iKey, oKey)
if attrmap[oKey] != nil { if attrmap[oKey] != nil {
// Some of the fetched attributes are stateful properties such as // Some of the fetched attributes are stateful properties such as
// the number of subscriptions, the owner, etc. skip those // the number of subscriptions, the owner, etc. skip those
if resource.Schema[iKey] != nil { if resource.Schema[iKey] != nil {
value := *attrmap[oKey] value := *attrmap[oKey]
log.Printf("[DEBUG] Updating %s => %s -> %s", iKey, oKey, value) log.Printf("[DEBUG] Reading %s => %s -> %s", iKey, oKey, value)
d.Set(iKey, *attrmap[oKey]) d.Set(iKey, *attrmap[oKey])
} }
} }

View File

@ -1,6 +1,8 @@
package aws package aws
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"log" "log"
"strconv" "strconv"
@ -66,10 +68,24 @@ func resourceAwsSqsQueue() *schema.Resource {
"policy": &schema.Schema{ "policy": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
StateFunc: func(v interface{}) string {
s, ok := v.(string)
if !ok || s == "" {
return ""
}
jsonb := []byte(s)
buffer := new(bytes.Buffer)
if err := json.Compact(buffer, jsonb); err != nil {
log.Printf("[WARN] Error compacting JSON for Policy in SNS Queue")
return ""
}
return buffer.String()
},
}, },
"redrive_policy": &schema.Schema{ "redrive_policy": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
StateFunc: normalizeJson,
}, },
"arn": &schema.Schema{ "arn": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
@ -176,7 +192,9 @@ func resourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error {
return err return err
} }
d.Set(iKey, value) d.Set(iKey, value)
log.Printf("[DEBUG] Reading %s => %s -> %d", iKey, oKey, value)
} else { } else {
log.Printf("[DEBUG] Reading %s => %s -> %s", iKey, oKey, *attrmap[oKey])
d.Set(iKey, *attrmap[oKey]) d.Set(iKey, *attrmap[oKey])
} }
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/sqs" "github.com/aws/aws-sdk-go/service/sqs"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -33,6 +34,39 @@ func TestAccAWSSQSQueue_basic(t *testing.T) {
}) })
} }
func TestAccAWSSQSQueue_redrivePolicy(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSQSQueueDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSSQSConfigWithRedrive(acctest.RandStringFromCharSet(5, acctest.CharSetAlpha)),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.my_dead_letter_queue"),
),
},
},
})
}
// Tests formatting and compacting of Policy, Redrive json
func TestAccAWSSQSQueue_Policybasic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSQSQueueDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSSQSConfig_PolicyFormat,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSQSExistsWithOverrides("aws_sqs_queue.test-email-events"),
),
},
},
})
}
func testAccCheckAWSSQSQueueDestroy(s *terraform.State) error { func testAccCheckAWSSQSQueueDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).sqsconn conn := testAccProvider.Meta().(*AWSClient).sqsconn
@ -168,11 +202,83 @@ resource "aws_sqs_queue" "queue-with-defaults" {
const testAccAWSSQSConfigWithOverrides = ` const testAccAWSSQSConfigWithOverrides = `
resource "aws_sqs_queue" "queue-with-overrides" { resource "aws_sqs_queue" "queue-with-overrides" {
name = "test-sqs-queue-with-overrides" name = "test-sqs-queue-with-overrides"
delay_seconds = 90 delay_seconds = 90
max_message_size = 2048 max_message_size = 2048
message_retention_seconds = 86400 message_retention_seconds = 86400
receive_wait_time_seconds = 10 receive_wait_time_seconds = 10
visibility_timeout_seconds = 60 visibility_timeout_seconds = 60
}
`
func testAccAWSSQSConfigWithRedrive(name string) string {
return fmt.Sprintf(`
resource "aws_sqs_queue" "my_queue" {
name = "tftestqueuq-%s"
delay_seconds = 0
visibility_timeout_seconds = 300
redrive_policy = <<EOF
{
"maxReceiveCount": 3,
"deadLetterTargetArn": "${aws_sqs_queue.my_dead_letter_queue.arn}"
}
EOF
}
resource "aws_sqs_queue" "my_dead_letter_queue" {
name = "tfotherqueuq-%s"
}
`, name, name)
}
const testAccAWSSQSConfig_PolicyFormat = `
variable "sns_name" {
default = "tf-test-name-2"
}
variable "sqs_name" {
default = "tf-test-sqs-name-2"
}
resource "aws_sns_topic" "test_topic" {
name = "${var.sns_name}"
}
resource "aws_sqs_queue" "test-email-events" {
name = "${var.sqs_name}"
depends_on = ["aws_sns_topic.test_topic"]
delay_seconds = 90
max_message_size = 2048
message_retention_seconds = 86400
receive_wait_time_seconds = 10
visibility_timeout_seconds = 60
policy = <<EOF
{
"Version": "2012-10-17",
"Id": "sqspolicy",
"Statement": [
{
"Sid": "Stmt1451501026839",
"Effect": "Allow",
"Principal": "*",
"Action": "sqs:SendMessage",
"Resource": "arn:aws:sqs:us-west-2:470663696735:${var.sqs_name}",
"Condition": {
"ArnEquals": {
"aws:SourceArn": "arn:aws:sns:us-west-2:470663696735:${var.sns_name}"
}
}
}
]
}
EOF
}
resource "aws_sns_topic_subscription" "test_queue_target" {
topic_arn = "${aws_sns_topic.test_topic.arn}"
protocol = "sqs"
endpoint = "${aws_sqs_queue.test-email-events.arn}"
} }
` `

View File

@ -32,7 +32,7 @@ The following arguments are supported:
* `delay_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 0 seconds. * `delay_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 0 seconds.
* `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. * `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 * `policy` - (Optional) The JSON policy for the SQS queue
* `redrive_policy` - (Optional) The JSON policy to set up the Dead Letter Queue, see [AWS docs](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/SQSDeadLetterQueue.html). * `redrive_policy` - (Optional) The JSON policy to set up the Dead Letter Queue, see [AWS docs](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/SQSDeadLetterQueue.html). **Note:** when specifying `maxReceiveCount`, you must specify it as an integer (`5`), and not a string (`"5"`).
## Attributes Reference ## Attributes Reference