Skip to content

Commit

Permalink
Add uuid(prefix) interpolation function
Browse files Browse the repository at this point in the history
  • Loading branch information
Garrett Johnson committed Oct 20, 2015
1 parent 59680d4 commit 76bb96d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
24 changes: 24 additions & 0 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/hashicorp/terraform/config/lang/ast"
"github.com/hashicorp/terraform/helper/resource"
"github.com/mitchellh/go-homedir"
)

Expand All @@ -33,6 +34,7 @@ func init() {
"split": interpolationFuncSplit(),
"base64encode": interpolationFuncBase64Encode(),
"base64decode": interpolationFuncBase64Decode(),
"uuid": interpolationFuncUuid(),
}
}

Expand Down Expand Up @@ -442,3 +444,25 @@ func interpolationFuncBase64Decode() ast.Function {
},
}
}

// interpolationFuncUuid implements an uuid v4 generator with an optional
// prefix string. Refer to pkg helper/resource/id for implementation details
// of the uuid generator.
func interpolationFuncUuid() ast.Function {
return ast.Function{
Variadic: true,
VariadicType: ast.TypeString,
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
var prefix string

if len(args) == 1 {
if v, ok := args[0].(string); ok {
prefix = v
}
}

return resource.PrefixedUniqueId(prefix), nil
},
}
}
42 changes: 41 additions & 1 deletion config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"reflect"
"testing"

"strings"
"github.com/hashicorp/terraform/config/lang"
"github.com/hashicorp/terraform/config/lang/ast"
)
Expand Down Expand Up @@ -644,6 +644,46 @@ func TestInterpolateFuncBase64Decode(t *testing.T) {
})
}

func TestInterpolateUuid(t *testing.T) {
cases := []struct{
Input string
Result int
Prefix string
Vars map[string]ast.Variable
}{
{`${uuid()}`, 26, "", map[string]ast.Variable{}},
{`${uuid("")}`, 26, "", map[string]ast.Variable{}},
{`${uuid("lb-")}`, 29, "lb-", map[string]ast.Variable{}},
}

// defaults
for i, tc := range cases {
ast, err := lang.Parse(tc.Input)
if err != nil {
t.Fatalf("Case #%d: input: %#v\nerr: %s", i, tc.Input, err)
}

out, _, err := lang.Eval(ast, langEvalConfig(tc.Vars))
if err != nil {
t.Fatalf("Case #%d: input: %#v\nerr: %s", i, tc.Input, err)
}

t.Logf("%s=%s", tc.Input, out)


if len(out.(string)) != tc.Result {
err = fmt.Errorf("expected input length (%d) to equal (%d)", len(out.(string)), tc.Result)
t.Fatalf("Case #%d: input: %#v\nerr: %s", i, tc.Input, err)
}

if !strings.HasPrefix(out.(string), tc.Prefix) {
err = fmt.Errorf("expected prefix %s", tc.Prefix)
t.Fatalf("Case #%d: input: %#v\nerr: %s", i, tc.Input, err)
}

}
}

type testFunctionConfig struct {
Cases []testFunctionCase
Vars map[string]ast.Variable
Expand Down
5 changes: 5 additions & 0 deletions website/source/docs/configuration/interpolation.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ are documented below.

The supported built-in functions are:

* `uuid(string)` - Returns a uuid string, with an optional prefix. This
uses a RFC 4122 v4 UUID with some basic cosmetic filters
applied (base32, remove padding, downcase) to make visually distinguishing
identifiers easier. This is the format used internally.

* `base64decode(string)` - Given a base64-encoded string, decodes it and
returns the original string.

Expand Down

0 comments on commit 76bb96d

Please sign in to comment.