Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transpose function for lists #35909

Open
romanvilu opened this issue Oct 28, 2024 · 3 comments
Open

Add transpose function for lists #35909

romanvilu opened this issue Oct 28, 2024 · 3 comments
Labels
enhancement functions new new issue not yet triaged

Comments

@romanvilu
Copy link

romanvilu commented Oct 28, 2024

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

@romanvilu romanvilu added enhancement new new issue not yet triaged labels Oct 28, 2024
@rajat-ventura
Copy link

Hi @bschaatsbergen, I would like to work on this

@bschaatsbergen
Copy link
Member

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
Copy link
Member

bschaatsbergen commented Oct 29, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement functions new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants