Skip to content

Commit

Permalink
terraform: Add support for Terraform v1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Sep 21, 2022
1 parent a09464b commit fdf36f4
Show file tree
Hide file tree
Showing 13 changed files with 401 additions and 1,350 deletions.
62 changes: 59 additions & 3 deletions terraform/addrs/instance_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package addrs

import (
"fmt"
"strings"
"unicode"

"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
Expand Down Expand Up @@ -72,9 +74,10 @@ func (k StringKey) instanceKeySigil() {
}

func (k StringKey) String() string {
// FIXME: This isn't _quite_ right because Go's quoted string syntax is
// slightly different than HCL's, but we'll accept it for now.
return fmt.Sprintf("[%q]", string(k))
// We use HCL's quoting syntax here so that we can in principle parse
// an address constructed by this package as if it were an HCL
// traversal, even if the string contains HCL's own metacharacters.
return fmt.Sprintf("[%s]", toHCLQuotedString(string(k)))
}

func (k StringKey) Value() cty.Value {
Expand All @@ -93,3 +96,56 @@ const (
IntKeyType InstanceKeyType = 'I'
StringKeyType InstanceKeyType = 'S'
)

// toHCLQuotedString is a helper which formats the given string in a way that
// HCL's expression parser would treat as a quoted string template.
//
// This includes:
// - Adding quote marks at the start and the end.
// - Using backslash escapes as needed for characters that cannot be represented directly.
// - Escaping anything that would be treated as a template interpolation or control sequence.
func toHCLQuotedString(s string) string {
// This is an adaptation of a similar function inside the hclwrite package,
// inlined here because hclwrite's version generates HCL tokens but we
// only need normal strings.
if len(s) == 0 {
return `""`
}
var buf strings.Builder
buf.WriteByte('"')
for i, r := range s {
switch r {
case '\n':
buf.WriteString(`\n`)
case '\r':
buf.WriteString(`\r`)
case '\t':
buf.WriteString(`\t`)
case '"':
buf.WriteString(`\"`)
case '\\':
buf.WriteString(`\\`)
case '$', '%':
buf.WriteRune(r)
remain := s[i+1:]
if len(remain) > 0 && remain[0] == '{' {
// Double up our template introducer symbol to escape it.
buf.WriteRune(r)
}
default:
if !unicode.IsPrint(r) {
var fmted string
if r < 65536 {
fmted = fmt.Sprintf("\\u%04x", r)
} else {
fmted = fmt.Sprintf("\\U%08x", r)
}
buf.WriteString(fmted)
} else {
buf.WriteRune(r)
}
}
}
buf.WriteByte('"')
return buf.String()
}
19 changes: 15 additions & 4 deletions terraform/addrs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,20 @@ func (m Module) String() string {
if len(m) == 0 {
return ""
}
var steps []string
for _, s := range m {
steps = append(steps, "module", s)
// Calculate necessary space.
l := 0
for _, step := range m {
l += len(step)
}
return strings.Join(steps, ".")
buf := strings.Builder{}
// 8 is len(".module.") which separates entries.
buf.Grow(l + len(m)*8)
sep := ""
for _, step := range m {
buf.WriteString(sep)
buf.WriteString("module.")
buf.WriteString(step)
sep = "."
}
return buf.String()
}
8 changes: 8 additions & 0 deletions terraform/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ func (d *evaluationData) GetInputVariable(addr addrs.InputVariable, rng hcl.Rang
val = config.Default
}

// Apply defaults from the variable's type constraint to the value,
// unless the value is null. We do not apply defaults to top-level
// null values, as doing so could prevent assigning null to a nullable
// variable.
if config.TypeDefaults != nil && !val.IsNull() {
val = config.TypeDefaults.Apply(val)
}

var err error
val, err = convert.Convert(val, config.ConstraintType)
if err != nil {
Expand Down
116 changes: 115 additions & 1 deletion terraform/lang/funcs/datetime.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package funcs

import (
"fmt"
"time"

"github.com/zclconf/go-cty/cty"
Expand Down Expand Up @@ -30,7 +31,7 @@ var TimeAddFunc = function.New(&function.Spec{
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
ts, err := time.Parse(time.RFC3339, args[0].AsString())
ts, err := parseTimestamp(args[0].AsString())
if err != nil {
return cty.UnknownVal(cty.String), err
}
Expand All @@ -43,6 +44,41 @@ var TimeAddFunc = function.New(&function.Spec{
},
})

// TimeCmpFunc is a function that compares two timestamps.
var TimeCmpFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "timestamp_a",
Type: cty.String,
},
{
Name: "timestamp_b",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.Number),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
tsA, err := parseTimestamp(args[0].AsString())
if err != nil {
return cty.UnknownVal(cty.String), function.NewArgError(0, err)
}
tsB, err := parseTimestamp(args[1].AsString())
if err != nil {
return cty.UnknownVal(cty.String), function.NewArgError(1, err)
}

switch {
case tsA.Equal(tsB):
return cty.NumberIntVal(0), nil
case tsA.Before(tsB):
return cty.NumberIntVal(-1), nil
default:
// By elimintation, tsA must be after tsB.
return cty.NumberIntVal(1), nil
}
},
})

// Timestamp returns a string representation of the current date and time.
//
// In the Terraform language, timestamps are conventionally represented as
Expand All @@ -68,3 +104,81 @@ func Timestamp() (cty.Value, error) {
func TimeAdd(timestamp cty.Value, duration cty.Value) (cty.Value, error) {
return TimeAddFunc.Call([]cty.Value{timestamp, duration})
}

// TimeCmp compares two timestamps, indicating whether they are equal or
// if one is before the other.
//
// TimeCmp considers the UTC offset of each given timestamp when making its
// decision, so for example 6:00 +0200 and 4:00 UTC are equal.
//
// In the Terraform language, timestamps are conventionally represented as
// strings using RFC 3339 "Date and Time format" syntax. TimeCmp requires
// the timestamp argument to be a string conforming to this syntax.
//
// The result is always a number between -1 and 1. -1 indicates that
// timestampA is earlier than timestampB. 1 indicates that timestampA is
// later. 0 indicates that the two timestamps represent the same instant.
func TimeCmp(timestampA, timestampB cty.Value) (cty.Value, error) {
return TimeCmpFunc.Call([]cty.Value{timestampA, timestampB})
}

func parseTimestamp(ts string) (time.Time, error) {
t, err := time.Parse(time.RFC3339, ts)
if err != nil {
switch err := err.(type) {
case *time.ParseError:
// If err is a time.ParseError then its string representation is not
// appropriate since it relies on details of Go's strange date format
// representation, which a caller of our functions is not expected
// to be familiar with.
//
// Therefore we do some light transformation to get a more suitable
// error that should make more sense to our callers. These are
// still not awesome error messages, but at least they refer to
// the timestamp portions by name rather than by Go's example
// values.
if err.LayoutElem == "" && err.ValueElem == "" && err.Message != "" {
// For some reason err.Message is populated with a ": " prefix
// by the time package.
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp%s", err.Message)
}
var what string
switch err.LayoutElem {
case "2006":
what = "year"
case "01":
what = "month"
case "02":
what = "day of month"
case "15":
what = "hour"
case "04":
what = "minute"
case "05":
what = "second"
case "Z07:00":
what = "UTC offset"
case "T":
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: missing required time introducer 'T'")
case ":", "-":
if err.ValueElem == "" {
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: end of string where %q is expected", err.LayoutElem)
} else {
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: found %q where %q is expected", err.ValueElem, err.LayoutElem)
}
default:
// Should never get here, because time.RFC3339 includes only the
// above portions, but since that might change in future we'll
// be robust here.
what = "timestamp segment"
}
if err.ValueElem == "" {
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: end of string before %s", what)
} else {
return time.Time{}, fmt.Errorf("not a valid RFC3339 timestamp: cannot use %q as %s", err.ValueElem, what)
}
}
return time.Time{}, err
}
return t, nil
}
97 changes: 97 additions & 0 deletions terraform/lang/funcs/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,100 @@ func TestTimeadd(t *testing.T) {
})
}
}

