From 32f7a86e1008d7af74486daf3eb41da3e957a6a6 Mon Sep 17 00:00:00 2001 From: Tim Gossett Date: Fri, 6 Jan 2017 10:20:35 -0500 Subject: [PATCH] lambda_function_association attribute as []interface{} --- ...nt_distribution_configuration_structure.go | 30 ++++--------------- ...stribution_configuration_structure_test.go | 13 ++++---- .../resource_aws_cloudfront_distribution.go | 2 -- 3 files changed, 12 insertions(+), 33 deletions(-) diff --git a/builtin/providers/aws/cloudfront_distribution_configuration_structure.go b/builtin/providers/aws/cloudfront_distribution_configuration_structure.go index 3506fe7ff..6bafc54bb 100644 --- a/builtin/providers/aws/cloudfront_distribution_configuration_structure.go +++ b/builtin/providers/aws/cloudfront_distribution_configuration_structure.go @@ -228,11 +228,6 @@ func defaultCacheBehaviorHash(v interface{}) int { buf.WriteString(fmt.Sprintf("%s-", e.(string))) } } - if d, ok := m["lambda_function_association"]; ok { - for _, e := range d.(*schema.Set).List() { - buf.WriteString(fmt.Sprintf("%d-", lambdaFunctionAssociationHash(e.(map[string]interface{})))) - } - } return hashcode.String(buf.String()) } @@ -365,11 +360,6 @@ func cacheBehaviorHash(v interface{}) int { if d, ok := m["path_pattern"]; ok { buf.WriteString(fmt.Sprintf("%s-", d)) } - if d, ok := m["lambda_function_association"]; ok { - for _, e := range d.(*schema.Set).List() { - buf.WriteString(fmt.Sprintf("%d-", lambdaFunctionAssociationHash(e.(map[string]interface{})))) - } - } return hashcode.String(buf.String()) } @@ -400,11 +390,11 @@ func expandLambdaFunctionAssociations(v interface{}) *cloudfront.LambdaFunctionA } } - s := v.(*schema.Set) + s := v.([]interface{}) var lfa cloudfront.LambdaFunctionAssociations - lfa.Quantity = aws.Int64(int64(s.Len())) - lfa.Items = make([]*cloudfront.LambdaFunctionAssociation, s.Len()) - for i, lf := range s.List() { + lfa.Quantity = aws.Int64(int64(len(s))) + lfa.Items = make([]*cloudfront.LambdaFunctionAssociation, len(s)) + for i, lf := range s { lfa.Items[i] = expandLambdaFunctionAssociation(lf.(map[string]interface{})) } return &lfa @@ -421,12 +411,12 @@ func expandLambdaFunctionAssociation(lf map[string]interface{}) *cloudfront.Lamb return &lfa } -func flattenLambdaFunctionAssociations(lfa *cloudfront.LambdaFunctionAssociations) *schema.Set { +func flattenLambdaFunctionAssociations(lfa *cloudfront.LambdaFunctionAssociations) []interface{} { s := make([]interface{}, len(lfa.Items)) for i, v := range lfa.Items { s[i] = flattenLambdaFunctionAssociation(v) } - return schema.NewSet(lambdaFunctionAssociationHash, s) + return s } func flattenLambdaFunctionAssociation(lfa *cloudfront.LambdaFunctionAssociation) map[string]interface{} { @@ -438,14 +428,6 @@ func flattenLambdaFunctionAssociation(lfa *cloudfront.LambdaFunctionAssociation) return m } -func lambdaFunctionAssociationHash(v interface{}) int { - var buf bytes.Buffer - m := v.(map[string]interface{}) - buf.WriteString(fmt.Sprintf("%s-", m["event_type"].(string))) - buf.WriteString(fmt.Sprintf("%s", m["lambda_arn"].(string))) - return hashcode.String(buf.String()) -} - func expandForwardedValues(m map[string]interface{}) *cloudfront.ForwardedValues { fv := &cloudfront.ForwardedValues{ QueryString: aws.Bool(m["query_string"].(bool)), diff --git a/builtin/providers/aws/cloudfront_distribution_configuration_structure_test.go b/builtin/providers/aws/cloudfront_distribution_configuration_structure_test.go index f06230b81..e83a96b1c 100644 --- a/builtin/providers/aws/cloudfront_distribution_configuration_structure_test.go +++ b/builtin/providers/aws/cloudfront_distribution_configuration_structure_test.go @@ -46,7 +46,7 @@ func trustedSignersConf() []interface{} { return []interface{}{"1234567890EX", "1234567891EX"} } -func lambdaFunctionAssociationsConf() *schema.Set { +func lambdaFunctionAssociationsConf() []interface{} { s := []interface{}{ map[string]interface{}{ "event_type": "viewer-request", @@ -57,7 +57,7 @@ func lambdaFunctionAssociationsConf() *schema.Set { "lambda_arn": "arn:aws:lambda:us-east-1:999999999:function2:alias", }, } - return schema.NewSet(lambdaFunctionAssociationHash, s) + return s } func forwardedValuesConf() map[string]interface{} { @@ -359,9 +359,8 @@ func TestCloudFrontStructure_flattenCacheBehavior(t *testing.T) { if out["target_origin_id"] != "myS3Origin" { t.Fatalf("Expected out[target_origin_id] to be myS3Origin, got %v", out["target_origin_id"]) } - diff = out["lambda_function_association"].(*schema.Set).Difference(in["lambda_function_association"].(*schema.Set)) - if diff.Len() > 0 { - t.Fatalf("Expected out[lambda_function_association] to be %v, got %v, diff: %v", out["lambda_function_association"], in["lambda_function_association"], diff) + if reflect.DeepEqual(out["lambda_function_associations"], in["lambda_function_associations"]) != true { + t.Fatalf("Expected out[lambda_function_associations] to be %v, got %v", in["lambda_function_associations"], out["lambda_function_associations"]) } diff = out["forwarded_values"].(*schema.Set).Difference(in["forwarded_values"].(*schema.Set)) if len(diff.List()) > 0 { @@ -475,13 +474,13 @@ func TestCloudFrontStructure_flattenlambdaFunctionAssociations(t *testing.T) { lfa := expandLambdaFunctionAssociations(in) out := flattenLambdaFunctionAssociations(lfa) - if out.Difference(in).Len() != 0 { + if reflect.DeepEqual(in, out) != true { t.Fatalf("Expected out to be %v, got %v", in, out) } } func TestCloudFrontStructure_expandlambdaFunctionAssociations_empty(t *testing.T) { - data := schema.NewSet(lambdaFunctionAssociationHash, []interface{}{}) + data := []interface{}{} lfa := expandLambdaFunctionAssociations(data) if *lfa.Quantity != 0 { t.Fatalf("Expected Quantity to be 0, got %v", *lfa.Quantity) diff --git a/builtin/providers/aws/resource_aws_cloudfront_distribution.go b/builtin/providers/aws/resource_aws_cloudfront_distribution.go index 301f0481a..55afa516a 100644 --- a/builtin/providers/aws/resource_aws_cloudfront_distribution.go +++ b/builtin/providers/aws/resource_aws_cloudfront_distribution.go @@ -105,7 +105,6 @@ func resourceAwsCloudFrontDistribution() *schema.Resource { "lambda_function_association": { Type: schema.TypeSet, Optional: true, - Set: lambdaFunctionAssociationHash, MaxItems: 4, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -253,7 +252,6 @@ func resourceAwsCloudFrontDistribution() *schema.Resource { "lambda_function_association": { Type: schema.TypeSet, Optional: true, - Set: lambdaFunctionAssociationHash, MaxItems: 4, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{