Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add multiple account support for dashboard widget nrql block #2784

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/newrelic/terraform-provider-newrelic/v2

go 1.22

replace github.com/newrelic/newrelic-client-go/v2 => github.com/newrelic/newrelic-client-go/v2 v2.51.4-0.20241217072802-15dfd8382ed7

toolchain go1.22.6

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ github.com/newrelic/go-agent/v3 v3.30.0 h1:ZXHCT/Cot4iIPwcegCZURuRQOsfmGA6wilW+S
github.com/newrelic/go-agent/v3 v3.30.0/go.mod h1:9utrgxlSryNqRrTvII2XBL+0lpofXbqXApvVWPpbzUg=
github.com/newrelic/go-insights v1.0.3 h1:zSNp1CEZnXktzSIEsbHJk8v6ZihdPFP2WsO/fzau3OQ=
github.com/newrelic/go-insights v1.0.3/go.mod h1:A20BoT8TNkqPGX2nS/Z2fYmKl3Cqa3iKZd4whzedCY4=
github.com/newrelic/newrelic-client-go/v2 v2.51.3 h1:Bu/cUs6nfMjQMPBcxxHt4Xm30tKDT7ttYy/XRDsWP6Y=
github.com/newrelic/newrelic-client-go/v2 v2.51.3/go.mod h1:+RRjI3nDGWT3kLm9Oi3QxpBm70uu8q1upEHBVWCZFpo=
github.com/newrelic/newrelic-client-go/v2 v2.51.4-0.20241217072802-15dfd8382ed7 h1:NQDera2yb7jq/Lz1cl99TGUXKSfg1UrRKH1Y5p1ac/s=
github.com/newrelic/newrelic-client-go/v2 v2.51.4-0.20241217072802-15dfd8382ed7/go.mod h1:+RRjI3nDGWT3kLm9Oi3QxpBm70uu8q1upEHBVWCZFpo=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
Expand Down
5 changes: 3 additions & 2 deletions newrelic/resource_newrelic_one_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,12 @@ func dashboardWidgetDataFormatSchemaElem() *schema.Resource {
func dashboardWidgetNRQLQuerySchemaElem() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"account_id": {
Type: schema.TypeInt,
"account_ids": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "The account id used for the NRQL query.",
Elem: &schema.Schema{Type: schema.TypeInt},
},
"query": {
Type: schema.TypeString,
Expand Down
12 changes: 6 additions & 6 deletions newrelic/resource_newrelic_one_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ func testAccCheckNewRelicOneDashboardConfig_FilterCurrentDashboard(dashboardName
column = 1

nrql_query {
account_id = ` + accountID + `
account_ids = [` + accountID + `]
query = "FROM Transaction SELECT average(duration) FACET appName"
}

Expand Down Expand Up @@ -651,7 +651,7 @@ func testAccCheckNewRelicOneDashboardConfig_PageFull(pageName string, accountID
refresh_rate = 30000

nrql_query {
account_id = ` + accountID + `
account_ids = [` + accountID + `]
query = "FROM Transaction SELECT 51 TIMESERIES"
}
}
Expand Down Expand Up @@ -724,7 +724,7 @@ func testAccCheckNewRelicOneDashboardConfig_PageFull(pageName string, accountID
row = 10
column = 1
nrql_query {
account_id = ` + accountID + `
account_ids = [` + accountID + `]
query = "FROM Transaction SELECT 1 TIMESERIES"
}
nrql_query {
Expand Down Expand Up @@ -913,7 +913,7 @@ func testAccCheckNewRelicOneDashboardConfig_PageFullChanged(pageName string, acc
width = 12

nrql_query {
account_id = ` + accountID + `
account_ids = [` + accountID + `]
query = "FROM Transaction SELECT 51 TIMESERIES LIMIT 10"
}
}
Expand Down Expand Up @@ -983,7 +983,7 @@ func testAccCheckNewRelicOneDashboardConfig_PageFullChanged(pageName string, acc
row = 10
column = 1
nrql_query {
account_id = ` + accountID + `
account_ids = [` + accountID + `]
query = "FROM Transaction SELECT 1 TIMESERIES LIMIT 10"
}
nrql_query {
Expand Down Expand Up @@ -1347,7 +1347,7 @@ func testAccCheckNewRelicOneDashboardConfig_RawConfig(dashboardName string, acco
width = 4

nrql_query {
account_id = ` + accountID + `
account_ids = [` + accountID + `]
query = <<EOT
FROM JVMSampleActiveMQ SELECT latest(HeapMemoryUsage.Used) / 1000, latest(HeapMemoryUsage.Max) / 1000, latest(HeapMemoryUsage.Committed) / 1000, latest(HeapMemoryUsage.Init) / 1000 TIMESERIES AUTO SINCE 3 month ago
EOT
Expand Down
15 changes: 7 additions & 8 deletions newrelic/structures_newrelic_one_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ func expandVariableNRQLQuery(in []interface{}) *dashboards.DashboardVariableNRQL
for _, v := range in {
cfg := v.(map[string]interface{})
out = dashboards.DashboardVariableNRQLQueryInput{
AccountIDs: expandVariableAccountIDs(cfg["account_ids"].([]interface{})),
AccountIDs: expandAccountIDs(cfg["account_ids"].([]interface{})),
Query: nrdb.NRQL(cfg["query"].(string))}
}

return &out
}

func expandVariableAccountIDs(in []interface{}) []int {
func expandAccountIDs(in []interface{}) []int {
out := make([]int, len(in))

for i := range out {
Expand Down Expand Up @@ -990,17 +990,16 @@ func expandDashboardWidgetNRQLQueryInput(queries []interface{}, meta interface{}
var query dashboards.DashboardWidgetNRQLQueryInput
q := v.(map[string]interface{})

if acct, ok := q["account_id"]; ok {
query.AccountID = acct.(int)
if acct, ok := q["account_ids"]; ok {
query.AccountIDS = expandAccountIDs(acct.([]interface{}))
}

if query.AccountID < 1 {
if len(query.AccountIDS) < 1 {
defs := meta.(map[string]interface{})
if acct, ok := defs["account_id"]; ok {
query.AccountID = acct.(int)
query.AccountIDS = expandAccountIDs([]interface{}{acct})
}
}

if nrql, ok := q["query"]; ok {
query.Query = nrdb.NRQL(nrql.(string))
}
Expand Down Expand Up @@ -1461,7 +1460,7 @@ func flattenDashboardWidgetNRQLQuery(in *[]dashboards.DashboardWidgetNRQLQueryIn
for i, v := range *in {
m := make(map[string]interface{})

m["account_id"] = v.AccountID
m["account_ids"] = v.AccountIDS
m["query"] = v.Query

out[i] = m
Expand Down
16 changes: 8 additions & 8 deletions website/docs/r/one_dashboard.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ resource "newrelic_one_dashboard" "exampledash" {
height = 3

nrql_query {
account_id = 12345
account_ids = [12345]
query = "FROM Transaction SELECT average(duration) FACET appName"
}

Expand All @@ -88,7 +88,7 @@ resource "newrelic_one_dashboard" "exampledash" {
refresh_rate = 300000 // 5 minutes

nrql_query {
account_id = 12345
account_ids = [12345,896756]
query = "FROM Transaction SELECT average(duration) FACET appName"
}

Expand Down Expand Up @@ -119,7 +119,7 @@ resource "newrelic_one_dashboard" "exampledash" {
refresh_rate = 30000 // 30 seconds

nrql_query {
account_id = 12345
account_ids = [12345]
query = "FROM Transaction select max(duration) as 'max duration' where httpResponseCode = '504' timeseries since 5 minutes ago"
}

Expand Down Expand Up @@ -414,7 +414,7 @@ This attribute requires specifying the following attributes in a nested block -
height = 3

nrql_query {
account_id = Account_ID
account_ids = [Account_ID]
query = "SELECT average(duration), max(duration), min(duration) FROM Transaction FACET name SINCE 1 day ago"
}

Expand Down Expand Up @@ -466,7 +466,7 @@ Nested `nrql_query` blocks allow you to make one or more NRQL queries within a w

The following arguments are supported:

* `account_id` - (Optional) The New Relic account ID to issue the query against. Defaults to the Account ID where the dashboard was created. When using an account ID you don't have permissions for the widget will be replaced with a widget showing the data is inaccessible. Terraform will not throw an error, so this widget will only be visible in the UI.
* `account_ids` - (Optional) List of New Relic account IDs to issue the query against. Defaults to the Account ID where the dashboard was created. When using an account ID you don't have permissions for the widget will be replaced with a widget showing the data is inaccessible. Terraform will not throw an error, so this widget will only be visible in the UI.
* `query` - (Required) Valid NRQL query string. See [Writing NRQL Queries](https://docs.newrelic.com/docs/insights/nrql-new-relic-query-language/using-nrql/introduction-nrql) for help.

```hcl
Expand All @@ -478,7 +478,7 @@ widget_line {
height = 3

nrql_query {
account_id = Another_Account_ID
account_ids = [Another_Account_ID]
query = "FROM Transaction SELECT average(duration) FACET appName"
}

Expand Down Expand Up @@ -580,11 +580,11 @@ resource "newrelic_one_dashboard" "multi_page_dashboard" {
column = 1
width = 12
nrql_query {
account_id = First_Account_ID
account_ids = [First_Account_ID]
query = "FROM Metric SELECT rate(count(apm.service.transaction.duration), 1 minute) as 'First Account Throughput' TIMESERIES"
}
nrql_query {
account_id = Second_Account_ID
account_ids = [Second_Account_ID]
query = "FROM Metric SELECT rate(count(apm.service.transaction.duration), 1 minute) as 'Second Account Throughput' TIMESERIES"
}
y_axis_left_zero = false
Expand Down
Loading