feat: 更新多个模块以支持新的数据结构和日志格式
This commit is contained in:
@@ -1,19 +1,20 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"be.ems/src/framework/database/redis"
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/redis"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/ssh"
|
||||
"be.ems/src/framework/telnet"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/framework/utils/ssh"
|
||||
"be.ems/src/framework/vo/result"
|
||||
"be.ems/src/modules/network_element/model"
|
||||
neService "be.ems/src/modules/network_element/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 NeHostController 结构体
|
||||
@@ -31,11 +32,10 @@ type NeHostController struct {
|
||||
// 网元主机列表
|
||||
//
|
||||
// GET /list
|
||||
func (s *NeHostController) List(c *gin.Context) {
|
||||
querys := ctx.QueryMap(c)
|
||||
data := s.neHostService.SelectPage(querys)
|
||||
func (s NeHostController) List(c *gin.Context) {
|
||||
query := reqctx.QueryMap(c)
|
||||
rows, total := s.neHostService.FindByPage(query)
|
||||
|
||||
rows := data["rows"].([]model.NeHost)
|
||||
arr := &rows
|
||||
for i := range *arr {
|
||||
(*arr)[i].Password = "-"
|
||||
@@ -43,129 +43,139 @@ func (s *NeHostController) List(c *gin.Context) {
|
||||
(*arr)[i].PassPhrase = "-"
|
||||
}
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
c.JSON(200, resp.OkData(map[string]any{"total": total, "rows": rows}))
|
||||
}
|
||||
|
||||
// 网元主机信息
|
||||
//
|
||||
// GET /:hostId
|
||||
func (s *NeHostController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
hostId := c.Param("hostId")
|
||||
if hostId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
// GET /:id
|
||||
func (s NeHostController) Info(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
id := parse.Number(c.Param("id"))
|
||||
if id <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: id is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
neHost := s.neHostService.SelectById(hostId)
|
||||
if neHost.HostID != hostId {
|
||||
neHost := s.neHostService.FindById(id)
|
||||
if neHost.ID != id {
|
||||
// 没有可访问主机信息数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.noData")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, result.OkData(neHost))
|
||||
c.JSON(200, resp.OkData(neHost))
|
||||
}
|
||||
|
||||
// 网元主机新增
|
||||
//
|
||||
// POST /
|
||||
func (s *NeHostController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
func (s NeHostController) Add(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.NeHost
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.HostID != "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
if body.ID == 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: id not is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
if body.GroupID == "1" {
|
||||
// 主机信息操作【%s】失败,禁止操作网元
|
||||
msg := i18n.TKey(language, "neHost.banNE")
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueHost := s.neHostService.CheckUniqueHostTitle(body.GroupID, body.Title, body.HostType, "")
|
||||
uniqueHost := s.neHostService.CheckUniqueHostTitle(body.GroupID, body.Title, body.HostType, 0)
|
||||
if !uniqueHost {
|
||||
// 主机信息操作【%s】失败,同组内名称已存在
|
||||
msg := i18n.TTemplate(language, "neHost.errKeyExists", map[string]any{"name": body.Title})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||
body.CreateBy = reqctx.LoginUserToUserName(c)
|
||||
insertId := s.neHostService.Insert(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
if insertId > 0 {
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 网元主机修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *NeHostController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
func (s NeHostController) Edit(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.NeHost
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.HostID == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
if body.ID <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: id is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueHost := s.neHostService.CheckUniqueHostTitle(body.GroupID, body.Title, body.HostType, body.HostID)
|
||||
uniqueHost := s.neHostService.CheckUniqueHostTitle(body.GroupID, body.Title, body.HostType, body.ID)
|
||||
if !uniqueHost {
|
||||
// 主机信息操作【%s】失败,同组内名称已存在
|
||||
msg := i18n.TTemplate(language, "neHost.errKeyExists", map[string]any{"name": body.Title})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
neHost := s.neHostService.SelectById(body.HostID)
|
||||
if neHost.HostID != body.HostID {
|
||||
neHost := s.neHostService.FindById(body.ID)
|
||||
if neHost.ID != body.ID {
|
||||
// 没有可访问主机信息数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.noData")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
body.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rows := s.neHostService.Update(body)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 网元主机删除
|
||||
//
|
||||
// DELETE /:hostIds
|
||||
func (s *NeHostController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
hostIds := c.Param("hostIds")
|
||||
if hostIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
// DELETE /:id
|
||||
func (s NeHostController) Remove(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: id is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(hostIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
uniqueIDs := parse.RemoveDuplicatesToArray(id, ",")
|
||||
// 转换成int64数组类型
|
||||
ids := make([]int64, 0)
|
||||
for _, v := range uniqueIDs {
|
||||
ids = append(ids, parse.Number(v))
|
||||
}
|
||||
rows, err := s.neHostService.DeleteByIds(uniqueIDs)
|
||||
|
||||
rows, err := s.neHostService.DeleteByIds(ids)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, err.Error())))
|
||||
return
|
||||
}
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
c.JSON(200, resp.OkMsg(msg))
|
||||
}
|
||||
|
||||
// 网元主机测试连接
|
||||
@@ -181,11 +191,12 @@ func (s *NeHostController) Remove(c *gin.Context) {
|
||||
// @Summary Network element host test connection
|
||||
// @Description Network element host test connection
|
||||
// @Router /ne/host/test [post]
|
||||
func (s *NeHostController) Test(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
func (s NeHostController) Test(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.NeHost
|
||||
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -201,11 +212,11 @@ func (s *NeHostController) Test(c *gin.Context) {
|
||||
}
|
||||
if err != nil {
|
||||
// 连接主机失败,请检查连接参数后重试
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -216,16 +227,16 @@ func (s *NeHostController) Test(c *gin.Context) {
|
||||
client, err := connTelnet.NewClient()
|
||||
if err != nil {
|
||||
// 连接主机失败,请检查连接参数后重试
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
// 是否有终止符
|
||||
if strings.HasSuffix(client.LastResult, ">") || strings.HasSuffix(client.LastResult, "> ") || strings.HasSuffix(client.LastResult, "# ") {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
} else {
|
||||
// 连接主机失败,请检查连接参数后重试
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -237,11 +248,11 @@ func (s *NeHostController) Test(c *gin.Context) {
|
||||
client, err := connRedis.NewClient()
|
||||
if err != nil {
|
||||
// 连接主机失败,请检查连接参数后重试
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -259,22 +270,23 @@ func (s *NeHostController) Test(c *gin.Context) {
|
||||
// @Summary The network element host sends the command
|
||||
// @Description The network element host sends the command
|
||||
// @Router /ne/host/cmd [post]
|
||||
func (s *NeHostController) Cmd(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
func (s NeHostController) Cmd(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
HostID string `json:"hostId" binding:"required"` // 主机ID
|
||||
Cmd string `json:"cmd" binding:"required"` // 执行命令
|
||||
ID int64 `json:"id" binding:"required"` // 主机ID
|
||||
Cmd string `json:"cmd" binding:"required"` // 执行命令
|
||||
}
|
||||
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
neHost := s.neHostService.SelectById(body.HostID)
|
||||
if neHost.HostID != body.HostID {
|
||||
neHost := s.neHostService.FindById(body.ID)
|
||||
if neHost.ID != body.ID {
|
||||
// 没有可访问主机信息数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.noData")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -290,7 +302,7 @@ func (s *NeHostController) Cmd(c *gin.Context) {
|
||||
}
|
||||
if err != nil {
|
||||
// 连接主机失败,请检查连接参数后重试
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
@@ -298,10 +310,10 @@ func (s *NeHostController) Cmd(c *gin.Context) {
|
||||
// 执行命令
|
||||
output, err := client.RunCMD(body.Cmd)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.OkData(output))
|
||||
c.JSON(200, resp.OkData(output))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -312,7 +324,7 @@ func (s *NeHostController) Cmd(c *gin.Context) {
|
||||
client, err := connTelnet.NewClient()
|
||||
if err != nil {
|
||||
// 连接主机失败,请检查连接参数后重试
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
@@ -320,10 +332,10 @@ func (s *NeHostController) Cmd(c *gin.Context) {
|
||||
// 执行命令
|
||||
output, err := client.RunCMD(body.Cmd)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.OkData(output))
|
||||
c.JSON(200, resp.OkData(output))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -341,11 +353,12 @@ func (s *NeHostController) Cmd(c *gin.Context) {
|
||||
// @Summary Checking the server environment by SSH method of Net Element Hosting
|
||||
// @Description Checking the server environment by SSH method of Net Element Hosting
|
||||
// @Router /ne/host/checkBySSH [post]
|
||||
func (s *NeHostController) CheckBySSH(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
func (s NeHostController) CheckBySSH(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.NeHost
|
||||
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -361,7 +374,7 @@ func (s *NeHostController) CheckBySSH(c *gin.Context) {
|
||||
}
|
||||
if err != nil {
|
||||
// 连接主机失败,请检查连接参数后重试
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
@@ -381,7 +394,7 @@ func (s *NeHostController) CheckBySSH(c *gin.Context) {
|
||||
// 执行命令 检查系统环境
|
||||
output, err := client.RunCMD("uname -snrm && cat /etc/os-release | grep PRETTY_NAME")
|
||||
if err != nil {
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
return
|
||||
}
|
||||
output = strings.TrimSuffix(output, "\n")
|
||||
@@ -426,7 +439,7 @@ func (s *NeHostController) CheckBySSH(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
}
|
||||
|
||||
// 网元主机SSH方式授权免密发送
|
||||
@@ -442,11 +455,16 @@ func (s *NeHostController) CheckBySSH(c *gin.Context) {
|
||||
// @Summary Network element host SSH method of authorization for password-free sending
|
||||
// @Description Network element host SSH method of authorization for password-free sending
|
||||
// @Router /ne/host/authorizedBySSH [post]
|
||||
func (s *NeHostController) AuthorizedBySSH(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
func (s NeHostController) AuthorizedBySSH(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.NeHost
|
||||
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil || body.AuthMode == "2" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
if body.AuthMode == "2" {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: auth mode not equals 2"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -464,7 +482,7 @@ func (s *NeHostController) AuthorizedBySSH(c *gin.Context) {
|
||||
}
|
||||
if sshLink {
|
||||
// 连接主机成功,无需重复免密授权认证
|
||||
c.JSON(200, result.OkMsg(i18n.TKey(language, "neHost.okBySSHLink")))
|
||||
c.JSON(200, resp.OkMsg(i18n.TKey(language, "neHost.okBySSHLink")))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -474,7 +492,7 @@ func (s *NeHostController) AuthorizedBySSH(c *gin.Context) {
|
||||
client, err := connSSH.NewClient()
|
||||
if err != nil {
|
||||
// 连接主机失败,请检查连接参数后重试
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
@@ -482,8 +500,8 @@ func (s *NeHostController) AuthorizedBySSH(c *gin.Context) {
|
||||
// 发送密钥
|
||||
err = client.SendToAuthorizedKeys()
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user