Skip to content

Commit

Permalink
fix(dashboard): fix error handling around default_values to disallo…
Browse files Browse the repository at this point in the history
…w empty values (#2734)
  • Loading branch information
vinay-newrelic authored Sep 3, 2024
1 parent 92323f7 commit bd4ab81
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions newrelic/resource_newrelic_one_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func dashboardVariableSchemaElem() *schema.Resource {
"default_values": {
Type: schema.TypeList,
Optional: true,
MinItems: 1,
Description: "Default values for this variable.",
Elem: &schema.Schema{
Type: schema.TypeString,
Expand Down
30 changes: 24 additions & 6 deletions newrelic/structures_newrelic_one_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func expandDashboardInput(d *schema.ResourceData, meta interface{}, dashboardNam
return nil, err
}

dash.Variables = expandDashboardVariablesInput(d.Get("variable").([]interface{}))
dash.Variables, err = expandDashboardVariablesInput(d.Get("variable").([]interface{}))
if err != nil {
return nil, err
}

// Optional, with default
perm := d.Get("permissions").(string)
Expand All @@ -50,9 +53,18 @@ func expandDashboardInput(d *schema.ResourceData, meta interface{}, dashboardNam
return &dash, nil
}

func expandDashboardVariablesInput(variables []interface{}) []dashboards.DashboardVariableInput {
func checkForNilElements(d []interface{}) bool {
for _, item := range d {
if item == nil {
return true
}
}
return false
}

func expandDashboardVariablesInput(variables []interface{}) ([]dashboards.DashboardVariableInput, error) {
if len(variables) < 1 {
return []dashboards.DashboardVariableInput{}
return []dashboards.DashboardVariableInput{}, nil
}

expanded := make([]dashboards.DashboardVariableInput, len(variables))
Expand All @@ -61,8 +73,14 @@ func expandDashboardVariablesInput(variables []interface{}) []dashboards.Dashboa
var variable dashboards.DashboardVariableInput
v := val.(map[string]interface{})

if d, ok := v["default_values"]; ok && len(d.([]interface{})) > 0 {
variable.DefaultValues = expandVariableDefaultValues(d.([]interface{}))
if d, ok := v["default_values"].([]interface{}); ok && d != nil && len(d) > 0 {
// Check if the slice is empty or contains only nil elements as we are receiving d as [<nil>] when an empty string is given
hasNil := checkForNilElements(d)
if len(d) > 0 && !hasNil {
variable.DefaultValues = expandVariableDefaultValues(d)
} else {
return []dashboards.DashboardVariableInput{}, fmt.Errorf("default_values is an empty slice or contains only nil values")
}
}

if m, ok := v["is_multi_selection"]; ok {
Expand Down Expand Up @@ -99,7 +117,7 @@ func expandDashboardVariablesInput(variables []interface{}) []dashboards.Dashboa

expanded[i] = variable
}
return expanded
return expanded, nil
}

func expandVariableDefaultValues(in []interface{}) *[]dashboards.DashboardVariableDefaultItemInput {
Expand Down

0 comments on commit bd4ab81

Please sign in to comment.