Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_sns_topic_subscription: Add redrive_policy argument #14800

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions aws/resource_aws_sns_topic_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func resourceAwsSnsTopicSubscription() *schema.Resource {
ValidateFunc: validation.ValidateJsonString,
DiffSuppressFunc: suppressEquivalentSnsTopicSubscriptionDeliveryPolicy,
},
"redrive_policy": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.ValidateJsonString,
},
"raw_message_delivery": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -148,6 +153,12 @@ func resourceAwsSnsTopicSubscriptionUpdate(d *schema.ResourceData, meta interfac
}
}

if d.HasChange("redrive_policy") {
if err := snsSubscriptionAttributeUpdate(snsconn, d.Id(), "RedrivePolicy", d.Get("redrive_policy").(string)); err != nil {
return err
}
}

return resourceAwsSnsTopicSubscriptionRead(d, meta)
}

Expand Down Expand Up @@ -176,6 +187,7 @@ func resourceAwsSnsTopicSubscriptionRead(d *schema.ResourceData, meta interface{

d.Set("arn", attributeOutput.Attributes["SubscriptionArn"])
d.Set("delivery_policy", attributeOutput.Attributes["DeliveryPolicy"])
d.Set("redrive_policy", attributeOutput.Attributes["RedrivePolicy"])
d.Set("endpoint", attributeOutput.Attributes["Endpoint"])
d.Set("filter_policy", attributeOutput.Attributes["FilterPolicy"])
d.Set("protocol", attributeOutput.Attributes["Protocol"])
Expand Down Expand Up @@ -335,6 +347,11 @@ func snsSubscriptionAttributeUpdate(snsconn *sns.SNS, subscriptionArn, attribute
AttributeName: aws.String(attributeName),
AttributeValue: aws.String(attributeValue),
}

if attributeName == "RedrivePolicy" && attributeValue == "" {
req.AttributeValue = nil
}

_, err := snsconn.SetSubscriptionAttributes(req)

if err != nil {
Expand All @@ -343,6 +360,10 @@ func snsSubscriptionAttributeUpdate(snsconn *sns.SNS, subscriptionArn, attribute
return nil
}

type snsTopicSubscriptionRedrivePolicy struct {
DeadLetterTargetArn string `json:"deadLetterTargetArn,omitempty"`
}

type snsTopicSubscriptionDeliveryPolicy struct {
Guaranteed bool `json:"guaranteed,omitempty"`
HealthyRetryPolicy *snsTopicSubscriptionDeliveryPolicyHealthyRetryPolicy `json:"healthyRetryPolicy,omitempty"`
Expand Down
108 changes: 108 additions & 0 deletions aws/resource_aws_sns_topic_subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,60 @@ func TestAccAWSSNSTopicSubscription_deliveryPolicy(t *testing.T) {
})
}

func TestAccAWSSNSTopicSubscription_redrivePolicy(t *testing.T) {
attributes := make(map[string]string)
resourceName := "aws_sns_topic_subscription.test_subscription"
ri := acctest.RandInt()
dlqQueueName := fmt.Sprintf("queue-dlq-%d", ri)
updatedDlqQueueName := fmt.Sprintf("updated-queue-dlq-%d", ri)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSNSTopicSubscriptionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSNSTopicSubscriptionConfig_redrivePolicy(
ri,
dlqQueueName,
),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSNSTopicSubscriptionExists(resourceName, attributes),
testAccCheckAWSSNSTopicSubscriptionRedrivePolicyAttribute(attributes, dlqQueueName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"confirmation_timeout_in_minutes",
"endpoint_auto_confirms",
},
},
// Test attribute update
{
Config: testAccAWSSNSTopicSubscriptionConfig_redrivePolicy(
ri,
updatedDlqQueueName,
),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSNSTopicSubscriptionExists(resourceName, attributes),
testAccCheckAWSSNSTopicSubscriptionRedrivePolicyAttribute(attributes, updatedDlqQueueName),
),
},
// Test attribute removal
{
Config: testAccAWSSNSTopicSubscriptionConfig(ri),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSNSTopicSubscriptionExists(resourceName, attributes),
resource.TestCheckResourceAttr(resourceName, "redrive_policy", ""),
),
},
},
})
}

