2018-04-04 03:11:28 +02:00
|
|
|
package lang
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"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
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/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{
|
|
|
|
"abs": stdlib.AbsoluteFunc,
|
2018-05-23 00:04:49 +02:00
|
|
|
"basename": funcs.BasenameFunc,
|
2018-05-23 01:29:45 +02:00
|
|
|
"base64decode": funcs.Base64DecodeFunc,
|
2018-05-23 16:27:28 +02:00
|
|
|
"base64encode": funcs.Base64EncodeFunc,
|
|
|
|
"base64gzip": funcs.Base64GzipFunc,
|
2018-05-23 18:16:42 +02:00
|
|
|
"base64sha256": funcs.Base64Sha256Func,
|
|
|
|
"base64sha512": funcs.Base64Sha512Func,
|
|
|
|
"bcrypt": funcs.BcryptFunc,
|
2018-05-23 21:41:55 +02:00
|
|
|
"ceil": funcs.CeilFunc,
|
2018-05-23 22:57:37 +02:00
|
|
|
"chomp": funcs.ChompFunc,
|
2018-05-24 00:42:04 +02:00
|
|
|
"cidrhost": funcs.CidrHostFunc,
|
|
|
|
"cidrnetmask": funcs.CidrNetmaskFunc,
|
|
|
|
"cidrsubnet": funcs.CidrSubnetFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"coalesce": stdlib.CoalesceFunc,
|
2018-05-24 20:48:44 +02:00
|
|
|
"coalescelist": funcs.CoalesceListFunc,
|
2018-05-25 00:20:22 +02:00
|
|
|
"compact": funcs.CompactFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"concat": stdlib.ConcatFunc,
|
2018-05-25 21:57:26 +02:00
|
|
|
"contains": funcs.ContainsFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"csvdecode": stdlib.CSVDecodeFunc,
|
2018-05-23 00:24:50 +02:00
|
|
|
"dirname": funcs.DirnameFunc,
|
2018-05-26 01:15:50 +02:00
|
|
|
"distinct": funcs.DistinctFunc,
|
2018-05-22 02:39:26 +02:00
|
|
|
"element": funcs.ElementFunc,
|
2018-05-26 01:15:50 +02:00
|
|
|
"chunklist": funcs.ChunklistFunc,
|
2018-05-22 02:39:26 +02:00
|
|
|
"file": funcs.MakeFileFunc(s.BaseDir, false),
|
|
|
|
"filebase64": funcs.MakeFileFunc(s.BaseDir, true),
|
2018-05-30 16:26:19 +02:00
|
|
|
"flatten": funcs.FlattenFunc,
|
2018-05-23 22:57:37 +02:00
|
|
|
"floor": funcs.FloorFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"format": stdlib.FormatFunc,
|
|
|
|
"formatlist": stdlib.FormatListFunc,
|
2018-05-24 00:42:04 +02:00
|
|
|
"indent": funcs.IndentFunc,
|
2018-05-25 21:57:26 +02:00
|
|
|
"index": funcs.IndexFunc,
|
2018-05-22 02:39:26 +02:00
|
|
|
"join": funcs.JoinFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"jsondecode": stdlib.JSONDecodeFunc,
|
|
|
|
"jsonencode": stdlib.JSONEncodeFunc,
|
2018-05-31 00:38:55 +02:00
|
|
|
"keys": funcs.KeysFunc,
|
2018-05-22 02:39:26 +02:00
|
|
|
"length": funcs.LengthFunc,
|
2018-05-30 17:41:31 +02:00
|
|
|
"list": funcs.ListFunc,
|
2018-05-23 22:57:37 +02:00
|
|
|
"log": funcs.LogFunc,
|
2018-05-31 18:33:43 +02:00
|
|
|
"lookup": funcs.LookupFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"lower": stdlib.LowerFunc,
|
2018-05-30 22:37:48 +02:00
|
|
|
"map": funcs.MapFunc,
|
2018-05-30 16:26:19 +02:00
|
|
|
"matchkeys": funcs.MatchkeysFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"max": stdlib.MaxFunc,
|
2018-05-23 18:38:12 +02:00
|
|
|
"md5": funcs.Md5Func,
|
2018-05-31 20:47:50 +02:00
|
|
|
"merge": funcs.MergeFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"min": stdlib.MinFunc,
|
2018-05-23 00:43:29 +02:00
|
|
|
"pathexpand": funcs.PathExpandFunc,
|
2018-05-23 22:57:37 +02:00
|
|
|
"pow": funcs.PowFunc,
|
2018-05-23 23:52:50 +02:00
|
|
|
"replace": funcs.ReplaceFunc,
|
2018-05-23 18:38:12 +02:00
|
|
|
"rsadecrypt": funcs.RsaDecryptFunc,
|
2018-05-23 21:11:25 +02:00
|
|
|
"sha1": funcs.Sha1Func,
|
|
|
|
"sha256": funcs.Sha256Func,
|
|
|
|
"sha512": funcs.Sha512Func,
|
2018-05-24 00:42:04 +02:00
|
|
|
"signum": funcs.SignumFunc,
|
2018-05-31 23:46:24 +02:00
|
|
|
"slice": funcs.SliceFunc,
|
2018-05-22 02:39:26 +02:00
|
|
|
"sort": funcs.SortFunc,
|
|
|
|
"split": funcs.SplitFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"substr": stdlib.SubstrFunc,
|
2018-05-23 01:11:31 +02:00
|
|
|
"timestamp": funcs.TimestampFunc,
|
2018-05-23 16:27:28 +02:00
|
|
|
"timeadd": funcs.TimeAddFunc,
|
2018-05-23 23:52:50 +02:00
|
|
|
"title": funcs.TitleFunc,
|
2018-05-31 23:46:24 +02:00
|
|
|
"transpose": funcs.TransposeFunc,
|
2018-05-24 00:42:04 +02:00
|
|
|
"trimspace": funcs.TrimSpaceFunc,
|
2018-04-04 03:11:28 +02:00
|
|
|
"upper": stdlib.UpperFunc,
|
2018-05-23 18:38:12 +02:00
|
|
|
"urlencode": funcs.URLEncodeFunc,
|
2018-05-22 02:39:26 +02:00
|
|
|
"uuid": funcs.UUIDFunc,
|
2018-05-23 18:16:42 +02:00
|
|
|
"values": unimplFunc, // TODO
|
2018-04-04 03:11:28 +02:00
|
|
|
"zipmap": unimplFunc, // TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
},
|
|
|
|
})
|