2018-04-04 03:11:28 +02:00
|
|
|
package lang
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-01-03 23:01:14 +01:00
|
|
|
"github.com/hashicorp/hcl/v2/ext/tryfunc"
|
2019-05-26 19:25:18 +02:00
|
|
|
ctyyaml "github.com/zclconf/go-cty-yaml"
|
2018-04-04 03:11:28 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/zclconf/go-cty/cty/function"
|
|
|
|
"github.com/zclconf/go-cty/cty/function/stdlib"
|
2018-05-22 02:39:26 +02:00
|
|
|
|
2021-05-17 20:38:39 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/experiments"
|
2021-05-17 21:23:42 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/lang/funcs"
|
2018-04-04 03:11:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var impureFunctions = []string{
|
2018-05-23 18:38:12 +02:00
|
|
|
"bcrypt",
|
2018-04-04 03:11:28 +02:00
|
|
|
"timestamp",
|
|
|
|
"uuid",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Functions returns the set of functions that should be used to when evaluating
|
|
|
|
// expressions in the receiving scope.
|
|
|
|
func (s *Scope) Functions() map[string]function.Function {
|
|
|
|
s.funcsLock.Lock()
|
|
|
|
if s.funcs == nil {
|
2018-05-22 02:39:26 +02:00
|
|
|
// Some of our functions are just directly the cty stdlib functions.
|
|
|
|
// Others are implemented in the subdirectory "funcs" here in this
|
|
|
|
// repository. New functions should generally start out their lives
|
|
|
|
// in the "funcs" directory and potentially graduate to cty stdlib
|
|
|
|
// later if the functionality seems to be something domain-agnostic
|
|
|
|
// that would be useful to all applications using cty functions.
|
|
|
|
|
2018-04-04 03:11:28 +02:00
|
|
|
s.funcs = map[string]function.Function{
|
2019-01-25 18:16:43 +01:00
|
|
|
"abs": stdlib.AbsoluteFunc,
|
2019-07-02 14:30:30 +02:00
|
|
|
"abspath": funcs.AbsPathFunc,
|
2020-09-22 15:06:42 +02:00
|
|
|
"alltrue": funcs.AllTrueFunc,
|
2020-10-23 22:52:48 +02:00
|
|
|
"anytrue": funcs.AnyTrueFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"basename": funcs.BasenameFunc,
|
|
|
|
"base64decode": funcs.Base64DecodeFunc,
|
|
|
|
"base64encode": funcs.Base64EncodeFunc,
|
|
|
|
"base64gzip": funcs.Base64GzipFunc,
|
|
|
|
"base64sha256": funcs.Base64Sha256Func,
|
|
|
|
"base64sha512": funcs.Base64Sha512Func,
|
|
|
|
"bcrypt": funcs.BcryptFunc,
|
2020-01-03 23:01:14 +01:00
|
|
|
"can": tryfunc.CanFunc,
|
2020-02-24 15:29:04 +01:00
|
|
|
"ceil": stdlib.CeilFunc,
|
|
|
|
"chomp": stdlib.ChompFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"cidrhost": funcs.CidrHostFunc,
|
|
|
|
"cidrnetmask": funcs.CidrNetmaskFunc,
|
|
|
|
"cidrsubnet": funcs.CidrSubnetFunc,
|
2019-09-20 04:06:15 +02:00
|
|
|
"cidrsubnets": funcs.CidrSubnetsFunc,
|
2019-04-12 19:57:52 +02:00
|
|
|
"coalesce": funcs.CoalesceFunc,
|
2020-02-24 15:29:04 +01:00
|
|
|
"coalescelist": stdlib.CoalesceListFunc,
|
|
|
|
"compact": stdlib.CompactFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"concat": stdlib.ConcatFunc,
|
2020-02-24 15:29:04 +01:00
|
|
|
"contains": stdlib.ContainsFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"csvdecode": stdlib.CSVDecodeFunc,
|
2020-10-31 01:19:32 +01:00
|
|
|
"defaults": s.experimentalFunction(experiments.ModuleVariableOptionalAttrs, funcs.DefaultsFunc),
|
2019-01-25 18:16:43 +01:00
|
|
|
"dirname": funcs.DirnameFunc,
|
2020-02-24 15:29:04 +01:00
|
|
|
"distinct": stdlib.DistinctFunc,
|
|
|
|
"element": stdlib.ElementFunc,
|
|
|
|
"chunklist": stdlib.ChunklistFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"file": funcs.MakeFileFunc(s.BaseDir, false),
|
|
|
|
"fileexists": funcs.MakeFileExistsFunc(s.BaseDir),
|
2019-08-20 10:50:01 +02:00
|
|
|
"fileset": funcs.MakeFileSetFunc(s.BaseDir),
|
2019-01-25 18:16:43 +01:00
|
|
|
"filebase64": funcs.MakeFileFunc(s.BaseDir, true),
|
|
|
|
"filebase64sha256": funcs.MakeFileBase64Sha256Func(s.BaseDir),
|
|
|
|
"filebase64sha512": funcs.MakeFileBase64Sha512Func(s.BaseDir),
|
|
|
|
"filemd5": funcs.MakeFileMd5Func(s.BaseDir),
|
|
|
|
"filesha1": funcs.MakeFileSha1Func(s.BaseDir),
|
|
|
|
"filesha256": funcs.MakeFileSha256Func(s.BaseDir),
|
|
|
|
"filesha512": funcs.MakeFileSha512Func(s.BaseDir),
|
2020-02-24 15:29:04 +01:00
|
|
|
"flatten": stdlib.FlattenFunc,
|
|
|
|
"floor": stdlib.FloorFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"format": stdlib.FormatFunc,
|
|
|
|
"formatdate": stdlib.FormatDateFunc,
|
|
|
|
"formatlist": stdlib.FormatListFunc,
|
2020-02-24 15:29:04 +01:00
|
|
|
"indent": stdlib.IndentFunc,
|
|
|
|
"index": funcs.IndexFunc, // stdlib.IndexFunc is not compatible
|
|
|
|
"join": stdlib.JoinFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"jsondecode": stdlib.JSONDecodeFunc,
|
|
|
|
"jsonencode": stdlib.JSONEncodeFunc,
|
2020-02-24 15:29:04 +01:00
|
|
|
"keys": stdlib.KeysFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"length": funcs.LengthFunc,
|
|
|
|
"list": funcs.ListFunc,
|
2020-02-24 15:29:04 +01:00
|
|
|
"log": stdlib.LogFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"lookup": funcs.LookupFunc,
|
|
|
|
"lower": stdlib.LowerFunc,
|
|
|
|
"map": funcs.MapFunc,
|
|
|
|
"matchkeys": funcs.MatchkeysFunc,
|
|
|
|
"max": stdlib.MaxFunc,
|
|
|
|
"md5": funcs.Md5Func,
|
2020-02-24 15:29:04 +01:00
|
|
|
"merge": stdlib.MergeFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"min": stdlib.MinFunc,
|
lang/funcs: "one" function
In the Terraform language we typically use lists of zero or one values in
some sense interchangably with single values that might be null, because
various Terraform language constructs are designed to work with
collections rather than with nullable values.
In Terraform v0.12 we made the splat operator [*] have a "special power"
of concisely converting from a possibly-null single value into a
zero-or-one list as a way to make that common operation more concise.
In a sense this "one" function is the opposite operation to that special
power: it goes from a zero-or-one collection (list, set, or tuple) to a
possibly-null single value.
This is a concise alternative to the following clunky conditional
expression, with the additional benefit that the following expression is
also not viable for set values, and it also properly handles the case
where there's unexpectedly more than one value:
length(var.foo) != 0 ? var.foo[0] : null
Instead, we can write:
one(var.foo)
As with the splat operator, this is a tricky tradeoff because it could be
argued that it's not something that'd be immediately intuitive to someone
unfamiliar with Terraform. However, I think that's justified given how
often zero-or-one collections arise in typical Terraform configurations.
Unlike the splat operator, it should at least be easier to search for its
name and find its documentation the first time you see it in a
configuration.
My expectation that this will become a common pattern is also my
justification for giving it a short, concise name. Arguably it could be
better named something like "oneornull", but that's a pretty clunky name
and I'm not convinced it really adds any clarity for someone who isn't
already familiar with it.
2021-01-09 01:02:56 +01:00
|
|
|
"one": funcs.OneFunc,
|
2020-02-24 15:29:04 +01:00
|
|
|
"parseint": stdlib.ParseIntFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"pathexpand": funcs.PathExpandFunc,
|
2020-02-24 15:29:04 +01:00
|
|
|
"pow": stdlib.PowFunc,
|
2019-05-26 22:09:44 +02:00
|
|
|
"range": stdlib.RangeFunc,
|
2019-08-06 17:21:22 +02:00
|
|
|
"regex": stdlib.RegexFunc,
|
|
|
|
"regexall": stdlib.RegexAllFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"replace": funcs.ReplaceFunc,
|
2020-02-24 15:29:04 +01:00
|
|
|
"reverse": stdlib.ReverseListFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"rsadecrypt": funcs.RsaDecryptFunc,
|
2020-12-19 00:46:30 +01:00
|
|
|
"sensitive": funcs.SensitiveFunc,
|
|
|
|
"nonsensitive": funcs.NonsensitiveFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"setintersection": stdlib.SetIntersectionFunc,
|
2020-02-24 15:29:04 +01:00
|
|
|
"setproduct": stdlib.SetProductFunc,
|
2020-02-06 18:49:11 +01:00
|
|
|
"setsubtract": stdlib.SetSubtractFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"setunion": stdlib.SetUnionFunc,
|
|
|
|
"sha1": funcs.Sha1Func,
|
|
|
|
"sha256": funcs.Sha256Func,
|
|
|
|
"sha512": funcs.Sha512Func,
|
2020-02-24 15:29:04 +01:00
|
|
|
"signum": stdlib.SignumFunc,
|
|
|
|
"slice": stdlib.SliceFunc,
|
|
|
|
"sort": stdlib.SortFunc,
|
|
|
|
"split": stdlib.SplitFunc,
|
2019-04-24 20:52:39 +02:00
|
|
|
"strrev": stdlib.ReverseFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"substr": stdlib.SubstrFunc,
|
2020-04-15 20:27:06 +02:00
|
|
|
"sum": funcs.SumFunc,
|
2020-10-21 19:56:56 +02:00
|
|
|
"textdecodebase64": funcs.TextDecodeBase64Func,
|
|
|
|
"textencodebase64": funcs.TextEncodeBase64Func,
|
2019-01-25 18:16:43 +01:00
|
|
|
"timestamp": funcs.TimestampFunc,
|
2020-02-24 15:29:04 +01:00
|
|
|
"timeadd": stdlib.TimeAddFunc,
|
|
|
|
"title": stdlib.TitleFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"tostring": funcs.MakeToFunc(cty.String),
|
|
|
|
"tonumber": funcs.MakeToFunc(cty.Number),
|
|
|
|
"tobool": funcs.MakeToFunc(cty.Bool),
|
|
|
|
"toset": funcs.MakeToFunc(cty.Set(cty.DynamicPseudoType)),
|
|
|
|
"tolist": funcs.MakeToFunc(cty.List(cty.DynamicPseudoType)),
|
|
|
|
"tomap": funcs.MakeToFunc(cty.Map(cty.DynamicPseudoType)),
|
|
|
|
"transpose": funcs.TransposeFunc,
|
2020-02-24 15:29:04 +01:00
|
|
|
"trim": stdlib.TrimFunc,
|
|
|
|
"trimprefix": stdlib.TrimPrefixFunc,
|
|
|
|
"trimspace": stdlib.TrimSpaceFunc,
|
|
|
|
"trimsuffix": stdlib.TrimSuffixFunc,
|
2020-01-03 23:01:14 +01:00
|
|
|
"try": tryfunc.TryFunc,
|
2019-01-25 18:16:43 +01:00
|
|
|
"upper": stdlib.UpperFunc,
|
|
|
|
"urlencode": funcs.URLEncodeFunc,
|
|
|
|
"uuid": funcs.UUIDFunc,
|
2019-05-08 11:13:30 +02:00
|
|
|
"uuidv5": funcs.UUIDV5Func,
|
2020-02-24 15:29:04 +01:00
|
|
|
"values": stdlib.ValuesFunc,
|
2019-05-26 19:25:18 +02:00
|
|
|
"yamldecode": ctyyaml.YAMLDecodeFunc,
|
|
|
|
"yamlencode": ctyyaml.YAMLEncodeFunc,
|
2020-02-24 15:29:04 +01:00
|
|
|
"zipmap": stdlib.ZipmapFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 02:42:42 +01:00
|
|
|
s.funcs["templatefile"] = funcs.MakeTemplateFileFunc(s.BaseDir, func() map[string]function.Function {
|
|
|
|
// The templatefile function prevents recursive calls to itself
|
|
|
|
// by copying this map and overwriting the "templatefile" entry.
|
|
|
|
return s.funcs
|
|
|
|
})
|
|
|
|
|
2021-04-23 16:29:50 +02:00
|
|
|
if s.ConsoleMode {
|
|
|
|
// The type function is only available in terraform console.
|
|
|
|
s.funcs["type"] = funcs.TypeFunc
|
|
|
|
}
|
|
|
|
|
2018-04-04 03:11:28 +02:00
|
|
|
if s.PureOnly {
|
|
|
|
// Force our few impure functions to return unknown so that we
|
|
|
|
// can defer evaluating them until a later pass.
|
|
|
|
for _, name := range impureFunctions {
|
|
|
|
s.funcs[name] = function.Unpredictable(s.funcs[name])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.funcsLock.Unlock()
|
|
|
|
|
|
|
|
return s.funcs
|
|
|
|
}
|
|
|
|
|
2020-10-30 22:19:30 +01:00
|
|
|
// experimentalFunction checks whether the given experiment is enabled for
|
|
|
|
// the recieving scope. If so, it will return the given function verbatim.
|
|
|
|
// If not, it will return a placeholder function that just returns an
|
|
|
|
// error explaining that the function requires the experiment to be enabled.
|
|
|
|
func (s *Scope) experimentalFunction(experiment experiments.Experiment, fn function.Function) function.Function {
|
|
|
|
if s.activeExperiments.Has(experiment) {
|
|
|
|
return fn
|
|
|
|
}
|
|
|
|
|
|
|
|
err := fmt.Errorf(
|
|
|
|
"this function is experimental and available only when the experiment keyword %s is enabled for the current module",
|
|
|
|
experiment.Keyword(),
|
|
|
|
)
|
|
|
|
|
|
|
|
return function.New(&function.Spec{
|
|
|
|
Params: fn.Params(),
|
|
|
|
VarParam: fn.VarParam(),
|
|
|
|
Type: func(args []cty.Value) (cty.Type, error) {
|
|
|
|
return cty.DynamicPseudoType, err
|
|
|
|
},
|
|
|
|
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
|
|
|
// It would be weird to get here because the Type function always
|
|
|
|
// fails, but we'll return an error here too anyway just to be
|
|
|
|
// robust.
|
|
|
|
return cty.DynamicVal, err
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|