Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add interpolate uuid function #3563

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you run gofmt on this? That will line up these declarations.

}
}

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should document the prefix argument here.


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

Expand Down