github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/lang/functions.go (about)

     1  package lang
     2  
     3  import (
     4  	ctyyaml "github.com/zclconf/go-cty-yaml"
     5  	"github.com/zclconf/go-cty/cty"
     6  	"github.com/zclconf/go-cty/cty/function"
     7  	"github.com/zclconf/go-cty/cty/function/stdlib"
     8  
     9  	"github.com/hashicorp/terraform-plugin-sdk/internal/lang/funcs"
    10  )
    11  
    12  var impureFunctions = []string{
    13  	"bcrypt",
    14  	"timestamp",
    15  	"uuid",
    16  }
    17  
    18  // Functions returns the set of functions that should be used to when evaluating
    19  // expressions in the receiving scope.
    20  func (s *Scope) Functions() map[string]function.Function {
    21  	s.funcsLock.Lock()
    22  	if s.funcs == nil {
    23  		// Some of our functions are just directly the cty stdlib functions.
    24  		// Others are implemented in the subdirectory "funcs" here in this
    25  		// repository. New functions should generally start out their lives
    26  		// in the "funcs" directory and potentially graduate to cty stdlib
    27  		// later if the functionality seems to be something domain-agnostic
    28  		// that would be useful to all applications using cty functions.
    29  
    30  		s.funcs = map[string]function.Function{
    31  			"abs":              stdlib.AbsoluteFunc,
    32  			"abspath":          funcs.AbsPathFunc,
    33  			"basename":         funcs.BasenameFunc,
    34  			"base64decode":     funcs.Base64DecodeFunc,
    35  			"base64encode":     funcs.Base64EncodeFunc,
    36  			"base64gzip":       funcs.Base64GzipFunc,
    37  			"base64sha256":     funcs.Base64Sha256Func,
    38  			"base64sha512":     funcs.Base64Sha512Func,
    39  			"bcrypt":           funcs.BcryptFunc,
    40  			"ceil":             funcs.CeilFunc,
    41  			"chomp":            funcs.ChompFunc,
    42  			"cidrhost":         funcs.CidrHostFunc,
    43  			"cidrnetmask":      funcs.CidrNetmaskFunc,
    44  			"cidrsubnet":       funcs.CidrSubnetFunc,
    45  			"cidrsubnets":      funcs.CidrSubnetsFunc,
    46  			"coalesce":         funcs.CoalesceFunc,
    47  			"coalescelist":     funcs.CoalesceListFunc,
    48  			"compact":          funcs.CompactFunc,
    49  			"concat":           stdlib.ConcatFunc,
    50  			"contains":         funcs.ContainsFunc,
    51  			"csvdecode":        stdlib.CSVDecodeFunc,
    52  			"dirname":          funcs.DirnameFunc,
    53  			"distinct":         funcs.DistinctFunc,
    54  			"element":          funcs.ElementFunc,
    55  			"chunklist":        funcs.ChunklistFunc,
    56  			"file":             funcs.MakeFileFunc(s.BaseDir, false),
    57  			"fileexists":       funcs.MakeFileExistsFunc(s.BaseDir),
    58  			"filebase64":       funcs.MakeFileFunc(s.BaseDir, true),
    59  			"filebase64sha256": funcs.MakeFileBase64Sha256Func(s.BaseDir),
    60  			"filebase64sha512": funcs.MakeFileBase64Sha512Func(s.BaseDir),
    61  			"filemd5":          funcs.MakeFileMd5Func(s.BaseDir),
    62  			"filesha1":         funcs.MakeFileSha1Func(s.BaseDir),
    63  			"filesha256":       funcs.MakeFileSha256Func(s.BaseDir),
    64  			"filesha512":       funcs.MakeFileSha512Func(s.BaseDir),
    65  			"flatten":          funcs.FlattenFunc,
    66  			"floor":            funcs.FloorFunc,
    67  			"format":           stdlib.FormatFunc,
    68  			"formatdate":       stdlib.FormatDateFunc,
    69  			"formatlist":       stdlib.FormatListFunc,
    70  			"indent":           funcs.IndentFunc,
    71  			"index":            funcs.IndexFunc,
    72  			"join":             funcs.JoinFunc,
    73  			"jsondecode":       stdlib.JSONDecodeFunc,
    74  			"jsonencode":       stdlib.JSONEncodeFunc,
    75  			"keys":             funcs.KeysFunc,
    76  			"length":           funcs.LengthFunc,
    77  			"list":             funcs.ListFunc,
    78  			"log":              funcs.LogFunc,
    79  			"lookup":           funcs.LookupFunc,
    80  			"lower":            stdlib.LowerFunc,
    81  			"map":              funcs.MapFunc,
    82  			"matchkeys":        funcs.MatchkeysFunc,
    83  			"max":              stdlib.MaxFunc,
    84  			"md5":              funcs.Md5Func,
    85  			"merge":            funcs.MergeFunc,
    86  			"min":              stdlib.MinFunc,
    87  			"parseint":         funcs.ParseIntFunc,
    88  			"pathexpand":       funcs.PathExpandFunc,
    89  			"pow":              funcs.PowFunc,
    90  			"range":            stdlib.RangeFunc,
    91  			"regex":            stdlib.RegexFunc,
    92  			"regexall":         stdlib.RegexAllFunc,
    93  			"replace":          funcs.ReplaceFunc,
    94  			"reverse":          funcs.ReverseFunc,
    95  			"rsadecrypt":       funcs.RsaDecryptFunc,
    96  			"setintersection":  stdlib.SetIntersectionFunc,
    97  			"setproduct":       funcs.SetProductFunc,
    98  			"setunion":         stdlib.SetUnionFunc,
    99  			"sha1":             funcs.Sha1Func,
   100  			"sha256":           funcs.Sha256Func,
   101  			"sha512":           funcs.Sha512Func,
   102  			"signum":           funcs.SignumFunc,
   103  			"slice":            funcs.SliceFunc,
   104  			"sort":             funcs.SortFunc,
   105  			"split":            funcs.SplitFunc,
   106  			"strrev":           stdlib.ReverseFunc,
   107  			"substr":           stdlib.SubstrFunc,
   108  			"timestamp":        funcs.TimestampFunc,
   109  			"timeadd":          funcs.TimeAddFunc,
   110  			"title":            funcs.TitleFunc,
   111  			"tostring":         funcs.MakeToFunc(cty.String),
   112  			"tonumber":         funcs.MakeToFunc(cty.Number),
   113  			"tobool":           funcs.MakeToFunc(cty.Bool),
   114  			"toset":            funcs.MakeToFunc(cty.Set(cty.DynamicPseudoType)),
   115  			"tolist":           funcs.MakeToFunc(cty.List(cty.DynamicPseudoType)),
   116  			"tomap":            funcs.MakeToFunc(cty.Map(cty.DynamicPseudoType)),
   117  			"transpose":        funcs.TransposeFunc,
   118  			"trimspace":        funcs.TrimSpaceFunc,
   119  			"upper":            stdlib.UpperFunc,
   120  			"urlencode":        funcs.URLEncodeFunc,
   121  			"uuid":             funcs.UUIDFunc,
   122  			"uuidv5":           funcs.UUIDV5Func,
   123  			"values":           funcs.ValuesFunc,
   124  			"yamldecode":       ctyyaml.YAMLDecodeFunc,
   125  			"yamlencode":       ctyyaml.YAMLEncodeFunc,
   126  			"zipmap":           funcs.ZipmapFunc,
   127  		}
   128  
   129  		s.funcs["templatefile"] = funcs.MakeTemplateFileFunc(s.BaseDir, func() map[string]function.Function {
   130  			// The templatefile function prevents recursive calls to itself
   131  			// by copying this map and overwriting the "templatefile" entry.
   132  			return s.funcs
   133  		})
   134  
   135  		if s.PureOnly {
   136  			// Force our few impure functions to return unknown so that we
   137  			// can defer evaluating them until a later pass.
   138  			for _, name := range impureFunctions {
   139  				s.funcs[name] = function.Unpredictable(s.funcs[name])
   140  			}
   141  		}
   142  	}
   143  	s.funcsLock.Unlock()
   144  
   145  	return s.funcs
   146  }