implement CloudFront Lambda function associations

This commit is contained in:
Tim Gossett 2016-12-31 15:37:26 -05:00
parent af5baf7b13
commit 39520ab568
No known key found for this signature in database
GPG Key ID: 6708169C08D3CCF0
3 changed files with 182 additions and 12 deletions

View File

@ -228,6 +228,11 @@ 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())
}
@ -267,6 +272,11 @@ func expandCacheBehavior(m map[string]interface{}) *cloudfront.CacheBehavior {
} else {
cb.TrustedSigners = expandTrustedSigners([]interface{}{})
}
if v, ok := m["lambda_function_association"]; ok {
cb.LambdaFunctionAssociations = expandLambdaFunctionAssociations(v)
}
if v, ok := m["smooth_streaming"]; ok {
cb.SmoothStreaming = aws.Bool(v.(bool))
}
@ -294,6 +304,9 @@ func flattenCacheBehavior(cb *cloudfront.CacheBehavior) map[string]interface{} {
if len(cb.TrustedSigners.Items) > 0 {
m["trusted_signers"] = flattenTrustedSigners(cb.TrustedSigners)
}
if len(cb.LambdaFunctionAssociations.Items) > 0 {
m["lambda_function_association"] = flattenLambdaFunctionAssociations(cb.LambdaFunctionAssociations)
}
if cb.MaxTTL != nil {
m["max_ttl"] = int(*cb.MaxTTL)
}
@ -352,6 +365,11 @@ 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())
}
@ -375,6 +393,58 @@ func flattenTrustedSigners(ts *cloudfront.TrustedSigners) []interface{} {
return []interface{}{}
}
func expandLambdaFunctionAssociations(v interface{}) *cloudfront.LambdaFunctionAssociations {
if v == nil {
return &cloudfront.LambdaFunctionAssociations{
Quantity: aws.Int64(0),
}
}
s := v.(*schema.Set)
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.Items[i] = expandLambdaFunctionAssociation(lf.(map[string]interface{}))
}
return &lfa
}
func expandLambdaFunctionAssociation(lf map[string]interface{}) *cloudfront.LambdaFunctionAssociation {
var lfa cloudfront.LambdaFunctionAssociation
if v, ok := lf["event_type"]; ok {
lfa.EventType = aws.String(v.(string))
}
if v, ok := lf["lambda_arn"]; ok {
lfa.LambdaFunctionARN = aws.String(v.(string))
}
return &lfa
}
func flattenLambdaFunctionAssociations(lfa *cloudfront.LambdaFunctionAssociations) *schema.Set {
s := make([]interface{}, len(lfa.Items))
for i, v := range lfa.Items {
s[i] = flattenLambdaFunctionAssociation(v)
}
return schema.NewSet(lambdaFunctionAssociationHash, s)
}
func flattenLambdaFunctionAssociation(lfa *cloudfront.LambdaFunctionAssociation) map[string]interface{} {
m := map[string]interface{}{}
if lfa != nil {
m["event_type"] = *lfa.EventType
m["lambda_arn"] = *lfa.LambdaFunctionARN
}
return m
}
func lambdaFunctionAssociationHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(m["event_type"].(string))
return hashcode.String(buf.String())
}
func expandForwardedValues(m map[string]interface{}) *cloudfront.ForwardedValues {
fv := &cloudfront.ForwardedValues{
QueryString: aws.Bool(m["query_string"].(bool)),

View File

@ -5,22 +5,24 @@ import (
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/hashicorp/terraform/helper/schema"
)
func defaultCacheBehaviorConf() map[string]interface{} {
return map[string]interface{}{
"viewer_protocol_policy": "allow-all",
"target_origin_id": "myS3Origin",
"forwarded_values": schema.NewSet(forwardedValuesHash, []interface{}{forwardedValuesConf()}),
"min_ttl": 86400,
"trusted_signers": trustedSignersConf(),
"max_ttl": 365000000,
"smooth_streaming": false,
"default_ttl": 86400,
"allowed_methods": allowedMethodsConf(),
"cached_methods": cachedMethodsConf(),
"compress": true,
"viewer_protocol_policy": "allow-all",
"target_origin_id": "myS3Origin",
"forwarded_values": schema.NewSet(forwardedValuesHash, []interface{}{forwardedValuesConf()}),
"min_ttl": 86400,
"trusted_signers": trustedSignersConf(),
"lambda_function_association": lambdaFunctionAssociationsConf(),
"max_ttl": 365000000,
"smooth_streaming": false,
"default_ttl": 86400,
"allowed_methods": allowedMethodsConf(),
"cached_methods": cachedMethodsConf(),
"compress": true,
}
}
@ -44,6 +46,20 @@ func trustedSignersConf() []interface{} {
return []interface{}{"1234567890EX", "1234567891EX"}
}
func lambdaFunctionAssociationsConf() *schema.Set {
s := []interface{}{
map[string]interface{}{
"event_type": "viewer-request",
"lambda_arn": "arn:aws:lambda:us-east-1:999999999:function1:alias",
},
map[string]interface{}{
"event_type": "origin-response",
"lambda_arn": "arn:aws:lambda:us-east-1:999999999:function2:alias",
},
}
return schema.NewSet(lambdaFunctionAssociationHash, s)
}
func forwardedValuesConf() map[string]interface{} {
return map[string]interface{}{
"query_string": true,
@ -263,6 +279,9 @@ func TestCloudFrontStructure_expandDefaultCacheBehavior(t *testing.T) {
if *dcb.DefaultTTL != 86400 {
t.Fatalf("Expected DefaultTTL to be 86400, got %v", *dcb.DefaultTTL)
}
if *dcb.LambdaFunctionAssociations.Quantity != 2 {
t.Fatalf("Expected LambdaFunctionAssociations to be 2, got %v", *dcb.LambdaFunctionAssociations.Quantity)
}
if reflect.DeepEqual(dcb.AllowedMethods.Items, expandStringList(allowedMethodsConf())) != true {
t.Fatalf("Expected TrustedSigners.Items to be %v, got %v", allowedMethodsConf(), dcb.AllowedMethods.Items)
}
@ -312,6 +331,9 @@ func TestCloudFrontStructure_expandCacheBehavior(t *testing.T) {
if *cb.DefaultTTL != 86400 {
t.Fatalf("Expected DefaultTTL to be 86400, got %v", *cb.DefaultTTL)
}
if *cb.LambdaFunctionAssociations.Quantity != 2 {
t.Fatalf("Expected LambdaFunctionAssociations to be 2, got %v", *cb.LambdaFunctionAssociations.Quantity)
}
if reflect.DeepEqual(cb.AllowedMethods.Items, expandStringList(allowedMethodsConf())) != true {
t.Fatalf("Expected AllowedMethods.Items to be %v, got %v", allowedMethodsConf(), cb.AllowedMethods.Items)
}
@ -337,6 +359,10 @@ 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)
}
diff = out["forwarded_values"].(*schema.Set).Difference(in["forwarded_values"].(*schema.Set))
if len(diff.List()) > 0 {
t.Fatalf("Expected out[forwarded_values] to be %v, got %v, diff: %v", out["forwarded_values"], in["forwarded_values"], diff)
@ -417,7 +443,7 @@ func TestCloudFrontStructure_expandTrustedSigners_empty(t *testing.T) {
data := []interface{}{}
ts := expandTrustedSigners(data)
if *ts.Quantity != 0 {
t.Fatalf("Expected Quantity to be 2, got %v", *ts.Quantity)
t.Fatalf("Expected Quantity to be 0, got %v", *ts.Quantity)
}
if *ts.Enabled != false {
t.Fatalf("Expected Enabled to be true, got %v", *ts.Enabled)
@ -427,6 +453,44 @@ func TestCloudFrontStructure_expandTrustedSigners_empty(t *testing.T) {
}
}
func TestCloudFrontStructure_expandLambdaFunctionAssociations(t *testing.T) {
data := lambdaFunctionAssociationsConf()
lfa := expandLambdaFunctionAssociations(data)
if *lfa.Quantity != 2 {
t.Fatalf("Expected Quantity to be 2, got %v", *lfa.Quantity)
}
if len(lfa.Items) != 2 {
t.Fatalf("Expected Items to be len 2, got %v", len(lfa.Items))
}
if et := "origin-response"; *lfa.Items[0].EventType != et {
t.Fatalf("Expected first Item's EventType to be len %q, got %q", et, *lfa.Items[0].EventType)
}
}
func TestCloudFrontStructure_flattenlambdaFunctionAssociations(t *testing.T) {
in := lambdaFunctionAssociationsConf()
lfa := expandLambdaFunctionAssociations(in)
out := flattenLambdaFunctionAssociations(lfa)
if out.Difference(in).Len() != 0 {
t.Fatalf("Expected out to be %v, got %v", in, out)
}
}
func TestCloudFrontStructure_expandlambdaFunctionAssociations_empty(t *testing.T) {
data := schema.NewSet(lambdaFunctionAssociationHash, []interface{}{})
lfa := expandLambdaFunctionAssociations(data)
if *lfa.Quantity != 0 {
t.Fatalf("Expected Quantity to be 0, got %v", *lfa.Quantity)
}
if len(lfa.Items) != 0 {
t.Fatalf("Expected Items to be len 0, got %v", len(lfa.Items))
}
if reflect.DeepEqual(lfa.Items, []*cloudfront.LambdaFunctionAssociation{}) != true {
t.Fatalf("Expected Items to be empty, got %v", lfa.Items)
}
}
func TestCloudFrontStructure_expandForwardedValues(t *testing.T) {
data := forwardedValuesConf()
fv := expandForwardedValues(data)

View File

@ -102,6 +102,24 @@ func resourceAwsCloudFrontDistribution() *schema.Resource {
},
},
},
"lambda_function_association": {
Type: schema.TypeSet,
Optional: true,
Set: lambdaFunctionAssociationHash,
MaxItems: 4,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"event_type": {
Type: schema.TypeString,
Required: true,
},
"lambda_arn": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"max_ttl": {
Type: schema.TypeInt,
Required: true,
@ -232,6 +250,24 @@ func resourceAwsCloudFrontDistribution() *schema.Resource {
},
},
},
"lambda_function_association": {
Type: schema.TypeSet,
Optional: true,
Set: lambdaFunctionAssociationHash,
MaxItems: 4,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"event_type": {
Type: schema.TypeString,
Required: true,
},
"lambda_arn": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"max_ttl": {
Type: schema.TypeInt,
Required: true,