Skip to content

Commit

Permalink
Change index to append
Browse files Browse the repository at this point in the history
  • Loading branch information
fformica committed Apr 22, 2024
1 parent 867a17e commit 3bcca07
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions ns1/resource_notifylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ func notifyListToResourceData(d *schema.ResourceData, nl *monitor.NotifyList) er
func resourceDataToNotifyList(nl *monitor.NotifyList, d *schema.ResourceData) error {
nl.ID = d.Id()

index := 0
if rawNotifications := d.Get("notifications").(*schema.Set); rawNotifications.Len() > 0 {
ns := make([]*monitor.Notification, rawNotifications.Len())
ns := make([]*monitor.Notification, 0, rawNotifications.Len())
for _, notificationRaw := range rawNotifications.List() {
ni := notificationRaw.(map[string]interface{})
config := ni["config"].(map[string]interface{})
Expand All @@ -78,28 +77,28 @@ func resourceDataToNotifyList(nl *monitor.NotifyList, d *schema.ResourceData) er
case "email":
email := config["email"]
if email != nil {
ns[index] = monitor.NewEmailNotification(email.(string))
ns = append(ns, monitor.NewEmailNotification(email.(string)))
} else {
return fmt.Errorf("wrong config for email expected email field into config")
}
case "datafeed":
sourceId := config["sourceid"]
if sourceId != nil {
ns[index] = monitor.NewFeedNotification(sourceId.(string))
ns = append(ns, monitor.NewFeedNotification(sourceId.(string)))
} else {
return fmt.Errorf("wrong config for datafeed expected sourceid field into config")
}
case "webhook":
url := config["url"]
if url != nil {
ns[index] = monitor.NewWebNotification(url.(string), nil)
ns = append(ns, monitor.NewWebNotification(url.(string), nil))
} else {
return fmt.Errorf("wrong config for webhook expected url field into config")
}
case "pagerduty":
serviceKey := config["service_key"]
if serviceKey != nil {
ns[index] = monitor.NewPagerDutyNotification(serviceKey.(string))
ns = append(ns, monitor.NewPagerDutyNotification(serviceKey.(string)))
} else {
return fmt.Errorf("wrong config for pagerduty expected serviceKey field into config")
}
Expand All @@ -108,18 +107,16 @@ func resourceDataToNotifyList(nl *monitor.NotifyList, d *schema.ResourceData) er
username := config["username"]
channel := config["channel"]
if url != nil && username != nil && channel != nil {
ns[index] = monitor.NewSlackNotification(url.(string), username.(string), channel.(string))
ns = append(ns, monitor.NewSlackNotification(url.(string), username.(string), channel.(string)))
} else {
return fmt.Errorf("wrong config for slack expected url, username and channel fields into config")
}
default:
return fmt.Errorf("%s is not a valid notifier type", ni["type"])
}
// Only increase if not empty
index++
}
}
nl.Notifications = ns[:index]
nl.Notifications = ns
}
return nil
}
Expand Down

0 comments on commit 3bcca07

Please sign in to comment.