diff --git a/.changelog/14255.txt b/.changelog/14255.txt new file mode 100644 index 00000000000..e4ff1a10ffd --- /dev/null +++ b/.changelog/14255.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_sns_topic_subscription: Fix to avoid `delivery_policy` always showing diff. +``` \ 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 1174bca2841..99d7ff0578a 100644 --- a/aws/resource_aws_sns_topic_subscription.go +++ b/aws/resource_aws_sns_topic_subscription.go @@ -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 } diff --git a/aws/resource_aws_sns_topic_subscription_test.go b/aws/resource_aws_sns_topic_subscription_test.go index 6e54f345ff8..95fe10e4cdf 100644 --- a/aws/resource_aws_sns_topic_subscription_test.go +++ b/aws/resource_aws_sns_topic_subscription_test.go @@ -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}}`,