2022-05-17 20:23:08 +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/mongo/domain/entity"
|
|
|
|
|
|
"mayfly-go/internal/mongo/domain/repository"
|
2024-11-20 22:43:53 +08:00
|
|
|
|
"mayfly-go/internal/mongo/imsg"
|
2023-10-27 17:41:45 +08:00
|
|
|
|
"mayfly-go/internal/mongo/mgm"
|
2023-12-05 23:03:51 +08:00
|
|
|
|
tagapp "mayfly-go/internal/tag/application"
|
2024-05-09 21:29:34 +08:00
|
|
|
|
tagdto "mayfly-go/internal/tag/application/dto"
|
2024-04-09 12:55:51 +08:00
|
|
|
|
tagentity "mayfly-go/internal/tag/domain/entity"
|
2023-10-26 17:15:49 +08:00
|
|
|
|
"mayfly-go/pkg/base"
|
2023-10-27 17:41:45 +08:00
|
|
|
|
"mayfly-go/pkg/errorx"
|
2022-06-02 17:41:11 +08:00
|
|
|
|
"mayfly-go/pkg/model"
|
2024-10-16 17:24:50 +08:00
|
|
|
|
"mayfly-go/pkg/utils/stringx"
|
2022-05-17 20:23:08 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Mongo interface {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
base.App[*entity.Mongo]
|
|
|
|
|
|
|
2022-05-17 20:23:08 +08:00
|
|
|
|
// 分页获取机器脚本信息列表
|
2023-10-26 17:15:49 +08:00
|
|
|
|
GetPageList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
2022-05-17 20:23:08 +08:00
|
|
|
|
|
2023-11-12 20:14:44 +08:00
|
|
|
|
TestConn(entity *entity.Mongo) error
|
|
|
|
|
|
|
2024-04-17 21:28:28 +08:00
|
|
|
|
SaveMongo(ctx context.Context, entity *entity.Mongo, tagCodePaths ...string) error
|
2022-05-17 20:23:08 +08:00
|
|
|
|
|
|
|
|
|
|
// 删除数据库信息
|
2023-11-07 21:05:21 +08:00
|
|
|
|
Delete(ctx context.Context, id uint64) error
|
2022-05-17 20:23:08 +08:00
|
|
|
|
|
2023-08-25 19:41:52 +08:00
|
|
|
|
// 获取mongo连接实例
|
2022-05-17 20:23:08 +08:00
|
|
|
|
// @param id mongo id
|
2023-10-27 17:41:45 +08:00
|
|
|
|
GetMongoConn(id uint64) (*mgm.MongoConn, error)
|
2022-05-17 20:23:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
|
type mongoAppImpl struct {
|
2023-10-26 17:15:49 +08:00
|
|
|
|
base.AppImpl[*entity.Mongo, repository.Mongo]
|
2023-12-05 23:03:51 +08:00
|
|
|
|
|
2024-01-23 19:30:28 +08:00
|
|
|
|
tagApp tagapp.TagTree `inject:"TagTreeApp"`
|
2024-01-21 22:52:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-17 20:23:08 +08:00
|
|
|
|
// 分页获取数据库信息列表
|
2023-10-26 17:15:49 +08:00
|
|
|
|
func (d *mongoAppImpl) GetPageList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
|
|
|
|
|
return d.GetRepo().GetList(condition, pageParam, toEntity, orderBy...)
|
2022-05-17 20:23:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-07 21:05:21 +08:00
|
|
|
|
func (d *mongoAppImpl) Delete(ctx context.Context, id uint64) error {
|
2024-05-05 14:53:30 +08:00
|
|
|
|
mongoEntity, err := d.GetById(id)
|
2023-12-13 14:01:13 +08:00
|
|
|
|
if err != nil {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
return errorx.NewBiz("mongo not found")
|
2023-12-13 14:01:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
mgm.CloseConn(id)
|
2023-12-13 14:01:13 +08:00
|
|
|
|
return d.Tx(ctx,
|
|
|
|
|
|
func(ctx context.Context) error {
|
|
|
|
|
|
return d.DeleteById(ctx, id)
|
|
|
|
|
|
},
|
|
|
|
|
|
func(ctx context.Context) error {
|
2024-05-09 21:29:34 +08:00
|
|
|
|
return d.tagApp.SaveResourceTag(ctx, &tagdto.SaveResourceTag{ResourceTag: &tagdto.ResourceTag{
|
2024-04-12 13:24:20 +08:00
|
|
|
|
Type: tagentity.TagTypeMongo,
|
|
|
|
|
|
Code: mongoEntity.Code,
|
2024-04-17 21:28:28 +08:00
|
|
|
|
}})
|
2023-12-13 14:01:13 +08:00
|
|
|
|
})
|
2022-05-17 20:23:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-12 20:14:44 +08:00
|
|
|
|
func (d *mongoAppImpl) TestConn(me *entity.Mongo) error {
|
|
|
|
|
|
conn, err := me.ToMongoInfo().Conn()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
conn.Close()
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-17 21:28:28 +08:00
|
|
|
|
func (d *mongoAppImpl) SaveMongo(ctx context.Context, m *entity.Mongo, tagCodePaths ...string) error {
|
2024-10-23 17:30:05 +08:00
|
|
|
|
oldMongo := &entity.Mongo{Uri: m.Uri, SshTunnelMachineId: m.SshTunnelMachineId}
|
2024-04-28 23:45:57 +08:00
|
|
|
|
err := d.GetByCond(oldMongo)
|
2023-12-05 23:03:51 +08:00
|
|
|
|
|
2022-05-17 20:23:08 +08:00
|
|
|
|
if m.Id == 0 {
|
2023-12-05 23:03:51 +08:00
|
|
|
|
if err == nil {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
return errorx.NewBizI(ctx, imsg.ErrMongoInfoExist)
|
2023-12-05 23:03:51 +08:00
|
|
|
|
}
|
2024-10-16 17:24:50 +08:00
|
|
|
|
// 生成随机编号
|
|
|
|
|
|
m.Code = stringx.Rand(10)
|
2023-12-05 23:03:51 +08:00
|
|
|
|
|
|
|
|
|
|
return d.Tx(ctx, func(ctx context.Context) error {
|
|
|
|
|
|
return d.Insert(ctx, m)
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
2024-05-09 21:29:34 +08:00
|
|
|
|
return d.tagApp.SaveResourceTag(ctx, &tagdto.SaveResourceTag{
|
|
|
|
|
|
ResourceTag: &tagdto.ResourceTag{
|
2024-04-17 21:28:28 +08:00
|
|
|
|
Type: tagentity.TagTypeMongo,
|
|
|
|
|
|
Code: m.Code,
|
2024-10-16 17:24:50 +08:00
|
|
|
|
Name: m.Name,
|
2024-04-17 21:28:28 +08:00
|
|
|
|
},
|
|
|
|
|
|
ParentTagCodePaths: tagCodePaths,
|
2024-04-06 18:19:17 +08:00
|
|
|
|
})
|
2023-12-05 23:03:51 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果存在该库,则校验修改的库是否为该库
|
|
|
|
|
|
if err == nil && oldMongo.Id != m.Id {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
return errorx.NewBizI(ctx, imsg.ErrMongoInfoExist)
|
2022-05-17 20:23:08 +08:00
|
|
|
|
}
|
2023-12-20 23:01:51 +08:00
|
|
|
|
// 如果调整了ssh等会查不到旧数据,故需要根据id获取旧信息将code赋值给标签进行关联
|
|
|
|
|
|
if oldMongo.Code == "" {
|
2024-05-05 14:53:30 +08:00
|
|
|
|
oldMongo, _ = d.GetById(m.Id)
|
2023-12-20 23:01:51 +08:00
|
|
|
|
}
|
2023-10-26 17:15:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 先关闭连接
|
2023-10-27 17:41:45 +08:00
|
|
|
|
mgm.CloseConn(m.Id)
|
2024-04-06 18:19:17 +08:00
|
|
|
|
m.Code = ""
|
2023-12-05 23:03:51 +08:00
|
|
|
|
return d.Tx(ctx, func(ctx context.Context) error {
|
|
|
|
|
|
return d.UpdateById(ctx, m)
|
|
|
|
|
|
}, func(ctx context.Context) error {
|
2024-04-17 21:28:28 +08:00
|
|
|
|
if oldMongo.Name != m.Name {
|
2024-10-16 17:24:50 +08:00
|
|
|
|
if err := d.tagApp.UpdateTagName(ctx, tagentity.TagTypeMongo, oldMongo.Code, m.Name); err != nil {
|
2024-04-17 21:28:28 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-09 21:29:34 +08:00
|
|
|
|
return d.tagApp.SaveResourceTag(ctx, &tagdto.SaveResourceTag{
|
|
|
|
|
|
ResourceTag: &tagdto.ResourceTag{
|
2024-04-17 21:28:28 +08:00
|
|
|
|
Type: tagentity.TagTypeMongo,
|
|
|
|
|
|
Code: oldMongo.Code,
|
|
|
|
|
|
},
|
|
|
|
|
|
ParentTagCodePaths: tagCodePaths,
|
2024-04-06 18:19:17 +08:00
|
|
|
|
})
|
2023-12-05 23:03:51 +08:00
|
|
|
|
})
|
2022-05-17 20:23:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-27 17:41:45 +08:00
|
|
|
|
func (d *mongoAppImpl) GetMongoConn(id uint64) (*mgm.MongoConn, error) {
|
|
|
|
|
|
return mgm.GetMongoConn(id, func() (*mgm.MongoInfo, error) {
|
2024-05-05 14:53:30 +08:00
|
|
|
|
me, err := d.GetById(id)
|
2022-05-17 20:23:08 +08:00
|
|
|
|
if err != nil {
|
2024-11-20 22:43:53 +08:00
|
|
|
|
return nil, errorx.NewBiz("mongo not found")
|
2022-05-17 20:23:08 +08:00
|
|
|
|
}
|
2024-04-12 13:24:20 +08:00
|
|
|
|
return me.ToMongoInfo(d.tagApp.ListTagPathByTypeAndCode(consts.ResourceTypeMongo, me.Code)...), nil
|
2022-05-17 20:23:08 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|