From d063bc0e78b9562072ae254ffef091c4adb5bc34 Mon Sep 17 00:00:00 2001 From: Ulric Qin Date: Tue, 1 Dec 2020 23:39:03 +0800 Subject: [PATCH] for jeff, dirty data --- src/modules/ams/http/router_host.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/modules/ams/http/router_host.go b/src/modules/ams/http/router_host.go index 603b0e99..6e3d9f4b 100644 --- a/src/modules/ams/http/router_host.go +++ b/src/modules/ams/http/router_host.go @@ -301,12 +301,17 @@ func v1HostRegister(c *gin.Context) { "disk": struct{}{}, } + var dels []string for k := range f.Fields { if _, ok := hFixed[k]; !ok { - delete(f.Fields, k) + dels = append(dels, k) } } + for i := 0; i < len(dels); i++ { + delete(f.Fields, dels[i]) + } + if host == nil { var err error host, err = models.HostNew(f.SN, f.IP, f.Ident, f.Name, f.Cate, f.Fields) @@ -350,6 +355,19 @@ func v1HostRegister(c *gin.Context) { res, err := models.ResourceGet("uuid=?", fmt.Sprintf("host-%d", host.Id)) dangerous(err) + if res == nil { + // 数据不干净,ams里有这个host,而且是已分配状态,但是resource表里没有,重新注册一下 + dangerous(models.ResourceRegister([]models.Host{*host}, host.Tenant)) + + // 注册完了,重新查询一下试试 + res, err = models.ResourceGet("uuid=?", fmt.Sprintf("host-%d", host.Id)) + dangerous(err) + + if res == nil { + bomb("resource register fail, unknown error") + } + } + res.Ident = f.Ident res.Name = f.Name res.Cate = f.Cate