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

Fix suppressEquivalentSnsTopicSubscriptionDeliveryPolicy #14255

Merged
merged 2 commits into from
Feb 25, 2021
Merged
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
3 changes: 3 additions & 0 deletions .changelog/14255.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_sns_topic_subscription: Fix to avoid `delivery_policy` always showing diff.
```
37 changes: 23 additions & 14 deletions aws/resource_aws_sns_topic_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,29 +408,38 @@ type snsTopicSubscriptionRedrivePolicy struct {
}

func suppressEquivalentSnsTopicSubscriptionDeliveryPolicy(k, old, new string, d *schema.ResourceData) bool {
var deliveryPolicy snsTopicSubscriptionDeliveryPolicy

if err := json.Unmarshal([]byte(old), &deliveryPolicy); err != nil {
log.Printf("[WARN] Unable to unmarshal SNS Topic Subscription delivery policy JSON: %s", err)
ob, err := normalizeSnsTopicSubscriptionDeliveryPolicy(old)
if err != nil {
log.Print(err)
return false
}

normalizedDeliveryPolicy, err := json.Marshal(deliveryPolicy)

nb, err := normalizeSnsTopicSubscriptionDeliveryPolicy(new)
if err != nil {
log.Printf("[WARN] Unable to marshal SNS Topic Subscription delivery policy back to JSON: %s", err)
log.Print(err)
return false
}

ob := bytes.NewBufferString("")
if err := json.Compact(ob, normalizedDeliveryPolicy); err != nil {
return false
return jsonBytesEqual(ob, nb)
}

func normalizeSnsTopicSubscriptionDeliveryPolicy(policy string) ([]byte, error) {
var deliveryPolicy snsTopicSubscriptionDeliveryPolicy

if err := json.Unmarshal([]byte(policy), &deliveryPolicy); err != nil {
return nil, fmt.Errorf("[WARN] Unable to unmarshal SNS Topic Subscription delivery policy JSON: %s", err)
}

nb := bytes.NewBufferString("")
if err := json.Compact(nb, []byte(new)); err != nil {
return false
normalizedDeliveryPolicy, err := json.Marshal(deliveryPolicy)

if err != nil {
return nil, fmt.Errorf("[WARN] Unable to marshal SNS Topic Subscription delivery policy back to JSON: %s", err)
}

b := bytes.NewBufferString("")
if err := json.Compact(b, normalizedDeliveryPolicy); err != nil {
return nil, fmt.Errorf("[WARN] Unable to marshal SNS Topic Subscription delivery policy back to JSON: %s", err)
}

return jsonBytesEqual(ob.Bytes(), nb.Bytes())
return b.Bytes(), nil
}
5 changes: 5 additions & 0 deletions aws/resource_aws_sns_topic_subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func TestSuppressEquivalentSnsTopicSubscriptionDeliveryPolicy(t *testing.T) {
new: `{"healthyRetryPolicy":{"minDelayTarget":5,"maxDelayTarget":20,"numRetries":5}}`,
equivalent: true,
},
{
old: `{"healthyRetryPolicy":{"minDelayTarget":5,"maxDelayTarget":20,"numRetries":5,"numMaxDelayRetries":null,"numNoDelayRetries":null,"numMinDelayRetries":null,"backoffFunction":null},"throttlePolicy":{}}`,
new: `{"healthyRetryPolicy":{"minDelayTarget":5,"maxDelayTarget":20,"numRetries":5,"numMaxDelayRetries":null,"numNoDelayRetries":null,"numMinDelayRetries":null,"backoffFunction":null},"throttlePolicy":{}}`,
equivalent: true,
},
{
old: `{"healthyRetryPolicy":{"minDelayTarget":5,"maxDelayTarget":20,"numRetries":5,"numMaxDelayRetries":null,"numNoDelayRetries":null,"numMinDelayRetries":null,"backoffFunction":null},"sicklyRetryPolicy":null,"throttlePolicy":null,"guaranteed":false}`,
new: `{"healthyRetryPolicy":{"minDelayTarget":5,"maxDelayTarget":20,"numRetries":6}}`,
Expand Down