Skip to content

Commit

Permalink
addressing comments. still need to fix acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Barry Tam committed Mar 16, 2019
1 parent 7ac40cb commit bdf9ec3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 91 deletions.
74 changes: 35 additions & 39 deletions aws/cloudfront_distribution_configuration_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,12 +625,12 @@ func flattenOriginGroups(ogs *cloudfront.OriginGroups) *schema.Set {
}

func expandOriginGroup(m map[string]interface{}) *cloudfront.OriginGroup {
failoverCriteria := m["failover_criteria"].(*schema.Set)
members := m["members"].(*schema.Set)
failoverCriteria := m["failover_criteria"].([]interface{})[0].(map[string]interface{})
members := m["member"].([]interface{})
originGroup := &cloudfront.OriginGroup{
Id: aws.String(m["origin_id"].(string)),
FailoverCriteria: expandOriginGroupFailoverCriteria(failoverCriteria),
Members: expandOriginGroupMembers(members),
Members: expandMembers(members),
}
return originGroup
}
Expand All @@ -642,17 +642,16 @@ func flattenOriginGroup(og *cloudfront.OriginGroup) map[string]interface{} {
m["failover_criteria"] = flattenOriginGroupFailoverCriteria(og.FailoverCriteria)
}
if og.Members != nil {
m["members"] = flattenOriginGroupMembers(og.Members)
m["member"] = flattenOriginGroupMembers(og.Members)
}
return m
}

