From 82dadb31b562b2aa6a9e1f25a57cb80214df4659 Mon Sep 17 00:00:00 2001 From: alick-liming Date: Mon, 30 Nov 2020 15:17:43 +0800 Subject: [PATCH] =?UTF-8?q?1.=20ams=E6=89=A9=E5=B1=95=E5=AD=97=E6=AE=B5=20?= =?UTF-8?q?2.rdb=E6=A0=87=E7=AD=BE=E6=89=B9=E9=87=8F=E4=BF=AE=E6=94=B9=20(?= =?UTF-8?q?#428)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * rdb资源增加volume * rdb用户增加创建时间 * rdb用户添加时间 * rdb新增添加用户时间代码调整 * test * 1.agent上报扩展字段 2.rdb标签批量修改 * 代码调整 * 代码调整 Co-authored-by: alickliming --- src/modules/ams/http/router_host.go | 60 +++++++++++++++++++++++++ src/modules/rdb/http/router.go | 1 + src/modules/rdb/http/router_resource.go | 38 ++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/src/modules/ams/http/router_host.go b/src/modules/ams/http/router_host.go index 152671af..fc9971b1 100644 --- a/src/modules/ams/http/router_host.go +++ b/src/modules/ams/http/router_host.go @@ -8,6 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/toolkits/pkg/cache" + "github.com/toolkits/pkg/logger" "github.com/didi/nightingale/src/models" ) @@ -251,6 +252,11 @@ func v1HostRegister(c *gin.Context) { bind(c, &f) f.Validate() + oldFields := make(map[string]interface{}, len(f.Fields)) + for k, v := range f.Fields { + oldFields[k] = v + } + uniqValue := f.SN if f.UniqKey == "ip" { uniqValue = f.IP @@ -282,10 +288,31 @@ func v1HostRegister(c *gin.Context) { host, err := models.HostGet(f.UniqKey+" = ?", uniqValue) dangerous(err) + hFixed := map[string]struct{}{ + "sn": struct{}{}, + "ip": struct{}{}, + "ident": struct{}{}, + "name": struct{}{}, + "note": struct{}{}, + "cate": struct{}{}, + "clock": struct{}{}, + "cpu": struct{}{}, + "mem": struct{}{}, + "disk": struct{}{}, + } + + for k := range f.Fields { + if _, ok := hFixed[k]; !ok { + delete(f.Fields, k) + } + } + if host == nil { err = models.HostNew(f.SN, f.IP, f.Ident, f.Name, f.Cate, f.Fields) if err == nil { cache.Set(cacheKey, f.Digest, cache.DEFAULT) + } else { + logger.Warning(err) } renderMessage(c, err) return @@ -318,6 +345,39 @@ func v1HostRegister(c *gin.Context) { err = host.Update(f.Fields) if err == nil { cache.Set(cacheKey, f.Digest, cache.DEFAULT) + } else { + logger.Warning(err) + } + + var objs []models.HostFieldValue + for k, v := range oldFields { + if k == "tenant" { + vStr := v.(string) + if vStr != "" { + err = models.HostUpdateTenant([]int64{host.Id}, vStr) + if err != nil { + logger.Warning(err) + continue + } + + err = models.ResourceRegister([]models.Host{*host}, vStr) + if err != nil { + logger.Warning(err) + continue + } + } + continue + } + + if _, ok := hFixed[k]; !ok { + tmp := models.HostFieldValue{HostId: host.Id, FieldIdent: k, FieldValue: v.(string)} + objs = append(objs, tmp) + } + } + + if len(objs) > 0 { + err = models.HostFieldValuePuts(host.Id, objs) + dangerous(err) } renderMessage(c, err) diff --git a/src/modules/rdb/http/router.go b/src/modules/rdb/http/router.go index 5bad6631..871a6021 100644 --- a/src/modules/rdb/http/router.go +++ b/src/modules/rdb/http/router.go @@ -120,6 +120,7 @@ func Config(r *gin.Engine) { userLogin.POST("/node/:id/resources/bind", resourceBindNode) userLogin.POST("/node/:id/resources/unbind", resourceUnbindNode) userLogin.PUT("/node/:id/resources/note", resourceUnderNodeNotePut) + userLogin.PUT("/node/:id/resources/labels", resourceUnderNodeLabelsPut) userLogin.GET("/tree", treeUntilLeafGets) userLogin.GET("/tree/projs", treeUntilProjectGets) diff --git a/src/modules/rdb/http/router_resource.go b/src/modules/rdb/http/router_resource.go index c44c0d51..4e89f617 100644 --- a/src/modules/rdb/http/router_resource.go +++ b/src/modules/rdb/http/router_resource.go @@ -58,12 +58,23 @@ type resourceNotePutForm struct { Note string `json:"note"` } +type resourceLabelsPutForm struct { + Ids []int64 `json:"ids" binding:"required"` + Labels string `json:"labels"` +} + func (f resourceNotePutForm) Validate() { if len(f.Ids) == 0 { bomb("arg[ids] is empty") } } +func (f resourceLabelsPutForm) Validate() { + if len(f.Ids) == 0 { + bomb("arg[ids] is empty") + } +} + func resourceHttpRegister(count int, items []v1ContainersRegisterItem) { for i := 0; i < count; i++ { items[i].Validate() @@ -386,6 +397,33 @@ func resourceUnderNodeNotePut(c *gin.Context) { renderMessage(c, nil) } +func resourceUnderNodeLabelsPut(c *gin.Context) { + var f resourceLabelsPutForm + bind(c, &f) + f.Validate() + + node := Node(urlParamInt64(c, "id")) + loginUser(c).CheckPermByNode(node, "rdb_resource_modify") + + for i := 0; i < len(f.Ids); i++ { + res, err := models.ResourceGet("id=?", f.Ids[i]) + dangerous(err) + + if res == nil { + continue + } + + if res.Labels == f.Labels { + continue + } + + res.Labels = f.Labels + dangerous(res.Update("labels")) + } + + renderMessage(c, nil) +} + type v1ResourcesRegisterItem struct { UUID string `json:"uuid"` Ident string `json:"ident"`