provider/aws: normalize json policy for sns topic policy attribute (#6089)

* provider/aws: test empty plan with sns_topic policy with random order

If we setup a sns_topic policy with a policy with a different order
to the one set by the AWS API, terraform plan will be not empty between
runs.

* 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.
This commit is contained in:
Hector Rivas Gandara 2016-04-08 19:55:50 +01:00 committed by Paul Stack
parent ed7ff1dc7f
commit 0fdf91661d
2 changed files with 13 additions and 6 deletions

View File

@ -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)
}
}
}

View File

@ -128,8 +128,6 @@ resource "aws_sns_topic" "test_topic" {
name = "example"
policy = <<EOF
{
"Version": "2012-10-17",
"Id": "Policy1445931846145",
"Statement": [
{
"Sid": "Stmt1445931846145",
@ -140,7 +138,9 @@ resource "aws_sns_topic" "test_topic" {
"Action": "sns:Publish",
"Resource": "arn:aws:sns:us-west-2::example"
}
]
],
"Version": "2012-10-17",
"Id": "Policy1445931846145"
}
EOF
}