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:
parent
a9dce86bf2
commit
6c27f175b5
|
@ -1,19 +1,17 @@
|
||||||
package aws
|
package aws
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"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"
|
||||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||||
"github.com/aws/aws-sdk-go/service/sns"
|
"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
|
// Mutable attributes
|
||||||
|
@ -49,21 +47,11 @@ func resourceAwsSnsTopic() *schema.Resource {
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
|
ValidateFunc: validateJsonString,
|
||||||
DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs,
|
DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs,
|
||||||
StateFunc: func(v interface{}) string {
|
StateFunc: func(v interface{}) string {
|
||||||
s, ok := v.(string)
|
json, _ := normalizeJsonString(v)
|
||||||
if !ok || s == "" {
|
return json
|
||||||
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
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"delivery_policy": &schema.Schema{
|
"delivery_policy": &schema.Schema{
|
||||||
|
@ -191,7 +179,10 @@ func resourceAwsSnsTopicRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
if resource.Schema[iKey] != nil {
|
if resource.Schema[iKey] != nil {
|
||||||
var value string
|
var value string
|
||||||
if iKey == "policy" {
|
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 {
|
} else {
|
||||||
value = *attrmap[oKey]
|
value = *attrmap[oKey]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue