37 Star 403 Fork 75

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
alert_action.go 3.67 KB
一键复制 编辑 原始数据 按行查看 历史
package alert
import (
"net/http"
"strings"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Handler struct {
ClusterAlerts v3.ClusterAlertInterface
ProjectAlerts v3.ProjectAlertInterface
Notifiers v3.NotifierInterface
}
func Formatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.AddAction(apiContext, "unmute")
resource.AddAction(apiContext, "activate")
resource.AddAction(apiContext, "mute")
resource.AddAction(apiContext, "deactivate")
}
func (h *Handler) ClusterActionHandler(actionName string, action *types.Action, request *types.APIContext) error {
parts := strings.Split(request.ID, ":")
ns := parts[0]
id := parts[1]
alert, err := h.ClusterAlerts.GetNamespaced(ns, id, metav1.GetOptions{})
if err != nil {
logrus.Errorf("Error while getting alert for %s :%v", request.ID, err)
return err
}
switch actionName {
case "activate":
if alert.Status.AlertState == "inactive" {
alert.Status.AlertState = "active"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not inactive")
}
case "deactivate":
if alert.Status.AlertState == "active" {
alert.Status.AlertState = "inactive"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not active")
}
case "mute":
if alert.Status.AlertState == "alerting" {
alert.Status.AlertState = "muted"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not alerting")
}
case "unmute":
if alert.Status.AlertState == "muted" {
alert.Status.AlertState = "alerting"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not muted")
}
}
alert, err = h.ClusterAlerts.Update(alert)
if err != nil {
logrus.Errorf("Error while updating alert:%v", err)
return err
}
data := map[string]interface{}{}
if err := access.ByID(request, request.Version, request.Type, request.ID, &data); err != nil {
return err
}
request.WriteResponse(http.StatusOK, data)
return nil
}
func (h *Handler) ProjectActionHandler(actionName string, action *types.Action, request *types.APIContext) error {
parts := strings.Split(request.ID, ":")
ns := parts[0]
id := parts[1]
alert, err := h.ProjectAlerts.GetNamespaced(ns, id, metav1.GetOptions{})
if err != nil {
logrus.Errorf("Error while getting alert for %s :%v", request.ID, err)
return err
}
switch actionName {
case "activate":
if alert.Status.AlertState == "inactive" {
alert.Status.AlertState = "active"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not inactive")
}
case "deactivate":
if alert.Status.AlertState == "active" {
alert.Status.AlertState = "inactive"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not active")
}
case "mute":
if alert.Status.AlertState == "alerting" {
alert.Status.AlertState = "muted"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not alerting")
}
case "unmute":
if alert.Status.AlertState == "muted" {
alert.Status.AlertState = "alerting"
} else {
return httperror.NewAPIError(httperror.ActionNotAvailable, "state is not muted")
}
}
alert, err = h.ProjectAlerts.Update(alert)
if err != nil {
logrus.Errorf("Error while updating alert:%v", err)
return err
}
data := map[string]interface{}{}
if err := access.ByID(request, request.Version, request.Type, request.ID, &data); err != nil {
return err
}
request.WriteResponse(http.StatusOK, data)
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.0-beta4-rc4

搜索帮助