Skip to content

Commit

Permalink
Merge pull request #8150 from terraform-providers/b-aws_cloudfront_di…
Browse files Browse the repository at this point in the history
…stribution-headers-TypeList

resource/aws_cloudfront_distribution: Ignore attribute ordering of cache behavior forwarded values `headers` and cookies `whitelisted_names` arguments
  • Loading branch information
bflad authored Apr 4, 2019
2 parents 90b9618 + 905e981 commit 86059fd
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 86059fd

Please sign in to comment.