feat: OMC配置修改通知参数
This commit is contained in:
30
src/framework/config/expand.go
Normal file
30
src/framework/config/expand.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SedReplace 替换文件内容,文件来自外部文件配置config传入
|
||||||
|
//
|
||||||
|
// sed 's/port: [0-9]\+ # trace port/port: 6964 # trace port/' /usr/local/etc/omc/omc.yaml
|
||||||
|
func SedReplace(pattern, replacement string) error {
|
||||||
|
// 外部文件配置
|
||||||
|
externalConfig := conf.GetString("config")
|
||||||
|
if externalConfig == "" {
|
||||||
|
return fmt.Errorf("config file path not found")
|
||||||
|
}
|
||||||
|
// 读取文件内容
|
||||||
|
data, err := os.ReadFile(externalConfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义正则表达式
|
||||||
|
re := regexp.MustCompile(pattern)
|
||||||
|
// 使用正则替换,将匹配到的部分替换为新的内容
|
||||||
|
replacedData := re.ReplaceAll(data, []byte(replacement))
|
||||||
|
// 写回文件
|
||||||
|
return os.WriteFile(externalConfig, replacedData, 0644)
|
||||||
|
}
|
||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
cm_omc "be.ems/features/cm/omc"
|
|
||||||
"be.ems/src/framework/i18n"
|
"be.ems/src/framework/i18n"
|
||||||
"be.ems/src/framework/reqctx"
|
"be.ems/src/framework/reqctx"
|
||||||
"be.ems/src/framework/resp"
|
"be.ems/src/framework/resp"
|
||||||
@@ -215,16 +214,6 @@ func (s NeConfigController) DataInfo(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if query.NeType == "OMC" {
|
if query.NeType == "OMC" {
|
||||||
if query.ParamName == "alarmEmailForward" || query.ParamName == "alarmSMSForward" {
|
|
||||||
var o *cm_omc.ConfigOMC
|
|
||||||
resData, err := o.Query(query.ParamName)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.JSON(200, resp.OkData(resData))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
resData := s.neConfigService.GetOMCYaml(query.ParamName)
|
resData := s.neConfigService.GetOMCYaml(query.ParamName)
|
||||||
c.JSON(200, resp.OkData(resData))
|
c.JSON(200, resp.OkData(resData))
|
||||||
return
|
return
|
||||||
@@ -274,16 +263,6 @@ func (s NeConfigController) DataEdit(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if body.NeType == "OMC" {
|
if body.NeType == "OMC" {
|
||||||
if body.ParamName == "alarmEmailForward" || body.ParamName == "alarmSMSForward" {
|
|
||||||
var o *cm_omc.ConfigOMC
|
|
||||||
resData, err := o.Modify(body.ParamName, body.ParamData)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.JSON(200, resp.OkData(resData))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err := s.neConfigService.ModifyOMCYaml(body.ParamName, body.Loc, body.ParamData)
|
err := s.neConfigService.ModifyOMCYaml(body.ParamName, body.Loc, body.ParamData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||||
|
|||||||
@@ -2,15 +2,32 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"be.ems/src/framework/cmd"
|
|
||||||
"be.ems/src/framework/config"
|
"be.ems/src/framework/config"
|
||||||
|
"github.com/tsmask/go-oam/src/framework/utils/parse"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetOMCYaml 获取OMC网元配置文件 /usr/local/etc/omc/omc.yaml
|
// GetOMCYaml 获取OMC网元配置文件
|
||||||
func (r NeConfig) GetOMCYaml(paramName string) []map[string]any {
|
func (r NeConfig) GetOMCYaml(paramName string) []map[string]any {
|
||||||
|
if paramName == "notificationEmail" {
|
||||||
|
notificationEmailData := config.Get("notification.email").(map[string]any)
|
||||||
|
for k, v := range notificationEmailData {
|
||||||
|
if k == "password" && v != nil {
|
||||||
|
notificationEmailData[k] = parse.SafeContent(fmt.Sprint(v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []map[string]any{notificationEmailData}
|
||||||
|
}
|
||||||
|
if paramName == "notificationSMSC" {
|
||||||
|
notificationSMSCData := config.Get("notification.smsc").(map[string]any)
|
||||||
|
for k, v := range notificationSMSCData {
|
||||||
|
if k == "password" && v != nil {
|
||||||
|
notificationSMSCData[k] = parse.SafeContent(fmt.Sprint(v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []map[string]any{notificationSMSCData}
|
||||||
|
}
|
||||||
if paramName == "trace" {
|
if paramName == "trace" {
|
||||||
traceData := config.Get("trace").(map[string]any)
|
traceData := config.Get("trace").(map[string]any)
|
||||||
return []map[string]any{traceData}
|
return []map[string]any{traceData}
|
||||||
@@ -18,30 +35,113 @@ func (r NeConfig) GetOMCYaml(paramName string) []map[string]any {
|
|||||||
return []map[string]any{}
|
return []map[string]any{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ModifyOMCYaml 修改OMC网元配置文件 /usr/local/etc/omc/omc.yaml
|
// ModifyOMCYaml 修改OMC网元配置文件
|
||||||
func (r NeConfig) ModifyOMCYaml(paramName, loc string, paramData any) error {
|
func (r NeConfig) ModifyOMCYaml(paramName, loc string, paramData any) error {
|
||||||
neConfig := r.FindByNeTypeAndParamName("OMC", paramName)
|
neConfig := r.FindByNeTypeAndParamName("OMC", paramName)
|
||||||
if neConfig.ParamType == "list" {
|
if neConfig.ParamType == "list" {
|
||||||
|
if paramName == "notificationEmail" {
|
||||||
|
notificationEmailData := config.Get("notification.email").(map[string]any)
|
||||||
|
for k, v := range paramData.(map[string]any) {
|
||||||
|
keyLower := strings.ToLower(k)
|
||||||
|
// 改程序内
|
||||||
|
notificationEmailData[keyLower] = v
|
||||||
|
// 改文件
|
||||||
|
switch keyLower {
|
||||||
|
case "enabled":
|
||||||
|
pattern := `enabled: (true|false) # email enable`
|
||||||
|
replacement := fmt.Sprintf(`enabled: %v # email enable`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
case "list":
|
||||||
|
pattern := `list: ".*" # toEmail`
|
||||||
|
replacement := fmt.Sprintf(`list: "%v" # toEmail`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
case "title":
|
||||||
|
pattern := `title: ".*" # email title`
|
||||||
|
replacement := fmt.Sprintf(`title: "%v" # email title`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
case "smtp":
|
||||||
|
pattern := `smtp: ".*" # email smtp`
|
||||||
|
replacement := fmt.Sprintf(`smtp: "%v" # email smtp`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
case "port":
|
||||||
|
pattern := `port: [0-9]+ # email port`
|
||||||
|
replacement := fmt.Sprintf(`port: %v # email port`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
case "user":
|
||||||
|
pattern := `user: ".*" # email user`
|
||||||
|
replacement := fmt.Sprintf(`user: "%v" # email user`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
case "password":
|
||||||
|
pattern := `password: ".*" # email password`
|
||||||
|
replacement := fmt.Sprintf(`password: "%v" # email password`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if paramName == "notificationSMSC" {
|
||||||
|
notificationSMSCData := config.Get("notification.smsc").(map[string]any)
|
||||||
|
for k, v := range paramData.(map[string]any) {
|
||||||
|
keyLower := strings.ToLower(k)
|
||||||
|
// 改程序内
|
||||||
|
notificationSMSCData[keyLower] = v
|
||||||
|
// 改文件
|
||||||
|
switch keyLower {
|
||||||
|
case "enabled":
|
||||||
|
pattern := `enabled: (true|false) # smsc enable`
|
||||||
|
replacement := fmt.Sprintf(`enabled: %v # smsc enable`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
case "list":
|
||||||
|
pattern := `list: ".*" # toMobile`
|
||||||
|
replacement := fmt.Sprintf(`list: "%v" # toMobile`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
case "addr":
|
||||||
|
pattern := `addr: ".*" # smsc addr`
|
||||||
|
replacement := fmt.Sprintf(`addr: "%v" # smsc addr`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
case "systemid":
|
||||||
|
pattern := `systemid: ".*" # smsc system id`
|
||||||
|
replacement := fmt.Sprintf(`systemid: "%v" # smsc system id`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
case "systemtype":
|
||||||
|
pattern := `systemtype: ".*" # smsc system type`
|
||||||
|
replacement := fmt.Sprintf(`systemtype: "%v" # smsc system type`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
case "password":
|
||||||
|
pattern := `password: ".*" # smsc password`
|
||||||
|
replacement := fmt.Sprintf(`password: "%v" # smsc password`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
case "coding":
|
||||||
|
pattern := `coding: [0-9]+ # smsc codingMap`
|
||||||
|
replacement := fmt.Sprintf(`coding: %v # smsc codingMap`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
case "servicenumber":
|
||||||
|
pattern := `servicenumber: ".*" # smsc service number`
|
||||||
|
replacement := fmt.Sprintf(`servicenumber: "%v" # smsc service number`, v)
|
||||||
|
return config.SedReplace(pattern, replacement)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if paramName == "trace" {
|
if paramName == "trace" {
|
||||||
configPath := fmt.Sprint(config.Get("config")) // 获取配置文件路径
|
traceData := config.Get("trace").(map[string]any)
|
||||||
paramDataMap := paramData.(map[string]any)
|
for k, v := range paramData.(map[string]any) {
|
||||||
for k, v := range paramDataMap {
|
keyLower := strings.ToLower(k)
|
||||||
config.Set(fmt.Sprintf("trace.%s", strings.ToLower(k)), v)
|
// 改程序内
|
||||||
if runtime.GOOS == "windows" {
|
traceData[keyLower] = v
|
||||||
continue // Windows系统不支持sed命令
|
// 改文件
|
||||||
}
|
switch keyLower {
|
||||||
// 修改参数较少,直接命令改文件内容
|
case "enabled":
|
||||||
if k == "enabled" {
|
pattern := `enabled: (true|false) # trace enabled`
|
||||||
// sed 's/enabled: \(true\|false\) # trace enabled/enabled: true # trace enabled/' /usr/local/etc/omc/omc.yaml
|
replacement := fmt.Sprintf(`enabled: %v # trace enabled`, v)
|
||||||
cmd.Execf("sed -i 's/enabled: \\(true\\|false\\) # trace enabled/enabled: %v # trace enabled/' %s", v, configPath)
|
return config.SedReplace(pattern, replacement)
|
||||||
}
|
case "host":
|
||||||
if k == "host" {
|
pattern := `host: ".*" # trace host`
|
||||||
// sed 's/host: ".*" # trace host/host: "127.2.2.2" # trace host/' /usr/local/etc/omc/omc.yaml
|
replacement := fmt.Sprintf(`host: "%v" # trace host`, v)
|
||||||
cmd.Execf("sed -i 's/host: \".*\" # trace host/host: \"%v\" # trace host/' %s", v, configPath)
|
return config.SedReplace(pattern, replacement)
|
||||||
}
|
case "port":
|
||||||
if k == "port" {
|
pattern := `port: [0-9]+ # trace port`
|
||||||
// sed 's/port: [0-9]\+ # trace port/port: 6964 # trace port/' /usr/local/etc/omc/omc.yaml
|
replacement := fmt.Sprintf(`port: %v # trace port`, v)
|
||||||
cmd.Execf("sed -i 's/port: [0-9]\\+ # trace port/port: %v # trace port/' %s", v, configPath)
|
return config.SedReplace(pattern, replacement)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 重开跟踪任务信令数据通道UDP
|
// 重开跟踪任务信令数据通道UDP
|
||||||
|
|||||||
Reference in New Issue
Block a user