hashicorp / hcl2

Former temporary home for experimental new version of HCL
https://github.com/hashicorp/hcl
Mozilla Public License 2.0
373 stars 66 forks source link

Custom function, create more HCL blocks #62

Closed stevenaldinger closed 5 years ago

stevenaldinger commented 5 years ago

Sorry about the terrible issue title, I have no idea how to make this concise.

I want to include a forEach function in the HCL eval context (or elsewhere) that lets me take a list of strings and return a list of the block the forEach function was in + one element per block from the list (not sure how to say that better).

As an example... given this block:

resource "some_resource" {
  dns_server = forEach(["8.8.8.8", "8.8.4.4"])
}

I want to be able to get some near-equivalent of:

resource "some_resource" {
  dns_server = "8.8.8.8"
}
resource "some_resource" {
  dns_server = "8.8.4.4"
}

I found this example usage of a custom function which makes me think it might not be possible to do what I want to do (this way at least).

Can anyone steer me in the right direction?

Edit: I can't just require to make it multiple blocks in documentation because the list is likely to be a variable/output from another block.

Thanks!

stevenaldinger commented 5 years ago

Just ran into this: for_each example in go_patterns and this Terraform preview article on for and for_each.

Do you guys have any advice on how to go about it right now? I tried this in my project (not Terraform) hoping for an easy out but it doesn't seem to be recognized. Seeing how you guys plan on doing it is still helpful, I'll likely code some garbage into my project that can be replaced by your better version when it's ready lol.

resource "some_resource" {
  for_each = ["1", "2", "3"]
}

Any thoughts on when you think it might be ready would be great too but as a developer I understand how loaded that question is.

Thanks guys! HCL2 is awesome for what that's worth, really appreciate your efforts. Planning on posting some blog articles soon on how to use it to help adoption.