From 4642a6aee124fda4efe6134bdd113831a9e07067 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 5 Sep 2013 12:19:56 -0700 Subject: [PATCH] packer: uuid function --- packer/config_template.go | 7 +++++++ packer/config_template_test.go | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/packer/config_template.go b/packer/config_template.go index 354208cbc..5a8e48d1e 100644 --- a/packer/config_template.go +++ b/packer/config_template.go @@ -2,6 +2,8 @@ package packer import ( "bytes" + "cgl.tideland.biz/identifier" + "encoding/hex" "fmt" "strconv" "text/template" @@ -28,6 +30,7 @@ func NewConfigTemplate() (*ConfigTemplate, error) { result.root.Funcs(template.FuncMap{ "timestamp": templateTimestamp, "user": result.templateUser, + "uuid": templateUuid, }) return result, nil @@ -79,3 +82,7 @@ func (t *ConfigTemplate) templateUser(n string) (string, error) { func templateTimestamp() string { return strconv.FormatInt(time.Now().UTC().Unix(), 10) } + +func templateUuid() string { + return hex.EncodeToString(identifier.NewUUID().Raw()) +} diff --git a/packer/config_template_test.go b/packer/config_template_test.go index 4c4864cda..fed328922 100644 --- a/packer/config_template_test.go +++ b/packer/config_template_test.go @@ -47,6 +47,22 @@ func TestConfigTemplateProcess_user(t *testing.T) { } } +func TestConfigTemplateProcess_uuid(t *testing.T) { + tpl, err := NewConfigTemplate() + if err != nil { + t.Fatalf("err: %s", err) + } + + result, err := tpl.Process(`{{uuid}}`, nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + if len(result) != 32 { + t.Fatalf("err: %s", result) + } +} + func TestConfigTemplateValidate(t *testing.T) { tpl, err := NewConfigTemplate() if err != nil {