diff --git a/packer/config_template.go b/packer/config_template.go index 5a8e48d1e..1f46cf92f 100644 --- a/packer/config_template.go +++ b/packer/config_template.go @@ -28,6 +28,7 @@ func NewConfigTemplate() (*ConfigTemplate, error) { result.root = template.New("configTemplateRoot") result.root.Funcs(template.FuncMap{ + "isotime": templateISOTime, "timestamp": templateTimestamp, "user": result.templateUser, "uuid": templateUuid, @@ -79,6 +80,10 @@ func (t *ConfigTemplate) templateUser(n string) (string, error) { return result, nil } +func templateISOTime() string { + return time.Now().UTC().Format(time.RFC3339) +} + func templateTimestamp() string { return strconv.FormatInt(time.Now().UTC().Unix(), 10) } diff --git a/packer/config_template_test.go b/packer/config_template_test.go index fed328922..317732f0f 100644 --- a/packer/config_template_test.go +++ b/packer/config_template_test.go @@ -7,6 +7,28 @@ import ( "time" ) +func TestConfigTemplateProcess_isotime(t *testing.T) { + tpl, err := NewConfigTemplate() + if err != nil { + t.Fatalf("err: %s", err) + } + + result, err := tpl.Process(`{{isotime}}`, nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + val, err := time.Parse(time.RFC3339, result) + if err != nil { + t.Fatalf("err: %s", err) + } + + currentTime := time.Now().UTC() + if currentTime.Sub(val) > 2*time.Second { + t.Fatalf("val: %d (current: %d)", val, currentTime) + } +} + func TestConfigTemplateProcess_timestamp(t *testing.T) { tpl, err := NewConfigTemplate() if err != nil {