From 29822d1bc6c7435a5ab703c251e480dbfcf24f1d Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Thu, 10 Nov 2016 09:19:29 +0000 Subject: [PATCH] Rebase gzip patch on top of master https://github.com/hashicorp/terraform/pull/3858 --- config/interpolate_funcs.go | 28 ++++++++++++++++++++++++++++ config/interpolate_funcs_test.go | 12 ++++++++++++ 2 files changed, 40 insertions(+) diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index 19595e735e7d..bab5716979af 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -1,6 +1,7 @@ package config import ( + "compress/gzip" "crypto/md5" "crypto/sha1" "crypto/sha256" @@ -67,6 +68,7 @@ func Funcs() map[string]ast.Function { "floor": interpolationFuncFloor(), "format": interpolationFuncFormat(), "formatlist": interpolationFuncFormatList(), + "gzip": interpolationFuncGzip(), "index": interpolationFuncIndex(), "join": interpolationFuncJoin(), "jsonencode": interpolationFuncJSONEncode(), @@ -565,6 +567,32 @@ func interpolationFuncFormatList() ast.Function { } } +// interpolationFuncGzip implements the "gzip" function that allows gzip compression. +func interpolationFuncGzip() ast.Function { + return ast.Function{ + ArgTypes: []ast.Type{ast.TypeString}, + ReturnType: ast.TypeString, + Callback: func(args []interface{}) (interface{}, error) { + s := args[0].(string) + + var b bytes.Buffer + gz := gzip.NewWriter(&b) + if _, err := gz.Write([]byte(s)); err != nil { + return "", fmt.Errorf("failed to gzip raw data: '%s'", s) + } + if err := gz.Flush(); err != nil { + return "", fmt.Errorf("failed to gzip raw data: '%s'", s) + } + if err := gz.Close(); err != nil { + return "", fmt.Errorf("failed to gzip raw data: '%s'", s) + } + + return b.String(), nil + }, + } +} + + // interpolationFuncIndex implements the "index" function that allows one to // find the index of a specific element in a list func interpolationFuncIndex() ast.Function { diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index ccdb22363388..57dab0dcb706 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -1010,6 +1010,18 @@ func TestInterpolateFuncFormatList(t *testing.T) { }) } +func TestInterpolateFuncGzip(t *testing.T) { + testFunction(t, testFunctionConfig{ + Cases: []testFunctionCase{ + { + `${base64encode(gzip("test"))}`, + "H4sIAAAJbogA/ypJLS4BAAAA//8BAAD//wx+f9gEAAAA", + false, + }, + }, + }) +} + func TestInterpolateFuncIndex(t *testing.T) { testFunction(t, testFunctionConfig{ Vars: map[string]ast.Variable{