From 4739f3ccd24de5af4cec562023f3c81b557840ee Mon Sep 17 00:00:00 2001 From: Vincent Ting Date: Fri, 14 Aug 2020 08:17:00 +0800 Subject: [PATCH] refactor: renaming CloudFormationTemplate to JsonOrYaml --- aws/data_source_aws_cloudformation_stack.go | 2 +- aws/diff_suppress_funcs.go | 8 +++----- aws/diff_suppress_funcs_test.go | 4 ++-- aws/resource_aws_apigatewayv2_api.go | 4 ++-- aws/resource_aws_cloudformation_stack.go | 10 +++++----- aws/resource_aws_cloudformation_stack_set.go | 4 ++-- aws/structure.go | 2 +- aws/structure_test.go | 6 +++--- aws/validators.go | 4 +--- aws/validators_test.go | 11 ++++++----- 10 files changed, 26 insertions(+), 29 deletions(-) diff --git a/aws/data_source_aws_cloudformation_stack.go b/aws/data_source_aws_cloudformation_stack.go index 4e67bb9de34..f84b8eccb20 100644 --- a/aws/data_source_aws_cloudformation_stack.go +++ b/aws/data_source_aws_cloudformation_stack.go @@ -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) } diff --git a/aws/diff_suppress_funcs.go b/aws/diff_suppress_funcs.go index c3fae0590ff..15e2e2e6011 100644 --- a/aws/diff_suppress_funcs.go +++ b/aws/diff_suppress_funcs.go @@ -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) @@ -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 { diff --git a/aws/diff_suppress_funcs_test.go b/aws/diff_suppress_funcs_test.go index 0b406d6438d..3096217fd9d 100644 --- a/aws/diff_suppress_funcs_test.go +++ b/aws/diff_suppress_funcs_test.go @@ -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 @@ -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) diff --git a/aws/resource_aws_apigatewayv2_api.go b/aws/resource_aws_apigatewayv2_api.go index 2658677d04e..b0a455fa54b 100644 --- a/aws/resource_aws_apigatewayv2_api.go +++ b/aws/resource_aws_apigatewayv2_api.go @@ -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, diff --git a/aws/resource_aws_cloudformation_stack.go b/aws/resource_aws_cloudformation_stack.go index f9399d3dc1e..1561363e5b1 100644 --- a/aws/resource_aws_cloudformation_stack.go +++ b/aws/resource_aws_cloudformation_stack.go @@ -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 }, }, @@ -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) } @@ -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) } @@ -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) } diff --git a/aws/resource_aws_cloudformation_stack_set.go b/aws/resource_aws_cloudformation_stack_set.go index e82a78864c0..92adbce18bf 100644 --- a/aws/resource_aws_cloudformation_stack_set.go +++ b/aws/resource_aws_cloudformation_stack_set.go @@ -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, diff --git a/aws/structure.go b/aws/structure.go index 9782f58ff84..050ab5f8006 100644 --- a/aws/structure.go +++ b/aws/structure.go @@ -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)) } diff --git a/aws/structure_test.go b/aws/structure_test.go index 43aa0da03c4..bed89b372b7 100644 --- a/aws/structure_test.go +++ b/aws/structure_test.go @@ -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) } @@ -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) } diff --git a/aws/validators.go b/aws/validators.go index e32203ae2d8..12ae9401c9e 100644 --- a/aws/validators.go +++ b/aws/validators.go @@ -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)) @@ -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, diff --git a/aws/validators_test.go b/aws/validators_test.go index ec996f73152..9439b384c91 100644 --- a/aws/validators_test.go +++ b/aws/validators_test.go @@ -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) { @@ -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 @@ -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) } @@ -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) }