pcm-coordinator/api/internal/logic/cloud/registerclusterlogic.go

69 lines
1.7 KiB
Go

package cloud
import (
"context"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/models"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils"
"strconv"
"github.com/zeromicro/go-zero/core/logx"
)
type RegisterClusterLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewRegisterClusterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterClusterLogic {
return &RegisterClusterLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *RegisterClusterLogic) RegisterCluster(req *types.RegisterClusterReq) (*types.CloudResp, error) {
var phyInfos []models.ScParticipantPhyInfo
var resp types.CloudResp
l.svcCtx.DbEngin.Raw("select * from sc_participant_phy_info where `name` = ?", req.Name).Scan(&phyInfos)
if len(phyInfos) != 0 {
resp.Code = "400"
resp.Msg = "cluster name already exist"
resp.Data = ""
return &resp, nil
}
participant := models.ScParticipantPhyInfo{
Token: req.Token,
Name: req.Name,
Address: req.Address,
Type: "CLOUD",
Id: utils.GenSnowflakeID(),
MetricsUrl: req.MetricsUrl,
}
tx := l.svcCtx.DbEngin.Create(&participant)
if tx.Error != nil {
return nil, tx.Error
}
labelInfo := models.ScParticipantLabelInfo{
Id: utils.GenSnowflakeID(),
ParticipantId: participant.Id,
Key: "cloud",
Value: "sealos",
}
tx2 := l.svcCtx.DbEngin.Create(&labelInfo)
if tx2.Error != nil {
return nil, tx.Error
}
resp.Code = string(200)
resp.Msg = "success"
resp.Data = "participantId:" + strconv.FormatInt(participant.Id, 10)
return &resp, nil
}