ams agent上报注册代码调整 (#436)

* rdb资源增加volume

* rdb用户增加创建时间

* rdb用户添加时间

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

* test

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

* 代码调整

* 代码调整

* ams扩展代码调整

* test

* test

* 测试

* 错误调整

* ams agent上报注册代码调整

* map clear返回值去掉

Co-authored-by: alickliming <alickliming@didi.global.com>
This commit is contained in:
alick-liming 2020-12-02 11:49:26 +08:00 committed by GitHub
parent e673c5340c
commit 94eb306692
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 57 additions and 45 deletions

View File

@ -246,6 +246,20 @@ func (f hostRegisterForm) Validate() {
}
}
// mapKeyClear map key clear
func mapKeyClear(src map[string]interface{}, save map[string]struct{}) {
var dels []string
for k := range src {
if _, ok := save[k]; !ok {
dels = append(dels, k)
}
}
for i := 0; i < len(dels); i++ {
delete(src, dels[i])
}
}
// agent主动上报注册信息
func v1HostRegister(c *gin.Context) {
var f hostRegisterForm
@ -283,47 +297,52 @@ func v1HostRegister(c *gin.Context) {
renderMessage(c, nil)
return
}
} else {
if err.Error() != cache.ErrCacheMiss.Error() {
msg := "get cache err"
logger.Error(err)
renderMessage(c, msg)
return
}
}
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{}{},
"cpu": struct{}{},
"mem": struct{}{},
"disk": struct{}{},
}
var dels []string
for k := range f.Fields {
if _, ok := hFixed[k]; !ok {
dels = append(dels, k)
}
}
for i := 0; i < len(dels); i++ {
delete(f.Fields, dels[i])
}
mapKeyClear(f.Fields, hFixed)
if host == nil {
var err error
msg := "create host failed"
host, 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)
if err != nil {
logger.Error(err)
renderMessage(c, msg)
return
}
if host == nil {
msg := "create host failed"
logger.Warningf("%s info:%v", msg, f)
logger.Errorf("%s, report info:%v", msg, f)
renderMessage(c, msg)
return
}
} else {
f.Fields["sn"] = f.SN
f.Fields["ip"] = f.IP
f.Fields["ident"] = f.Ident
f.Fields["name"] = f.Name
f.Fields["cate"] = f.Cate
f.Fields["clock"] = time.Now().Unix()
err = host.Update(f.Fields)
if err != nil {
logger.Error(err)
msg := "update host err"
renderMessage(c, msg)
return
}
@ -372,6 +391,8 @@ func v1HostRegister(c *gin.Context) {
res.Name = f.Name
res.Cate = f.Cate
mapKeyClear(f.Fields, hFixed)
js, err := json.Marshal(f.Fields)
dangerous(err)
@ -380,23 +401,6 @@ func v1HostRegister(c *gin.Context) {
dangerous(res.Update("ident", "name", "cate", "extend"))
}
f.Fields["sn"] = f.SN
f.Fields["ip"] = f.IP
f.Fields["ident"] = f.Ident
f.Fields["name"] = f.Name
f.Fields["cate"] = f.Cate
f.Fields["clock"] = time.Now().Unix()
err = host.Update(f.Fields)
if err == nil {
cache.Set(cacheKey, f.Digest, cache.DEFAULT)
} else {
logger.Error(err)
msg := "update host err"
renderMessage(c, msg)
return
}
var objs []models.HostFieldValue
for k, v := range oldFields {
if k == "tenant" {
@ -414,5 +418,13 @@ func v1HostRegister(c *gin.Context) {
dangerous(err)
}
renderMessage(c, err)
err = cache.Set(cacheKey, f.Digest, cache.DEFAULT)
if err != nil {
msg := "set cache err"
logger.Error(err)
renderMessage(c, msg)
return
}
renderMessage(c, nil)
}