ams agent直接挂载到节点 (#518)

* ams agent直接挂载到节点

* 代码调整

Co-authored-by: alickliming <alickliming@didi.global.com>
This commit is contained in:
alick-liming 2021-01-15 19:54:25 +08:00 committed by GitHub
parent b00b7817f2
commit 51cf58fcdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 1 deletions

View File

@ -292,6 +292,17 @@ func ResourceRegister(hosts []Host, tenant string) error {
return err 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 { if res == nil {
res = &Resource{ res = &Resource{
UUID: uuid, UUID: uuid,
@ -326,6 +337,7 @@ func ResourceRegister(hosts []Host, tenant string) error {
return err return err
} }
} }
res.UUID = uuid
res.Ident = hosts[i].Ident res.Ident = hosts[i].Ident
res.Name = hosts[i].Name res.Name = hosts[i].Name
res.Cate = hosts[i].Cate res.Cate = hosts[i].Cate
@ -343,7 +355,7 @@ func ResourceRegister(hosts []Host, tenant string) error {
} }
res.Extend = string(js) res.Extend = string(js)
err = res.Update("ident", "name", "cate", "extend", "tenant") err = res.Update("uuid", "ident", "name", "cate", "extend", "tenant")
if err != nil { if err != nil {
return err return err
} }

View File

@ -14,6 +14,7 @@ func Config(r *gin.Engine) {
userLogin.POST("/hosts", hostPost) userLogin.POST("/hosts", hostPost)
userLogin.GET("/host/:id", hostGet) userLogin.GET("/host/:id", hostGet)
userLogin.PUT("/hosts/tenant", hostTenantPut) userLogin.PUT("/hosts/tenant", hostTenantPut)
userLogin.PUT("/hosts/node", hostNodePut)
userLogin.PUT("/hosts/back", hostBackPut) userLogin.PUT("/hosts/back", hostBackPut)
userLogin.PUT("/hosts/note", hostNotePut) userLogin.PUT("/hosts/note", hostNotePut)
userLogin.PUT("/hosts/cate", hostCatePut) userLogin.PUT("/hosts/cate", hostCatePut)

View File

@ -146,6 +146,78 @@ func hostTenantPut(c *gin.Context) {
renderMessage(c, err) 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 { type hostNoteForm struct {
Ids []int64 `json:"ids"` Ids []int64 `json:"ids"`
Note string `json:"note"` Note string `json:"note"`