Add JSON validation to the aws_sns_topic resource.

This commit adds support for new helper function which is used to
normalise and validate JSON string.

This commit also removes unnecessary code from the StateFunc function,
and reduces it so that it only uses the normalizeJsonString helper.

Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
This commit is contained in:
Krzysztof Wilczynski 2016-09-14 17:18:16 +01:00 committed by stack72
parent a9dce86bf2
commit 6c27f175b5
No known key found for this signature in database
GPG Key ID: 8619A619B085CB16
1 changed files with 10 additions and 19 deletions

View File

@ -1,19 +1,17 @@
package aws
import (
"bytes"
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
// Mutable attributes
@ -49,21 +47,11 @@ func resourceAwsSnsTopic() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validateJsonString,
DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs,
StateFunc: func(v interface{}) string {
s, ok := v.(string)
if !ok || s == "" {
return ""
}
jsonb := []byte(s)
buffer := new(bytes.Buffer)
if err := json.Compact(buffer, jsonb); err != nil {
log.Printf("[WARN] Error compacting JSON for Policy in SNS Topic")
return ""
}
value := normalizeJson(buffer.String())
log.Printf("[DEBUG] topic policy before save: %s", value)
return value
json, _ := normalizeJsonString(v)
return json
},
},
"delivery_policy": &schema.Schema{
@ -191,7 +179,10 @@ func resourceAwsSnsTopicRead(d *schema.ResourceData, meta interface{}) error {
if resource.Schema[iKey] != nil {
var value string
if iKey == "policy" {
value = normalizeJson(*attrmap[oKey])
value, err = normalizeJsonString(*attrmap[oKey])
if err != nil {
return errwrap.Wrapf("policy contains an invalid JSON: {{err}}", err)
}
} else {
value = *attrmap[oKey]
}