37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
validator.go 2.21 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-06-12 13:38 . goimport linting changes
package alert
import (
"fmt"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/ref"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
v3client "github.com/rancher/types/client/management/v3"
)
const monitoringEnabled = "MonitoringEnabled"
func ClusterAlertRuleValidator(resquest *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
var clusterID string
if resquest.ID != "" {
clusterID, _ = ref.Parse(resquest.ID)
} else {
if cid := data["clusterId"]; cid != nil {
clusterID = cid.(string)
} else {
return fmt.Errorf("cluster id is empty")
}
}
var spec v3.ClusterAlertRuleSpec
if err := convert.ToObj(data, &spec); err != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent, fmt.Sprintf("%v", err))
}
if spec.MetricRule != nil {
var cluster v3client.Cluster
if err := access.ByID(resquest, resquest.Version, v3client.ClusterType, clusterID, &cluster); err != nil {
return err
}
if cluster.Conditions != nil {
for _, v := range cluster.Conditions {
if v.Type == monitoringEnabled && v.Status == "True" {
return nil
}
}
}
return fmt.Errorf("if you want to use metric alert, need to enable monitoring for cluster %s", clusterID)
}
return nil
}
func ProjectAlertRuleValidator(resquest *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
projectID := data["projectId"].(string)
var spec v3.ProjectAlertRuleSpec
if err := convert.ToObj(data, &spec); err != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent, fmt.Sprintf("%v", err))
}
if spec.MetricRule != nil {
project := &v3client.Project{}
if err := access.ByID(resquest, resquest.Version, v3client.ProjectType, projectID, project); err != nil {
return fmt.Errorf("access project by id failed, %v", err)
}
if project.Conditions != nil {
for _, v := range project.Conditions {
if v.Type == monitoringEnabled && v.Status == "True" {
return nil
}
}
}
return fmt.Errorf("if you want to use metric alert, need to enable monitoring for project %s", projectID)
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.11-rc4

搜索帮助