Skip to content

Commit

Permalink
implements gzip interpolation func
Browse files Browse the repository at this point in the history
  • Loading branch information
glerchundi committed May 23, 2017
1 parent 0ffcadd commit f273344
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
29 changes: 29 additions & 0 deletions config/interpolate_funcs.go
@@ -1,6 +1,8 @@
package config

import (
"bytes"
"compress/gzip"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
Expand Down Expand Up @@ -57,6 +59,7 @@ func Funcs() map[string]ast.Function {
"basename": interpolationFuncBasename(),
"base64decode": interpolationFuncBase64Decode(),
"base64encode": interpolationFuncBase64Encode(),
"base64gzip": interpolationFuncBase64Gzip(),
"base64sha256": interpolationFuncBase64Sha256(),
"base64sha512": interpolationFuncBase64Sha512(),
"ceil": interpolationFuncCeil(),
Expand Down Expand Up @@ -1178,6 +1181,32 @@ func interpolationFuncBase64Decode() ast.Function {
}
}

// interpolationFuncBase64Gzip implements the "gzip" function that allows gzip
// compression encoding the result using base64
func interpolationFuncBase64Gzip() 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 write gzip raw data: '%s'", s)
}
if err := gz.Flush(); err != nil {
return "", fmt.Errorf("failed to flush gzip writer: '%s'", s)
}
if err := gz.Close(); err != nil {
return "", fmt.Errorf("failed to close gzip writer: '%s'", s)
}

return base64.StdEncoding.EncodeToString(b.Bytes()), nil
},
}
}

// interpolationFuncLower implements the "lower" function that does
// string lower casing.
func interpolationFuncLower() ast.Function {
Expand Down
12 changes: 12 additions & 0 deletions config/interpolate_funcs_test.go
Expand Up @@ -2215,6 +2215,18 @@ func TestInterpolateFuncTrimSpace(t *testing.T) {
})
}

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

func TestInterpolateFuncBase64Sha256(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
Expand Down
2 changes: 1 addition & 1 deletion terraform/instancetype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion terraform/walkoperation_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f273344

Please sign in to comment.