I wrote a reply to this old post:
Hclwrite(v2) a object with a reference value - Terraform - HashiCorp Discuss
In regards of the issue presented there. However, instead of reviving that old thread I thought it perhaps would be wise to create a new one. With that context and babbling out of the way, here goes the question:
Using the hclwrite package, I need to create the following custom_data
object attribute:
post-processor "manifest" {
custom_data = {
subscription = "${var.subscription_id}"
resource_group = "${var.resource_group}"
gallery_name = "${var.gallery_name}"
image_name = "${var.image_name}"
image_version = "${var.versionToBuild}"
source_version = "${var.info_source_version}"
source_branch = "${var.info_source_branch}"
source_repo = "${var.info_source_repo}"
gallery_artifact_id = "/subscriptions/${var.subscription_id}/resourceGroups/${var.resource_group}/providers/Microsoft.Compute/galleries/${var.gallery_name}/images/${var.image_name}/versions/${var.versionToBuild}"
managed_artifact_id = "/subscriptions/${var.subscription_id}/resourceGroups/${var.resource_group}/providers/Microsoft.Compute/images/${var.image_name}-${var.versionToBuild}"
}
}
I can for instance use SetAttributeRaw
like so:
customDataTokens := hclwrite.Tokens{
{
Type: hclsyntax.ObjectConsItem
},
{
Type: hclsyntax.TokenOBrace,
Bytes: []byte(`{`),
},
{
Type: hclsyntax.TokenCBrace,
Bytes: []byte(`}`),
},
}
body.SetAttributeRaw("custom_data", customDataTokens)
Which will result in:
custom_data = {}
But how should I go on about creating an attribute object which contains multiple attributes with reference values, such as the custom_data
example object above?
Unfortunately I’m in a position where using the JSON Configuration Syntax is not possible.
Since as it’s pointed out (in the post I linked to), I’d need to use SetAttributeRaw
to meet this use-case with today’s hclwrite, I’m all for that but I have no idea how to do it - an example would be super helpful and really appreciated!
Or, perhaps sine it’s been 2 years since that was posted, perhaps hclwrite now have a helper function for generating this sort of expression?