Skip to content

Commit

Permalink
Merge pull request #5575 from hashicorp/phinze/uuid
Browse files Browse the repository at this point in the history
core: Add uuid() interpolate function.
  • Loading branch information
phinze committed Mar 11, 2016
2 parents 65024db + 13fc7b8 commit ece259f
Show file tree
Hide file tree
Showing 8 changed files with 470 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Godeps/Godeps.json

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

12 changes: 12 additions & 0 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strings"

"github.com/apparentlymart/go-cidr/cidr"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/hil/ast"
"github.com/mitchellh/go-homedir"
)
Expand All @@ -42,6 +43,7 @@ func Funcs() map[string]ast.Function {
"length": interpolationFuncLength(),
"lower": interpolationFuncLower(),
"md5": interpolationFuncMd5(),
"uuid": interpolationFuncUUID(),
"replace": interpolationFuncReplace(),
"sha1": interpolationFuncSha1(),
"sha256": interpolationFuncSha256(),
Expand Down Expand Up @@ -682,3 +684,13 @@ func interpolationFuncBase64Sha256() ast.Function {
},
}
}

func interpolationFuncUUID() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
return uuid.GenerateUUID()
},
}
}
22 changes: 22 additions & 0 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,28 @@ func TestInterpolateFuncMd5(t *testing.T) {
})
}

func TestInterpolateFuncUUID(t *testing.T) {
results := make(map[string]bool)

for i := 0; i < 100; i++ {
ast, err := hil.Parse("${uuid()}")
if err != nil {
t.Fatalf("err: %s", err)
}

out, _, err := hil.Eval(ast, langEvalConfig(nil))
if err != nil {
t.Fatalf("err: %s", err)
}

if results[out.(string)] {
t.Fatalf("Got unexpected duplicate uuid: %s", out)
}

results[out.(string)] = true
}
}

type testFunctionConfig struct {
Cases []testFunctionCase
Vars map[string]ast.Variable
Expand Down
Loading

0 comments on commit ece259f

Please sign in to comment.