Skip to content

Commit

Permalink
CR updates; pass arg on create, generalize test check
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 committed Feb 6, 2021
1 parent 234e6a9 commit 4ac0362
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .changelog/11770.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_sns_topic_subscription: Add `redrive_policy` argument
```
24 changes: 16 additions & 8 deletions aws/resource_aws_sns_topic_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"])
Expand All @@ -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
Expand All @@ -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{}
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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"`
Expand Down Expand Up @@ -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

Expand Down
64 changes: 34 additions & 30 deletions aws/resource_aws_sns_topic_subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -203,22 +204,19 @@ 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) },
Providers: testAccProviders,
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),
),
},
{
Expand All @@ -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),
Expand Down Expand Up @@ -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"]

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -538,24 +542,24 @@ 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" {
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}"
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 {
Expand Down

0 comments on commit 4ac0362

Please sign in to comment.