2018-04-03 20:11:28 -05:00
|
|
|
package lang
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-01-03 16:01:14 -06:00
|
|
|
"github.com/hashicorp/hcl/v2/ext/tryfunc"
|
2019-05-26 12:25:18 -05:00
|
|
|
ctyyaml "github.com/zclconf/go-cty-yaml"
|
2018-04-03 20:11:28 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/zclconf/go-cty/cty/function"
|
|
|
|
"github.com/zclconf/go-cty/cty/function/stdlib"
|
2018-05-21 19:39:26 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/lang/funcs"
|
2018-04-03 20:11:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var impureFunctions = []string{
|
2018-05-23 11:38:12 -05:00
|
|
|
"bcrypt",
|
2018-04-03 20:11:28 -05: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-21 19:39:26 -05: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-03 20:11:28 -05:00
|
|
|
s.funcs = map[string]function.Function{
|
2019-01-25 11:16:43 -06:00
|
|
|
"abs": stdlib.AbsoluteFunc,
|
2019-07-02 07:30:30 -05:00
|
|
|
"abspath": funcs.AbsPathFunc,
|
2020-09-22 08:06:42 -05:00
|
|
|
"alltrue": funcs.AllTrueFunc,
|
2020-10-23 15:52:48 -05:00
|
|
|
"anytrue": funcs.AnyTrueFunc,
|
2019-01-25 11:16:43 -06: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 16:01:14 -06:00
|
|
|
"can": tryfunc.CanFunc,
|
2020-02-24 08:29:04 -06:00
|
|
|
"ceil": stdlib.CeilFunc,
|
|
|
|
"chomp": stdlib.ChompFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"cidrhost": funcs.CidrHostFunc,
|
|
|
|
"cidrnetmask": funcs.CidrNetmaskFunc,
|
|
|
|
"cidrsubnet": funcs.CidrSubnetFunc,
|
2019-09-19 21:06:15 -05:00
|
|
|
"cidrsubnets": funcs.CidrSubnetsFunc,
|
2019-04-12 12:57:52 -05:00
|
|
|
"coalesce": funcs.CoalesceFunc,
|
2020-02-24 08:29:04 -06:00
|
|
|
"coalescelist": stdlib.CoalesceListFunc,
|
|
|
|
"compact": stdlib.CompactFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"concat": stdlib.ConcatFunc,
|
2020-02-24 08:29:04 -06:00
|
|
|
"contains": stdlib.ContainsFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"csvdecode": stdlib.CSVDecodeFunc,
|
|
|
|
"dirname": funcs.DirnameFunc,
|
2020-02-24 08:29:04 -06:00
|
|
|
"distinct": stdlib.DistinctFunc,
|
|
|
|
"element": stdlib.ElementFunc,
|
|
|
|
"chunklist": stdlib.ChunklistFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"file": funcs.MakeFileFunc(s.BaseDir, false),
|
|
|
|
"fileexists": funcs.MakeFileExistsFunc(s.BaseDir),
|
2019-08-20 03:50:01 -05:00
|
|
|
"fileset": funcs.MakeFileSetFunc(s.BaseDir),
|
2019-01-25 11:16:43 -06: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 08:29:04 -06:00
|
|
|
"flatten": stdlib.FlattenFunc,
|
|
|
|
"floor": stdlib.FloorFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"format": stdlib.FormatFunc,
|
|
|
|
"formatdate": stdlib.FormatDateFunc,
|
|
|
|
"formatlist": stdlib.FormatListFunc,
|
2020-02-24 08:29:04 -06:00
|
|
|
"indent": stdlib.IndentFunc,
|
|
|
|
"index": funcs.IndexFunc, // stdlib.IndexFunc is not compatible
|
|
|
|
"join": stdlib.JoinFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"jsondecode": stdlib.JSONDecodeFunc,
|
|
|
|
"jsonencode": stdlib.JSONEncodeFunc,
|
2020-02-24 08:29:04 -06:00
|
|
|
"keys": stdlib.KeysFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"length": funcs.LengthFunc,
|
|
|
|
"list": funcs.ListFunc,
|
2020-02-24 08:29:04 -06:00
|
|
|
"log": stdlib.LogFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"lookup": funcs.LookupFunc,
|
|
|
|
"lower": stdlib.LowerFunc,
|
|
|
|
"map": funcs.MapFunc,
|
|
|
|
"matchkeys": funcs.MatchkeysFunc,
|
|
|
|
"max": stdlib.MaxFunc,
|
|
|
|
"md5": funcs.Md5Func,
|
2020-02-24 08:29:04 -06:00
|
|
|
"merge": stdlib.MergeFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"min": stdlib.MinFunc,
|
2020-02-24 08:29:04 -06:00
|
|
|
"parseint": stdlib.ParseIntFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"pathexpand": funcs.PathExpandFunc,
|
2020-02-24 08:29:04 -06:00
|
|
|
"pow": stdlib.PowFunc,
|
2019-05-26 15:09:44 -05:00
|
|
|
"range": stdlib.RangeFunc,
|
2019-08-06 10:21:22 -05:00
|
|
|
"regex": stdlib.RegexFunc,
|
|
|
|
"regexall": stdlib.RegexAllFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"replace": funcs.ReplaceFunc,
|
2020-02-24 08:29:04 -06:00
|
|
|
"reverse": stdlib.ReverseListFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"rsadecrypt": funcs.RsaDecryptFunc,
|
|
|
|
"setintersection": stdlib.SetIntersectionFunc,
|
2020-02-24 08:29:04 -06:00
|
|
|
"setproduct": stdlib.SetProductFunc,
|
2020-02-06 11:49:11 -06:00
|
|
|
"setsubtract": stdlib.SetSubtractFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"setunion": stdlib.SetUnionFunc,
|
|
|
|
"sha1": funcs.Sha1Func,
|
|
|
|
"sha256": funcs.Sha256Func,
|
|
|
|
"sha512": funcs.Sha512Func,
|
2020-02-24 08:29:04 -06:00
|
|
|
"signum": stdlib.SignumFunc,
|
|
|
|
"slice": stdlib.SliceFunc,
|
|
|
|
"sort": stdlib.SortFunc,
|
|
|
|
"split": stdlib.SplitFunc,
|
2019-04-24 13:52:39 -05:00
|
|
|
"strrev": stdlib.ReverseFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"substr": stdlib.SubstrFunc,
|
2020-04-15 13:27:06 -05:00
|
|
|
"sum": funcs.SumFunc,
|
2020-10-21 12:56:56 -05:00
|
|
|
"textdecodebase64": funcs.TextDecodeBase64Func,
|
|
|
|
"textencodebase64": funcs.TextEncodeBase64Func,
|
2019-01-25 11:16:43 -06:00
|
|
|
"timestamp": funcs.TimestampFunc,
|
2020-02-24 08:29:04 -06:00
|
|
|
"timeadd": stdlib.TimeAddFunc,
|
|
|
|
"title": stdlib.TitleFunc,
|
2019-01-25 11:16:43 -06: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 08:29:04 -06:00
|
|
|
"trim": stdlib.TrimFunc,
|
|
|
|
"trimprefix": stdlib.TrimPrefixFunc,
|
|
|
|
"trimspace": stdlib.TrimSpaceFunc,
|
|
|
|
"trimsuffix": stdlib.TrimSuffixFunc,
|
2020-01-03 16:01:14 -06:00
|
|
|
"try": tryfunc.TryFunc,
|
2019-01-25 11:16:43 -06:00
|
|
|
"upper": stdlib.UpperFunc,
|
|
|
|
"urlencode": funcs.URLEncodeFunc,
|
|
|
|
"uuid": funcs.UUIDFunc,
|
2019-05-08 04:13:30 -05:00
|
|
|
"uuidv5": funcs.UUIDV5Func,
|
2020-02-24 08:29:04 -06:00
|
|
|
"values": stdlib.ValuesFunc,
|
2019-05-26 12:25:18 -05:00
|
|
|
"yamldecode": ctyyaml.YAMLDecodeFunc,
|
|
|
|
"yamlencode": ctyyaml.YAMLEncodeFunc,
|
2020-02-24 08:29:04 -06:00
|
|
|
"zipmap": stdlib.ZipmapFunc,
|
2018-04-03 20:11:28 -05:00
|
|
|
}
|
|
|
|
|
2018-12-20 19:42:42 -06: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
|
|
|
|
})
|
|
|
|
|
2018-04-03 20:11:28 -05: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
|
|
|
|
}
|
|
|
|
|
|
|
|
var unimplFunc = function.New(&function.Spec{
|
|
|
|
Type: func([]cty.Value) (cty.Type, error) {
|
|
|
|
return cty.DynamicPseudoType, fmt.Errorf("function not yet implemented")
|
|
|
|
},
|
|
|
|
Impl: func([]cty.Value, cty.Type) (cty.Value, error) {
|
|
|
|
return cty.DynamicVal, fmt.Errorf("function not yet implemented")
|
|
|
|
},
|
|
|
|
})
|