Add the timestamp interpolation function.

Adds the timestamp interpolation function, tests, and documentation to allow a user to insert an RFC 3339 formatted UTC timestamp.
This commit is contained in:
Jake Champlin 2016-12-01 14:51:01 -05:00
parent ac257b8048
commit 1f33952330
No known key found for this signature in database
GPG Key ID: DC31F41958EF4AC2
3 changed files with 39 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import (
"github.com/hashicorp/hil"
"github.com/hashicorp/hil/ast"
"github.com/mitchellh/go-homedir"
"time"
)
// stringSliceToVariableValue converts a string slice into the value
@ -85,6 +86,7 @@ func Funcs() map[string]ast.Function {
"signum": interpolationFuncSignum(),
"sort": interpolationFuncSort(),
"split": interpolationFuncSplit(),
"timestamp": interpolationFuncTimestamp(),
"title": interpolationFuncTitle(),
"trimspace": interpolationFuncTrimSpace(),
"upper": interpolationFuncUpper(),
@ -1109,6 +1111,17 @@ func interpolationFuncUUID() ast.Function {
}
}
// interpolationFuncTimestamp
func interpolationFuncTimestamp() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
return time.Now().UTC().Format(time.RFC3339), nil
},
}
}
// interpolationFuncTitle implements the "title" function that returns a copy of the
// string in which first characters of all the words are capitalized.
func interpolationFuncTitle() ast.Function {

View File

@ -9,6 +9,7 @@ import (
"github.com/hashicorp/hil"
"github.com/hashicorp/hil/ast"
"time"
)
func TestInterpolateFuncZipMap(t *testing.T) {
@ -1921,6 +1922,27 @@ func TestInterpolateFuncUUID(t *testing.T) {
}
}
func TestInterpolateFuncTimestamp(t *testing.T) {
currentTime := time.Now().UTC()
ast, err := hil.Parse("${timestamp()}")
if err != nil {
t.Fatalf("err: %s", err)
}
result, err := hil.Eval(ast, langEvalConfig(nil))
if err != nil {
t.Fatalf("err: %s", err)
}
resultTime, err := time.Parse(time.RFC3339, result.Value.(string))
if err != nil {
t.Fatalf("Error parsing timestamp: %s", err)
}
if resultTime.Sub(currentTime).Seconds() > 10.0 {
t.Fatalf("Timestamp Diff too large. Expected: %s\nRecieved: %s", currentTime.Format(time.RFC3339), result.Value.(string))
}
}
type testFunctionConfig struct {
Cases []testFunctionCase
Vars map[string]ast.Variable

View File

@ -267,6 +267,10 @@ The supported built-in functions are:
`a_resource_param = ["${split(",", var.CSV_STRING)}"]`.
Example: `split(",", module.amod.server_ids)`
* `timestamp()` - Returns a UTC timestamp string in RFC 3339 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.
* `title(string)` - Returns a copy of the string with the first characters of all the words capitalized.
* `trimspace(string)` - Returns a copy of the string with all leading and trailing white spaces removed.