feat: 网元主机支持redis配置项
This commit is contained in:
61
src/framework/redis/conn.go
Normal file
61
src/framework/redis/conn.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// ConnRedis 连接redis对象
|
||||
type ConnRedis struct {
|
||||
Addr string `json:"addr"` // 地址
|
||||
Port int64 `json:"port"` // 端口
|
||||
User string `json:"user"` // 用户名
|
||||
Password string `json:"password"` // 认证密码
|
||||
Database int `json:"database"` // 数据库名称
|
||||
|
||||
DialTimeOut time.Duration `json:"dialTimeOut"` // 连接超时断开
|
||||
|
||||
Client *redis.Client `json:"client"`
|
||||
}
|
||||
|
||||
// NewClient 创建Redis客户端
|
||||
func (c *ConnRedis) NewClient() (*ConnRedis, error) {
|
||||
// IPV6地址协议
|
||||
if strings.Contains(c.Addr, ":") {
|
||||
c.Addr = fmt.Sprintf("[%s]", c.Addr)
|
||||
}
|
||||
addr := fmt.Sprintf("%s:%d", c.Addr, c.Port)
|
||||
|
||||
// 默认等待5s
|
||||
if c.DialTimeOut == 0 {
|
||||
c.DialTimeOut = 5 * time.Second
|
||||
}
|
||||
|
||||
// 连接
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: addr,
|
||||
// Username: c.User,
|
||||
Password: c.Password,
|
||||
DB: c.Database,
|
||||
DialTimeout: c.DialTimeOut,
|
||||
})
|
||||
|
||||
// 测试数据库连接
|
||||
if _, err := rdb.Ping(context.Background()).Result(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.Client = rdb
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// Close 关闭当前Redis客户端
|
||||
func (c *ConnRedis) Close() {
|
||||
if c.Client != nil {
|
||||
c.Client.Close()
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,15 @@ if tonumber(current) == 1 then
|
||||
end
|
||||
return tonumber(current);`)
|
||||
|
||||
// 连接Redis实例
|
||||
func ConnectPush(source string, rdb *redis.Client) {
|
||||
if rdb == nil {
|
||||
delete(rdbMap, source)
|
||||
return
|
||||
}
|
||||
rdbMap[source] = rdb
|
||||
}
|
||||
|
||||
// 连接Redis实例
|
||||
func Connect() {
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/redis"
|
||||
"be.ems/src/framework/telnet"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
@@ -211,6 +212,21 @@ func (s *NeHostController) Test(c *gin.Context) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if body.HostType == "redis" {
|
||||
var connRedis redis.ConnRedis
|
||||
body.CopyTo(&connRedis)
|
||||
|
||||
client, err := connRedis.NewClient()
|
||||
if err != nil {
|
||||
// 连接主机失败,请检查连接参数后重试
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 网元主机发送命令
|
||||
|
||||
@@ -5,16 +5,17 @@ import "encoding/json"
|
||||
// NeHost 网元主机表 ne_host
|
||||
type NeHost struct {
|
||||
HostID string `json:"hostId" gorm:"column:host_id"` // 主机主键
|
||||
HostType string `json:"hostType" gorm:"column:host_type" binding:"oneof=ssh telnet"` // 主机类型 ssh telnet redis
|
||||
HostType string `json:"hostType" gorm:"column:host_type" binding:"oneof=ssh telnet redis"` // 连接类型 ssh telnet redis
|
||||
GroupID string `json:"groupId" gorm:"column:group_id"` // 分组(0默认 1网元 2系统)
|
||||
Title string `json:"title" gorm:"column:title"` // 标题名称
|
||||
Addr string `json:"addr" gorm:"column:addr" binding:"required"` // 主机地址
|
||||
Port int64 `json:"port" gorm:"column:port" binding:"required,number,max=65535,min=1"` // 主机端口 22 4100
|
||||
User string `json:"user" gorm:"column:user" binding:"required"` // 主机用户名
|
||||
Port int64 `json:"port" gorm:"column:port" binding:"required,number,max=65535,min=1"` // 端口 22 4100 6379
|
||||
User string `json:"user" gorm:"column:user" binding:"required"` // 认证用户名
|
||||
AuthMode string `json:"authMode" gorm:"column:auth_mode" binding:"oneof=0 1 2"` // 认证模式(0密码 1主机私钥 2已免密)
|
||||
Password string `json:"password" gorm:"column:password"` // 认证密码
|
||||
PrivateKey string `json:"privateKey" gorm:"column:private_key"` // 认证私钥
|
||||
PassPhrase string `json:"passPhrase" gorm:"column:pass_phrase"` // 认证私钥密码
|
||||
DBName string `json:"dbName" gorm:"column:db_name"` // 数据库名称
|
||||
Remark string `json:"remark" gorm:"column:remark"` // 备注
|
||||
CreateBy string `json:"createBy" gorm:"column:create_by"` // 创建者
|
||||
CreateTime int64 `json:"createTime" gorm:"column:create_time"` // 创建时间
|
||||
@@ -23,7 +24,7 @@ type NeHost struct {
|
||||
}
|
||||
|
||||
// TableName 表名称
|
||||
func (NeHost) TableName() string {
|
||||
func (*NeHost) TableName() string {
|
||||
return "ne_host"
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
// 实例化数据层 NeHost 结构体
|
||||
var NewNeHost = &NeHost{
|
||||
selectSql: `select
|
||||
host_id, host_type, group_id, title, addr, port, user, auth_mode, password, private_key, pass_phrase, remark, create_by, create_time, update_by, update_time
|
||||
host_id, host_type, group_id, title, addr, port, user, auth_mode, password, private_key, pass_phrase, db_name, remark, create_by, create_time, update_by, update_time
|
||||
from ne_host`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
@@ -30,6 +30,7 @@ var NewNeHost = &NeHost{
|
||||
"password": "Password",
|
||||
"private_key": "PrivateKey",
|
||||
"private_password": "PassPhrase",
|
||||
"db_name": "DBName",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
@@ -245,6 +246,9 @@ func (r *NeHost) Insert(neHost model.NeHost) string {
|
||||
if neHost.PassPhrase != "" {
|
||||
params["pass_phrase"] = neHost.PassPhrase
|
||||
}
|
||||
if neHost.DBName != "" {
|
||||
params["db_name"] = neHost.DBName
|
||||
}
|
||||
if neHost.Remark != "" {
|
||||
params["remark"] = neHost.Remark
|
||||
}
|
||||
@@ -328,6 +332,9 @@ func (r *NeHost) Update(neHost model.NeHost) int64 {
|
||||
if neHost.PassPhrase != "" {
|
||||
params["pass_phrase"] = neHost.PassPhrase
|
||||
}
|
||||
if neHost.DBName != "" {
|
||||
params["db_name"] = neHost.DBName
|
||||
}
|
||||
params["remark"] = neHost.Remark
|
||||
if neHost.UpdateBy != "" {
|
||||
params["update_by"] = neHost.UpdateBy
|
||||
|
||||
Reference in New Issue
Block a user