Directory equivalent of `fileexists` and `fileset` functions
Terraform Version
Terraform 1.7.3
on darwin_arm64
Use Cases
I'd like to be able to have a directory structure of pure data encoded in some formats like YAML, or even HCL. E.g. a monorepo that contains the configuration of git repositories.
$ ls -lh
total 8
-rw-r--r-- 1 bryanhonof staff 63B Feb 17 16:55 main.tf
drwxr-xr-x 2 bryanhonof staff 64B Feb 17 16:55 repo1.git
drwxr-xr-x 2 bryanhonof staff 64B Feb 17 16:55 repo10.git
drwxr-xr-x 2 bryanhonof staff 64B Feb 17 16:55 repo2.git
drwxr-xr-x 2 bryanhonof staff 64B Feb 17 16:55 repo3.git
drwxr-xr-x 2 bryanhonof staff 64B Feb 17 16:55 repo4.git
drwxr-xr-x 2 bryanhonof staff 64B Feb 17 16:55 repo5.git
drwxr-xr-x 2 bryanhonof staff 64B Feb 17 16:55 repo6.git
drwxr-xr-x 2 bryanhonof staff 64B Feb 17 16:55 repo7.git
drwxr-xr-x 2 bryanhonof staff 64B Feb 17 16:55 repo8.git
drwxr-xr-x 2 bryanhonof staff 64B Feb 17 16:55 repo9.git
Then, I'd like to be able to do the following in HCL/Terraform.
# main.tf
locals {
repositories = directoryset(path.module, "*.git")
}
# Some logic using modules and `for_each` to create resources
# The module would then internally use `fileset` and `file` to do more complicated operations
# ...
output "scanned_repositories" {
value = local.repositories
}
$ terraform init
...
$ terraform plan
...
$ terraform apply
...
$ terraform output
scanned_repositories = toset([
"repo1.git",
"repo10.git",
"repo2.git",
"repo3.git",
"repo4.git",
"repo5.git",
"repo6.git",
"repo7.git",
"repo8.git",
"repo9.git",
])
Attempted Solutions
Using fileset in many ways.
Some other hacky solutions to figure out if something's a directory or not.
Proposal
Implement the directory equivalent of fileexists and fileset.
References
- hashicorp/terraform#33394
- hashicorp/terraform#25316
Thanks for this feature request! If you are viewing this issue and would like to indicate your interest, please use the 👍 reaction on the issue description to upvote this issue. We also welcome additional use case descriptions. Thanks again!
I will add that it would likely be better to make the function names direxists and dirset to be consistent with dirname that already exists. Or, just include them into fileexists and fileset with some modifiers to for different behaviors. Perhaps we don't care if it is a file or a directory, we just want to know it is valid. Maybe have an optional return value (i.e. 0 = does not exist, 1 = exists as a file, 2 = exists as a directory).
Many ways to handle it. However, separate functions would be very simple to implement. Likely some copy/pasta for the file equivalents. Alternatively, maybe just use logic in the function definitions to make multiple functions from each with the unique names. Then if there are further functions needed that have the same underlying purpose, it would be easy to add.
EDIT: I do see that the local provider now has a direxists function, which is very handy. It would be nice to see them implemented the same, though. If there is going to be an effort to start using provider functions as a means to have libraries, that would be fine as long as the related functions are packaged together. The other thing that would be useful is to offer a way to alias the provider function namespace, but that isn't related to this issue.