Skip to content

Commit

Permalink
add gzipbase64 interpolation function
Browse files Browse the repository at this point in the history
  • Loading branch information
cemo committed May 23, 2017
1 parent d1308c1 commit 8b1ae07
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
"bytes"
"compress/gzip"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
Expand Down Expand Up @@ -72,6 +74,7 @@ func Funcs() map[string]ast.Function {
"distinct": interpolationFuncDistinct(),
"element": interpolationFuncElement(),
"file": interpolationFuncFile(),
"gzipbase64": interpolationFuncGzipBase64(),
"matchkeys": interpolationFuncMatchKeys(),
"floor": interpolationFuncFloor(),
"format": interpolationFuncFormat(),
Expand Down Expand Up @@ -1322,6 +1325,28 @@ func interpolationFuncBase64Sha512() ast.Function {
}
}

func interpolationFuncGzipBase64() 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 base64.StdEncoding.EncodeToString(b.Bytes()), nil
},
}
}

func interpolationFuncUUID() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{},
Expand Down
12 changes: 12 additions & 0 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2249,6 +2249,18 @@ func TestInterpolateFuncBase64Sha512(t *testing.T) {
})
}

func TestInterpolateFuncGzip(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${gzipbase64("test")}`,
"H4sIAAAAAAAA/ypJLS4BAAAA//8BAAD//wx+f9gEAAAA",

This comment has been minimized.

Copy link
@cemo

cemo May 23, 2017

Author Owner

"H4sIAAAJbogA/ypJLS4BAAAA//8BAAD//wx+f9gEAAAA" is expected at original test.

see https://github.com/hashicorp/terraform/pull/3858/files#diff-81efb6e97968eff0813cc8f2d6655e54R377

false,
},
},
})
}

func TestInterpolateFuncMd5(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
Expand Down

0 comments on commit 8b1ae07

Please sign in to comment.