/vendor/github.com/hashicorp/terraform-plugin-sdk/internal/lang/functions.go

https://github.com/terraform-providers/terraform-provider-azurerm · Go · 146 lines · 126 code · 8 blank · 12 comment · 4 complexity · dcd0d0594f19c17592bbb61e0e4bd0e3 MD5 · raw file

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