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

72 lines
1.9 KiB
Go

package cloud
import (
"context"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/models"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils"
"strconv"
"time"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types"
"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{}
participant.Token = req.Token
participant.Name = req.Name
participant.Address = req.Address
participant.Type = "SEALOS"
participant.Id = utils.GenSnowflakeID()
participant.MetricsUrl = req.MetricsUrl
participant.CreatedTime = time.Now()
participant.UpdatedTime = time.Now()
labelInfo := models.ScParticipantLabelInfo{}
labelInfo.Id = utils.GenSnowflakeID()
labelInfo.ParticipantId = participant.Id
labelInfo.CreatedTime = time.Now()
labelInfo.Key = "cloud"
labelInfo.Value = "sealos"
tx := l.svcCtx.DbEngin.Create(&participant)
if tx.Error != nil {
return nil, tx.Error
}
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
}