Skip to content

Commit

Permalink
Add Terraform 1.3 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Feb 16, 2023
1 parent 57f2df3 commit 8361ecd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
59 changes: 59 additions & 0 deletions internal/funcs/1.3/functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package funcs

import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/schema"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"

funcs_v0_15 "github.com/hashicorp/terraform-schema/internal/funcs/0.15"
)

func Functions(v *version.Version) map[string]schema.FunctionSignature {
f := funcs_v0_15.Functions(v)

f["endswith"] = schema.FunctionSignature{
Params: []function.Parameter{
{
Name: "str",
Type: cty.String,
},
{
Name: "suffix",
Type: cty.String,
},
},
ReturnType: cty.Bool,
Description: "`endswith` takes two values: a string to check and a suffix string. The function returns true if the first string ends with that exact suffix.",
}
f["startswith"] = schema.FunctionSignature{
Params: []function.Parameter{
{
Name: "str",
Type: cty.String,
},
{
Name: "prefix",
Type: cty.String,
},
},
ReturnType: cty.Bool,
Description: "`startswith` takes two values: a string to check and a prefix string. The function returns true if the string begins with that exact prefix.",
}
f["timecmp"] = schema.FunctionSignature{
Params: []function.Parameter{
{
Name: "timestamp_a",
Type: cty.String,
},
{
Name: "timestamp_b",
Type: cty.String,
},
},
ReturnType: cty.Number,
Description: "`timecmp` compares two timestamps and returns a number that represents the ordering of the instants those timestamps represent.",
}

return f
}
4 changes: 4 additions & 0 deletions schema/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
funcs_v0_13 "github.com/hashicorp/terraform-schema/internal/funcs/0.13"
funcs_v0_14 "github.com/hashicorp/terraform-schema/internal/funcs/0.14"
funcs_v0_15 "github.com/hashicorp/terraform-schema/internal/funcs/0.15"
funcs_v1_3 "github.com/hashicorp/terraform-schema/internal/funcs/1.3"
)

func FunctionsForVersion(v *version.Version) (map[string]schema.FunctionSignature, error) {
Expand All @@ -18,6 +19,9 @@ func FunctionsForVersion(v *version.Version) (map[string]schema.FunctionSignatur
return nil, fmt.Errorf("invalid version: %w", err)
}

if ver.GreaterThanOrEqual(v1_3) {
return funcs_v1_3.Functions(ver), nil
}
if ver.GreaterThanOrEqual(v0_15) {
return funcs_v0_15.Functions(ver), nil
}
Expand Down

0 comments on commit 8361ecd

Please sign in to comment.