From f728c24764c62d484f7af227bd51b3d214b0f5b1 Mon Sep 17 00:00:00 2001 From: Chris Goller Date: Thu, 30 Nov 2017 16:26:58 -0600 Subject: [PATCH] Update kapacitor rule response to have empty arrays Signed-off-by: Deniz Kusefoglu --- server/kapacitors.go | 111 +++++++++++++++++++++++++++++++++----- server/kapacitors_test.go | 26 +++++++-- 2 files changed, 119 insertions(+), 18 deletions(-) diff --git a/server/kapacitors.go b/server/kapacitors.go index 937770ca7a..d1f6af5c0d 100644 --- a/server/kapacitors.go +++ b/server/kapacitors.go @@ -358,26 +358,111 @@ func newAlertResponse(task *kapa.Task, srcID, kapaID int) *alertResponse { }, } - if res.Alerts == nil { - res.Alerts = make([]string, 0) + if res.AlertHandlers.Alerta == nil { + res.AlertHandlers.Alerta = []*chronograf.Alerta{} } - if res.AlertNodes == nil { - res.AlertNodes = make([]chronograf.KapacitorNode, 0) + for i, a := range res.AlertHandlers.Alerta { + if a.Service == nil { + a.Service = []string{} + res.AlertHandlers.Alerta[i] = a + } } - for _, n := range res.AlertNodes { - if n.Args == nil { - n.Args = make([]string, 0) + if res.AlertHandlers.Email == nil { + res.AlertHandlers.Email = []*chronograf.Email{} + } + + for i, a := range res.AlertHandlers.Email { + if a.To == nil { + a.To = []string{} + res.AlertHandlers.Email[i] = a } - if n.Properties == nil { - n.Properties = make([]chronograf.KapacitorProperty, 0) + } + + if res.AlertHandlers.Exec == nil { + res.AlertHandlers.Exec = []*chronograf.Exec{} + } + + for i, a := range res.AlertHandlers.Exec { + if a.Command == nil { + a.Command = []string{} + res.AlertHandlers.Exec[i] = a } - for _, p := range n.Properties { - if p.Args == nil { - p.Args = make([]string, 0) - } + } + + if res.AlertHandlers.HipChat == nil { + res.AlertHandlers.HipChat = []*chronograf.HipChat{} + } + + if res.AlertHandlers.Log == nil { + res.AlertHandlers.Log = []*chronograf.Log{} + } + + if res.AlertHandlers.OpsGenie == nil { + res.AlertHandlers.OpsGenie = []*chronograf.OpsGenie{} + } + + for i, a := range res.AlertHandlers.OpsGenie { + if a.Teams == nil { + a.Teams = []string{} + res.AlertHandlers.OpsGenie[i] = a } + + if a.Recipients == nil { + a.Recipients = []string{} + res.AlertHandlers.OpsGenie[i] = a + } + } + + if res.AlertHandlers.PagerDuty == nil { + res.AlertHandlers.PagerDuty = []*chronograf.PagerDuty{} + } + + if res.AlertHandlers.Posts == nil { + res.AlertHandlers.Posts = []*chronograf.Post{} + } + + for i, a := range res.AlertHandlers.Posts { + if a.Headers == nil { + a.Headers = map[string]string{} + res.AlertHandlers.Posts[i] = a + } + } + + if res.AlertHandlers.Pushover == nil { + res.AlertHandlers.Pushover = []*chronograf.Pushover{} + } + + if res.AlertHandlers.Sensu == nil { + res.AlertHandlers.Sensu = []*chronograf.Sensu{} + } + + for i, a := range res.AlertHandlers.Sensu { + if a.Handlers == nil { + a.Handlers = []string{} + res.AlertHandlers.Sensu[i] = a + } + } + + if res.AlertHandlers.Slack == nil { + res.AlertHandlers.Slack = []*chronograf.Slack{} + } + + if res.AlertHandlers.Talk == nil { + res.AlertHandlers.Talk = []*chronograf.Talk{} + } + + if res.AlertHandlers.TCPs == nil { + res.AlertHandlers.TCPs = []*chronograf.TCP{} + } + + if res.AlertHandlers.Telegram == nil { + res.AlertHandlers.Telegram = []*chronograf.Telegram{} + } + + if res.AlertHandlers.VictorOps == nil { + res.AlertHandlers.VictorOps = []*chronograf.VictorOps{} } if res.Query != nil { diff --git a/server/kapacitors_test.go b/server/kapacitors_test.go index d746f7b5c9..11738c8643 100644 --- a/server/kapacitors_test.go +++ b/server/kapacitors_test.go @@ -94,9 +94,9 @@ func Test_KapacitorRulesGet(t *testing.T) { expected []chronograf.AlertRule }{ { - "basic", - "/chronograf/v1/sources/1/kapacitors/1/rules", - []chronograf.AlertRule{ + name: "basic", + requestPath: "/chronograf/v1/sources/1/kapacitors/1/rules", + mockAlerts: []chronograf.AlertRule{ { ID: "cpu_alert", Name: "cpu_alert", @@ -106,15 +106,31 @@ func Test_KapacitorRulesGet(t *testing.T) { TICKScript: tickScript, }, }, - []chronograf.AlertRule{ + expected: []chronograf.AlertRule{ { ID: "cpu_alert", Name: "cpu_alert", Status: "enabled", Type: "stream", DBRPs: []chronograf.DBRP{{DB: "telegraf", RP: "autogen"}}, - Alerts: []string{}, TICKScript: tickScript, + AlertHandlers: chronograf.AlertHandlers{ + Posts: []*chronograf.Post{}, + TCPs: []*chronograf.TCP{}, + Email: []*chronograf.Email{}, + Exec: []*chronograf.Exec{}, + Log: []*chronograf.Log{}, + VictorOps: []*chronograf.VictorOps{}, + PagerDuty: []*chronograf.PagerDuty{}, + Pushover: []*chronograf.Pushover{}, + Sensu: []*chronograf.Sensu{}, + Slack: []*chronograf.Slack{}, + Telegram: []*chronograf.Telegram{}, + HipChat: []*chronograf.HipChat{}, + Alerta: []*chronograf.Alerta{}, + OpsGenie: []*chronograf.OpsGenie{}, + Talk: []*chronograf.Talk{}, + }, }, }, },