Skip to content

Commit

Permalink
fix(dashboards): fixed integration tests failures
Browse files Browse the repository at this point in the history
  • Loading branch information
shashank-reddy-nr committed Oct 14, 2024
1 parent e195b8c commit 60857c9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 243 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ module github.com/newrelic/terraform-provider-newrelic/v2

go 1.21

replace github.com/newrelic/newrelic-client-go/v2 => ../newrelic-client-go

require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1
github.com/mitchellh/go-homedir v1.1.0
github.com/newrelic/go-agent/v3 v3.30.0
github.com/newrelic/go-insights v1.0.3
github.com/newrelic/newrelic-client-go/v2 v2.47.0
github.com/newrelic/newrelic-client-go/v2 v2.48.3-0.20241014040204-073c578d29e2
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +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.48.3-0.20241014040204-073c578d29e2 h1:cc9A/bdL2PXn42uXoCFfXHRHA5GoIv9TQFKRqBF9fhI=
github.com/newrelic/newrelic-client-go/v2 v2.48.3-0.20241014040204-073c578d29e2/go.mod h1:pDFY24/6iIMEbPIdowTRrRn9YYwkXc3j+B+XpTb4oF4=
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
3 changes: 3 additions & 0 deletions newrelic/resource_newrelic_one_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@ func TestAccNewRelicOneDashboard_VariablesNRQL(t *testing.T) {
ResourceName: "newrelic_one_dashboard.bar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"variable.0.options",
},
},
},
})
Expand Down
29 changes: 18 additions & 11 deletions newrelic/structures_newrelic_one_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func expandDashboardInput(d *schema.ResourceData, meta interface{}, dashboardNam
return nil, err
}

dash.Variables, err = expandDashboardVariablesInput(d.Get("variable").([]interface{}))
dash.Variables, err = expandDashboardVariablesInput(d, d.Get("variable").([]interface{}))
if err != nil {
return nil, err
}
Expand All @@ -63,7 +63,7 @@ func checkForNilElements(d []interface{}) bool {
return false
}

