2021-07-28 18:03:19 +08:00
|
|
|
|
package application
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-11-07 21:05:21 +08:00
|
|
|
|
"context"
|
2023-12-05 23:03:51 +08:00
|
|
|
|
"mayfly-go/internal/common/consts"
|
2022-09-09 18:26:08 +08:00
|
|
|
|
"mayfly-go/internal/redis/domain/entity"
|
|
|
|
|
|
"mayfly-go/internal/redis/domain/repository"
|
2023-10-27 17:41:45 +08:00
|
|
|
|
"mayfly-go/internal/redis/rdm"
|
2023-12-05 23:03:51 +08:00
|
|
|
|
tagapp "mayfly-go/internal/tag/application"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/base"
|
|
|
|
|
|
"mayfly-go/pkg/errorx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2023-12-05 23:03:51 +08:00
|
|
|
|
"mayfly-go/pkg/utils/stringx"
|
2022-09-29 13:14:50 +08:00
|
|
|
|
"strconv"
|
2022-07-10 12:14:06 +08:00
|
|
|
|
"strings"
|
2021-07-28 18:03:19 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Redis interface {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
base.App[*entity.Redis]
|
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// 分页获取机器脚本信息列表
|
2023-10-26 17:15:49 +08:00
|
|
|
|
GetPageList(condition *entity.RedisQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
2023-11-12 20:14:44 +08:00
|
|
|
|
// 测试连接
|
|
|
|
|
|
TestConn(re *entity.Redis) error
|
|
|
|
|
|
|
2024-01-07 21:46:25 +08:00
|
|
|
|
SaveRedis(ctx context.Context, re *entity.Redis, tagIds ...uint64) error
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
// 删除数据库信息
|
2023-11-07 21:05:21 +08:00
|
|
|
|
Delete(ctx context.Context, id uint64) error
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取数据库连接实例
|
2022-09-29 13:14:50 +08:00
|
|
|
|
// id: 数据库实例id
|
|
|
|
|
|
// db: 库号
|
2023-10-27 17:41:45 +08:00
|
|
|
|
GetRedisConn(id uint64, db int) (*rdm.RedisConn, error)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-05 23:03:51 +08:00
|
|
|
|
func newRedisApp(redisRepo repository.Redis, tagApp tagapp.TagTree) Redis {
|
|
|
|
|
|
app := &redisAppImpl{
|
|
|
|
|
|
tagApp: tagApp,
|
2022-09-09 18:26:08 +08:00
|
|
|
|
}
|
2023-12-05 23:03:51 +08:00
|
|
|
|
app.Repo = redisRepo
|
|
|
|
|
|
return app
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
type redisAppImpl struct {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
base.AppImpl[*entity.Redis, repository.Redis]
|
2023-12-05 23:03:51 +08:00
|
|
|
|
|
|
|
|
|
|
tagApp tagapp.TagTree
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
// 分页获取redis列表
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (r *redisAppImpl) GetPageList(condition *entity.RedisQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
|
|
|
|
|
return r.GetRepo().GetRedisList(condition, pageParam, toEntity, orderBy...)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-12 20:14:44 +08:00
|
|
|
|
func (r *redisAppImpl) TestConn(re *entity.Redis) error {
|
|
|
|
|
|
db := 0
|
|
|
|
|
|
if re.Db != "" {
|
|
|
|
|
|
db, _ = strconv.Atoi(strings.Split(re.Db, ",")[0])
|
2022-07-18 20:36:31 +08:00
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
2023-11-12 20:14:44 +08:00
|
|
|
|
rc, err := re.ToRedisInfo(db).Conn()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
rc.Close()
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-07 21:46:25 +08:00
|
|
|
|
func (r *redisAppImpl) SaveRedis(ctx context.Context, re *entity.Redis, tagIds ...uint64) error {
|
2021-07-28 18:03:19 +08:00
|
|
|
|
// 查找是否存在该库
|
2023-12-11 01:00:09 +08:00
|
|
|
|
oldRedis := &entity.Redis{
|
|
|
|
|
|
Host: re.Host,
|
|
|
|
|
|
SshTunnelMachineId: re.SshTunnelMachineId,
|
2023-03-17 09:46:41 +08:00
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
err := r.GetBy(oldRedis)
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
if re.Id == 0 {
|
|
|
|
|
|
if err == nil {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
return errorx.NewBiz("该实例已存在")
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
2024-01-05 08:55:34 +08:00
|
|
|
|
if errEnc := re.PwdEncrypt(); errEnc != nil {
|
|
|
|
|
|
return errorx.NewBiz(errEnc.Error())
|
|
|
|
|
|
}
|
2023-12-05 23:03:51 +08:00
|
|
|
|
|
|
|
|
|
|
resouceCode := stringx.Rand(16)
|
|
|
|
|
|
re.Code = resouceCode
|
|
|
|
|
|
|
|
|
|
|
|
return r.Tx(ctx, func(ctx context.Context) error {
|
|
|
|
|
|
return r.Insert(ctx, re)
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
|
|
|
|
|
return r.tagApp.RelateResource(ctx, resouceCode, consts.TagResourceTypeRedis, tagIds)
|
|
|
|
|
|
})
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果存在该库,则校验修改的库是否为该库
|
|
|
|
|
|
if err == nil && oldRedis.Id != re.Id {
|
|
|
|
|
|
return errorx.NewBiz("该实例已存在")
|
|
|
|
|
|
}
|
|
|
|
|
|
// 如果修改了redis实例的库信息,则关闭旧库的连接
|
|
|
|
|
|
if oldRedis.Db != re.Db || oldRedis.SshTunnelMachineId != re.SshTunnelMachineId {
|
|
|
|
|
|
for _, dbStr := range strings.Split(oldRedis.Db, ",") {
|
|
|
|
|
|
db, _ := strconv.Atoi(dbStr)
|
2023-10-27 17:41:45 +08:00
|
|
|
|
rdm.CloseConn(re.Id, db)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-12-20 23:01:51 +08:00
|
|
|
|
// 如果调整了ssh等会查不到旧数据,故需要根据id获取旧信息将code赋值给标签进行关联
|
|
|
|
|
|
if oldRedis.Code == "" {
|
|
|
|
|
|
oldRedis, _ = r.GetById(new(entity.Redis), re.Id)
|
|
|
|
|
|
}
|
2023-12-05 23:03:51 +08:00
|
|
|
|
|
2024-01-05 08:55:34 +08:00
|
|
|
|
if errEnc := re.PwdEncrypt(); errEnc != nil {
|
|
|
|
|
|
return errorx.NewBiz(errEnc.Error())
|
|
|
|
|
|
}
|
2023-12-05 23:03:51 +08:00
|
|
|
|
return r.Tx(ctx, func(ctx context.Context) error {
|
|
|
|
|
|
return r.UpdateById(ctx, re)
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
|
|
|
|
|
return r.tagApp.RelateResource(ctx, oldRedis.Code, consts.TagResourceTypeRedis, tagIds)
|
|
|
|
|
|
})
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 删除Redis信息
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (r *redisAppImpl) Delete(ctx context.Context, id uint64) error {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
re, err := r.GetById(new(entity.Redis), id)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errorx.NewBiz("该redis信息不存在")
|
|
|
|
|
|
}
|
2022-09-29 13:14:50 +08:00
|
|
|
|
// 如果存在连接,则关闭所有库连接信息
|
|
|
|
|
|
for _, dbStr := range strings.Split(re.Db, ",") {
|
|
|
|
|
|
db, _ := strconv.Atoi(dbStr)
|
2023-10-27 17:41:45 +08:00
|
|
|
|
rdm.CloseConn(re.Id, db)
|
2022-09-29 13:14:50 +08:00
|
|
|
|
}
|
2023-12-13 14:01:13 +08:00
|
|
|
|
|
|
|
|
|
|
return r.Tx(ctx, func(ctx context.Context) error {
|
|
|
|
|
|
return r.DeleteById(ctx, id)
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
|
|
|
|
|
var tagIds []uint64
|
|
|
|
|
|
return r.tagApp.RelateResource(ctx, re.Code, consts.TagResourceTypeRedis, tagIds)
|
|
|
|
|
|
})
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取数据库连接实例
|
2023-10-27 17:41:45 +08:00
|
|
|
|
func (r *redisAppImpl) GetRedisConn(id uint64, db int) (*rdm.RedisConn, error) {
|
|
|
|
|
|
return rdm.GetRedisConn(id, db, func() (*rdm.RedisInfo, error) {
|
|
|
|
|
|
// 缓存不存在,则回调获取redis信息
|
|
|
|
|
|
re, err := r.GetById(new(entity.Redis), id)
|
2023-10-26 17:15:49 +08:00
|
|
|
|
if err != nil {
|
2023-10-27 17:41:45 +08:00
|
|
|
|
return nil, errorx.NewBiz("redis信息不存在")
|
2023-10-26 17:15:49 +08:00
|
|
|
|
}
|
2024-01-05 08:55:34 +08:00
|
|
|
|
if err := re.PwdDecrypt(); err != nil {
|
|
|
|
|
|
return nil, errorx.NewBiz(err.Error())
|
|
|
|
|
|
}
|
2023-12-05 23:03:51 +08:00
|
|
|
|
return re.ToRedisInfo(db, r.tagApp.ListTagPathByResource(consts.TagResourceTypeRedis, re.Code)...), nil
|
2021-08-18 17:57:33 +08:00
|
|
|
|
})
|
2021-07-28 18:03:19 +08:00
|
|
|
|
}
|