feat: 新增网元端公共配置文件读写接口

This commit is contained in:
TsMask
2024-04-16 19:40:29 +08:00
parent 4c37968925
commit 07f6b9c31a
9 changed files with 145 additions and 5 deletions

View File

@@ -184,7 +184,7 @@ func (s *NeInfoController) ConfigFileWrite(c *gin.Context) {
Content any `json:"content" binding:"required"` // 内容
Sync bool `json:"sync"` // 同步到网元
}
if err := c.ShouldBindJSON(&body); err != nil {
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -204,6 +204,38 @@ func (s *NeInfoController) ConfigFileWrite(c *gin.Context) {
c.JSON(200, result.Ok(nil))
}
// 网元端公共配置文件读取
//
// GET /para5GFile
func (s *NeInfoController) Para5GFileRead(c *gin.Context) {
fileType := c.Query("fileType")
data := s.neInfoService.NeConfPara5GRead(fileType)
c.JSON(200, result.OkData(data))
}
// 网元端公共配置文件写入
//
// PUT /para5GFile
func (s *NeInfoController) Para5GFileWrite(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
FileType string `json:"fileType" binding:"oneof='' txt json yaml yml"` // 解析内容数据到对应文件类型
Content any `json:"content" binding:"required"` // 内容
SyncNE []string `json:"syncNe"` // 同步到网元
}
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
err := s.neInfoService.NeConfPara5GWirte(body.FileType, body.Content, body.SyncNE)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
c.JSON(200, result.Ok(nil))
}
// 网元信息列表
//
// GET /list

View File

@@ -64,8 +64,18 @@ func Setup(router *gin.Engine) {
)
neInfoGroup.PUT("/configFile",
middleware.PreAuthorize(nil),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_OTHER)),
controller.NewNeInfo.ConfigFileWrite,
)
neInfoGroup.GET("/para5GFile",
middleware.PreAuthorize(nil),
controller.NewNeInfo.Para5GFileRead,
)
neInfoGroup.PUT("/para5GFile",
middleware.PreAuthorize(nil),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_OTHER)),
controller.NewNeInfo.Para5GFileWrite,
)
neInfoGroup.GET("/list",
middleware.PreAuthorize(nil),
controller.NewNeInfo.List,

View File

@@ -59,4 +59,12 @@ type INeInfo interface {
// NeConfigFileWirte 网元配置文件写入 content内容 sync同步到网元端
NeConfigFileWirte(neInfo model.NeInfo, filePath, fileType string, content any, sync bool) error
// NeConfPara5GRead 网元公共配置文件读取
//
// 返回 string map[string]any
NeConfPara5GRead(fileType string) any
// NeConfPara5GWirte 网元公共配置文件写入 content内容 syncNE同步到网元端NeType@NeId
NeConfPara5GWirte(fileType string, content any, syncNE []string) error
}

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
@@ -470,7 +471,7 @@ func (r *NeInfoImpl) NeConfigFileWirte(neInfo model.NeInfo, filePath, fileType s
// 同步到网元端
if sync {
// 网元主机的SSH客户端
sshClient, err := NewNeInfoImpl.NeRunSSHclient(neInfo.NeType, neInfo.NeId)
sshClient, err := r.NeRunSSHclient(neInfo.NeType, neInfo.NeId)
if err != nil {
return err
}
@@ -494,3 +495,89 @@ func (r *NeInfoImpl) NeConfigFileWirte(neInfo model.NeInfo, filePath, fileType s
return nil
}
// NeConfPara5GRead 网元公共配置文件读取
//
// 返回 string map[string]any
func (r *NeInfoImpl) NeConfPara5GRead(fileType string) any {
// 网管本地路径
omcFilePath := "/usr/local/omc/etc/para5G.yaml"
if runtime.GOOS == "windows" {
omcFilePath = fmt.Sprintf("C:%s", omcFilePath)
}
// 读取文件内容
bytes, err := os.ReadFile(omcFilePath)
if err != nil {
logger.Warnf("NeConfPara5GRead ReadFile => %s", err.Error())
return "read file error"
}
content := string(bytes)
if fileType == "" || fileType == "txt" {
return content
}
// 序列化Map
mapData, err := parse.ConvertConfigToMap(fileType, content)
if err != nil {
logger.Warnf("NeConfPara5GRead ConvertConfigToMap => %s", err.Error())
return "content convert type error"
}
return mapData
}
// NeConfPara5GWirte 网元公共配置文件写入 content内容 syncNE同步到网元端NeType@NeId
func (r *NeInfoImpl) NeConfPara5GWirte(fileType string, content any, syncNE []string) error {
// 网管本地路径
omcFilePath := "/usr/local/omc/etc/para5G.yaml"
if runtime.GOOS == "windows" {
omcFilePath = fmt.Sprintf("C:%s", omcFilePath)
}
var err error
if fileType == "" || fileType == "txt" {
err = parse.ConvertConfigToFile(fileType, omcFilePath, content)
}
if fileType == "json" || fileType == "yaml" || fileType == "yml" {
err = parse.ConvertConfigToFile(fileType, omcFilePath, content)
}
if err != nil {
return fmt.Errorf("please check if the file exists or write permissions")
}
// 同步到网元端
if len(syncNE) > 0 {
errMsg := []string{}
for _, neTI := range syncNE {
ti := strings.SplitN(neTI, "@", 2)
// 网元主机的SSH客户端
sshClient, err := r.NeRunSSHclient(ti[0], ti[1])
if err != nil {
errMsg = append(errMsg, fmt.Sprintf("%s : %s", ti, err.Error()))
continue
}
defer sshClient.Close()
// 网元主机的SSH客户端进行文件传输
sftpClient, err := sshClient.NewClientSFTP()
if err != nil {
errMsg = append(errMsg, fmt.Sprintf("%s : %s", ti, err.Error()))
continue
}
defer sftpClient.Close()
// 网元端配置路径
neFilePath := "/usr/local/etc/conf/para5G.yaml"
neFileDir := filepath.Dir(neFilePath)
// 修改网元文件权限
sshClient.RunCMD(fmt.Sprintf("sudo mkdir -p %s && sudo chmod o+w %s && sudo chmod o+w %s", neFileDir, neFileDir, neFilePath))
// 复制到网元进行覆盖
if err = sftpClient.CopyFileLocalToRemote(omcFilePath, neFilePath); err != nil {
errMsg = append(errMsg, fmt.Sprintf("%s : please check if scp remote copy is allowed", ti))
continue
}
}
if len(errMsg) > 0 {
return fmt.Errorf(strings.Join(errMsg, "\r\n"))
}
}
return nil
}