pcm-coordinator/api/internal/logic/adapters/createclusterlogic.go

66 lines
1.8 KiB
Go

package adapters
import (
"context"
"errors"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
"gorm.io/gorm"
"time"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type CreateClusterLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCreateClusterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateClusterLogic {
return &CreateClusterLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CreateClusterLogic) CreateCluster(req *types.ClusterCreateReq) (resp *types.ClusterResp, err error) {
adapter := &types.AdapterInfo{}
result := l.svcCtx.DbEngin.Table("t_adapter").First(&adapter, req.AdapterId)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, errors.New("adapter does not exist")
}
cluster := types.ClusterInfo{}
utils.Convert(req, &cluster)
cluster.Id = utils.GenSnowflakeIDStr()
cluster.CreateTime = time.Now().Format("2006-01-02 15:04:05")
cluster.OwnerId = "0"
tx := l.svcCtx.DbEngin.Table("t_cluster").Create(&cluster)
if tx.Error != nil {
logx.Errorf(tx.Error.Error())
return nil, errors.New("cluster create failed")
}
// push cluster info to adapter
var adapterServer string
l.svcCtx.DbEngin.Raw("select server from t_adapter where id = ?", req.AdapterId).Scan(&adapterServer)
response, err := l.svcCtx.HttpClient.R().
SetBody(&types.ClusterInfo{
Name: req.Name,
Server: req.Server,
Token: req.Token,
MonitorServer: req.MonitorServer,
}).
ForceContentType("application/json").
Post(adapterServer + "/api/v1/cluster/info")
if err != nil {
}
if response.IsError() {
}
return
}