Skip to content

Commit

Permalink
refactor: renaming CloudFormationTemplate to JsonOrYaml
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Ting committed Aug 14, 2020
1 parent 2fec944 commit 4739f3c
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion aws/data_source_aws_cloudformation_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func dataSourceAwsCloudFormationStackRead(d *schema.ResourceData, meta interface
return err
}

template, err := normalizeCloudFormationTemplate(*tOut.TemplateBody)
template, err := normalizeJsonOrYamlString(*tOut.TemplateBody)
if err != nil {
return fmt.Errorf("template body contains an invalid JSON or YAML: %s", err)
}
Expand Down
8 changes: 3 additions & 5 deletions aws/diff_suppress_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ func suppressOpenIdURL(k, old, new string, d *schema.ResourceData) bool {
return oldUrl.String() == newUrl.String()
}

func suppressCloudFormationTemplateBodyDiffs(k, old, new string, d *schema.ResourceData) bool {
normalizedOld, err := normalizeCloudFormationTemplate(old)
func suppressEquivalentJsonOrYamlDiffs(k, old, new string, d *schema.ResourceData) bool {
normalizedOld, err := normalizeJsonOrYamlString(old)

if err != nil {
log.Printf("[WARN] Unable to normalize Terraform state CloudFormation template body: %s", err)
return false
}

normalizedNew, err := normalizeCloudFormationTemplate(new)
normalizedNew, err := normalizeJsonOrYamlString(new)

if err != nil {
log.Printf("[WARN] Unable to normalize Terraform configuration CloudFormation template body: %s", err)
Expand All @@ -115,8 +115,6 @@ func suppressCloudFormationTemplateBodyDiffs(k, old, new string, d *schema.Resou
return normalizedOld == normalizedNew
}

var suppressOpenAPISpecificationDiffs = suppressCloudFormationTemplateBodyDiffs

// suppressEqualCIDRBlockDiffs provides custom difference suppression for CIDR blocks
// that have different string values but represent the same CIDR.
func suppressEqualCIDRBlockDiffs(k, old, new string, d *schema.ResourceData) bool {
Expand Down
4 changes: 2 additions & 2 deletions aws/diff_suppress_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestSuppressEquivalentTypeStringBoolean(t *testing.T) {
}
}

func TestSuppressCloudFormationTemplateBodyDiffs(t *testing.T) {
func TestSuppressEquivalentJsonOrYamlDiffs(t *testing.T) {
testCases := []struct {
description string
equivalent bool
Expand Down Expand Up @@ -253,7 +253,7 @@ Outputs:
}

for _, tc := range testCases {
value := suppressCloudFormationTemplateBodyDiffs("test_property", tc.old, tc.new, nil)
value := suppressEquivalentJsonOrYamlDiffs("test_property", tc.old, tc.new, nil)

if tc.equivalent && !value {
t.Fatalf("expected test case (%s) to be equivalent", tc.description)
Expand Down
4 changes: 2 additions & 2 deletions aws/resource_aws_apigatewayv2_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func resourceAwsApiGatewayV2Api() *schema.Resource {
"body": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: suppressOpenAPISpecificationDiffs,
ValidateFunc: validateOpenAPISpecification,
DiffSuppressFunc: suppressEquivalentJsonOrYamlDiffs,
ValidateFunc: validateStringIsJsonOrYaml,
},
"protocol_type": {
Type: schema.TypeString,
Expand Down
10 changes: 5 additions & 5 deletions aws/resource_aws_cloudformation_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func resourceAwsCloudFormationStack() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validateCloudFormationTemplate,
ValidateFunc: validateStringIsJsonOrYaml,
StateFunc: func(v interface{}) string {
template, _ := normalizeCloudFormationTemplate(v)
template, _ := normalizeJsonOrYamlString(v)
return template
},
},
Expand Down Expand Up @@ -121,7 +121,7 @@ func resourceAwsCloudFormationStackCreate(d *schema.ResourceData, meta interface
StackName: aws.String(d.Get("name").(string)),
}
if v, ok := d.GetOk("template_body"); ok {
template, err := normalizeCloudFormationTemplate(v)
template, err := normalizeJsonOrYamlString(v)
if err != nil {
return fmt.Errorf("template body contains an invalid JSON or YAML: %s", err)
}
Expand Down Expand Up @@ -300,7 +300,7 @@ func resourceAwsCloudFormationStackRead(d *schema.ResourceData, meta interface{}
return err
}

template, err := normalizeCloudFormationTemplate(*out.TemplateBody)
template, err := normalizeJsonOrYamlString(*out.TemplateBody)
if err != nil {
return fmt.Errorf("template body contains an invalid JSON or YAML: %s", err)
}
Expand Down Expand Up @@ -365,7 +365,7 @@ func resourceAwsCloudFormationStackUpdate(d *schema.ResourceData, meta interface
input.TemplateURL = aws.String(v.(string))
}
if v, ok := d.GetOk("template_body"); ok && input.TemplateURL == nil {
template, err := normalizeCloudFormationTemplate(v)
template, err := normalizeJsonOrYamlString(v)
if err != nil {
return fmt.Errorf("template body contains an invalid JSON or YAML: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions aws/resource_aws_cloudformation_stack_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func resourceAwsCloudFormationStackSet() *schema.Resource {
Optional: true,
Computed: true,
ConflictsWith: []string{"template_url"},
DiffSuppressFunc: suppressCloudFormationTemplateBodyDiffs,
ValidateFunc: validateCloudFormationTemplate,
DiffSuppressFunc: suppressEquivalentJsonOrYamlDiffs,
ValidateFunc: validateStringIsJsonOrYaml,
},
"template_url": {
Type: schema.TypeString,
Expand Down
2 changes: 1 addition & 1 deletion aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2267,7 +2267,7 @@ func checkYamlString(yamlString interface{}) (string, error) {
return s, err
}

func normalizeCloudFormationTemplate(templateString interface{}) (string, error) {
func normalizeJsonOrYamlString(templateString interface{}) (string, error) {
if looksLikeJsonString(templateString) {
return structure.NormalizeJsonString(templateString.(string))
}
Expand Down
6 changes: 3 additions & 3 deletions aws/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,12 +1266,12 @@ abc:
}
}

func TestNormalizeCloudFormationTemplate(t *testing.T) {
func TestNormalizeJsonOrYamlString(t *testing.T) {
var err error
var actual string

validNormalizedJson := `{"abc":"1"}`
actual, err = normalizeCloudFormationTemplate(validNormalizedJson)
actual, err = normalizeJsonOrYamlString(validNormalizedJson)
if err != nil {
t.Fatalf("Expected not to throw an error while parsing template, but got: %s", err)
}
Expand All @@ -1281,7 +1281,7 @@ func TestNormalizeCloudFormationTemplate(t *testing.T) {

validNormalizedYaml := `abc: 1
`
actual, err = normalizeCloudFormationTemplate(validNormalizedYaml)
actual, err = normalizeJsonOrYamlString(validNormalizedYaml)
if err != nil {
t.Fatalf("Expected not to throw an error while parsing template, but got: %s", err)
}
Expand Down
4 changes: 1 addition & 3 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ func validateIAMPolicyJson(v interface{}, k string) (ws []string, errors []error
return
}

func validateCloudFormationTemplate(v interface{}, k string) (ws []string, errors []error) {
func validateStringIsJsonOrYaml(v interface{}, k string) (ws []string, errors []error) {
if looksLikeJsonString(v) {
if _, err := structure.NormalizeJsonString(v); err != nil {
errors = append(errors, fmt.Errorf("%q contains an invalid JSON: %s", k, err))
Expand All @@ -989,8 +989,6 @@ func validateCloudFormationTemplate(v interface{}, k string) (ws []string, error
return
}

var validateOpenAPISpecification = validateCloudFormationTemplate

func validateApiGatewayIntegrationContentHandling() schema.SchemaValidateFunc {
return validation.StringInSlice([]string{
apigateway.ContentHandlingStrategyConvertToBinary,
Expand Down
11 changes: 6 additions & 5 deletions aws/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package aws

import (
"fmt"
"github.com/aws/aws-sdk-go/service/cognitoidentity"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"regexp"
"strings"
"testing"

"github.com/aws/aws-sdk-go/service/cognitoidentity"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
)

func TestValidateTypeStringNullableBoolean(t *testing.T) {
Expand Down Expand Up @@ -823,7 +824,7 @@ func TestValidateIAMPolicyJsonString(t *testing.T) {
}
}

func TestValidateCloudFormationTemplate(t *testing.T) {
func TestValidateStringIsJsonOrYaml(t *testing.T) {
type testCases struct {
Value string
ErrCount int
Expand All @@ -841,7 +842,7 @@ func TestValidateCloudFormationTemplate(t *testing.T) {
}

for _, tc := range invalidCases {
_, errors := validateCloudFormationTemplate(tc.Value, "template")
_, errors := validateStringIsJsonOrYaml(tc.Value, "template")
if len(errors) != tc.ErrCount {
t.Fatalf("Expected %q to trigger a validation error.", tc.Value)
}
Expand All @@ -859,7 +860,7 @@ func TestValidateCloudFormationTemplate(t *testing.T) {
}

for _, tc := range validCases {
_, errors := validateCloudFormationTemplate(tc.Value, "template")
_, errors := validateStringIsJsonOrYaml(tc.Value, "template")
if len(errors) != tc.ErrCount {
t.Fatalf("Expected %q not to trigger a validation error.", tc.Value)
}
Expand Down

0 comments on commit 4739f3c

Please sign in to comment.