Skip to content

Commit ff32281

Browse files
authored
fix: Modify the logic for adding hosts (#7976)
1 parent 4087a7a commit ff32281

File tree

1 file changed

+8
-31
lines changed

1 file changed

+8
-31
lines changed

core/app/service/host.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ func (u *HostService) SearchForTree(search dto.SearchForTree) ([]dto.HostTree, e
231231
}
232232

233233
func (u *HostService) Create(req dto.HostOperate) (*dto.HostInfo, error) {
234+
if req.Name == "local" {
235+
return nil, buserr.New("ErrRecordExist")
236+
}
237+
hostItem, _ := hostRepo.Get(hostRepo.WithByAddr(req.Addr), hostRepo.WithByUser(req.User), hostRepo.WithByPort(req.Port))
238+
if hostItem.ID != 0 {
239+
return nil, buserr.New("ErrRecordExist")
240+
}
241+
234242
var err error
235243
if len(req.Password) != 0 && req.AuthMode == "password" {
236244
req.Password, err = u.EncryptHost(req.Password)
@@ -265,37 +273,6 @@ func (u *HostService) Create(req dto.HostOperate) (*dto.HostInfo, error) {
265273
host.GroupID = group.ID
266274
req.GroupID = group.ID
267275
}
268-
var sameHostID uint
269-
if req.Name == "local" {
270-
hostSame, _ := hostRepo.Get(repo.WithByName("local"))
271-
sameHostID = hostSame.ID
272-
} else {
273-
hostSame, _ := hostRepo.Get(hostRepo.WithByAddr(req.Addr), hostRepo.WithByUser(req.User), hostRepo.WithByPort(req.Port))
274-
sameHostID = hostSame.ID
275-
}
276-
if sameHostID != 0 {
277-
host.ID = sameHostID
278-
upMap := make(map[string]interface{})
279-
upMap["name"] = req.Name
280-
upMap["group_id"] = req.GroupID
281-
upMap["addr"] = req.Addr
282-
upMap["port"] = req.Port
283-
upMap["user"] = req.User
284-
upMap["auth_mode"] = req.AuthMode
285-
upMap["password"] = req.Password
286-
upMap["private_key"] = req.PrivateKey
287-
upMap["pass_phrase"] = req.PassPhrase
288-
upMap["remember_password"] = req.RememberPassword
289-
upMap["description"] = req.Description
290-
if err := hostRepo.Update(sameHostID, upMap); err != nil {
291-
return nil, err
292-
}
293-
var hostinfo dto.HostInfo
294-
if err := copier.Copy(&hostinfo, &host); err != nil {
295-
return nil, buserr.WithDetail("ErrStructTransform", err.Error(), nil)
296-
}
297-
return &hostinfo, nil
298-
}
299276

300277
if err := hostRepo.Create(&host); err != nil {
301278
return nil, err

0 commit comments

Comments
 (0)