diff --git a/src/models/resource.go b/src/models/resource.go index 5f9f2b9f..9103522a 100644 --- a/src/models/resource.go +++ b/src/models/resource.go @@ -292,6 +292,17 @@ func ResourceRegister(hosts []Host, tenant string) error { return err } + // ident agent修改ident带来重复问题 + if res == nil { + ident := hosts[i].Ident + if ident != "" { + res, err = ResourceGet("ident=?", ident) + if err != nil { + return err + } + } + } + if res == nil { res = &Resource{ UUID: uuid, @@ -326,6 +337,7 @@ func ResourceRegister(hosts []Host, tenant string) error { return err } } + res.UUID = uuid res.Ident = hosts[i].Ident res.Name = hosts[i].Name res.Cate = hosts[i].Cate @@ -343,7 +355,7 @@ func ResourceRegister(hosts []Host, tenant string) error { } res.Extend = string(js) - err = res.Update("ident", "name", "cate", "extend", "tenant") + err = res.Update("uuid", "ident", "name", "cate", "extend", "tenant") if err != nil { return err } diff --git a/src/modules/ams/http/router.go b/src/modules/ams/http/router.go index 5932a0e3..d176e6ba 100644 --- a/src/modules/ams/http/router.go +++ b/src/modules/ams/http/router.go @@ -14,6 +14,7 @@ func Config(r *gin.Engine) { userLogin.POST("/hosts", hostPost) userLogin.GET("/host/:id", hostGet) userLogin.PUT("/hosts/tenant", hostTenantPut) + userLogin.PUT("/hosts/node", hostNodePut) userLogin.PUT("/hosts/back", hostBackPut) userLogin.PUT("/hosts/note", hostNotePut) userLogin.PUT("/hosts/cate", hostCatePut) diff --git a/src/modules/ams/http/router_host.go b/src/modules/ams/http/router_host.go index 7bdb5c3a..12cd25d4 100644 --- a/src/modules/ams/http/router_host.go +++ b/src/modules/ams/http/router_host.go @@ -146,6 +146,78 @@ func hostTenantPut(c *gin.Context) { renderMessage(c, err) } +type hostNodeForm struct { + Ids []int64 `json:"ids"` + NodeId int64 `json:"nodeid"` +} + +func (f *hostNodeForm) Validate() { + if len(f.Ids) == 0 { + bomb("ids is empty") + } + + if f.NodeId == 0 { + bomb("nodeid is blank") + } + + if f.NodeId < 0 { + bomb("nodeid is illegal") + } +} + +// 管理员修改主机设备的节点,相当于挂载设备到节点 +func hostNodePut(c *gin.Context) { + var f hostNodeForm + bind(c, &f) + f.Validate() + + loginUser(c).CheckPermGlobal("ams_host_modify") + node, err := models.NodeGet("id=?", f.NodeId) + dangerous(err) + if node == nil { + bomb("node is nil") + } + + if node.Leaf != 1 { + bomb("node is not leaf") + } + + hosts, err := models.HostByIds(f.Ids) + dangerous(err) + if len(hosts) == 0 { + bomb("hosts is empty") + } + + for _, h := range hosts { + if h.Tenant != "" { + bomb("function only for agent first bind, some agent tenant not null, please clear tenant manual") + } + } + + // 绑定租户 + tenant := node.Tenant() + err = models.HostUpdateTenant(f.Ids, tenant) + dangerous(err) + dangerous(models.ResourceRegister(hosts, tenant)) + + // 绑定到节点 + var resUuids []string + for _, id := range f.Ids { + idStr := fmt.Sprintf("host-%d", id) + resUuids = append(resUuids, idStr) + } + if len(resUuids) == 0 { + bomb("res is empty") + } + resIds, err := models.ResourceIdsByUUIDs(resUuids) + dangerous(err) + if len(resIds) == 0 { + bomb("res ids is empty") + } + + renderMessage(c, node.Bind(resIds)) +} + type hostNoteForm struct { Ids []int64 `json:"ids"` Note string `json:"note"`