2022-08-18 18:54:21 +08:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/copier"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/ssh"
|
2022-08-18 18:54:21 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2022-08-30 18:49:07 +08:00
|
|
|
func (b *BaseApi) CreateHost(c *gin.Context) {
|
2022-08-31 23:16:10 +08:00
|
|
|
var req dto.HostOperate
|
2022-08-18 18:54:21 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
host, err := hostService.Create(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, host)
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:25:38 +08:00
|
|
|
func (b *BaseApi) TestConn(c *gin.Context) {
|
2022-09-01 16:52:58 +08:00
|
|
|
var req dto.HostConnTest
|
2022-09-01 10:25:38 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var connInfo ssh.ConnInfo
|
|
|
|
if err := copier.Copy(&connInfo, &req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, constant.ErrStructTransform)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
client, err := connInfo.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2022-08-30 18:49:07 +08:00
|
|
|
func (b *BaseApi) HostTree(c *gin.Context) {
|
|
|
|
var req dto.SearchForTree
|
2022-08-18 18:54:21 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-30 18:49:07 +08:00
|
|
|
data, err := hostService.SearchForTree(req)
|
2022-08-18 18:54:21 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-30 18:49:07 +08:00
|
|
|
helper.SuccessWithData(c, data)
|
2022-08-18 18:54:21 +08:00
|
|
|
}
|
|
|
|
|
2022-08-31 23:16:10 +08:00
|
|
|
func (b *BaseApi) GetHostInfo(c *gin.Context) {
|
|
|
|
id, err := helper.GetParamID(c)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
host, err := hostService.GetHostInfo(id)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var hostDto dto.HostInfo
|
|
|
|
if err := copier.Copy(&hostDto, host); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, hostDto)
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:54:21 +08:00
|
|
|
func (b *BaseApi) DeleteHost(c *gin.Context) {
|
2022-09-01 10:25:38 +08:00
|
|
|
id, err := helper.GetParamID(c)
|
|
|
|
if err != nil {
|
2022-08-18 18:54:21 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:25:38 +08:00
|
|
|
if err := hostService.Delete(id); err != nil {
|
2022-08-18 18:54:21 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseApi) UpdateHost(c *gin.Context) {
|
2022-08-31 23:16:10 +08:00
|
|
|
var req dto.HostOperate
|
2022-08-18 18:54:21 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id, err := helper.GetParamID(c)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
upMap := make(map[string]interface{})
|
|
|
|
upMap["name"] = req.Name
|
2022-08-31 23:16:10 +08:00
|
|
|
upMap["group_belong"] = req.GroupBelong
|
2022-08-18 18:54:21 +08:00
|
|
|
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 := hostService.Update(id, upMap); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|