hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io
Other
42.77k stars 9.56k forks source link

provide means of getting set of folders #32654

Open oliver-sentianse opened 1 year ago

oliver-sentianse commented 1 year ago

Terraform Version

1.3.7

Use Cases

I have a terraform root module that needs to find all the sibling root modules to get some json files there (which it will use to generate IAM policies to be used in those other root modules, but that is not important). Eg

manager
root1
root2
root3

and manager folder has tf code with fileset("..", "root*"), the result will be empty.

Attempted Solutions

Use [for x in fileset("..", "root*/variables.tf"): dirname(x)] which as you can see is way more complicated than necessary

Proposal

Make fileset() support folders / directories too, or create a new function dirset()

References

No response

crw commented 1 year ago

Thanks for this request!

erthmeld commented 1 year ago

If I wanted to work on and submit a PR for this functionality should I just reference this ticket, or is more feedback and feature detail required?

I have a use case for a dirset function to allow for module instantiation/iteration over a set of directories with for_each. Then I can use the directory names as parameters for some of the individual resource instantiations in my module allowing me to pick up the config files in those paths in the generated AWS resources.

bflad commented 1 year ago

This sort of functionality could also (co-)exist as a data source of the hashicorp/local provider since it already works in the space of generic file management or really another provider, which would also allow the directory listing to be part of the graph rather than an upfront function call. Created https://github.com/hashicorp/terraform-provider-local/issues/215 to gauge interest on that sort of proposal.

crw commented 8 months ago

Thank you for your continued interest in this issue.

Terraform version 1.8 launches with support of provider-defined functions. It is now possible to implement your own functions! We would love to see this implemented as a provider-defined function.

Please see the provider-defined functions documentation to learn how to implement functions in your providers. If you are new to provider development, learn how to create a new provider with the Terraform Plugin Framework. If you have any questions, please visit the Terraform Plugin Development category in our official forum.

We hope this feature unblocks future function development and provides more flexibility for the Terraform community. Thank you for your continued support of Terraform!

chokhareganesh commented 7 months ago

I know this not been implemented yet, but you can achieve it through with below sample

locals {
   # edit your path here
   batch_directory = "${path.module}/users_dir"

   subdirectories_with_files  = fileset(local.batch_directory, "**")
   subdirectories = setsubtract(flatten([for k, _ in toset(local.subdirectories_with_files) : dirname(k)]),["."])  //remove "." which is not directory name
}

output "subdirectories" {
  value = local.subdirectories
}