Skip to content

Commit

Permalink
feat(dbm-services): 资源池标签和资源流转功能支持 TencentBlueKing#7586
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq committed Nov 4, 2024
1 parent 7d5e6bf commit 3fea3a1
Show file tree
Hide file tree
Showing 24 changed files with 668 additions and 400 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE tb_rp_detail change column label labels json;
ALTER TABLE tb_rp_detail_archive change column label labels json;
53 changes: 28 additions & 25 deletions dbm-services/common/db-resource/internal/controller/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"dbm-services/common/go-pubpkg/logger"

"github.com/gin-gonic/gin"
"github.com/samber/lo"
)

func init() {
Expand Down Expand Up @@ -65,33 +66,27 @@ func (c *ApplyHandler) ConfirmApply(r *gin.Context) {
if c.Prepare(r, &param) != nil {
return
}
requestId := r.GetString("request_id")
hostIds := cmutil.RemoveDuplicate(param.HostIds)
hostIds := lo.Uniq(param.HostIds)
var cnt int64
err := model.DB.Self.Table(model.TbRpApplyDetailLogName()).Where("request_id = ?", param.RequestId).Count(&cnt).Error
if err != nil {
logger.Error("use request id %s,query apply resouece failed %s", param.RequestId, err.Error())
c.SendResponse(r, fmt.Errorf("%w", err), "use request id search applyed resource failed", requestId)
return
}
if len(hostIds) != int(cnt) {
c.SendResponse(r, fmt.Errorf("need return resource count is %d,but use request id only found total count %d",
len(hostIds), cnt), requestId, "")
len(hostIds), cnt), "")
return
}
var rs []model.TbRpDetail
err = model.DB.Self.Table(model.TbRpDetailName()).Where(" bk_host_id in (?) and status != ? ", hostIds,
model.Prepoccupied).Find(&rs).Error
if err != nil {
c.SendResponse(r, err, err.Error(), requestId)
c.SendResponse(r, err, err.Error())
return
}
if len(rs) > 0 {
var errMsg string
for _, v := range rs {
errMsg += fmt.Sprintf("%s:%s\n", v.IP, v.Status)
}
c.SendResponse(r, fmt.Errorf("the following example:%s,abnormal state", errMsg), "", requestId)
c.SendResponse(r, fmt.Errorf("the following example:%s,abnormal state", buildErrMsg(rs)), "")
return
}
// update to used status
Expand All @@ -103,7 +98,7 @@ func (c *ApplyHandler) ConfirmApply(r *gin.Context) {
},
)
if err != nil {
c.SendResponse(r, err, err.Error(), requestId)
c.SendResponse(r, err, err.Error())
return
}
uerr := model.DB.Self.Table(model.TbRpOperationInfoTableName()).Where("request_id = ?",
Expand All @@ -112,7 +107,15 @@ func (c *ApplyHandler) ConfirmApply(r *gin.Context) {
logger.Warn("update tb_rp_operation_info failed %s ", uerr.Error())
}
archive(hostIds)
c.SendResponse(r, nil, "successful", requestId)
c.SendResponse(r, nil, "successful")
}

func buildErrMsg(abnormalRsList []model.TbRpDetail) string {
var errMsg string
for _, v := range abnormalRsList {
errMsg += fmt.Sprintf("%s:%s\n", v.IP, v.Status)
}
return errMsg
}