func TestTimeCmp(t *testing.T) {
tests := []struct {
TimeA, TimeB cty.Value
Want cty.Value
Err string
}{
{
cty.StringVal("2017-11-22T00:00:00Z"),
cty.StringVal("2017-11-22T00:00:00Z"),
cty.Zero,
``,
},
{
cty.StringVal("2017-11-22T00:00:00Z"),
cty.StringVal("2017-11-22T01:00:00+01:00"),
cty.Zero,
``,
},
{
cty.StringVal("2017-11-22T00:00:01Z"),
cty.StringVal("2017-11-22T01:00:00+01:00"),
cty.NumberIntVal(1),
``,
},
{
cty.StringVal("2017-11-22T01:00:00Z"),
cty.StringVal("2017-11-22T00:59:00-01:00"),
cty.NumberIntVal(-1),
``,
},
{
cty.StringVal("2017-11-22T01:00:00+01:00"),
cty.StringVal("2017-11-22T01:00:00-01:00"),
cty.NumberIntVal(-1),
``,
},
{
cty.StringVal("2017-11-22T01:00:00-01:00"),
cty.StringVal("2017-11-22T01:00:00+01:00"),
cty.NumberIntVal(1),
``,
},
{
cty.StringVal("2017-11-22T00:00:00Z"),
cty.StringVal("bloop"),
cty.UnknownVal(cty.String),
`not a valid RFC3339 timestamp: cannot use "bloop" as year`,
},
{
cty.StringVal("2017-11-22 00:00:00Z"),
cty.StringVal("2017-11-22T00:00:00Z"),
cty.UnknownVal(cty.String),
`not a valid RFC3339 timestamp: missing required time introducer 'T'`,
},
{
cty.StringVal("2017-11-22T00:00:00Z"),
cty.UnknownVal(cty.String),
cty.UnknownVal(cty.Number),
``,
},
{
cty.UnknownVal(cty.String),
cty.StringVal("2017-11-22T00:00:00Z"),
cty.UnknownVal(cty.Number),
``,
},
{
cty.UnknownVal(cty.String),
cty.UnknownVal(cty.String),
cty.UnknownVal(cty.Number),
``,
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("TimeCmp(%#v, %#v)", test.TimeA, test.TimeB), func(t *testing.T) {
got, err := TimeCmp(test.TimeA, test.TimeB)

if test.Err != "" {
if err == nil {
t.Fatal("succeeded; want error")
}
if got := err.Error(); got != test.Err {
t.Errorf("wrong error message\ngot: %s\nwant: %s", got, test.Err)
}
return
} else if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}
Loading

0 comments on commit fdf36f4

Please sign in to comment.