Skip to content

Commit

Permalink
core: Add uuid() interpolate function.
Browse files Browse the repository at this point in the history
Utilizes hashicorp's go-uuid library for proper random seeding setup.
  • Loading branch information
phinze committed Mar 11, 2016
1 parent ededbb5 commit 1e0b8ea
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
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
2 changes: 2 additions & 0 deletions website/source/docs/configuration/interpolation.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ The supported built-in functions are:

* `upper(string)` - Returns a copy of the string with all Unicode letters mapped to their upper case.

* `uuid()` - Returns a UUID string in RFC 4122 v4 format. This string will change with every invocation of the function, so in order to prevent diffs on every plan & apply, it must be used with the [`ignore_changes`](/docs/configuration/resources.html#ignore-changes) lifecycle attribute.

## Templates

Long strings can be managed using templates. [Templates](/docs/providers/template/index.html) are [resources](/docs/configuration/resources.html) defined by a filename and some variables to use during interpolation. They have a computed `rendered` attribute containing the result.
Expand Down
2 changes: 2 additions & 0 deletions website/source/docs/configuration/resources.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ The `lifecycle` block allows the following keys to be set:
destruction of a given resource. When this is set to `true`, any plan
that includes a destroy of this resource will return an error message.

<a id="ignore-changes"></a>

* `ignore_changes` (list of strings) - Customizes how diffs are evaluated for
resources, allowing individual attributes to be ignored through changes.
As an example, this can be used to ignore dynamic changes to the
Expand Down

0 comments on commit 1e0b8ea

Please sign in to comment.