Skip to content

Commit 5eb61ef

Browse files
Add nil checks to list converters + add empty string validation (#685)
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
1 parent bbbb157 commit 5eb61ef

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

grafana/adapters.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import (
77
func listToStringSlice(src []interface{}) []string {
88
dst := make([]string, 0, len(src))
99
for _, s := range src {
10-
dst = append(dst, s.(string))
10+
val, ok := s.(string)
11+
if !ok {
12+
val = ""
13+
}
14+
dst = append(dst, val)
1115
}
1216
return dst
1317
}
@@ -19,7 +23,11 @@ func setToStringSlice(src *schema.Set) []string {
1923
func listToIntSlice(src []interface{}) []int {
2024
dst := make([]int, 0, len(src))
2125
for _, s := range src {
22-
dst = append(dst, s.(int))
26+
val, ok := s.(int)
27+
if !ok {
28+
val = 0
29+
}
30+
dst = append(dst, val)
2331
}
2432
return dst
2533
}

grafana/resource_alerting_contact_point_notifiers.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
gapi "github.com/grafana/grafana-api-golang-client"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1011
)
1112

1213
type alertmanagerNotifier struct{}
@@ -258,7 +259,8 @@ func (e emailNotifier) schema() *schema.Resource {
258259
Required: true,
259260
Description: "The addresses to send emails to.",
260261
Elem: &schema.Schema{
261-
Type: schema.TypeString,
262+
Type: schema.TypeString,
263+
ValidateFunc: validation.StringIsNotEmpty,
262264
},
263265
}
264266
r.Schema["single_email"] = &schema.Schema{
@@ -336,10 +338,7 @@ func packAddrs(addrs string) []string {
336338
}
337339

338340
func unpackAddrs(addrs []interface{}) string {
339-
strs := make([]string, 0, len(addrs))
340-
for _, addr := range addrs {
341-
strs = append(strs, addr.(string))
342-
}
341+
strs := listToStringSlice(addrs)
343342
return strings.Join(strs, addrSeparator)
344343
}
345344

grafana/resource_alerting_notification_policy.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
gapi "github.com/grafana/grafana-api-golang-client"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1011
)
1112

1213
func ResourceNotificationPolicy() *schema.Resource {
@@ -40,7 +41,8 @@ This resource requires Grafana 9.1.0 or later.
4041
Required: true,
4142
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.",
4243
Elem: &schema.Schema{
43-
Type: schema.TypeString,
44+
Type: schema.TypeString,
45+
ValidateFunc: validation.StringIsNotEmpty,
4446
},
4547
},
4648
"group_wait": {

0 commit comments

Comments
 (0)