func expandDashboardVariablesInput(variables []interface{}) ([]dashboards.DashboardVariableInput, error) {
func expandDashboardVariablesInput(d *schema.ResourceData, variables []interface{}) ([]dashboards.DashboardVariableInput, error) {
if len(variables) < 1 {
return []dashboards.DashboardVariableInput{}, nil
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func expandDashboardVariablesInput(variables []interface{}) ([]dashboards.Dashbo
}

if options, ok := v["options"]; ok && len(options.([]interface{})) > 0 {
variable.Options = expandVariableOptions(options.([]interface{}))
variable.Options = expandVariableOptions(d, options.([]interface{}))
}

expanded[i] = variable
Expand Down Expand Up @@ -1010,19 +1010,23 @@ func expandDashboardWidgetNRQLQueryInput(queries []interface{}, meta interface{}
return expanded, nil
}

func expandVariableOptions(in []interface{}) *dashboards.DashboardVariableOptionsInput {
func expandVariableOptions(d *schema.ResourceData, in []interface{}) *dashboards.DashboardVariableOptionsInput {
var out dashboards.DashboardVariableOptionsInput
fmt.Println("the cfg in expand variable options is:", in)
for _, v := range in {
cfg := v.(map[string]interface{})
if value, ok := cfg["excluded"]; ok {
excludedInput := value.(bool)
out.IgnoreTimeRange = &excludedInput
if val, ok := d.GetOk("variable.0.options.0.excluded"); ok {
excludedInput := val.(bool)
out.Excluded = &excludedInput
fmt.Println("came to if condition excluded:", excludedInput)
}
if value, ok := cfg["ignore_time_range"]; ok {
ignoreTimeRangeInput := value.(bool)
if val, ok := d.GetOk("variable.0.options.0.ignore_time_range"); ok {
ignoreTimeRangeInput := val.(bool)
out.IgnoreTimeRange = &ignoreTimeRangeInput
fmt.Println("came to if condition ignoreTimeRangeInput:", ignoreTimeRangeInput)
}

fmt.Println("the cfg in expand variable options is:", cfg)
}
fmt.Println("the expanded options are", out)
return &out
Expand Down Expand Up @@ -1117,14 +1121,15 @@ func flattenDashboardVariable(in *[]entities.DashboardVariable, d *schema.Resour
fmt.Println("came to line 1113")
if v.Options != nil {
options := flattenVariableOptions(v.Options, d, i)
fmt.Println("came to line 1119")
if options != nil {
// set options -> ignore_time_range to the state only if they already exist in the configuration
// needed to make this backward compatible with configurations which do not yet have options -> ignore_time_range
m["options"] = options
fmt.Println("came to line 1120", m["options"])
}
}

fmt.Println("came to line 1127 v.Options", v.Options)
out[i] = m
}
return out
Expand Down Expand Up @@ -1169,12 +1174,14 @@ func flattenVariableOptions(in *entities.DashboardVariableOptions, d *schema.Res
// fetching the contents of the variable at the specified index (in the list of variables)
// and subsequently, finding its options field
variableFetched := d.Get(fmt.Sprintf("variable.%d", index)).(map[string]interface{})
fmt.Println("came to line 1171 fetched variables are", d.Get("variable"))
optionsOfVariableFetched, optionsOfVariableFetchedOk := variableFetched["options"]
fmt.Println("came to line 1173 fetchedOptions are", variableFetched)
if !optionsOfVariableFetchedOk {
return nil
}
options := optionsOfVariableFetched.([]interface{})
fmt.Println("came to line 1173 options are", options)
fmt.Println("came to line 1178 options are", options)
if len(options) == 0 {
// if nothing exists in the options list in the state (configuration), "do nothing", to avoid drift
// this is required to make options -> ignore_time_range backward compatible and show no drift
Expand Down
230 changes: 1 addition & 229 deletions testing/newrelic.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,232 +8,4 @@ terraform {

provider "newrelic" {
region = "US" # US or EU
}


resource "newrelic_one_dashboard" "shashankDashboard" {
name = "New Relic Terraform Example"
permissions = "public_read_only"

page {
name = "New Relic Terraform Example"

widget_table {
title = "List of Transactions"
row = 1
column = 4
width = 6
height = 3

refresh_rate = 60000 // data refreshes every 60 seconds

nrql_query {
query = "FROM Transaction SELECT *"
}

initial_sorting {
direction = "desc"
name = "timestamp"
}

data_format {
name = "duration"
type = "decimal"
}
}

widget_billboard {
title = "Requests per minute"
row = 1
column = 1
width = 6
height = 3

refresh_rate = 60000 // 60 seconds

data_format {
name = "rate"
type = "recent-relative"
}

nrql_query {
query = "FROM Transaction SELECT rate(count(*), 1 minute)"
}
}

widget_bar {
title = "Average transaction duration, by application"
row = 1
column = 7
width = 6
height = 3

nrql_query {
account_id = 3806526
query = "FROM Transaction SELECT average(duration) FACET appName"
}

}

widget_bar {
title = "Average transaction duration, by application"
row = 4
column = 1
width = 6
height = 3

refresh_rate = 300000 // 5 minutes

nrql_query {
account_id = 3806526
query = "FROM Transaction SELECT average(duration) FACET appName"
}

# Must be another dashboard GUID
filter_current_dashboard = true

# color customization
colors {
color = "#722727"
series_overrides {
color = "#722322"
series_name = "Node"
}
series_overrides {
color = "#236f70"
series_name = "Java"
}
}
}

widget_line {
title = "Average transaction duration and the request per minute, by application"
row = 4
column = 7
width = 6
height = 3

refresh_rate = 30000 // 30 seconds

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

nrql_query {
query = "FROM Transaction SELECT rate(count(*), 1 minute)"
}
legend_enabled = true
y_axis_left_zero = true
y_axis_left_min = 0
y_axis_left_max = 1

y_axis_right {
y_axis_right_zero = true
y_axis_right_min = 0
y_axis_right_max = 300
y_axis_right_series = ["A", "B"]
}

is_label_visible = true

threshold {
name = "Duration Threshold"
from = 1
to = 2
severity = "critical"
}

threshold {
name = "Duration Threshold Two"
from = 2.1
to = 3.3
severity = "warning"
}

units {
unit = "ms"
series_overrides {
unit = "ms"
series_name = "max duration"
}
}


}

widget_markdown {
title = "Dashboard Note"
row = 7
column = 1
width = 12
height = 3

text = "### Helpful Links\n\n* [New Relic One](https://one.newrelic.com)\n* [Developer Portal](https://developer.newrelic.com)"
}

widget_line {
title = "Overall CPU % Statistics"
row = 1
column = 5
height = 3
width = 4

nrql_query {
query = <<EOT
SELECT average(cpuSystemPercent), average(cpuUserPercent), average(cpuIdlePercent), average(cpuIOWaitPercent) FROM SystemSample SINCE 1 hour ago TIMESERIES
EOT
}
facet_show_other_series = false
legend_enabled = true
y_axis_left_zero = true
y_axis_left_min = 0
y_axis_left_max = 0
null_values {
null_value = "default"

series_overrides {
null_value = "remove"
series_name = "Avg Cpu User Percent"
}

series_overrides {
null_value = "zero"
series_name = "Avg Cpu Idle Percent"
}

series_overrides {
null_value = "default"
series_name = "Avg Cpu IO Wait Percent"
}

series_overrides {
null_value = "preserve"
series_name = "Avg Cpu System Percent"
}
}

}

}

variable {
default_values = ["value"]
is_multi_selection = true
item {
title = "item"
value = "ITEM"
}
name = "variable"
nrql_query {
account_ids = [3806526]
query = "FROM Metric select count(*) since 1 day ago where dataType = 'Log API'"
}
replacement_strategy = "default"
title = "title"
type = "nrql"
# options{
# ignore_time_range = false
# }
}
}
}

0 comments on commit 60857c9

Please sign in to comment.