Skip to content

Commit

Permalink
provider/aws: normalize json policy for sns topic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
keymon committed Apr 8, 2016
1 parent 1d2a8a6 commit 1a14a66
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions builtin/providers/aws/resource_aws_sns_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 = normalizeJson(*attrmap[oKey])
}
log.Printf("[DEBUG] Reading %s => %s -> %s", iKey, oKey, value)
d.Set(iKey, *attrmap[oKey])
d.Set(iKey, value)
}
}
}
Expand Down

0 comments on commit 1a14a66

Please sign in to comment.