newrelic / terraform-provider-newrelic

Terraform provider for New Relic
https://registry.terraform.io/providers/newrelic/newrelic/latest/docs
Mozilla Public License 2.0
200 stars 244 forks source link

Change to newrelic_one_dashboard resource type breaks use of dynamic widgets #1308

Closed mason-jones-ck closed 1 year ago

mason-jones-ck commented 3 years ago

New Relic provider:

provider "newrelic" {
  account_id = <our account id>
  region = "US"                        # Valid regions are US and EU
}

Affected resources: newrelic_one_dashboard

Configuration of the resources:

Basic main.tf we have been using:

module "example-dashboard" {
  source  = "git::https://our-module?ref=v0.0.1"
  service_name = "example-service"
  widgets = [
      <widgets passed in to module here>
  ]
}

Module tf:

resource "newrelic_dashboard" "example-dashboard" {
  title = "Dash: ${var.service_name}"

  <widgets here>

 dynamic "widget" {
    for_each = var.widgets
    content {
      account_id = widget.value["account_id"]
      title = widget.value["title"]
      visualization = widget.value["visualization"]
      nrql = widget.value["nrql"]
      row = widget.value["row"]
      column = widget.value["column"]
      width = widget.value["width"]
      height = widget.value["height"]
    }
  }
}

Current behavior:

We have been using the older newrelic_dashboard resource, as shown. This has allowed us to create a module with some basic widgets, and then allow the including Terraform to pass in an array of widgets. These are then inserted into the dashboard by the module.

With the newer newrelic_one_dashboard, because every widget is a different block type, this is no longer possible.

Expected behavior:

We need a way to be able to include an arbitrary number of widgets dynamically by passing them in to a module.

jthurman42 commented 3 years ago

This is a good argument for use of the Raw Configuration for widgets, which is in the API but not implemented.

deshdeep-airwallex commented 3 years ago

➕ We are facing same issue when I tried to use newrelic_one_dashboard_raw resource and dynamically create widgets

dashboard.tf

locals {
  l_newrelic_dashboard = {
    for obj in var.newrelic_dashboard : obj.title => merge(var.newrelic_dashboard_defaults, obj)
  }
}

resource "newrelic_one_dashboard_raw" "newrelic_service_dashboard_dynamic" {
  for_each = local.l_newrelic_dashboard
  name    = each.value.title

  page{
    name    = each.value.title

    dynamic "widget" {
      for_each = try(each.value["widget"], [])
        title         = widget.value.title
        visualization_id = widget.value.visualization_id
        column        = widget.value.column
        row           = widget.value.row
        height        = widget.value.height
        width         = widget.value.width
              configuration    = <<EOT
                    {
                      "nrqlQueries": [
                        {
                          "query": widget.value.nrql
                        }
                      ]
                    }
                    EOT
    }
  }
}

variables.tf

#---------------------------------------------------
# newrelic Overview Dashboard for Services
#---------------------------------------------------
variable "newrelic_dashboard" {
  description = "overview dashboard for multiple services"
  type        = any
}

#------------------------------------------------------------------------
# newrelic Overview Dashboard for Services defaults/required
#------------------------------------------------------------------------
variable "newrelic_dashboard_defaults" {
  type = object({
    name = string,
    page = object({
        name = string,
        widget = list(object({
            title         = string,
            visualization_id = string,
            row           = number,
            column        = number,
            height        = number,
            width         = number,
            configuration = object({
                nrqlQueries = list(object({
                    query = string
                }))
            })
        })
        )
    })
   })
   default = {
    "name" = "Test-dashboard",
    "page" = {
        "name" = "Test dashboard",
            "widget" = [
              {
                "title"         = "",
                "visualization_id" = "",
                "nrql"          = "",
                "row"           = 1,
                "column"        = 1,
                "height"          = 1,
                "width"           = 1,
                "configuration"    ={
                    "nrqlQueries": [
                        {
                          "query": ""
                        }
                      ]
                  }
              }
            ]
    }
  }
 }

terragrunt.hcl

  newrelic_dashboard = [
    {
      "name" = "Overview Dashboard"
      "page" = {
        "name" = "Overview Dashboard page 1"
        "widget" = [
          {
            "title"            = "[SB]Throughput (rpm)"
            "visualization_id" = "facet_bar_chart"
            "column"           = 1
            "row"              = 1
            "height"           = 4
            "width"            = 4
            "notes"            = "rpm per endpoint"
            "configuration" = {
              "nrqlQueries" = [
                {
                  "query" = "SELECT rate(count(apm.service.transaction.duration), 1 minute) as 'requests per min' FROM Metric WHERE appName='sandbox_service' AND transactionName!='WebTransaction/SpringController/health-shallow (GET)' SINCE 1 week AGO UNTIL now FACET `transactionName` "
                }
              ]
            }
          }
        ]
      }
    }
  ]
martinimartinimartini commented 2 years ago

Hey guys! Anyone managed to fix or bypass this error?

danger-cabbage commented 2 years ago

Looking for a fix too!

kidk commented 2 years ago

Hi all

We've added support for dashboard json in latest release: https://github.com/newrelic/terraform-provider-newrelic/discussions/2044

I think this might be a workaround for this particular use-case.

Thoughts?

NSSPKrishna commented 1 year ago

We haven’t heard back from you in a long time so we will close the ticket. If you feel this is still a valid request or bug, feel free to create a new issue.