func TestAccAWSSNSTopicSubscription_rawMessageDelivery(t *testing.T) {
attributes := make(map[string]string)
resourceName := "aws_sns_topic_subscription.test_subscription"
Expand Down Expand Up @@ -379,6 +433,37 @@ func testAccCheckAWSSNSTopicSubscriptionDeliveryPolicyAttribute(attributes map[s
}
}

func testAccCheckAWSSNSTopicSubscriptionRedrivePolicyAttribute(attributes map[string]string, expectedDlqName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
apiRedrivePolicyJSONString, ok := attributes["RedrivePolicy"]

if !ok {
return fmt.Errorf("RedrivePolicy attribute not found in attributes: %s", attributes)
}

var apiRedrivePolicy snsTopicSubscriptionRedrivePolicy
if err := json.Unmarshal([]byte(apiRedrivePolicyJSONString), &apiRedrivePolicy); err != nil {
return fmt.Errorf("unable to unmarshal SNS Topic Subscription redrive policy JSON (%s): %s", apiRedrivePolicyJSONString, err)
}

accountID := testAccProvider.Meta().(*AWSClient).accountid
expectedDlqArn := fmt.Sprintf(
"arn:aws:sqs:us-west-2:%s:%s",
accountID,
expectedDlqName,
)
expectedRedrivePolicy := &snsTopicSubscriptionRedrivePolicy{
DeadLetterTargetArn: expectedDlqArn,
}

if reflect.DeepEqual(apiRedrivePolicy, *expectedRedrivePolicy) {
return nil
}

return fmt.Errorf("SNS Topic Subscription redrive policy did not match:\n\nReceived\n\n%s\n\nExpected\n\n%s\n\n", apiRedrivePolicy, *expectedRedrivePolicy)
}
}

func TestObfuscateEndpointPassword(t *testing.T) {
checks := map[string]string{
"https://example.com/myroute": "https://example.com/myroute",
Expand Down Expand Up @@ -450,6 +535,29 @@ resource "aws_sns_topic_subscription" "test_subscription" {
`, i, i, policy)
}

func testAccAWSSNSTopicSubscriptionConfig_redrivePolicy(i int, dlqName string) string {
return fmt.Sprintf(`
resource "aws_sns_topic" "test_topic" {
name = "terraform-test-topic-%d"
}

resource "aws_sqs_queue" "test_queue" {
name = "terraform-subscription-test-queue-%d"
}

resource "aws_sqs_queue" "test_queue_dlq" {
name = "%s"
}

resource "aws_sns_topic_subscription" "test_subscription" {
redrive_policy = %s
endpoint = "${aws_sqs_queue.test_queue.arn}"
protocol = "sqs"
topic_arn = "${aws_sns_topic.test_topic.arn}"
}
`, i, i, dlqName, strconv.Quote(`{"deadLetterTargetArn": "${aws_sqs_queue.test_queue_dlq.arn}"}`))
}

func testAccAWSSNSTopicSubscriptionConfig_rawMessageDelivery(i int, rawMessageDelivery bool) string {
return fmt.Sprintf(`
resource "aws_sns_topic" "test_topic" {
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/sns_topic_subscription.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ The following arguments are supported:
* `raw_message_delivery` - (Optional) Boolean indicating whether or not to enable raw message delivery (the original message is directly passed, not wrapped in JSON with the original message in the message property) (default is false).
* `filter_policy` - (Optional) JSON String with the filter policy that will be used in the subscription to filter messages seen by the target resource. Refer to the [SNS docs](https://docs.aws.amazon.com/sns/latest/dg/message-filtering.html) for more details.
* `delivery_policy` - (Optional) JSON String with the delivery policy (retries, backoff, etc.) that will be used in the subscription - this only applies to HTTP/S subscriptions. Refer to the [SNS docs](https://docs.aws.amazon.com/sns/latest/dg/DeliveryPolicies.html) for more details.
* `redrive_policy` - (Optional) JSON String with the redrive policy that will be used in the subscription. Refer to the [SNS docs](https://docs.aws.amazon.com/sns/latest/dg/sns-dead-letter-queues.html#how-messages-moved-into-dead-letter-queue) for more details.

### Protocols supported

Expand Down