2022-08-18 18:54:21 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-08-30 18:49:07 +08:00
|
|
|
"fmt"
|
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-08-18 18:54:21 +08:00
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HostService struct{}
|
|
|
|
|
|
|
|
type IHostService interface {
|
2022-08-31 23:16:10 +08:00
|
|
|
GetHostInfo(id uint) (*model.Host, error)
|
2022-08-30 18:49:07 +08:00
|
|
|
SearchForTree(search dto.SearchForTree) ([]dto.HostTree, error)
|
2022-08-31 23:16:10 +08:00
|
|
|
Create(hostDto dto.HostOperate) (*dto.HostInfo, error)
|
2022-08-18 18:54:21 +08:00
|
|
|
Update(id uint, upMap map[string]interface{}) error
|
2022-09-01 10:25:38 +08:00
|
|
|
Delete(id uint) error
|
2022-08-18 18:54:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIHostService() IHostService {
|
|
|
|
return &HostService{}
|
|
|
|
}
|
|
|
|
|
2022-08-31 23:16:10 +08:00
|
|
|
func (u *HostService) GetHostInfo(id uint) (*model.Host, error) {
|
2022-08-18 18:54:21 +08:00
|
|
|
host, err := hostRepo.Get(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, constant.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
return &host, err
|
|
|
|
}
|
|
|
|
|
2022-08-30 18:49:07 +08:00
|
|
|
func (u *HostService) SearchForTree(search dto.SearchForTree) ([]dto.HostTree, error) {
|
|
|
|
hosts, err := hostRepo.GetList(hostRepo.WithByInfo(search.Info))
|
2022-08-31 23:16:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
groups, err := groupRepo.GetList()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-08-30 18:49:07 +08:00
|
|
|
}
|
2022-08-31 23:16:10 +08:00
|
|
|
var datas []dto.HostTree
|
|
|
|
for _, group := range groups {
|
|
|
|
var data dto.HostTree
|
|
|
|
data.ID = group.ID + 10000
|
|
|
|
data.Label = group.Name
|
|
|
|
for _, host := range hosts {
|
|
|
|
label := fmt.Sprintf("%s@%s:%d", host.User, host.Addr, host.Port)
|
|
|
|
if host.GroupBelong == group.Name {
|
|
|
|
data.Children = append(data.Children, dto.TreeChild{ID: host.ID, Label: label})
|
|
|
|
}
|
2022-08-18 18:54:21 +08:00
|
|
|
}
|
2022-08-31 23:16:10 +08:00
|
|
|
datas = append(datas, data)
|
2022-08-18 18:54:21 +08:00
|
|
|
}
|
2022-08-31 23:16:10 +08:00
|
|
|
return datas, err
|
2022-08-18 18:54:21 +08:00
|
|
|
}
|
|
|
|
|
2022-12-19 21:32:27 +08:00
|
|
|
func (u *HostService) Create(req dto.HostOperate) (*dto.HostInfo, error) {
|
|
|
|
host, _ := hostRepo.Get(commonRepo.WithByName(req.Name))
|
2022-08-18 18:54:21 +08:00
|
|
|
if host.ID != 0 {
|
|
|
|
return nil, constant.ErrRecordExist
|
|
|
|
}
|
2022-12-19 21:32:27 +08:00
|
|
|
if err := copier.Copy(&host, &req); err != nil {
|
2022-08-18 18:54:21 +08:00
|
|
|
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
2022-12-19 21:32:27 +08:00
|
|
|
var sameHostID uint
|
|
|
|
if req.Addr == "127.0.0.1" {
|
|
|
|
hostSame, _ := hostRepo.Get(hostRepo.WithByAddr(req.Addr))
|
|
|
|
sameHostID = hostSame.ID
|
|
|
|
} else {
|
|
|
|
hostSame, _ := hostRepo.Get(hostRepo.WithByAddr(req.Addr), hostRepo.WithByUser(req.User), hostRepo.WithByPort(req.Port))
|
|
|
|
sameHostID = hostSame.ID
|
|
|
|
}
|
|
|
|
if sameHostID != 0 {
|
|
|
|
host.ID = sameHostID
|
|
|
|
upMap := make(map[string]interface{})
|
|
|
|
upMap["name"] = req.Name
|
|
|
|
upMap["group_belong"] = req.GroupBelong
|
|
|
|
upMap["addr"] = req.Addr
|
|
|
|
upMap["port"] = req.Port
|
|
|
|
upMap["user"] = req.User
|
|
|
|
upMap["auth_mode"] = req.AuthMode
|
|
|
|
upMap["password"] = req.Password
|
|
|
|
upMap["private_key"] = req.PrivateKey
|
|
|
|
if err := hostRepo.Update(sameHostID, upMap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var hostinfo dto.HostInfo
|
|
|
|
if err := copier.Copy(&hostinfo, &host); err != nil {
|
|
|
|
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
return &hostinfo, nil
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:54:21 +08:00
|
|
|
if err := hostRepo.Create(&host); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var hostinfo dto.HostInfo
|
|
|
|
if err := copier.Copy(&hostinfo, &host); err != nil {
|
|
|
|
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
return &hostinfo, nil
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:25:38 +08:00
|
|
|
func (u *HostService) Delete(id uint) error {
|
|
|
|
host, _ := hostRepo.Get(commonRepo.WithByID(id))
|
|
|
|
if host.ID == 0 {
|
|
|
|
return constant.ErrRecordNotFound
|
2022-08-18 18:54:21 +08:00
|
|
|
}
|
2022-09-01 10:25:38 +08:00
|
|
|
return hostRepo.Delete(commonRepo.WithByID(id))
|
2022-08-18 18:54:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *HostService) Update(id uint, upMap map[string]interface{}) error {
|
|
|
|
return hostRepo.Update(id, upMap)
|
|
|
|
}
|