From 7c4a2426611a98c19d95bf512b3ecb9dc7dbc1e8 Mon Sep 17 00:00:00 2001 From: Dejan Golja Date: Fri, 28 Apr 2017 13:18:12 +1000 Subject: [PATCH 1/2] Fixes the bug where sns delivery policy get always recreated (see https://github.com/hashicorp/terraform/issues/14024) --- builtin/providers/aws/resource_aws_sns_topic.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/builtin/providers/aws/resource_aws_sns_topic.go b/builtin/providers/aws/resource_aws_sns_topic.go index f3320866a..63d308518 100644 --- a/builtin/providers/aws/resource_aws_sns_topic.go +++ b/builtin/providers/aws/resource_aws_sns_topic.go @@ -55,9 +55,15 @@ func resourceAwsSnsTopic() *schema.Resource { }, }, "delivery_policy": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - ForceNew: false, + Type: schema.TypeString, + Optional: true, + ForceNew: false, + ValidateFunc: validateJsonString, + DiffSuppressFunc: suppressEquivalentJsonDiffs, + StateFunc: func(v interface{}) string { + json, _ := normalizeJsonString(v) + return json + }, }, "arn": &schema.Schema{ Type: schema.TypeString, From 1df7e3afad9c8514adea6b0d6173b8374217cb92 Mon Sep 17 00:00:00 2001 From: Dejan Golja Date: Tue, 2 May 2017 23:59:26 +1000 Subject: [PATCH 2/2] acceptance test for SNS delivery policy --- .../aws/resource_aws_sns_topic_test.go | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/builtin/providers/aws/resource_aws_sns_topic_test.go b/builtin/providers/aws/resource_aws_sns_topic_test.go index 738614a25..c341c98b4 100644 --- a/builtin/providers/aws/resource_aws_sns_topic_test.go +++ b/builtin/providers/aws/resource_aws_sns_topic_test.go @@ -67,6 +67,25 @@ func TestAccAWSSNSTopic_withIAMRole(t *testing.T) { }) } +func TestAccAWSSNSTopic_withDeliveryPolicy(t *testing.T) { + expectedPolicy := `{"http":{"defaultHealthyRetryPolicy": {"minDelayTarget": 20,"maxDelayTarget": 20,"numMaxDelayRetries": 0,"numRetries": 3,"numNoDelayRetries": 0,"numMinDelayRetries": 0,"backoffFunction": "linear"},"disableSubscriptionOverrides": false}}` + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_sns_topic.test_topic", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSSNSTopicDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSSNSTopicConfig_withDeliveryPolicy, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"), + testAccCheckAWSNSTopicHasDeliveryPolicy("aws_sns_topic.test_topic", expectedPolicy), + ), + }, + }, + }) +} + func testAccCheckAWSNSTopicHasPolicy(n string, expectedPolicyText string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -117,6 +136,46 @@ func testAccCheckAWSNSTopicHasPolicy(n string, expectedPolicyText string) resour } } +func testAccCheckAWSNSTopicHasDeliveryPolicy(n string, expectedPolicyText string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No Queue URL specified!") + } + + conn := testAccProvider.Meta().(*AWSClient).snsconn + + params := &sns.GetTopicAttributesInput{ + TopicArn: aws.String(rs.Primary.ID), + } + resp, err := conn.GetTopicAttributes(params) + if err != nil { + return err + } + + var actualPolicyText string + for k, v := range resp.Attributes { + if k == "DeliveryPolicy" { + actualPolicyText = *v + break + } + } + + equivalent := suppressEquivalentJsonDiffs("", actualPolicyText, expectedPolicyText, nil) + + if !equivalent { + return fmt.Errorf("Non-equivalent delivery policy error:\n\nexpected: %s\n\n got: %s\n", + expectedPolicyText, actualPolicyText) + } + + return nil + } +} + func testAccCheckAWSSNSTopicDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).snsconn @@ -244,3 +303,26 @@ resource "aws_sns_topic" "test_topic" { EOF } ` + +// Test for https://github.com/hashicorp/terraform/issues/14024 +const testAccAWSSNSTopicConfig_withDeliveryPolicy = ` +resource "aws_sns_topic" "test_topic" { + name = "test_delivery_policy" + delivery_policy = <