From 234e6a9cb09f5dae237689a5b6f81d9f49e522cc Mon Sep 17 00:00:00 2001 From: Ricool06 <14017085+Ricool06@users.noreply.github.com> Date: Mon, 27 Jan 2020 13:57:33 +0000 Subject: [PATCH 1/2] resource/aws_sns_topic_subscription: Add redrive_policy argument --- aws/resource_aws_sns_topic_subscription.go | 21 ++++ ...esource_aws_sns_topic_subscription_test.go | 108 ++++++++++++++++++ .../r/sns_topic_subscription.html.markdown | 1 + 3 files changed, 130 insertions(+) diff --git a/aws/resource_aws_sns_topic_subscription.go b/aws/resource_aws_sns_topic_subscription.go index 353c20d9d9a0..ec60c7042c27 100644 --- a/aws/resource_aws_sns_topic_subscription.go +++ b/aws/resource_aws_sns_topic_subscription.go @@ -73,6 +73,11 @@ func resourceAwsSnsTopicSubscription() *schema.Resource { ValidateFunc: validation.StringIsJSON, DiffSuppressFunc: suppressEquivalentSnsTopicSubscriptionDeliveryPolicy, }, + "redrive_policy": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.ValidateJsonString, + }, "raw_message_delivery": { Type: schema.TypeBool, Optional: true, @@ -147,6 +152,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) } @@ -175,6 +186,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"]) @@ -373,6 +385,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 { @@ -381,6 +398,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"` diff --git a/aws/resource_aws_sns_topic_subscription_test.go b/aws/resource_aws_sns_topic_subscription_test.go index 083f647965b8..57cf11748337 100644 --- a/aws/resource_aws_sns_topic_subscription_test.go +++ b/aws/resource_aws_sns_topic_subscription_test.go @@ -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" @@ -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", @@ -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" { diff --git a/website/docs/r/sns_topic_subscription.html.markdown b/website/docs/r/sns_topic_subscription.html.markdown index 02e132769ead..d53f6b8186c0 100644 --- a/website/docs/r/sns_topic_subscription.html.markdown +++ b/website/docs/r/sns_topic_subscription.html.markdown @@ -239,6 +239,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 From 4ac036224780df3dbfb234c22abef073f802a25b Mon Sep 17 00:00:00 2001 From: Angie Pinilla Date: Sat, 6 Feb 2021 09:57:55 -0500 Subject: [PATCH 2/2] CR updates; pass arg on create, generalize test check --- .changelog/11770.txt | 3 + aws/resource_aws_sns_topic_subscription.go | 24 ++++--- ...esource_aws_sns_topic_subscription_test.go | 64 ++++++++++--------- 3 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 .changelog/11770.txt diff --git a/.changelog/11770.txt b/.changelog/11770.txt new file mode 100644 index 000000000000..c51abe16fc64 --- /dev/null +++ b/.changelog/11770.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_sns_topic_subscription: Add `redrive_policy` argument +``` \ No newline at end of file diff --git a/aws/resource_aws_sns_topic_subscription.go b/aws/resource_aws_sns_topic_subscription.go index ec60c7042c27..db3dd5ec76b6 100644 --- a/aws/resource_aws_sns_topic_subscription.go +++ b/aws/resource_aws_sns_topic_subscription.go @@ -74,9 +74,10 @@ func resourceAwsSnsTopicSubscription() *schema.Resource { DiffSuppressFunc: suppressEquivalentSnsTopicSubscriptionDeliveryPolicy, }, "redrive_policy": { - Type: schema.TypeString, - Optional: true, - ValidateFunc: validation.ValidateJsonString, + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringIsJSON, + DiffSuppressFunc: suppressEquivalentJsonDiffs, }, "raw_message_delivery": { Type: schema.TypeBool, @@ -186,7 +187,6 @@ 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"]) @@ -196,6 +196,7 @@ func resourceAwsSnsTopicSubscriptionRead(d *schema.ResourceData, meta interface{ d.Set("raw_message_delivery", true) } + d.Set("redrive_policy", attributeOutput.Attributes["RedrivePolicy"]) d.Set("topic_arn", attributeOutput.Attributes["TopicArn"]) return nil @@ -217,6 +218,7 @@ func getResourceAttributes(d *schema.ResourceData) (output map[string]*string) { delivery_policy := d.Get("delivery_policy").(string) filter_policy := d.Get("filter_policy").(string) raw_message_delivery := d.Get("raw_message_delivery").(bool) + redrive_policy := d.Get("redrive_policy").(string) // Collect attributes if available attributes := map[string]*string{} @@ -233,6 +235,10 @@ func getResourceAttributes(d *schema.ResourceData) (output map[string]*string) { attributes["RawMessageDelivery"] = aws.String(fmt.Sprintf("%t", raw_message_delivery)) } + if redrive_policy != "" { + attributes["RedrivePolicy"] = aws.String(redrive_policy) + } + return attributes } @@ -386,6 +392,8 @@ func snsSubscriptionAttributeUpdate(snsconn *sns.SNS, subscriptionArn, attribute AttributeValue: aws.String(attributeValue), } + // The AWS API requires a non-empty string value or nil for the RedrivePolicy attribute, + // else throws an InvalidParameter error if attributeName == "RedrivePolicy" && attributeValue == "" { req.AttributeValue = nil } @@ -398,10 +406,6 @@ 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"` @@ -465,6 +469,10 @@ func (s snsTopicSubscriptionDeliveryPolicyThrottlePolicy) GoString() string { return s.String() } +type snsTopicSubscriptionRedrivePolicy struct { + DeadLetterTargetArn string `json:"deadLetterTargetArn,omitempty"` +} + func suppressEquivalentSnsTopicSubscriptionDeliveryPolicy(k, old, new string, d *schema.ResourceData) bool { var deliveryPolicy snsTopicSubscriptionDeliveryPolicy diff --git a/aws/resource_aws_sns_topic_subscription_test.go b/aws/resource_aws_sns_topic_subscription_test.go index 57cf11748337..3ddc936a27ce 100644 --- a/aws/resource_aws_sns_topic_subscription_test.go +++ b/aws/resource_aws_sns_topic_subscription_test.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/sns" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -203,8 +204,8 @@ 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) + dlqName := fmt.Sprintf("tf-acc-test-queue-dlq-%d", ri) + updatedDlqName := fmt.Sprintf("tf-acc-test-queue-dlq-update-%d", ri) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -212,13 +213,10 @@ func TestAccAWSSNSTopicSubscription_redrivePolicy(t *testing.T) { CheckDestroy: testAccCheckAWSSNSTopicSubscriptionDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSSNSTopicSubscriptionConfig_redrivePolicy( - ri, - dlqQueueName, - ), + Config: testAccAWSSNSTopicSubscriptionConfig_redrivePolicy(ri, dlqName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSSNSTopicSubscriptionExists(resourceName, attributes), - testAccCheckAWSSNSTopicSubscriptionRedrivePolicyAttribute(attributes, dlqQueueName), + testAccCheckAWSSNSTopicSubscriptionRedrivePolicyAttribute(attributes, dlqName), ), }, { @@ -232,15 +230,21 @@ func TestAccAWSSNSTopicSubscription_redrivePolicy(t *testing.T) { }, // Test attribute update { - Config: testAccAWSSNSTopicSubscriptionConfig_redrivePolicy( - ri, - updatedDlqQueueName, - ), + Config: testAccAWSSNSTopicSubscriptionConfig_redrivePolicy(ri, updatedDlqName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSSNSTopicSubscriptionExists(resourceName, attributes), - testAccCheckAWSSNSTopicSubscriptionRedrivePolicyAttribute(attributes, updatedDlqQueueName), + testAccCheckAWSSNSTopicSubscriptionRedrivePolicyAttribute(attributes, updatedDlqName), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{ + "confirmation_timeout_in_minutes", + "endpoint_auto_confirms", + }, + }, // Test attribute removal { Config: testAccAWSSNSTopicSubscriptionConfig(ri), @@ -433,7 +437,7 @@ func testAccCheckAWSSNSTopicSubscriptionDeliveryPolicyAttribute(attributes map[s } } -func testAccCheckAWSSNSTopicSubscriptionRedrivePolicyAttribute(attributes map[string]string, expectedDlqName string) resource.TestCheckFunc { +func testAccCheckAWSSNSTopicSubscriptionRedrivePolicyAttribute(attributes map[string]string, expectedRedrivePolicyResource string) resource.TestCheckFunc { return func(s *terraform.State) error { apiRedrivePolicyJSONString, ok := attributes["RedrivePolicy"] @@ -446,21 +450,21 @@ func testAccCheckAWSSNSTopicSubscriptionRedrivePolicyAttribute(attributes map[st 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, + expectedRedrivePolicy := snsTopicSubscriptionRedrivePolicy{ + DeadLetterTargetArn: arn.ARN{ + AccountID: testAccGetAccountID(), + Partition: testAccGetPartition(), + Region: testAccGetRegion(), + Resource: expectedRedrivePolicyResource, + Service: "sqs", + }.String(), } - if reflect.DeepEqual(apiRedrivePolicy, *expectedRedrivePolicy) { + 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) + 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) } } @@ -538,11 +542,11 @@ resource "aws_sns_topic_subscription" "test_subscription" { func testAccAWSSNSTopicSubscriptionConfig_redrivePolicy(i int, dlqName string) string { return fmt.Sprintf(` resource "aws_sns_topic" "test_topic" { - name = "terraform-test-topic-%d" + name = "terraform-test-topic-%[1]d" } resource "aws_sqs_queue" "test_queue" { - name = "terraform-subscription-test-queue-%d" + name = "terraform-subscription-test-queue-%[1]d" } resource "aws_sqs_queue" "test_queue_dlq" { @@ -550,12 +554,12 @@ resource "aws_sqs_queue" "test_queue_dlq" { } 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}" + redrive_policy = jsonencode({ deadLetterTargetArn : aws_sqs_queue.test_queue_dlq.arn }) + 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}"}`)) +`, i, dlqName) } func testAccAWSSNSTopicSubscriptionConfig_rawMessageDelivery(i int, rawMessageDelivery bool) string {