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
43.15k stars 9.58k forks source link

Add transpose function for lists #35909

Open romanvilu opened 1 month ago

romanvilu commented 1 month ago

Terraform Version

Terraform v1.9.8
on linux_amd64

Use Cases

Terraform has introduced the transpose function for maps, but there is no equivalent function for lists. A transpose function for lists would be beneficial for transforming an input like:

input_list = [
  [a1, a2, a3],
  [b1, b2, b3],
  [c1, c2, c3]
]

into an output like:

output_list = [
  [a1, b1, c1],
  [a2, b2, c2],
  [a3, b3, c3]
]

Such a function would be extremely useful when working with multiple lists as outputs from a module, which then need to be combined. For example, my Terraform module outputs the following lists:

output "node_names" {
  value = openstack_compute_instance_v2.this.*.name
}

output "fixed_ips" {
  value = openstack_networking_floatingip_v2.this.*.fixed_ip
}

output "floating_ips" {
  value = openstack_networking_floatingip_v2.this.*.address
}

Each output contains lists of node names, fixed IP addresses, and floating IP addresses. From these lists, I need to create a combined list of [name, fixed_ip, floating_ip] for each node.

Attempted Solutions

The following code snippet transposes a list of lists in Terraform using nested for expressions. The try function is applied to handle cases where some inner lists may be shorter than others, substituting null for any missing elements:

locals {
  input_list = [
    ["a1", "a2", "a3", "a4"],
    ["b1", "b2", "b3", "b4"],
    ["c1", "c2", "c3", "c4"]
  ]

  output_list = [
    for i in range(max([for l in local.input_list : length(l)]...)) : [
      for j in range(length(local.input_list)) : try(
        local.input_list[j][i], null
      )
    ]
  ]
}

Proposal

No response

References

No response

rajat-ventura commented 1 month ago

Hi @bschaatsbergen, I would like to work on this

bschaatsbergen commented 1 month ago

Thank you for raising this issue, @romanvilu!

Note that you now have the ability to develop your own functions using provider-defined functions.

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

bschaatsbergen commented 1 month ago

Hi @bschaatsbergen, I would like to work on this

Hey @rajat-ventura, feel free to propose a PR! If you’re interested in contributing to Terraform, please begin by reviewing the contributing guide and following the process outlined there. As mentioned above, depending on the Terraform core team’s input, this function may be preferred as a provider-defined function within a specific provider.

virajbhartiya commented 5 days ago

Hey @bschaatsbergen , Is someone working on this or can I pick this up?

crw commented 1 day ago

Hi @virajbhartiya, please see https://github.com/hashicorp/terraform/issues/35909#issuecomment-2443696798 -- same answer. Read the contributing guide, follow the process. Thanks!