2023-07-20 17:51:57 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-07-27 16:07:27 +08:00
|
|
|
"context"
|
2023-09-01 23:10:14 +08:00
|
|
|
"fmt"
|
2023-07-27 16:07:27 +08:00
|
|
|
|
2023-07-20 17:51:57 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2023-09-06 11:02:12 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2023-07-20 17:51:57 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-09-01 23:10:14 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
|
2023-07-20 17:51:57 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/mysql"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/mysql/client"
|
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2023-08-29 10:50:15 +08:00
|
|
|
type DatabaseService struct{}
|
2023-07-20 17:51:57 +08:00
|
|
|
|
2023-08-29 10:50:15 +08:00
|
|
|
type IDatabaseService interface {
|
|
|
|
Get(name string) (dto.DatabaseInfo, error)
|
|
|
|
SearchWithPage(search dto.DatabaseSearch) (int64, interface{}, error)
|
|
|
|
CheckDatabase(req dto.DatabaseCreate) bool
|
|
|
|
Create(req dto.DatabaseCreate) error
|
|
|
|
Update(req dto.DatabaseUpdate) error
|
2023-07-20 17:51:57 +08:00
|
|
|
Delete(id uint) error
|
2023-08-29 10:50:15 +08:00
|
|
|
List(dbType string) ([]dto.DatabaseOption, error)
|
2023-07-20 17:51:57 +08:00
|
|
|
}
|
|
|
|
|
2023-08-29 10:50:15 +08:00
|
|
|
func NewIDatabaseService() IDatabaseService {
|
|
|
|
return &DatabaseService{}
|
2023-07-20 17:51:57 +08:00
|
|
|
}
|
|
|
|
|
2023-08-29 10:50:15 +08:00
|
|
|
func (u *DatabaseService) SearchWithPage(search dto.DatabaseSearch) (int64, interface{}, error) {
|
|
|
|
total, dbs, err := databaseRepo.Page(search.Page, search.PageSize,
|
2023-09-01 12:28:12 +08:00
|
|
|
databaseRepo.WithTypeList(search.Type),
|
2023-07-20 17:51:57 +08:00
|
|
|
commonRepo.WithLikeName(search.Info),
|
2023-08-29 10:50:15 +08:00
|
|
|
databaseRepo.WithoutByFrom("local"),
|
2023-07-20 17:51:57 +08:00
|
|
|
)
|
2023-08-29 10:50:15 +08:00
|
|
|
var datas []dto.DatabaseInfo
|
2023-07-20 17:51:57 +08:00
|
|
|
for _, db := range dbs {
|
2023-08-29 10:50:15 +08:00
|
|
|
var item dto.DatabaseInfo
|
2023-07-20 17:51:57 +08:00
|
|
|
if err := copier.Copy(&item, &db); err != nil {
|
|
|
|
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
datas = append(datas, item)
|
|
|
|
}
|
|
|
|
return total, datas, err
|
|
|
|
}
|
|
|
|
|
2023-08-29 10:50:15 +08:00
|
|
|
func (u *DatabaseService) Get(name string) (dto.DatabaseInfo, error) {
|
|
|
|
var data dto.DatabaseInfo
|
|
|
|
remote, err := databaseRepo.Get(commonRepo.WithByName(name))
|
2023-07-27 16:07:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
if err := copier.Copy(&data, &remote); err != nil {
|
|
|
|
return data, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2023-08-29 10:50:15 +08:00
|
|
|
func (u *DatabaseService) List(dbType string) ([]dto.DatabaseOption, error) {
|
2023-09-01 12:28:12 +08:00
|
|
|
dbs, err := databaseRepo.GetList(databaseRepo.WithTypeList(dbType))
|
2023-08-29 10:50:15 +08:00
|
|
|
var datas []dto.DatabaseOption
|
2023-07-20 17:51:57 +08:00
|
|
|
for _, db := range dbs {
|
2023-08-29 10:50:15 +08:00
|
|
|
var item dto.DatabaseOption
|
2023-07-20 17:51:57 +08:00
|
|
|
if err := copier.Copy(&item, &db); err != nil {
|
|
|
|
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
2023-08-29 10:50:15 +08:00
|
|
|
item.Database = db.Name
|
2023-07-20 17:51:57 +08:00
|
|
|
datas = append(datas, item)
|
|
|
|
}
|
|
|
|
return datas, err
|
|
|
|
}
|
|
|
|
|
2023-08-29 10:50:15 +08:00
|
|
|
func (u *DatabaseService) CheckDatabase(req dto.DatabaseCreate) bool {
|
2023-08-11 22:20:15 +08:00
|
|
|
if _, err := mysql.NewMysqlClient(client.DBInfo{
|
|
|
|
From: "remote",
|
|
|
|
Address: req.Address,
|
|
|
|
Port: req.Port,
|
|
|
|
Username: req.Username,
|
|
|
|
Password: req.Password,
|
|
|
|
Timeout: 6,
|
|
|
|
}); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-08-29 10:50:15 +08:00
|
|
|
func (u *DatabaseService) Create(req dto.DatabaseCreate) error {
|
|
|
|
db, _ := databaseRepo.Get(commonRepo.WithByName(req.Name))
|
2023-07-20 17:51:57 +08:00
|
|
|
if db.ID != 0 {
|
2023-09-06 11:02:12 +08:00
|
|
|
if db.From == "local" {
|
|
|
|
return buserr.New(constant.ErrLocalExist)
|
|
|
|
}
|
2023-07-20 17:51:57 +08:00
|
|
|
return constant.ErrRecordExist
|
|
|
|
}
|
|
|
|
if _, err := mysql.NewMysqlClient(client.DBInfo{
|
|
|
|
From: "remote",
|
|
|
|
Address: req.Address,
|
|
|
|
Port: req.Port,
|
|
|
|
Username: req.Username,
|
|
|
|
Password: req.Password,
|
2023-08-11 22:20:15 +08:00
|
|
|
Timeout: 6,
|
2023-07-20 17:51:57 +08:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := copier.Copy(&db, &req); err != nil {
|
|
|
|
return errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
2023-08-29 22:22:18 +08:00
|
|
|
if err := databaseRepo.Create(context.Background(), &db); err != nil {
|
2023-07-20 17:51:57 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-29 10:50:15 +08:00
|
|
|
func (u *DatabaseService) Delete(id uint) error {
|
|
|
|
db, _ := databaseRepo.Get(commonRepo.WithByID(id))
|
2023-07-20 17:51:57 +08:00
|
|
|
if db.ID == 0 {
|
|
|
|
return constant.ErrRecordNotFound
|
|
|
|
}
|
2023-08-29 22:22:18 +08:00
|
|
|
if err := databaseRepo.Delete(context.Background(), commonRepo.WithByID(id)); err != nil {
|
2023-07-27 16:07:27 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if db.From != "local" {
|
2023-08-31 22:28:13 +08:00
|
|
|
if err := mysqlRepo.Delete(context.Background(), mysqlRepo.WithByMysqlName(db.Name)); err != nil {
|
2023-07-27 16:07:27 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2023-07-20 17:51:57 +08:00
|
|
|
}
|
|
|
|
|
2023-08-29 10:50:15 +08:00
|
|
|
func (u *DatabaseService) Update(req dto.DatabaseUpdate) error {
|
2023-07-20 17:51:57 +08:00
|
|
|
if _, err := mysql.NewMysqlClient(client.DBInfo{
|
|
|
|
From: "remote",
|
|
|
|
Address: req.Address,
|
|
|
|
Port: req.Port,
|
|
|
|
Username: req.Username,
|
|
|
|
Password: req.Password,
|
|
|
|
Timeout: 300,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-01 23:10:14 +08:00
|
|
|
pass, err := encrypt.StringEncrypt(req.Password)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decrypt database password failed, err: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-07-20 17:51:57 +08:00
|
|
|
upMap := make(map[string]interface{})
|
2023-09-06 21:20:16 +08:00
|
|
|
upMap["type"] = req.Type
|
2023-07-20 17:51:57 +08:00
|
|
|
upMap["version"] = req.Version
|
|
|
|
upMap["address"] = req.Address
|
|
|
|
upMap["port"] = req.Port
|
|
|
|
upMap["username"] = req.Username
|
2023-09-01 23:10:14 +08:00
|
|
|
upMap["password"] = pass
|
2023-07-20 17:51:57 +08:00
|
|
|
upMap["description"] = req.Description
|
2023-08-29 10:50:15 +08:00
|
|
|
return databaseRepo.Update(req.ID, upMap)
|
2023-07-20 17:51:57 +08:00
|
|
|
}
|