func expandOriginGroupFailoverCriteria(s *schema.Set) *cloudfront.OriginGroupFailoverCriteria {
func expandOriginGroupFailoverCriteria(m map[string]interface{}) *cloudfront.OriginGroupFailoverCriteria {
failoverCriteria := &cloudfront.OriginGroupFailoverCriteria{}
m := s.List()[0].(map[string]interface{})
if v, ok := m["status_codes"]; ok {
codes := []*int64{}
for _, code := range v.([]interface{}) {
for _, code := range v.(*schema.Set).List() {
codes = append(codes, aws.Int64(int64(code.(int))))
}
failoverCriteria.StatusCodes = &cloudfront.StatusCodes{
Expand All @@ -663,23 +662,26 @@ func expandOriginGroupFailoverCriteria(s *schema.Set) *cloudfront.OriginGroupFai
return failoverCriteria
}

func flattenOriginGroupFailoverCriteria(ogfc *cloudfront.OriginGroupFailoverCriteria) *schema.Set {
func flattenOriginGroupFailoverCriteria(ogfc *cloudfront.OriginGroupFailoverCriteria) []interface{} {
m := make(map[string]interface{})
codes := []interface{}{}
for _, code := range ogfc.StatusCodes.Items {
codes = append(codes, *code)
if ogfc.StatusCodes.Items != nil {
l := []interface{}{}
for _, i := range ogfc.StatusCodes.Items {
l = append(l, int(*i))
}
m["status_codes"] = schema.NewSet(schema.HashInt, l)
}
m["status_codes"] = codes
s := []interface{}{m}
return schema.NewSet(failoverCriteriaHash, s)
return []interface{}{m}
}

func expandOriginGroupMembers(s *schema.Set) *cloudfront.OriginGroupMembers {
members := s.List()[0].(map[string]interface{})["ordered_origin_group_member"].([]interface{})
func expandMembers(l []interface{}) *cloudfront.OriginGroupMembers {
qty := 0
items := []*cloudfront.OriginGroupMember{}
for _, v := range members {
items = append(items, expandOriginGroupMember(v.(map[string]interface{})))
for _, m := range l {
ogm := &cloudfront.OriginGroupMember{
OriginId: aws.String(m.(map[string]interface{})["origin_id"].(string)),
}
items = append(items, ogm)
qty++
}
return &cloudfront.OriginGroupMembers{
Expand All @@ -688,15 +690,15 @@ func expandOriginGroupMembers(s *schema.Set) *cloudfront.OriginGroupMembers {
}
}

func flattenOriginGroupMembers(ogm *cloudfront.OriginGroupMembers) *schema.Set {
func flattenOriginGroupMembers(ogm *cloudfront.OriginGroupMembers) []interface{} {
s := []interface{}{}
for _, v := range ogm.Items {
s = append(s, flattenOriginGroupMember(v))
for _, i := range ogm.Items {
m := map[string]interface{}{
"origin_id": *i.OriginId,
}
s = append(s, m)
}
m := []interface{}{map[string]interface{}{
"ordered_origin_group_member": s,
}}
return schema.NewSet(membersHash, m)
return s
}

func expandOriginGroupMember(m map[string]interface{}) *cloudfront.OriginGroupMember {
Expand Down Expand Up @@ -745,37 +747,31 @@ func originGroupHash(v interface{}) int {
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["origin_id"].(string)))
if v, ok := m["failover_criteria"]; ok {
if s := v.(*schema.Set).List(); len(s) > 0 {
buf.WriteString(fmt.Sprintf("%d-", failoverCriteriaHash(s[0])))
if l := v.([]interface{}); len(l) > 0 {
buf.WriteString(fmt.Sprintf("%d-", failoverCriteriaHash(l[0])))
}
}
if v, ok := m["members"]; ok {
if members := v.(*schema.Set).List(); len(members) > 0 {
if v, ok := m["member"]; ok {
if members := v.([]interface{}); len(members) > 0 {
for _, member := range members {
buf.WriteString(fmt.Sprintf("%d-", membersHash(member)))
buf.WriteString(fmt.Sprintf("%d-", memberHash(member)))
}
}
}
return hashcode.String(buf.String())
}

func membersHash(v interface{}) int {
func memberHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})["ordered_origin_group_member"]
gm := m.([]interface{})
for _, member := range gm {
buf.WriteString(fmt.Sprintf("%s-", member.(map[string]interface{})["origin_id"]))
}

buf.WriteString(fmt.Sprintf("%s-", v.(map[string]interface{})["origin_id"]))
return hashcode.String(buf.String())
}

func failoverCriteriaHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
if v, ok := m["status_codes"]; ok {
codes := sortIntInterfaceSlice(v.([]interface{}))
for _, w := range codes {
for _, w := range v.(*schema.Set).List() {
buf.WriteString(fmt.Sprintf("%d-", w))
}
}
Expand Down
30 changes: 10 additions & 20 deletions aws/cloudfront_distribution_configuration_structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,35 +146,25 @@ func multiOriginConf() *schema.Set {
return schema.NewSet(originHash, []interface{}{originWithCustomConf(), originWithS3Conf()})
}

func orderedMemberConf() map[string]interface{} {
return map[string]interface{}{
"ordered_origin_group_member": []interface{}{map[string]interface{}{
"origin_id": "S3failover",
}, map[string]interface{}{
"origin_id": "S3origin",
}},
}
}

func originGroupMembers() *schema.Set {
return schema.NewSet(membersHash, []interface{}{orderedMemberConf()})
func originGroupMembers() []interface{} {
return []interface{}{map[string]interface{}{
"origin_id": "S3origin",
}, map[string]interface{}{
"origin_id": "S3failover",
}}
}

func failoverStatusCodes() map[string]interface{} {
return map[string]interface{}{
"status_codes": []interface{}{503, 504},
"status_codes": schema.NewSet(schema.HashInt, []interface{}{503, 504}),
}
}

func failoverCriteriaConf() *schema.Set {
return schema.NewSet(failoverCriteriaHash, []interface{}{failoverStatusCodes()})
}

func originGroupConf() map[string]interface{} {
return map[string]interface{}{
"origin_id": "groupS3",
"failover_criteria": failoverCriteriaConf(),
"members": originGroupMembers(),
"failover_criteria": []interface{}{failoverStatusCodes()},
"member": originGroupMembers(),
}
}

Expand Down Expand Up @@ -588,7 +578,7 @@ func TestCloudFrontStructure_expandOriginGroups(t *testing.T) {
t.Fatalf("Expected origin group id to be %v, got %v", "groupS3", *originGroup.Id)
}
if *originGroup.FailoverCriteria.StatusCodes.Quantity != 2 {
t.Fatalf("Expected 2 origin group members, got %v", *originGroup.FailoverCriteria.StatusCodes.Quantity)
t.Fatalf("Expected 2 origin group status codes, got %v", *originGroup.FailoverCriteria.StatusCodes.Quantity)
}
statusCodes := originGroup.FailoverCriteria.StatusCodes.Items
for _, code := range statusCodes {
Expand Down
24 changes: 8 additions & 16 deletions aws/resource_aws_cloudfront_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,37 +490,29 @@ func resourceAwsCloudFrontDistribution() *schema.Resource {
ValidateFunc: validation.NoZeroValues,
},
"failover_criteria": {
Type: schema.TypeSet,
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Set: failoverCriteriaHash,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status_codes": {
Type: schema.TypeList,
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeInt},
},
},
},
},
"members": {
Type: schema.TypeSet,
"member": {
Type: schema.TypeList,
Required: true,
Set: membersHash,
MinItems: 2,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ordered_origin_group_member": {
Type: schema.TypeList,
"origin_id": {
Type: schema.TypeString,
Required: true,
MinItems: 2,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"origin_id": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_cloudfront_distribution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ resource "aws_cloudfront_distribution" "failover_distribution" {
}
}
}
enabled = true
enabled = true
restrictions {
geo_restriction {
restriction_type = "whitelist"
Expand Down
15 changes: 0 additions & 15 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -1906,21 +1906,6 @@ func sortInterfaceSlice(in []interface{}) []interface{} {
return b
}

func sortIntInterfaceSlice(in []interface{}) []int {
a := []int{}
for _, v := range in {
val, ok := v.(int)
if !ok {
val = int(v.(int64))
}
a = append(a, val)
}

sort.Ints(a)

return a
}

// This function sorts List A to look like a list found in the tf file.
func sortListBasedonTFFile(in []string, d *schema.ResourceData) ([]string, error) {
listName := "layer_ids"
Expand Down

0 comments on commit bdf9ec3

Please sign in to comment.