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

Feature Request: keys(map) - maintain ordering #18231

Closed
stefanthorpe opened this issue Jun 12, 2018 · 3 comments
Closed

Feature Request: keys(map) - maintain ordering #18231

stefanthorpe opened this issue Jun 12, 2018 · 3 comments

Comments

@stefanthorpe
Copy link

The keys(map) builtin - Returns a lexically sorted list of the map keys, the values counterpart also order based on the lexical order of keys.

The feature request is to support a 2nd parameter for keys and values, that toggles the ordering type. Default should remain lexical. But original and ascending and descending would be help.

keys(map, order)

And example of why this is needed.

I have a DNS module with the following components:

locals {
  # Host Expressions
  a_host_names         = "${keys(var.a_records_map)}"
  a_host_addresses     = "${values(var.a_records_map)}"
}

# Create each A provided in map
resource "google_dns_record_set" "a_records" {
  count = "${length(var.a_records_map)}"
  name  = "${local.a_host_names[count.index]}.${google_dns_managed_zone.dns_zone.dns_name}"
  type  = "A"
  ttl   = 300

  managed_zone = "${google_dns_managed_zone.dns_zone.name}"

  rrdatas    = ["${local.a_host_addresses[count.index]}"]
  depends_on = ["google_dns_managed_zone.dns_zone"]
}

Using the module I have a map like below:

   production = {
      my_record    = "35.185.1.2"
      this_record   = "35.18.119.2"
  }

This works fine on the initial deployment. How if I modifiy the map later with a new record where the record is lower in the alphabet. Terraform deletes all the values down to the new entry then rebuild. This causes errors as the builds and deletes aren't synced and causes clashes.

   production = {
      my_record    = "35.185.1.2" # not changed
      this_record   = "35.18.119.2" # deleted and rebuilt
      new_record = "56.23.56.7" # Added 
  }
@apparentlymart
Copy link
Contributor

Hi @stefanthorpe! Thanks for sharing this use-case.

Working with the map elements in reverse order seems reasonable, but I'd prefer to implement it as a new function reverse that takes a list and reverses the order of the elements. This way it can be used not only with keys and values but also with any other list.

It is unfortunately not possible to support "original order" because maps do not retain ordering in Terraform's type system: the order you give in source code is lost immediately after the map expression is evaluated.

The root problem you have here is actually also covered by #17179: a first-class feature for creating a in instance of a resource for each element in a map. This would remove the ordering from consideration altogether, because the resources would be identified by the map keys rather than by indexes as with count.

@mildwonkey
Copy link
Contributor

It's been a while, so I am going to close this issue. For anyone reading this after it is closed:

Thanks!

@ghost
Copy link

ghost commented Jul 26, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Jul 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants