forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier_action.go
66 lines (57 loc) · 1.82 KB
/
notifier_action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package alert
import (
"encoding/json"
"io/ioutil"
"github.com/pkg/errors"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/notifiers"
"github.com/rancher/rancher/pkg/ref"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const testSMTPTitle = "Alert From Rancher: SMTP configuration validated"
func NotifierCollectionFormatter(apiContext *types.APIContext, collection *types.GenericCollection) {
collection.AddAction(apiContext, "send")
}
func NotifierFormatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.AddAction(apiContext, "send")
}
func (h *Handler) NotifierActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error {
switch actionName {
case "send":
return h.testNotifier(actionName, action, apiContext)
}
return httperror.NewAPIError(httperror.InvalidAction, "invalid action: "+actionName)
}
func (h *Handler) testNotifier(actionName string, action *types.Action, apiContext *types.APIContext) error {
data, err := ioutil.ReadAll(apiContext.Request.Body)
if err != nil {
return errors.Wrap(err, "reading request body error")
}
input := &struct {
Message string
v3.NotifierSpec
}{}
if err = json.Unmarshal(data, input); err != nil {
return errors.Wrap(err, "unmarshaling input error")
}
notifier := &v3.Notifier{
Spec: input.NotifierSpec,
}
msg := input.Message
if apiContext.ID != "" {
ns, id := ref.Parse(apiContext.ID)
notifier, err = h.Notifiers.GetNamespaced(ns, id, metav1.GetOptions{})
if err != nil {
return err
}
}
notifierMessage := ¬ifiers.Message{
Content: msg,
}
if notifier.Spec.SMTPConfig != nil {
notifierMessage.Title = testSMTPTitle
}
return notifiers.SendMessage(notifier, "", notifierMessage)
}