Skip to content

Commit

Permalink
resource/aws_cloudfront_distribution: Ignore attribute ordering of ca…
Browse files Browse the repository at this point in the history
…che behavior forwarded values `headers` and cookies `whitelisted_names` arguments

Reference: #7867

Output from acceptance testing prior to code updates:

```
--- FAIL: TestAccAWSCloudFrontDistribution_DefaultCacheBehavior_ForwardedValues_Cookies_WhitelistedNames (528.44s)
    testing.go:538: Step 0 error: After applying this step, the plan was not empty:

        DIFF:

        UPDATE: aws_cloudfront_distribution.test
          default_cache_behavior.0.forwarded_values.0.cookies.0.whitelisted_names.0: "test1" => "test2"
          default_cache_behavior.0.forwarded_values.0.cookies.0.whitelisted_names.1: "test2" => "test3"
          default_cache_behavior.0.forwarded_values.0.cookies.0.whitelisted_names.2: "test3" => "test1"

--- FAIL: TestAccAWSCloudFrontDistribution_DefaultCacheBehavior_ForwardedValues_Headers (528.49s)
    testing.go:538: Step 0 error: After applying this step, the plan was not empty:

        DIFF:

        UPDATE: aws_cloudfront_distribution.test
          default_cache_behavior.0.forwarded_values.0.headers.0: "Access-Control-Request-Headers" => "Origin"
          default_cache_behavior.0.forwarded_values.0.headers.1: "Access-Control-Request-Method" => "Access-Control-Request-Headers"
          default_cache_behavior.0.forwarded_values.0.headers.2: "Origin" => "Access-Control-Request-Method"

--- FAIL: TestAccAWSCloudFrontDistribution_OrderedCacheBehavior_ForwardedValues_Cookies_WhitelistedNames (528.46s)
    testing.go:538: Step 0 error: After applying this step, the plan was not empty:

        DIFF:

        UPDATE: aws_cloudfront_distribution.test
          ordered_cache_behavior.0.forwarded_values.0.cookies.0.whitelisted_names.0: "test1" => "test2"
          ordered_cache_behavior.0.forwarded_values.0.cookies.0.whitelisted_names.1: "test2" => "test3"
          ordered_cache_behavior.0.forwarded_values.0.cookies.0.whitelisted_names.2: "test3" => "test1"

--- FAIL: TestAccAWSCloudFrontDistribution_OrderedCacheBehavior_ForwardedValues_Headers (528.43s)
    testing.go:538: Step 0 error: After applying this step, the plan was not empty:

        DIFF:

        UPDATE: aws_cloudfront_distribution.test
          ordered_cache_behavior.0.forwarded_values.0.headers.0: "Access-Control-Request-Headers" => "Origin"
          ordered_cache_behavior.0.forwarded_values.0.headers.1: "Access-Control-Request-Method" => "Access-Control-Request-Headers"
          ordered_cache_behavior.0.forwarded_values.0.headers.2: "Origin" => "Access-Control-Request-Method"
```

Output from acceptance testing:

```
--- PASS: TestAccAWSCloudFrontDistribution_Origin_EmptyOriginID (2.56s)
--- PASS: TestAccAWSCloudFrontDistribution_Origin_EmptyDomainName (2.56s)
--- PASS: TestAccAWSCloudFrontDistribution_OrderedCacheBehavior_ForwardedValues_Cookies_WhitelistedNames (543.49s)
--- PASS: TestAccAWSCloudFrontDistribution_DefaultCacheBehavior_ForwardedValues_Headers (544.62s)
--- PASS: TestAccAWSCloudFrontDistribution_OrderedCacheBehavior_ForwardedValues_Headers (546.32s)
--- PASS: TestAccAWSCloudFrontDistribution_disappears (550.45s)
--- PASS: TestAccAWSCloudFrontDistribution_DefaultCacheBehavior_ForwardedValues_Cookies_WhitelistedNames (580.39s)
--- PASS: TestAccAWSCloudFrontDistribution_IsIPV6EnabledConfig (1173.77s)
--- PASS: TestAccAWSCloudFrontDistribution_RetainOnDelete (1178.86s)
--- PASS: TestAccAWSCloudFrontDistribution_orderedCacheBehavior (1181.80s)
--- PASS: TestAccAWSCloudFrontDistribution_noCustomErrorResponseConfig (1183.92s)
--- PASS: TestAccAWSCloudFrontDistribution_customOrigin (1270.14s)
--- PASS: TestAccAWSCloudFrontDistribution_S3Origin (1270.44s)
--- PASS: TestAccAWSCloudFrontDistribution_OriginGroups (1268.27s)
--- PASS: TestAccAWSCloudFrontDistribution_noOptionalItemsConfig (1271.35s)
--- PASS: TestAccAWSCloudFrontDistribution_multiOrigin (1272.23s)
--- PASS: TestAccAWSCloudFrontDistribution_WaitForDeployment (1269.96s)
--- PASS: TestAccAWSCloudFrontDistribution_HTTP11Config (1275.52s)
--- PASS: TestAccAWSCloudFrontDistribution_ViewerCertificate_AcmCertificateArn (1276.02s)
--- PASS: TestAccAWSCloudFrontDistribution_ViewerCertificate_AcmCertificateArn_ConflictsWithCloudFrontDefaultCertificate (1284.86s)
--- PASS: TestAccAWSCloudFrontDistribution_Enabled (1736.60s)
--- PASS: TestAccAWSCloudFrontDistribution_S3OriginWithTags (1886.36s)
```
  • Loading branch information
