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

Add Timestamp Variable #3326

Closed
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
19 changes: 19 additions & 0 deletions config/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ type UserVariable struct {
key string
}

// A Timestamp variable, used to generate a timestamp. Possibly useful for things
// like naming and such.
// "$(timestamp)"
type TimestampVariable struct {
key string
}

func NewInterpolatedVariable(v string) (InterpolatedVariable, error) {
if strings.HasPrefix(v, "count.") {
return NewCountVariable(v)
Expand All @@ -97,6 +104,8 @@ func NewInterpolatedVariable(v string) (InterpolatedVariable, error) {
return NewUserVariable(v)
} else if strings.HasPrefix(v, "module.") {
return NewModuleVariable(v)
} else if strings.HasPrefix(v, "timestamp") {
return NewTimestampVariable(v)
} else {
return NewResourceVariable(v)
}
Expand Down Expand Up @@ -251,6 +260,16 @@ func (v *UserVariable) GoString() string {
return fmt.Sprintf("*%#v", *v)
}

func NewTimestampVariable(key string) (*TimestampVariable, error) {
return &TimestampVariable{
key: key,
}, nil
}

func (v *TimestampVariable) FullKey() string {
return v.key
}

// DetectVariables takes an AST root and returns all the interpolated
// variables that are detected in the AST tree.
func DetectVariables(root ast.Node) ([]InterpolatedVariable, error) {
Expand Down
37 changes: 37 additions & 0 deletions terraform/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ import (
"os"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"

"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/lang/ast"
"github.com/hashicorp/terraform/config/module"
)

var InitTime time.Time

// Initial time in UTC, when package was initialized.
func init() {
InitTime = time.Now().UTC()
}

const (
// VarEnvPrefix is the prefix of variables that are read from
// the environment to set variables here.
Expand Down Expand Up @@ -75,6 +84,8 @@ func (i *Interpolater) Values(
err = i.valueSelfVar(scope, n, v, result)
case *config.UserVariable:
err = i.valueUserVar(scope, n, v, result)
case *config.TimestampVariable:
err = i.valueTimestampVar(scope, n, v, result)
default:
err = fmt.Errorf("%s: unknown variable type: %T", n, rawV)
}
Expand Down Expand Up @@ -198,6 +209,32 @@ func (i *Interpolater) valuePathVar(

}

func (i *Interpolater) valueTimestampVar(
scope *InterpolationScope,
n string,
v *config.TimestampVariable,
result map[string]ast.Variable) error {
if scope.Resource == nil {
return fmt.Errorf("%s: timestamp is only valid within resources", n)
}

if i.Operation == walkValidate {
result[n] = ast.Variable{
Value: config.UnknownVariableValue,
Type: ast.TypeString,
}
return nil
}

value := strconv.FormatInt(InitTime.Unix(), 10)

result[n] = ast.Variable{
Value: value,
Type: ast.TypeString,
}
return nil
}

func (i *Interpolater) valueResourceVar(
scope *InterpolationScope,
n string,
Expand Down