Skip to content

Commit b351a72

Browse files
committed
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
1 parent a2695b4 commit b351a72

4 files changed

Lines changed: 135 additions & 11 deletions

File tree

builtin/providers/aws/resource_aws_sns_topic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,14 @@ func resourceAwsSnsTopicRead(d *schema.ResourceData, meta interface{}) error {
177177
resource := *resourceAwsSnsTopic()
178178
// iKey = internal struct key, oKey = AWS Attribute Map key
179179
for iKey, oKey := range SNSAttributeMap {
180-
log.Printf("[DEBUG] Updating %s => %s", iKey, oKey)
180+
log.Printf("[DEBUG] Reading %s => %s", iKey, oKey)
181181

182182
if attrmap[oKey] != nil {
183183
// Some of the fetched attributes are stateful properties such as
184184
// the number of subscriptions, the owner, etc. skip those
185185
if resource.Schema[iKey] != nil {
186186
value := *attrmap[oKey]
187-
log.Printf("[DEBUG] Updating %s => %s -> %s", iKey, oKey, value)
187+
log.Printf("[DEBUG] Reading %s => %s -> %s", iKey, oKey, value)
188188
d.Set(iKey, *attrmap[oKey])
189189
}
190190
}

builtin/providers/aws/resource_aws_sqs_queue.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package aws
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
57
"log"
68
"strconv"
@@ -66,10 +68,24 @@ func resourceAwsSqsQueue() *schema.Resource {
6668
"policy": &schema.Schema{
6769
Type: schema.TypeString,
6870
Optional: true,
71+
StateFunc: func(v interface{}) string {
72+
s, ok := v.(string)
73+
if !ok || s == "" {
74+
return ""
75+
}
76+
jsonb := []byte(s)
77+
buffer := new(bytes.Buffer)
78+
if err := json.Compact(buffer, jsonb); err != nil {
79+
log.Printf("[WARN] Error compacting JSON for Policy in SNS Queue")
80+
return ""
81+
}
82+
return buffer.String()
83+
},
6984
},
7085
"redrive_policy": &schema.Schema{
71-
Type: schema.TypeString,
72-
Optional: true,
86+
Type: schema.TypeString,
87+
Optional: true,
88+
StateFunc: normalizeJson,
7389
},
7490
"arn": &schema.Schema{
7591
Type: schema.TypeString,
@@ -176,7 +192,9 @@ func resourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error {
176192
return err
177193
}
178194
d.Set(iKey, value)
195+
log.Printf("[DEBUG] Reading %s => %s -> %d", iKey, oKey, value)
179196
} else {
197+
log.Printf("[DEBUG] Reading %s => %s -> %s", iKey, oKey, *attrmap[oKey])
180198
d.Set(iKey, *attrmap[oKey])
181199
}
182200
}

builtin/providers/aws/resource_aws_sqs_queue_test.go

Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/aws/aws-sdk-go/aws"
88
"github.com/aws/aws-sdk-go/aws/awserr"
99
"github.com/aws/aws-sdk-go/service/sqs"
10+
"github.com/hashicorp/terraform/helper/acctest"
1011
"github.com/hashicorp/terraform/helper/resource"
1112
"github.com/hashicorp/terraform/terraform"
1213
)
@@ -33,6 +34,39 @@ func TestAccAWSSQSQueue_basic(t *testing.T) {
3334
})
3435
}
3536

