-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier_action.go
89 lines (77 loc) · 2.65 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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/rbac"
"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) {
if canCreateNotifier(apiContext, nil, "") {
collection.AddAction(apiContext, "send")
}
}
func NotifierFormatter(apiContext *types.APIContext, resource *types.RawResource) {
if canCreateNotifier(apiContext, resource, "") {
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")
}
cluster := map[string]interface{}{}
if err = json.Unmarshal(data, &cluster); err != nil {
return errors.Wrap(err, "unmarshaling clusterID error")
}
clusterID, ok := cluster[rbac.ClusterID].(string)
if !ok || !canCreateNotifier(apiContext, nil, clusterID) {
return httperror.NewAPIError(httperror.NotFound, "not found")
}
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)
}
func canCreateNotifier(apiContext *types.APIContext, resource *types.RawResource, clusterID string) bool {
obj := rbac.ObjFromContext(apiContext, resource)
if clusterID != "" {
obj[rbac.NamespaceID] = clusterID
}
return apiContext.AccessControl.CanDo(v3.NotifierGroupVersionKind.Group, v3.NotifierResource.Name, "create", apiContext, obj, apiContext.Schema) == nil
}