Skip to content

Commit

Permalink
Add nil checks to list converters + add empty string validation (#685)
Browse files Browse the repository at this point in the history
Closes #674

Whenever an empty string is passed to `group_by` or email addresses, the API returns a nil which crashes the conversion functions

This fixes the bug in two locations, it should no longer panic and also refuse empty values
  • Loading branch information
julienduchesne committed Oct 17, 2022
1 parent bbbb157 commit 5eb61ef
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
12 changes: 10 additions & 2 deletions grafana/adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
func listToStringSlice(src []interface{}) []string {
dst := make([]string, 0, len(src))
for _, s := range src {
dst = append(dst, s.(string))
val, ok := s.(string)
if !ok {
val = ""
}
dst = append(dst, val)
}
return dst
}
Expand All @@ -19,7 +23,11 @@ func setToStringSlice(src *schema.Set) []string {
func listToIntSlice(src []interface{}) []int {
dst := make([]int, 0, len(src))
for _, s := range src {
dst = append(dst, s.(int))
val, ok := s.(int)
if !ok {
val = 0
}
dst = append(dst, val)
}
return dst
}
Expand Down
9 changes: 4 additions & 5 deletions grafana/resource_alerting_contact_point_notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

gapi "github.com/grafana/grafana-api-golang-client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

type alertmanagerNotifier struct{}
Expand Down Expand Up @@ -258,7 +259,8 @@ func (e emailNotifier) schema() *schema.Resource {
Required: true,
Description: "The addresses to send emails to.",
Elem: &schema.Schema{
Type: schema.TypeString,
Type: schema.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
}
r.Schema["single_email"] = &schema.Schema{
Expand Down Expand Up @@ -336,10 +338,7 @@ func packAddrs(addrs string) []string {
}

func unpackAddrs(addrs []interface{}) string {
strs := make([]string, 0, len(addrs))
for _, addr := range addrs {
strs = append(strs, addr.(string))
}
strs := listToStringSlice(addrs)
return strings.Join(strs, addrSeparator)
}

Expand Down
4 changes: 3 additions & 1 deletion grafana/resource_alerting_notification_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
gapi "github.com/grafana/grafana-api-golang-client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func ResourceNotificationPolicy() *schema.Resource {
Expand Down Expand Up @@ -40,7 +41,8 @@ This resource requires Grafana 9.1.0 or later.
Required: true,
Description: "A list of alert labels to group alerts into notifications by. Use the special label `...` to group alerts by all labels, effectively disabling grouping.",
Elem: &schema.Schema{
Type: schema.TypeString,
Type: schema.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
},
"group_wait": {
Expand Down

0 comments on commit 5eb61ef

Please sign in to comment.