Im in a scenario where I am using nested dynamic
blocks, dynamic "widget" { }
and dynamic "metric" { }
below.
The issue is that for each widget iterated upon in the outer dynamic block, just like title
, visualization
and nrql
are top-level attribute of that content { }
block, so is the attribute metric
when visualization = "metric_line_chart"
as per the documentation here
The issue is, just like I have try()
calls for when or when an attribute is not present such as in these lines:
threshold_red = try(widget.value.threshold_red, null)
source = try(widget.value.source, null)
I somehow need to put a try()
in for the dynamic "metric" { }
.
I’ve tried it in a variety of places, try()
outside such as try(dynamic "metric" { ...}, null)
, and dynamic "metric" { try( content {...}, null) ...}
, and I don’t know that I can go deeper than that since if I understand the documentation linked above correctly, both name
and values
are required for a metric { }
such as in:
metric {
name = "Apdex"
values = [ "score" ]
}
So here is my working code so far, without the try()
, so I could just use pointers on if I can do the above somehow with the below code:
resource "newrelic_dashboard" "exampledash" {
title = "New Relic Terraform Example"
filter {
event_types = [
"Transaction"
]
attributes = [
"appName",
"name"
]
}
dynamic "widget" {
for_each = var.widgets
content {
title = widget.value.title
visualization = widget.value.visualization
nrql = try(widget.value.nrql, null)
# I need a try() call at some point here for a `metric` when it is present
dynamic "metric" { # nested dynamic, required for when visualization = "metric_line_chart"
for_each = widget.value.metric
content {
name = widget.value.metric.value
values = widget.value.metric.values
}
}
threshold_red = try(widget.value.threshold_red, null)
source = try(widget.value.source, null)
}
}
}