2022-08-18 18:54:21 +08:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2023-03-15 15:58:26 +08:00
|
|
|
"encoding/base64"
|
|
|
|
|
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"
|
2022-08-18 18:54:21 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// @Tags Host
|
|
|
|
// @Summary Create host
|
|
|
|
// @Description 创建主机
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.HostOperate true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
2023-01-05 11:57:03 +08:00
|
|
|
// @Router /hosts [post]
|
2023-01-04 22:31:51 +08:00
|
|
|
// @x-panel-log {"bodyKeys":["name","addr"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"创建主机 [name][addr]","formatEN":"create host [name][addr]"}
|
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
|
|
|
|
}
|
2023-03-15 15:58:26 +08:00
|
|
|
if req.AuthMode == "password" && len(req.Password) != 0 {
|
|
|
|
password, err := base64.StdEncoding.DecodeString(req.Password)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Password = string(password)
|
|
|
|
}
|
|
|
|
if req.AuthMode == "key" && len(req.PrivateKey) != 0 {
|
|
|
|
privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.PrivateKey = string(privateKey)
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:54:21 +08:00
|
|
|
host, err := hostService.Create(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, host)
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// @Tags Host
|
|
|
|
// @Summary Test host conn by info
|
|
|
|
// @Description 测试主机连接
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.HostConnTest true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
2023-01-05 11:57:03 +08:00
|
|
|
// @Router /hosts/test/byinfo [post]
|
2023-01-03 14:37:17 +08:00
|
|
|
func (b *BaseApi) TestByInfo(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
|
|
|
|
}
|
|
|
|
|
2023-04-10 17:56:24 +08:00
|
|
|
connStatus := hostService.TestByInfo(req)
|
|
|
|
helper.SuccessWithData(c, connStatus)
|
2023-01-03 14:37:17 +08:00
|
|
|
}
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// @Tags Host
|
|
|
|
// @Summary Test host conn by host id
|
|
|
|
// @Description 测试主机连接
|
|
|
|
// @Accept json
|
|
|
|
// @Param id path integer true "request"
|
2023-01-05 11:57:03 +08:00
|
|
|
// @Success 200 {boolean} connStatus
|
2023-01-04 22:31:51 +08:00
|
|
|
// @Security ApiKeyAuth
|
2023-01-05 11:57:03 +08:00
|
|
|
// @Router /hosts/test/byid/:id [post]
|
2023-01-03 14:37:17 +08:00
|
|
|
func (b *BaseApi) TestByID(c *gin.Context) {
|
|
|
|
id, err := helper.GetParamID(c)
|
2022-09-01 10:25:38 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-03 14:37:17 +08:00
|
|
|
connStatus := hostService.TestLocalConn(id)
|
|
|
|
helper.SuccessWithData(c, connStatus)
|
2022-09-01 10:25:38 +08:00
|
|
|
}
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// @Tags Host
|
|
|
|
// @Summary Load host tree
|
|
|
|
// @Description 加载主机树
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.SearchForTree true "request"
|
|
|
|
// @Success 200 {anrry} dto.HostTree
|
|
|
|
// @Security ApiKeyAuth
|
2023-03-01 17:43:28 +08:00
|
|
|
// @Router /hosts/tree [post]
|
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
|
|
|
}
|
|
|
|
|
2023-03-01 17:43:28 +08:00
|
|
|
// @Tags Host
|
|
|
|
// @Summary Page host
|
|
|
|
// @Description 获取主机列表分页
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.SearchHostWithPage true "request"
|
|
|
|
// @Success 200 {anrry} dto.HostTree
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /hosts/search [post]
|
|
|
|
func (b *BaseApi) SearchHost(c *gin.Context) {
|
|
|
|
var req dto.SearchHostWithPage
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
total, list, err := hostService.SearchWithPage(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, dto.PageResult{
|
|
|
|
Items: list,
|
|
|
|
Total: total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// @Tags Host
|
|
|
|
// @Summary Load host info
|
|
|
|
// @Description 加载主机信息
|
|
|
|
// @Accept json
|
|
|
|
// @Param id path integer true "request"
|
|
|
|
// @Success 200 {object} dto.HostInfo
|
|
|
|
// @Security ApiKeyAuth
|
2023-01-05 11:57:03 +08:00
|
|
|
// @Router /hosts/:id [get]
|
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)
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// @Tags Host
|
|
|
|
// @Summary Delete host
|
|
|
|
// @Description 删除主机
|
|
|
|
// @Accept json
|
2023-03-01 17:43:28 +08:00
|
|
|
// @Param request body dto.BatchDeleteReq true "request"
|
2023-01-04 22:31:51 +08:00
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
2023-01-05 11:57:03 +08:00
|
|
|
// @Router /hosts/del [post]
|
2023-05-30 15:30:57 +08:00
|
|
|
// @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFuntions":[{"input_column":"id","input_value":"ids","isList":true,"db":"hosts","output_column":"addr","output_value":"addrs"}],"formatZH":"删除主机 [addrs]","formatEN":"delete host [addrs]"}
|
2022-08-18 18:54:21 +08:00
|
|
|
func (b *BaseApi) DeleteHost(c *gin.Context) {
|
2023-03-01 17:43:28 +08:00
|
|
|
var req dto.BatchDeleteReq
|
2022-12-13 18:54:28 +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 {
|
2022-08-18 18:54:21 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-01 17:43:28 +08:00
|
|
|
if err := hostService.Delete(req.Ids); err != nil {
|
2022-08-18 18:54:21 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:31:51 +08:00
|
|
|
// @Tags Host
|
|
|
|
// @Summary Update host
|
|
|
|
// @Description 更新主机
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.HostOperate true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
2023-01-05 11:57:03 +08:00
|
|
|
// @Router /hosts/update [post]
|
2023-01-04 22:31:51 +08:00
|
|
|
// @x-panel-log {"bodyKeys":["name","addr"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"更新主机信息 [name][addr]","formatEN":"update host [name][addr]"}
|
2022-08-18 18:54:21 +08:00
|
|
|
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
|
|
|
|
}
|
2023-03-15 15:58:26 +08:00
|
|
|
if req.AuthMode == "password" && len(req.Password) != 0 {
|
|
|
|
password, err := base64.StdEncoding.DecodeString(req.Password)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Password = string(password)
|
|
|
|
}
|
|
|
|
if req.AuthMode == "key" && len(req.PrivateKey) != 0 {
|
|
|
|
privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.PrivateKey = string(privateKey)
|
|
|
|
}
|
2022-08-18 18:54:21 +08:00
|
|
|
|
|
|
|
upMap := make(map[string]interface{})
|
|
|
|
upMap["name"] = req.Name
|
2023-03-06 17:18:13 +08:00
|
|
|
upMap["group_id"] = req.GroupID
|
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
|
2023-04-10 21:50:24 +08:00
|
|
|
upMap["remember_password"] = req.RememberPassword
|
2023-03-11 17:50:26 +08:00
|
|
|
if len(req.Password) != 0 {
|
|
|
|
upMap["password"] = req.Password
|
|
|
|
}
|
|
|
|
if len(req.PrivateKey) != 0 {
|
|
|
|
upMap["private_key"] = req.PrivateKey
|
2023-04-10 21:50:24 +08:00
|
|
|
upMap["pass_phrase"] = req.PassPhrase
|
2023-03-11 17:50:26 +08:00
|
|
|
}
|
2022-12-29 17:06:04 +08:00
|
|
|
upMap["description"] = req.Description
|
2022-12-13 18:54:28 +08:00
|
|
|
if err := hostService.Update(req.ID, upMap); err != nil {
|
2022-08-18 18:54:21 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
2023-03-01 17:43:28 +08:00
|
|
|
|
|
|
|
// @Tags Host
|
|
|
|
// @Summary Update host group
|
|
|
|
// @Description 切换分组
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.ChangeHostGroup true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
2023-04-07 17:46:10 +08:00
|
|
|
// @Router /hosts/update/group [post]
|
2023-05-30 15:30:57 +08:00
|
|
|
// @x-panel-log {"bodyKeys":["id","group"],"paramKeys":[],"BeforeFuntions":[{"input_column":"id","input_value":"id","isList":false,"db":"hosts","output_column":"addr","output_value":"addr"}],"formatZH":"切换主机[addr]分组 => [group]","formatEN":"change host [addr] group => [group]"}
|
2023-03-01 17:43:28 +08:00
|
|
|
func (b *BaseApi) UpdateHostGroup(c *gin.Context) {
|
|
|
|
var req dto.ChangeHostGroup
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
upMap := make(map[string]interface{})
|
2023-03-06 17:18:13 +08:00
|
|
|
upMap["group_id"] = req.GroupID
|
2023-03-01 17:43:28 +08:00
|
|
|
if err := hostService.Update(req.ID, upMap); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|