diff --git a/src/models/alerting_engine.go b/src/models/alerting_engine.go index 5ff99c35..074edc7f 100644 --- a/src/models/alerting_engine.go +++ b/src/models/alerting_engine.go @@ -47,6 +47,19 @@ func AlertingEngineGets(where string, args ...interface{}) ([]*AlertingEngines, return objs, err } +func AlertingEngineGet(where string, args ...interface{}) (*AlertingEngines, error) { + lst, err := AlertingEngineGets(where, args...) + if err != nil { + return nil, err + } + + if len(lst) == 0 { + return nil, nil + } + + return lst[0], nil +} + func AlertingEngineGetsInstances(where string, args ...interface{}) ([]string, error) { var arr []string var err error diff --git a/src/server/reader/reader.go b/src/server/reader/reader.go index 5377d763..c7b27a64 100644 --- a/src/server/reader/reader.go +++ b/src/server/reader/reader.go @@ -33,6 +33,12 @@ func Init() error { func initFromConfig() error { opts := config.C.Reader + + if opts.Url == "" { + logger.Warning("reader url is blank") + return nil + } + cli, err := api.NewClient(api.Config{ Address: opts.Url, RoundTripper: &http.Transport{ diff --git a/src/webapi/router/router.go b/src/webapi/router/router.go index ee0f59cb..63109222 100644 --- a/src/webapi/router/router.go +++ b/src/webapi/router/router.go @@ -290,6 +290,9 @@ func configRoute(r *gin.Engine, version string) { pages.POST("/busi-group/:id/tasks", auth(), user(), perm("/job-tasks/add"), bgrw(), taskAdd) pages.GET("/busi-group/:id/task/*url", auth(), user(), perm("/job-tasks"), taskProxy) pages.PUT("/busi-group/:id/task/*url", auth(), user(), perm("/job-tasks/put"), bgrw(), taskProxy) + + pages.GET("/servers", auth(), admin(), serversGet) + pages.PUT("/server/:id", auth(), admin(), serverBindCluster) } service := r.Group("/v1/n9e") diff --git a/src/webapi/router/router_server.go b/src/webapi/router/router_server.go new file mode 100644 index 00000000..ff1b2898 --- /dev/null +++ b/src/webapi/router/router_server.go @@ -0,0 +1,35 @@ +package router + +import ( + "github.com/didi/nightingale/v5/src/models" + "github.com/gin-gonic/gin" + "github.com/toolkits/pkg/ginx" +) + +// 页面上,拉取 server 列表 +func serversGet(c *gin.Context) { + list, err := models.AlertingEngineGets("") + ginx.NewRender(c).Data(list, err) +} + +type serverBindClusterForm struct { + Cluster string `json:"cluster"` +} + +// 用户为某个 n9e-server 分配一个集群,也可以清空,设置cluster为空字符串即可 +// 清空就表示这个server没啥用了,可能是要下线掉,或者仅仅用作转发器 +func serverBindCluster(c *gin.Context) { + id := ginx.UrlParamInt64(c, "id") + + ae, err := models.AlertingEngineGet("id = ?", id) + ginx.Dangerous(err) + + if ae == nil { + ginx.Dangerous("no such server") + } + + var f serverBindClusterForm + ginx.BindJSON(c, &f) + + ginx.NewRender(c).Message(ae.UpdateCluster(f.Cluster)) +}