37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
alert_action.go 3.98 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-06-12 13:38 . goimport linting changes
package alert
import (
"net/http"
"strings"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Handler struct {
ClusterAlertRule v3.ClusterAlertRuleInterface
ProjectAlertRule v3.ProjectAlertRuleInterface
Notifiers v3.NotifierInterface
}
func RuleFormatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.AddAction(apiContext, "unmute")
resource.AddAction(apiContext, "activate")
resource.AddAction(apiContext, "mute")
resource.AddAction(apiContext, "deactivate")
}
func GroupFormatter(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) ClusterAlertRuleActionHandler(actionName string, action *types.Action, request *types.APIContext) error {
parts := strings.Split(request.ID, ":")
ns := parts[0]
id := parts[1]
alert, err := h.ClusterAlertRule.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.ClusterAlertRule.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) ProjectAlertRuleActionHandler(actionName string, action *types.Action, request *types.APIContext) error {
parts := strings.Split(request.ID, ":")
ns := parts[0]
id := parts[1]
alert, err := h.ProjectAlertRule.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.ProjectAlertRule.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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.10-rc2

搜索帮助

0d507c66 1850385 C8b1a773 1850385