Adrien Delorme 77a29fc2f8
Allow to have dynamic blocks in a build block + tests (#10825)
This :
* allows to have a `build.dynamic` block
* add tests
* makes sure to show a correct message when a source was not found
  * display only name of source (instead of a weird map printout) 
  * use a "Did you mean %q" feature where possible 


Because dynamic blocks need all variables to be evaluated and available, I moved parsing of everything that is not a variable to "after" variables are extrapolated. Meaning that dynamic block get expanded in the `init` phase and then only we start interpreting HCL2 content.

After #10819 fix #10657
2021-03-30 15:53:04 +02:00

48 lines
864 B
HCL

source "file" "base" {
}
variables {
images = {
dummy = {
image = "dummy"
layers = ["base/main"]
}
postgres = {
image = "postgres/13"
layers = ["base/main", "base/init", "postgres"]
}
}
}
locals {
files = {
foo = {
destination = "fooo"
}
bar = {
destination = "baar"
}
}
}
build {
dynamic "source" {
for_each = var.images
labels = ["file.base"]
content {
name = source.key
target = "${source.value.image}.txt"
content = join("\n", formatlist("layers/%s/files", var.images[source.key].layers))
}
}
dynamic "provisioner" {
for_each = local.files
labels = ["shell-local"]
content {
inline = ["echo '' > ${var.images[source.name].image}-${provisioner.value.destination}.txt"]
}
}
}