func archive(bkHostIds []int) {
Expand Down Expand Up @@ -151,20 +154,18 @@ func (c *ApplyHandler) ApplyBase(r *gin.Context, mode string) {
var param apply.RequestInputParam
var pickers []*apply.PickerObject
var err error
var requestId string
if c.Prepare(r, &param) != nil {
return
}
requestId = r.GetString("request_id")
if err = param.ParamCheck(); err != nil {
c.SendResponse(r, errno.ErrApplyResourceParamCheck.AddErr(err), err.Error(), requestId)
c.SendResponse(r, errno.ErrApplyResourceParamCheck.AddErr(err), err.Error())
return
}
// get the resource lock if it is dry run you do not need to acquire it
if !param.DryRun {
lock := newLocker(param.LockKey(), requestId)
lock := newLocker(param.LockKey(), c.RequestId)
if err = lock.Lock(); err != nil {
c.SendResponse(r, errno.ErrResourceLock.AddErr(err), err.Error(), requestId)
c.SendResponse(r, errno.ErrResourceLock.AddErr(err), err.Error())
return
}
defer func() {
Expand All @@ -175,27 +176,29 @@ func (c *ApplyHandler) ApplyBase(r *gin.Context, mode string) {
}()
}
defer func() {
apply.RollBackAllInstanceUnused(pickers)
if err != nil {
apply.RollBackAllInstanceUnused(pickers)
}
}()
pickers, err = apply.CycleApply(param)
if err != nil {
c.SendResponse(r, err, "", requestId)
c.SendResponse(r, errno.ErrResourceinsufficient.Add(param.BuildMessage()+"\n"+err.Error()), "")
return
}
if param.DryRun {
c.SendResponse(r, nil, map[string]interface{}{"check_success": true}, requestId)
c.SendResponse(r, nil, map[string]interface{}{"check_success": true})
return
}
data, err := apply.LockReturnPickers(pickers, mode)
if err != nil {
c.SendResponse(r, errno.ErresourceLockReturn.AddErr(err), nil, requestId)
c.SendResponse(r, errno.ErresourceLockReturn.AddErr(err), nil)
return
}
logger.Info(fmt.Sprintf("The %s, will return %d machines", requestId, len(data)))
logger.Info(fmt.Sprintf("The %s, will return %d machines", c.RequestId, len(data)))
task.ApplyResponeLogChan <- task.ApplyResponeLogItem{
RequestId: requestId,
RequestId: c.RequestId,
Data: data,
}
task.RecordRsOperatorInfoChan <- param.GetOperationInfo(requestId, mode, data)
c.SendResponse(r, nil, data, requestId)
task.RecordRsOperatorInfoChan <- param.GetOperationInfo(c.RequestId, mode, data)
c.SendResponse(r, nil, data)
}
21 changes: 12 additions & 9 deletions dbm-services/common/db-resource/internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import (
"github.com/gin-gonic/gin"
)

// BaseHandler TODO
type BaseHandler struct{}
// BaseHandler base handler
type BaseHandler struct {
RequestId string
}

// Response http respone
type Response struct {
Expand All @@ -39,26 +41,27 @@ func (c *BaseHandler) Prepare(r *gin.Context, schema interface{}) error {
requestId := r.GetString("request_id")
if cmutil.IsEmpty(requestId) {
err := fmt.Errorf("get request id error ~")
c.SendResponse(r, err, nil, requestId)
c.SendResponse(r, err, nil)
return err
}
c.RequestId = requestId
if err := r.ShouldBind(&schema); err != nil {
logger.Error("ShouldBind Failed %s", err.Error())
c.SendResponse(r, err, nil, requestId)
c.SendResponse(r, err, nil)
return err
}
logger.Info("param is %v", schema)
return nil
}

// SendResponse retrnurns a response
func (c *BaseHandler) SendResponse(r *gin.Context, err error, data interface{}, requestId string) {
func (c *BaseHandler) SendResponse(r *gin.Context, err error, data interface{}) {
code, message := errno.DecodeErr(err)
r.JSON(http.StatusOK, Response{
Code: code,
Message: message,
Data: data,
RequestId: requestId,
RequestId: c.RequestId,
})
}

Expand All @@ -76,13 +79,13 @@ func (c *BackStageHandler) RegisterRouter(engine *gin.Engine) {
}
}

// RunModuleCheck 运行模块检查
// RunModuleCheck run module check
func (c BackStageHandler) RunModuleCheck(r *gin.Context) {
err := task.InspectCheckResource()
if err != nil {
logger.Error("inspectCheckResource failed %v", err)
}
c.SendResponse(r, nil, "Check Success", "")
c.SendResponse(r, nil, "Check Success")
}

// RunAsyncCmdb async from cmdb
Expand All @@ -91,5 +94,5 @@ func (c BackStageHandler) RunAsyncCmdb(r *gin.Context) {
if err != nil {
logger.Error("asyncResourceHardInfo failed %v", err)
}
c.SendResponse(r, nil, "async success", "")
c.SendResponse(r, nil, "async success")
}
Loading

0 comments on commit 3fea3a1

Please sign in to comment.