From c90496a7b69e2526f89d978ad7c460bba4a93b80 Mon Sep 17 00:00:00 2001 From: Hector Rivas Gandara Date: Fri, 8 Apr 2016 15:13:54 +0100 Subject: [PATCH] provider/aws: normalize json policy for sns topic For the policy attribute of the resource aws_sns_topic, AWS returns the policy in JSON format with the fields in a different order. If we store and compare the values without normalizing, terraform will unnecesary trigger and update of the resource. To avoid that, we must add a normalization function in the StateFunc of the policy attribute and also when we read the attribute from AWS. --- builtin/providers/aws/resource_aws_sns_topic.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/builtin/providers/aws/resource_aws_sns_topic.go b/builtin/providers/aws/resource_aws_sns_topic.go index 6a8e62fa7e33..4174e8732c94 100644 --- a/builtin/providers/aws/resource_aws_sns_topic.go +++ b/builtin/providers/aws/resource_aws_sns_topic.go @@ -56,7 +56,9 @@ func resourceAwsSnsTopic() *schema.Resource { log.Printf("[WARN] Error compacting JSON for Policy in SNS Topic") return "" } - return buffer.String() + value := normalizeJson(buffer.String()) + log.Printf("[DEBUG] topic policy before save: %s", value) + return value }, }, "delivery_policy": &schema.Schema{ @@ -183,9 +185,14 @@ func resourceAwsSnsTopicRead(d *schema.ResourceData, meta interface{}) error { // Some of the fetched attributes are stateful properties such as // the number of subscriptions, the owner, etc. skip those if resource.Schema[iKey] != nil { - value := *attrmap[oKey] + var value string + if iKey == "policy" { + value = normalizeJson(*attrmap[oKey]) + } else { + value = *attrmap[oKey] + } log.Printf("[DEBUG] Reading %s => %s -> %s", iKey, oKey, value) - d.Set(iKey, *attrmap[oKey]) + d.Set(iKey, value) } } }