From 469b73108feec03fa6cd81408c68ff928a4dd3be Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Fri, 16 Dec 2022 13:39:17 +0100 Subject: [PATCH] Add Terraform 0.12 functions --- internal/funcs/0.12/base_terraform.go | 482 +++++++++++++++++++++++++- 1 file changed, 481 insertions(+), 1 deletion(-) diff --git a/internal/funcs/0.12/base_terraform.go b/internal/funcs/0.12/base_terraform.go index f6b60e15..20b8f29f 100644 --- a/internal/funcs/0.12/base_terraform.go +++ b/internal/funcs/0.12/base_terraform.go @@ -2,10 +2,490 @@ package funcs import ( "github.com/hashicorp/hcl-lang/schema" + "github.com/zclconf/go-cty/cty" + "github.com/zclconf/go-cty/cty/function" ) func BaseTerraformFunctions() map[string]schema.FuncSignature { return map[string]schema.FuncSignature{ - // TODO! + "base64decode": { + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`base64decode` takes a string containing a Base64 character sequence and returns the original string.", + }, + "base64encode": { + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`base64encode` applies Base64 encoding to a string.", + }, + "base64gzip": { + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`base64gzip` compresses a string with gzip and then encodes the result in Base64 encoding.", + }, + "base64sha256": { + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`base64sha256` computes the SHA256 hash of a given string and encodes it with Base64. This is not equivalent to `base64encode(sha256("test"))` since `sha256()` returns hexadecimal representation.", + }, + "base64sha512": { + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`base64sha512` computes the SHA512 hash of a given string and encodes it with Base64. This is not equivalent to `base64encode(sha512("test"))` since `sha512()` returns hexadecimal representation.", + }, + "basename": { + Params: []function.Parameter{ + { + Name: "path", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`basename` takes a string containing a filesystem path and removes all except the last portion from it.", + }, + "bcrypt": { + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + }, + VarParam: &function.Parameter{ + Name: "cost", + Description: "The `cost` argument is optional and will default to 10 if unspecified.", + Type: cty.Number, + }, + ReturnTypes: cty.String, + Description: "`bcrypt` computes a hash of the given string using the Blowfish cipher, returning a string in [the _Modular Crypt Format_](https://passlib.readthedocs.io/en/stable/modular_crypt_format.html) usually expected in the shadow password file on many Unix systems.", + }, + "cidrhost": { + Params: []function.Parameter{ + { + Name: "prefix", + Description: "`prefix` must be given in CIDR notation, as defined in [RFC 4632 section 3.1](https://tools.ietf.org/html/rfc4632#section-3.1).", + Type: cty.String, + }, + { + Name: "hostnum", + Description: "`hostnum` is a whole number that can be represented as a binary integer with no more than the number of digits remaining in the address after the given prefix.", + Type: cty.Number, + }, + }, + ReturnTypes: cty.String, + Description: "`cidrhost` calculates a full host IP address for a given host number within a given IP network address prefix.", + }, + "cidrnetmask": { + Params: []function.Parameter{ + { + Name: "prefix", + Description: "`prefix` must be given in CIDR notation, as defined in [RFC 4632 section 3.1](https://tools.ietf.org/html/rfc4632#section-3.1).", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`cidrnetmask` converts an IPv4 address prefix given in CIDR notation into a subnet mask address.", + }, + "cidrsubnet": { + Params: []function.Parameter{ + { + Name: "prefix", + Description: "`prefix` must be given in CIDR notation, as defined in [RFC 4632 section 3.1](https://tools.ietf.org/html/rfc4632#section-3.1).", + Type: cty.String, + }, + { + Name: "newbits", + Description: "`newbits` is the number of additional bits with which to extend the prefix.", + Type: cty.Number, + }, + { + Name: "netnum", + Description: "`netnum` is a whole number that can be represented as a binary integer with no more than `newbits` binary digits, which will be used to populate the additional bits added to the prefix.", + Type: cty.Number, + }, + }, + ReturnTypes: cty.String, + Description: "`cidrsubnet` calculates a subnet address within given IP network address prefix.", + }, + "coalesce": { + VarParam: &function.Parameter{ + Name: "vals", + Description: "", + Type: cty.DynamicPseudoType, + }, + ReturnTypes: cty.DynamicPseudoType, + Description: "`coalesce` takes any number of arguments and returns the first one that isn't null or an empty string.", + }, + "dirname": { + Params: []function.Parameter{ + { + Name: "path", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`dirname` takes a string containing a filesystem path and removes the last portion from it.", + }, + "file": { + Params: []function.Parameter{ + { + Name: "path", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`file` reads the contents of a file at the given path and returns them as a string.", + }, + "filebase64": { + Params: []function.Parameter{ + { + Name: "path", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`filebase64` reads the contents of a file at the given path and returns them as a base64-encoded string.", + }, + "filebase64sha256": { + Params: []function.Parameter{ + { + Name: "path", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`filebase64sha256` is a variant of `base64sha256` that hashes the contents of a given file rather than a literal string.", + }, + "filebase64sha512": { + Params: []function.Parameter{ + { + Name: "path", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`filebase64sha512` is a variant of `base64sha512` that hashes the contents of a given file rather than a literal string.", + }, + "fileexists": { + Params: []function.Parameter{ + { + Name: "path", + Type: cty.String, + }, + }, + ReturnTypes: cty.Bool, + Description: "`fileexists` determines whether a file exists at a given path.", + }, + "filemd5": { + Params: []function.Parameter{ + { + Name: "path", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`filemd5` is a variant of `md5` that hashes the contents of a given file rather than a literal string.", + }, + "filesha1": { + Params: []function.Parameter{ + { + Name: "path", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`filesha1` is a variant of `sha1` that hashes the contents of a given file rather than a literal string.", + }, + "filesha256": { + Params: []function.Parameter{ + { + Name: "path", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`filesha256` is a variant of `sha256` that hashes the contents of a given file rather than a literal string.", + }, + "filesha512": { + Params: []function.Parameter{ + { + Name: "path", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`filesha512` is a variant of `sha512` that hashes the contents of a given file rather than a literal string.", + }, + "index": { + Params: []function.Parameter{ + { + Name: "list", + Type: cty.DynamicPseudoType, + }, + { + Name: "value", + Type: cty.DynamicPseudoType, + }, + }, + ReturnTypes: cty.DynamicPseudoType, + Description: "`index` finds the element index for a given value in a list.", + }, + "length": { + Params: []function.Parameter{ + { + Name: "value", + Type: cty.DynamicPseudoType, + }, + }, + ReturnTypes: cty.Number, + Description: "`length` determines the length of a given list, map, or string.", + }, + // TODO! "list": {}, + "lookup": { + Params: []function.Parameter{ + { + Name: "inputMap", + Type: cty.DynamicPseudoType, + }, + { + Name: "key", + Type: cty.String, + }, + }, + VarParam: &function.Parameter{ + Name: "default", + Description: "", + Type: cty.DynamicPseudoType, + }, + ReturnTypes: cty.DynamicPseudoType, + Description: "`lookup` retrieves the value of a single element from a map, given its key. If the given key does not exist, the given default value is returned instead.", + }, + // TODO! "map": {} + "matchkeys": { + Params: []function.Parameter{ + { + Name: "values", + Type: cty.List(cty.DynamicPseudoType), + }, + { + Name: "keys", + Type: cty.List(cty.DynamicPseudoType), + }, + { + Name: "searchset", + Type: cty.List(cty.DynamicPseudoType), + }, + }, + ReturnTypes: cty.List(cty.DynamicPseudoType), + Description: "`matchkeys` constructs a new list by taking a subset of elements from one list whose indexes match the corresponding indexes of values in another list.", + }, + "md5": { + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`md5` computes the MD5 hash of a given string and encodes it with hexadecimal digits.", + }, + "pathexpand": { + Params: []function.Parameter{ + { + Name: "path", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`pathexpand` takes a filesystem path that might begin with a `~` segment, and if so it replaces that segment with the current user's home directory path.", + }, + "replace": { + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + { + Name: "substr", + Type: cty.String, + }, + { + Name: "replace", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`replace` searches a given string for another given substring, and replaces each occurrence with a given replacement string.", + }, + "rsadecrypt": { + Params: []function.Parameter{ + { + Name: "ciphertext", + Type: cty.String, + }, + { + Name: "privatekey", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`rsadecrypt` decrypts an RSA-encrypted ciphertext, returning the corresponding cleartext.", + }, + "sha1": { + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`sha1` computes the SHA1 hash of a given string and encodes it with hexadecimal digits.", + }, + "sha256": { + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`sha256` computes the SHA256 hash of a given string and encodes it with hexadecimal digits.", + }, + "sha512": { + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`sha512` computes the SHA512 hash of a given string and encodes it with hexadecimal digits.", + }, + "templatefile": { + Params: []function.Parameter{ + { + Name: "path", + Type: cty.String, + }, + { + Name: "vars", + Type: cty.DynamicPseudoType, + }, + }, + ReturnTypes: cty.DynamicPseudoType, + Description: "`templatefile` reads the file at the given path and renders its content as a template using a supplied set of template variables.", + }, + "timestamp": { + ReturnTypes: cty.String, + Description: "`timestamp` returns a UTC timestamp string in [RFC 3339](https://tools.ietf.org/html/rfc3339) format.", + }, + "tobool": { + Params: []function.Parameter{ + { + Name: "v", + Type: cty.DynamicPseudoType, + }, + }, + ReturnTypes: cty.Bool, + Description: "`tobool` converts its argument to a boolean value.", + }, + "tolist": { + Params: []function.Parameter{ + { + Name: "v", + Type: cty.DynamicPseudoType, + }, + }, + ReturnTypes: cty.List(cty.DynamicPseudoType), + Description: "`tolist` converts its argument to a list value.", + }, + "tomap": { + Params: []function.Parameter{ + { + Name: "v", + Type: cty.DynamicPseudoType, + }, + }, + ReturnTypes: cty.Map(cty.DynamicPseudoType), + Description: "`tomap` converts its argument to a map value.", + }, + "tonumber": { + Params: []function.Parameter{ + { + Name: "v", + Type: cty.DynamicPseudoType, + }, + }, + ReturnTypes: cty.Number, + Description: "`tonumber` converts its argument to a number value.", + }, + "toset": { + Params: []function.Parameter{ + { + Name: "v", + Type: cty.DynamicPseudoType, + }, + }, + ReturnTypes: cty.Set(cty.DynamicPseudoType), + Description: "`toset` converts its argument to a set value.", + }, + "tostring": { + Params: []function.Parameter{ + { + Name: "v", + Type: cty.DynamicPseudoType, + }, + }, + ReturnTypes: cty.String, + Description: "`tostring` converts its argument to a string value.", + }, + "transpose": { + Params: []function.Parameter{ + { + Name: "values", + Type: cty.Map(cty.List(cty.String)), + }, + }, + ReturnTypes: cty.Map(cty.List(cty.String)), + Description: "`transpose` takes a map of lists of strings and swaps the keys and values to produce a new map of lists of strings.", + }, + "urlencode": { + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + }, + ReturnTypes: cty.String, + Description: "`urlencode` applies URL encoding to a given string.", + }, + "uuid": { + ReturnTypes: cty.String, + Description: "`uuid` generates a unique identifier string.", + }, } }