'Filtering through objects in terraform to create metrics in gcp
I am working on something like this (adding charts to gcp monitoring)
data "template_file" "templatefileone" {
template = file("${path.module}/templatefileone.json.tmpl")
count = length(var.one)
vars = {
title = var.metrics[count.index][0]
metricName = var.metrics[count.index][3]
endpoint = var.metrics[count.index][2]
environmentName = "#ENVIRONMENT"
applicationName = var.appName
clusterName = "#CLUSTERNAME"
envName = var.envName
}
}
data "template_file" "templatefiletwo" {
template = file("${path.module}/templatefiletwo.json.tmpl")
count = length(var.two)
vars = {
title = var.jvmMetrics[count.index][0]
metricName = var.jvmMetrics[count.index][1]
environmentName = "#ENVIRONMENT"
applicationName = var.appName
clusterName = "#CLUSTERNAME"
envName = var.envName
}
}
resource "google_monitoring_dashboard" "dashboard" {
for_each = local.environmentsLabels
dashboard_json = templatefile("${path.module}/dashboard.json.tmpl", {
app_name = var.appName,
env_name = each.key,
widgets = join(",",
[for metric in sort(concat(
data.template_file.templatefileone.*.rendered,
[for xyz in data.template_file.templatefiletwo: xyz if (try(sm.vars.endpoint, "") != "")].*.rendered
)) : doSomethingThatDoesntMatterHere()]
)
})
project = var.projectId
}
I am trying to filter through charts 'templatefiletwo', so that only those that have something in variable 'endpoint' will be joined in 'widgets' variable. Currently it looks like:
widgets = join(",",
[for metric in sort(concat(
data.template_file.templatefileone.*.rendered,
data.template_file.templatefiletwo.*.rendered
)) : doSomethingThatDoesntMatterHere()]
)
I expected
[for xyz in data.template_file.templatefiletwo: xyz if (try(sm.vars.endpoint, "") != "")]
to just return same thing as data.template_file.templatefiletwo so that I will be able to call .*.rendered on it, but apparently It doesn't work like this.
Solution 1:[1]
After half a day put into this I decided to just push it to environment and see the error that it sends me. Seems like all along my code worked perfectly as expected, it's just that the terrafrom plugin for IDEA couldn't handle it and thought it's an error. I won't delete the question because it probably will save someone a lot of time.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Marcin Plebanek |
