代码拉取完成,页面将自动刷新
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"
"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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。