Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the bug of the api's BindHostToHostGroup will truncate table ! #333

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions modules/api/app/controller/host/hostgroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ func BindHostToHostGroup(c *gin.Context) {
return
}
tx := db.Falcon.Begin()
if dt := tx.Where("grp_id = ?", hostgroup.ID).Delete(&f.GrpHost{}); dt.Error != nil {
h.JSONR(c, expecstatus, fmt.Sprintf("delete grp_host got error: %v", dt.Error))
dt.Rollback()
return
}
var ids []int64
for _, host := range inputs.Hosts {
ahost := f.Host{Hostname: host}
Expand All @@ -120,6 +115,17 @@ func BindHostToHostGroup(c *gin.Context) {
id = ahost.ID
ids = append(ids, id)
}
//通过hostgroup_id和hostid来查询是否数据存在避免有重复数据。修正了之前插入数据前清表的问题。
grphost_count := []f.GrpHost{}
if dt := tx.Where(&f.GrpHost{GrpID: hostgroup.ID, HostID: id}).Find(&grphost_count); dt.Error != nil {
h.JSONR(c, expecstatus, fmt.Sprintf("select grp_host got error: %s , grp_id: %v, host_id: %v", dt.Error, hostgroup.ID, id))
return
} else {
if len(grphost_count) > 0 {
log.Debugf("The grp_id: %v and host_id: %v are already in the grp_host !", hostgroup.ID, id)
continue
}
}
if dt := tx.Debug().Create(&f.GrpHost{GrpID: hostgroup.ID, HostID: id}); dt.Error != nil {
h.JSONR(c, expecstatus, fmt.Sprintf("create grphost got error: %s , grp_id: %v, host_id: %v", dt.Error, hostgroup.ID, id))
tx.Rollback()
Expand Down