bflad committed Apr 2, 2019
1 parent 8470477 commit 905e981
Show file tree
Hide file tree
Showing 4 changed files with 658 additions and 40 deletions.
8 changes: 4 additions & 4 deletions aws/cloudfront_distribution_configuration_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func expandForwardedValues(m map[string]interface{}) *cloudfront.ForwardedValues
fv.Cookies = expandCookiePreference(v.([]interface{})[0].(map[string]interface{}))
}
if v, ok := m["headers"]; ok {
fv.Headers = expandHeaders(v.([]interface{}))
fv.Headers = expandHeaders(v.(*schema.Set).List())
}
if v, ok := m["query_string_cache_keys"]; ok {
fv.QueryStringCacheKeys = expandQueryStringCacheKeys(v.([]interface{}))
Expand All @@ -433,7 +433,7 @@ func flattenForwardedValues(fv *cloudfront.ForwardedValues) map[string]interface
m["cookies"] = []interface{}{flattenCookiePreference(fv.Cookies)}
}
if fv.Headers != nil {
m["headers"] = flattenHeaders(fv.Headers)
m["headers"] = schema.NewSet(schema.HashString, flattenHeaders(fv.Headers))
}
if fv.QueryStringCacheKeys != nil {
m["query_string_cache_keys"] = flattenQueryStringCacheKeys(fv.QueryStringCacheKeys)
Expand Down Expand Up @@ -474,7 +474,7 @@ func expandCookiePreference(m map[string]interface{}) *cloudfront.CookiePreferen
Forward: aws.String(m["forward"].(string)),
}
if v, ok := m["whitelisted_names"]; ok {
cp.WhitelistedNames = expandCookieNames(v.([]interface{}))
cp.WhitelistedNames = expandCookieNames(v.(*schema.Set).List())
}
return cp
}
Expand All @@ -483,7 +483,7 @@ func flattenCookiePreference(cp *cloudfront.CookiePreference) map[string]interfa
m := make(map[string]interface{})
m["forward"] = *cp.Forward
if cp.WhitelistedNames != nil {
m["whitelisted_names"] = flattenCookieNames(cp.WhitelistedNames)
m["whitelisted_names"] = schema.NewSet(schema.HashString, flattenCookieNames(cp.WhitelistedNames))
}
return m
}
Expand Down
46 changes: 20 additions & 26 deletions aws/cloudfront_distribution_configuration_structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func forwardedValuesConf() map[string]interface{} {
}
}

