Merge pull request #14064 from dgolja/bugfix/sns_delivery_policy

provider/aws: Fixes the bug where SNS delivery policy get always recreated
This commit is contained in:
Jake Champlin 2017-05-02 10:33:42 -04:00 committed by GitHub
commit 43a2220808
2 changed files with 91 additions and 3 deletions

View File

@ -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,

View File

@ -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 = <<EOF
{
"http": {
"defaultHealthyRetryPolicy": {
"minDelayTarget": 20,
"maxDelayTarget": 20,
"numRetries": 3,
"numMaxDelayRetries": 0,
"numNoDelayRetries": 0,
"numMinDelayRetries": 0,
"backoffFunction": "linear"
},
"disableSubscriptionOverrides": false
}
}
EOF
}
`