37+
func TestAccAWSSQSQueue_redrivePolicy(t *testing.T) {
38+
resource.Test(t, resource.TestCase{
39+
PreCheck: func() { testAccPreCheck(t) },
40+
Providers: testAccProviders,
41+
CheckDestroy: testAccCheckAWSSQSQueueDestroy,
42+
Steps: []resource.TestStep{
43+
resource.TestStep{
44+
Config: testAccAWSSQSConfigWithRedrive(acctest.RandStringFromCharSet(5, acctest.CharSetAlpha)),
45+
Check: resource.ComposeTestCheckFunc(
46+
testAccCheckAWSSQSExistsWithDefaults("aws_sqs_queue.my_dead_letter_queue"),
47+
),
48+
},
49+
},
50+
})
51+
}
52+
53+
// Tests formatting and compacting of Policy, Redrive json
54+
func TestAccAWSSQSQueue_Policybasic(t *testing.T) {
55+
resource.Test(t, resource.TestCase{
56+
PreCheck: func() { testAccPreCheck(t) },
57+
Providers: testAccProviders,
58+
CheckDestroy: testAccCheckAWSSQSQueueDestroy,
59+
Steps: []resource.TestStep{
60+
resource.TestStep{
61+
Config: testAccAWSSQSConfig_PolicyFormat,
62+
Check: resource.ComposeTestCheckFunc(
63+
testAccCheckAWSSQSExistsWithOverrides("aws_sqs_queue.test-email-events"),
64+
),
65+
},
66+
},
67+
})
68+
}
69+
3670
func testAccCheckAWSSQSQueueDestroy(s *terraform.State) error {
3771
conn := testAccProvider.Meta().(*AWSClient).sqsconn
3872

@@ -168,11 +202,83 @@ resource "aws_sqs_queue" "queue-with-defaults" {
168202

169203
const testAccAWSSQSConfigWithOverrides = `
170204
resource "aws_sqs_queue" "queue-with-overrides" {
171-
name = "test-sqs-queue-with-overrides"
172-
delay_seconds = 90
173-
max_message_size = 2048
174-
message_retention_seconds = 86400
175-
receive_wait_time_seconds = 10
176-
visibility_timeout_seconds = 60
205+
name = "test-sqs-queue-with-overrides"
206+
delay_seconds = 90
207+
max_message_size = 2048
208+
message_retention_seconds = 86400
209+
receive_wait_time_seconds = 10
210+
visibility_timeout_seconds = 60
211+
}
212+
`
213+
214+
func testAccAWSSQSConfigWithRedrive(name string) string {
215+
return fmt.Sprintf(`
216+
resource "aws_sqs_queue" "my_queue" {
217+
name = "tftestqueuq-%s"
218+
delay_seconds = 0
219+
visibility_timeout_seconds = 300
220+
221+
redrive_policy = <<EOF
222+
{
223+
"maxReceiveCount": 3,
224+
"deadLetterTargetArn": "${aws_sqs_queue.my_dead_letter_queue.arn}"
225+
}
226+
EOF
227+
}
228+
229+
resource "aws_sqs_queue" "my_dead_letter_queue" {
230+
name = "tfotherqueuq-%s"
231+
}
232+
`, name, name)
233+
}
234+
235+
const testAccAWSSQSConfig_PolicyFormat = `
236+
variable "sns_name" {
237+
default = "tf-test-name-2"
238+
}
239+
240+
variable "sqs_name" {
241+
default = "tf-test-sqs-name-2"
242+
}
243+
244+
resource "aws_sns_topic" "test_topic" {
245+
name = "${var.sns_name}"
246+
}
247+
248+
resource "aws_sqs_queue" "test-email-events" {
249+
name = "${var.sqs_name}"
250+
depends_on = ["aws_sns_topic.test_topic"]
251+
delay_seconds = 90
252+
max_message_size = 2048
253+
message_retention_seconds = 86400
254+
receive_wait_time_seconds = 10
255+
visibility_timeout_seconds = 60
256+
257+
policy = <<EOF
258+
{
259+
"Version": "2012-10-17",
260+
"Id": "sqspolicy",
261+
"Statement": [
262+
{
263+
"Sid": "Stmt1451501026839",
264+
"Effect": "Allow",
265+
"Principal": "*",
266+
"Action": "sqs:SendMessage",
267+
"Resource": "arn:aws:sqs:us-west-2:470663696735:${var.sqs_name}",
268+
"Condition": {
269+
"ArnEquals": {
270+
"aws:SourceArn": "arn:aws:sns:us-west-2:470663696735:${var.sns_name}"
271+
}
272+
}
273+
}
274+
]
275+
}
276+
EOF
277+
}
278+
279+
resource "aws_sns_topic_subscription" "test_queue_target" {
280+
topic_arn = "${aws_sns_topic.test_topic.arn}"
281+
protocol = "sqs"
282+
endpoint = "${aws_sqs_queue.test-email-events.arn}"
177283
}
178284
`

website/source/docs/providers/aws/r/sqs_queue.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The following arguments are supported:
3232
* `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.
3333
* `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.
3434
* `policy` - (Optional) The JSON policy for the SQS queue
35-
* `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).
35+
* `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"`).
3636

3737
## Attributes Reference
3838

0 commit comments

Comments
 (0)