Create an easy way to configure a widget to filter the newrelic_one_dashboard to itself.
linked_entity_guids to contain the GUID of the dashboardnewrelic_entity to fetch the GUID for a dashboard, then use that data source within the dashboard definition.There are a few issues to resolve to make that work as expected:
newrelic_one_dashboard.<name>.guid would work. The work-around for this would be to use newrelic_entity to fetch the GUID for the dashboard.This is the continuation of #322
Here is an example work-around. Note: There is a boot-strap issue, so the data has to be added to the config after the dashboard is created. Also, since the newrelic_entity is searching by name, and dashboards can have the same name, it's not guaranteed to self-link if there is more than one dashboard with the same name.
data "newrelic_entity" "exampledash" {
name = "New Relic Terraform Example"
type = "DASHBOARD"
tag {
key = "accountId"
value = "<REDACTED>"
}
}
resource "newrelic_one_dashboard" "exampledash" {
name = "New Relic Terraform Example"
page {
name = "New Relic Terraform Example"
widget_bar {
title = "Average transaction duration, by application"
row = 1
column = 1
nrql_query {
account_id = <REDACTED>
query = "FROM Transaction SELECT average(duration) FACET appName"
}
# Linking to self
linked_entity_guids = [
data.newrelic_entity.exampledash.guid,
]
}
}
}
Thanks for sharing this; one of the first thing I was looking for since getting started with NR1 dashboards generation.
Really wish some variation of self reference would be accepted within resources.
Just ran into an error with the workaround; it's incompatible for dashboards with multiple pages.
This is because a normal 1-paged dashboard's GUID is in the format of <account_id>|VIZ|DASHBOARD|<insights_dashboard_id>. In contrast, upon addition of another page, the format changes to <account_id>|VIZ|DASHBOARD|da:<?_id> where I'm unaware of <?_id>'s source.
However, the change of suffix from <insights_dashboard_id> to da:<?_id> is enough to break any chance of filtering the currently active dashboard page.
To make matter worse, I found out that it isn't possible to revert from a multi-paged dashboard back to a normal 1-paged one since its GUID still retains the da:<?_id> suffix. The dashboard entity has to be destroyed and re-created from scratch. Which is a right shame.
Update to the above, @jthurman42's workaround _does_ work for multi-page dashboards given you amend your data.newrelic_entity query to search for the dashboard _page_ itself, in the following format: ${dashboard_name} / ${page_name}. From there, you can grab its GUID and pop it into linked_entity_guids = [鈥, as per usual.
Many thanks for addressing this, really appreciate the support 馃憤.
@jthurman42 I tried the above example but I am getting the following error. I presume the above solution will work only if the dashboard in question already exists. Correct? Tested with terraform 13.6 and 14.7 against NewRelic terraform provider v2.21.0.
$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
data.newrelic_entity.exampledash: Refreshing state...
Error: the name 'New Relic Terraform Example' does not match any New Relic One entity for the given search parameters (ignore_case: false)
on main.tf line 12, in data "newrelic_entity" "exampledash":
12: data "newrelic_entity" "exampledash" {
@avlesk The example above assumes that you have a dashboard exactly named: New Relic Terraform Example, which must exist before, so on the FIRST terraform apply ensure that you comment out / don't use the newrelic_entity in any way, then add it for a second terraform apply
Thanks, @jthurman42. It worked! I am trying to figure out a way without having to comment in & out to create dashboards at scale. Given 16380 I doubt if it is possible. If I figure it out, will share it here.
Hello! Any update on this? Did anyone find a solution with out commenting out resource? Thank you.
@ppanagiotis I followed the below approach to avoid having to comment out resources. The below example will also work if you want to create several copies of the same dashboard for different input.
Folder structure
Parent
/create
create.tf
/update
update.tf
example_dashboard.tfstate
Contents of create.tf
terraform {
required_providers {
newrelic = {
source = "newrelic/newrelic"
}
}
required_version = ">= 0.14.7"
}
terraform {
backend "local" {
path = "../example_dashboard.tfstate"
}
}
resource "newrelic_one_dashboard" "exampledash" {
name = "New Relic Terraform Example"
page {
name = "New Relic Terraform Example"
widget_bar {
title = "Average transaction duration, by application"
row = 1
column = 1
nrql_query {
account_id = <<redacted>>
query = "FROM Transaction SELECT average(duration) FACET appName"
}
}
}
}
Contents of update.tf
terraform {
required_providers {
newrelic = {
source = "newrelic/newrelic"
}
}
required_version = ">= 0.14.7"
}
terraform {
backend "local" {
path = "../example_dashboard.tfstate"
}
}
data "newrelic_entity" "exampledash" {
name = "New Relic Terraform Example"
type = "DASHBOARD"
tag {
key = "accountId"
value = <<redacted>>
}
}
resource "newrelic_one_dashboard" "exampledash" {
name = "New Relic Terraform Example"
page {
name = "New Relic Terraform Example"
widget_bar {
title = "Average transaction duration, by application"
row = 1
column = 1
nrql_query {
account_id = <<redacted>>
query = "FROM Transaction SELECT average(duration) FACET appName"
}
# Linking to self
linked_entity_guids = [data.newrelic_entity.exampledash.guid]
}
}
}
Go to ./create first
cd ./create
terraform init
terraform apply
The above two commands will initialize and create a dashboard
...
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
newrelic_one_dashboard.exampledash: Creating...
newrelic_one_dashboard.exampledash: Creation complete after 2s [id=<
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
...
Then go to ./update directory
cd ./update
terraform init
terraform apply
Now, given that we are referencing state from the previous command, it will execute an update for 'your_dashboard_output_id'
Refreshing state... [id=your_dashboard_output_id]
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
~ update in-place
Terraform will perform the following actions:
# newrelic_one_dashboard.exampledash will be updated in-place
~ resource "newrelic_one_dashboard" "exampledash" {
id = "your_dashboard_output_id"
name = "New Relic Terraform Example"
# (4 unchanged attributes hidden)
~ page {
name = "New Relic Terraform Example"
# (1 unchanged attribute hidden)
~ widget_bar {
id = "<<redacted>>"
~ linked_entity_guids = [
+ "your_dashboard_output_id",
]
# (5 unchanged attributes hidden)
# (1 unchanged block hidden)
}
}
}
Plan: 0 to add, 1 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
newrelic_one_dashboard.exampledash: Modifying... [id=your_dashboard_output_id]
newrelic_one_dashboard.exampledash: Modifications complete after 2s [id=your_dashboard_output_id]
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
Most helpful comment
Here is an example work-around. Note: There is a boot-strap issue, so the data has to be added to the config after the dashboard is created. Also, since the
newrelic_entityis searching by name, and dashboards can have the same name, it's not guaranteed to self-link if there is more than one dashboard with the same name.