func headersConf() []interface{} {
return []interface{}{"X-Example1", "X-Example2"}
func headersConf() *schema.Set {
return schema.NewSet(schema.HashString, []interface{}{"X-Example1", "X-Example2"})
}

func queryStringCacheKeysConf() []interface{} {
Expand All @@ -72,8 +72,8 @@ func cookiePreferenceConf() map[string]interface{} {
}
}

func cookieNamesConf() []interface{} {
return []interface{}{"Example1", "Example2"}
func cookieNamesConf() *schema.Set {
return schema.NewSet(schema.HashString, []interface{}{"Example1", "Example2"})
}

func allowedMethodsConf() *schema.Set {
Expand Down Expand Up @@ -280,7 +280,7 @@ func TestCloudFrontStructure_expandCloudFrontDefaultCacheBehavior(t *testing.T)
if *dcb.TargetOriginId != "myS3Origin" {
t.Fatalf("Expected TargetOriginId to be allow-all, got %v", *dcb.TargetOriginId)
}
if !reflect.DeepEqual(dcb.ForwardedValues.Headers.Items, expandStringList(headersConf())) {
if !reflect.DeepEqual(dcb.ForwardedValues.Headers.Items, expandStringSet(headersConf())) {
t.Fatalf("Expected Items to be %v, got %v", headersConf(), dcb.ForwardedValues.Headers.Items)
}
if *dcb.MinTTL != 0 {
Expand Down Expand Up @@ -394,10 +394,10 @@ func TestCloudFrontStructure_expandForwardedValues(t *testing.T) {
if !*fv.QueryString {
t.Fatalf("Expected QueryString to be true, got %v", *fv.QueryString)
}
if !reflect.DeepEqual(fv.Cookies.WhitelistedNames.Items, expandStringList(cookieNamesConf())) {
if !reflect.DeepEqual(fv.Cookies.WhitelistedNames.Items, expandStringSet(cookieNamesConf())) {
t.Fatalf("Expected Cookies.WhitelistedNames.Items to be %v, got %v", cookieNamesConf(), fv.Cookies.WhitelistedNames.Items)
}
if !reflect.DeepEqual(fv.Headers.Items, expandStringList(headersConf())) {
if !reflect.DeepEqual(fv.Headers.Items, expandStringSet(headersConf())) {
t.Fatalf("Expected Headers.Items to be %v, got %v", headersConf(), fv.Headers.Items)
}
}
Expand All @@ -410,31 +410,25 @@ func TestCloudFrontStructure_flattenForwardedValues(t *testing.T) {
if !out["query_string"].(bool) {
t.Fatalf("Expected out[query_string] to be true, got %v", out["query_string"])
}
if !reflect.DeepEqual(out["cookies"], in["cookies"]) {
t.Fatalf("Expected out[cookies] to be %v, got %v", in["cookies"], out["cookies"])
}
if !reflect.DeepEqual(out["headers"], in["headers"]) {
t.Fatalf("Expected out[headers] to be %v, got %v", in["headers"], out["headers"])
}
}

func TestCloudFrontStructure_expandHeaders(t *testing.T) {
data := headersConf()
h := expandHeaders(data)
h := expandHeaders(data.List())
if *h.Quantity != 2 {
t.Fatalf("Expected Quantity to be 2, got %v", *h.Quantity)
}
if !reflect.DeepEqual(h.Items, expandStringList(data)) {
if !reflect.DeepEqual(h.Items, expandStringSet(data)) {
t.Fatalf("Expected Items to be %v, got %v", data, h.Items)
}
}

func TestCloudFrontStructure_flattenHeaders(t *testing.T) {
in := headersConf()
h := expandHeaders(in)
out := flattenHeaders(h)
h := expandHeaders(in.List())
out := schema.NewSet(schema.HashString, flattenHeaders(h))

if !reflect.DeepEqual(in, out) {
if !in.Equal(out) {
t.Fatalf("Expected out to be %v, got %v", in, out)
}
}
Expand Down Expand Up @@ -466,7 +460,7 @@ func TestCloudFrontStructure_expandCookiePreference(t *testing.T) {
if *cp.Forward != "whitelist" {
t.Fatalf("Expected Forward to be whitelist, got %v", *cp.Forward)
}
if !reflect.DeepEqual(cp.WhitelistedNames.Items, expandStringList(cookieNamesConf())) {
if !reflect.DeepEqual(cp.WhitelistedNames.Items, expandStringSet(cookieNamesConf())) {
t.Fatalf("Expected WhitelistedNames.Items to be %v, got %v", cookieNamesConf(), cp.WhitelistedNames.Items)
}
}
Expand All @@ -476,28 +470,28 @@ func TestCloudFrontStructure_flattenCookiePreference(t *testing.T) {
cp := expandCookiePreference(in)
out := flattenCookiePreference(cp)

if !reflect.DeepEqual(in, out) {
t.Fatalf("Expected out to be %v, got %v", in, out)
if e, a := in["forward"], out["forward"]; e != a {
t.Fatalf("Expected forward to be %v, got %v", e, a)
}
}

func TestCloudFrontStructure_expandCookieNames(t *testing.T) {
data := cookieNamesConf()
cn := expandCookieNames(data)
cn := expandCookieNames(data.List())
if *cn.Quantity != 2 {
t.Fatalf("Expected Quantity to be 2, got %v", *cn.Quantity)
}
if !reflect.DeepEqual(cn.Items, expandStringList(data)) {
if !reflect.DeepEqual(cn.Items, expandStringSet(data)) {
t.Fatalf("Expected Items to be %v, got %v", data, cn.Items)
}
}

func TestCloudFrontStructure_flattenCookieNames(t *testing.T) {
in := cookieNamesConf()
cn := expandCookieNames(in)
out := flattenCookieNames(cn)
cn := expandCookieNames(in.List())
out := schema.NewSet(schema.HashString, flattenCookieNames(cn))

if !reflect.DeepEqual(in, out) {
if !in.Equal(out) {
t.Fatalf("Expected out to be %v, got %v", in, out)
}
}
Expand Down
12 changes: 6 additions & 6 deletions aws/resource_aws_cloudfront_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ func resourceAwsCloudFrontDistribution() *schema.Resource {
Required: true,
},
"whitelisted_names": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"headers": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
Expand Down Expand Up @@ -209,15 +209,15 @@ func resourceAwsCloudFrontDistribution() *schema.Resource {
Required: true,
},
"whitelisted_names": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"headers": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
Expand Down Expand Up @@ -366,15 +366,15 @@ func resourceAwsCloudFrontDistribution() *schema.Resource {
Required: true,
},
"whitelisted_names": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"headers": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
Expand Down
Loading

0 comments on commit 905e981

Please sign in to comment.