1. ams扩展字段 2.rdb标签批量修改 (#428)

* rdb资源增加volume

* rdb用户增加创建时间

* rdb用户添加时间

* rdb新增添加用户时间代码调整

* test

* 1.agent上报扩展字段 2.rdb标签批量修改

* 代码调整

* 代码调整

Co-authored-by: alickliming <alickliming@didi.global.com>
This commit is contained in:
alick-liming 2020-11-30 15:17:43 +08:00 committed by GitHub
parent b65e56b9fd
commit 82dadb31b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/toolkits/pkg/cache" "github.com/toolkits/pkg/cache"
"github.com/toolkits/pkg/logger"
"github.com/didi/nightingale/src/models" "github.com/didi/nightingale/src/models"
) )
@ -251,6 +252,11 @@ func v1HostRegister(c *gin.Context) {
bind(c, &f) bind(c, &f)
f.Validate() f.Validate()
oldFields := make(map[string]interface{}, len(f.Fields))
for k, v := range f.Fields {
oldFields[k] = v
}
uniqValue := f.SN uniqValue := f.SN
if f.UniqKey == "ip" { if f.UniqKey == "ip" {
uniqValue = f.IP uniqValue = f.IP
@ -282,10 +288,31 @@ func v1HostRegister(c *gin.Context) {
host, err := models.HostGet(f.UniqKey+" = ?", uniqValue) host, err := models.HostGet(f.UniqKey+" = ?", uniqValue)
dangerous(err) 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 { if host == nil {
err = models.HostNew(f.SN, f.IP, f.Ident, f.Name, f.Cate, f.Fields) err = models.HostNew(f.SN, f.IP, f.Ident, f.Name, f.Cate, f.Fields)
if err == nil { if err == nil {
cache.Set(cacheKey, f.Digest, cache.DEFAULT) cache.Set(cacheKey, f.Digest, cache.DEFAULT)
} else {
logger.Warning(err)
} }
renderMessage(c, err) renderMessage(c, err)
return return
@ -318,6 +345,39 @@ func v1HostRegister(c *gin.Context) {
err = host.Update(f.Fields) err = host.Update(f.Fields)
if err == nil { if err == nil {
cache.Set(cacheKey, f.Digest, cache.DEFAULT) 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) renderMessage(c, err)

View File

@ -120,6 +120,7 @@ func Config(r *gin.Engine) {
userLogin.POST("/node/:id/resources/bind", resourceBindNode) userLogin.POST("/node/:id/resources/bind", resourceBindNode)
userLogin.POST("/node/:id/resources/unbind", resourceUnbindNode) userLogin.POST("/node/:id/resources/unbind", resourceUnbindNode)
userLogin.PUT("/node/:id/resources/note", resourceUnderNodeNotePut) userLogin.PUT("/node/:id/resources/note", resourceUnderNodeNotePut)
userLogin.PUT("/node/:id/resources/labels", resourceUnderNodeLabelsPut)
userLogin.GET("/tree", treeUntilLeafGets) userLogin.GET("/tree", treeUntilLeafGets)
userLogin.GET("/tree/projs", treeUntilProjectGets) userLogin.GET("/tree/projs", treeUntilProjectGets)

View File

@ -58,12 +58,23 @@ type resourceNotePutForm struct {
Note string `json:"note"` Note string `json:"note"`
} }
type resourceLabelsPutForm struct {
Ids []int64 `json:"ids" binding:"required"`
Labels string `json:"labels"`
}
func (f resourceNotePutForm) Validate() { func (f resourceNotePutForm) Validate() {
if len(f.Ids) == 0 { if len(f.Ids) == 0 {
bomb("arg[ids] is empty") 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) { func resourceHttpRegister(count int, items []v1ContainersRegisterItem) {
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
items[i].Validate() items[i].Validate()
@ -386,6 +397,33 @@ func resourceUnderNodeNotePut(c *gin.Context) {
renderMessage(c, nil) 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 { type v1ResourcesRegisterItem struct {
UUID string `json:"uuid"` UUID string `json:"uuid"`
Ident string `json:"ident"` Ident string `json:"ident"`