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.46k stars 9.51k forks source link

Collection functions to "pick" and "omit" keys from a map #29415

Open coen-hyde opened 3 years ago

coen-hyde commented 3 years ago

Current Terraform Version

1.0.3

Use-cases

I would like Collection functions to do the functional equivalent of https://lodash.com/docs/4.17.15#pick and https://lodash.com/docs/4.17.15#omit. ie compose a new map from an existing map, keeping the structure identical but selectively manage which keys are in the new map based on a provided key list.

Attempted Solutions

I haven't found an elegant way to do this using Terraform's native functions. Please let me know if I have missed something or you have a simple solution.

Proposal

Implement equivalents of https://lodash.com/docs/4.17.15#pick and https://lodash.com/docs/4.17.15#omit.

remilapeyre commented 3 years ago

Hi @coen-hyde, have you tried

{
    for k, v in var.foo:  k => v 
    if contains(var.keep, k) 
}

?

coen-hyde commented 3 years ago

@realflash I have not. I made things way more complicated than that. Thanks for the suggestion.

I guess this request has less necessity now, but it would still be nice to have a native function.

apparentlymart commented 3 years ago

Hi @coen-hyde! Thanks for sharing this use-case.

I see in the discussion here that you've already found a way to get this done with existing functions. Given that this hasn't been a common request so far, my initial instinct is that the pair of existing features that @remilapeyre proposed compose to produce something good enough for this occasional situation, and for also generalizes to arbitrary filter expressions that can meet other use-cases beyond just key matching.

Given that, I'm going to label this as an enhancement so it can stay on our radar for future consideration, but I also want to be up front that it's likely to have a relatively low priority since we tend to prioritize adding and maintaining features that address situations that Terraform can't already support, or that can have a broad impact across multiple use-cases.

Thanks again!

crw commented 7 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!