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 Oct 28, 2024
1 parent 8d96e3c commit 0100106
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 210 deletions.
38 changes: 18 additions & 20 deletions dbm-services/common/db-resource/internal/controller/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,33 +65,31 @@ func (c *ApplyHandler) ConfirmApply(r *gin.Context) {
if c.Prepare(r, &param) != nil {
return
}
requestId := r.GetString("request_id")
hostIds := cmutil.RemoveDuplicate(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", errMsg), "")
return
}
// update to used status
Expand All @@ -103,7 +101,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 +110,7 @@ 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 archive(bkHostIds []int) {
Expand Down Expand Up @@ -151,20 +149,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 +171,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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ import (
// ImportMachParam 资源导入请求参数
type ImportMachParam struct {
// ForBizs 业务标签,表示这个资源将来给ForBizs这个业务使用
ForBiz int `json:"for_biz"`
RsType string `json:"resource_type"`
BkBizId int `json:"bk_biz_id" binding:"number"`
Hosts []HostBase `json:"hosts" binding:"gt=0,dive,required"`
Labels map[string]string `json:"labels"`
ForBiz int `json:"for_biz"`
RsType string `json:"resource_type"`
BkBizId int `json:"bk_biz_id" binding:"number"`
Hosts []HostBase `json:"hosts" binding:"gt=0,dive,required"`
Labels []string `json:"labels"`
apply.ActionInfo
}

Expand Down Expand Up @@ -82,7 +82,7 @@ func (p *ImportMachParam) existCheck() (err error) {
for _, r := range alreadyExistRs {
errMsg += fmt.Sprintf(" bk_cloud_id:%d,ip:%s \n", r.BkCloudID, r.IP)
}
return fmt.Errorf(errMsg)
return fmt.Errorf("%s", errMsg)
}
}
return nil
Expand All @@ -95,22 +95,22 @@ func (c *MachineResourceHandler) Import(r *rf.Context) {
logger.Error(fmt.Sprintf("Preare Error %s", err.Error()))
return
}
requestId := r.GetString("request_id")
// requestId := r.GetString("request_id")
if err := input.existCheck(); err != nil {
c.SendResponse(r, errno.RepeatedIpExistSystem.Add(err.Error()), requestId, err.Error())
c.SendResponse(r, errno.RepeatedIpExistSystem.Add(err.Error()), err.Error())
return
}
resp, err := Doimport(input)
if err != nil {
logger.Error(fmt.Sprintf("ImportByIps failed %s", err.Error()))
c.SendResponse(r, err, requestId, err.Error())
c.SendResponse(r, err, err.Error())
return
}
if len(resp.NotFoundInCCHosts) == len(input.Hosts) {
c.SendResponse(r, fmt.Errorf("all machines failed to query cmdb information"), resp, requestId)
c.SendResponse(r, fmt.Errorf("all machines failed to query cmdb information"), resp)
return
}
c.SendResponse(r, err, resp, requestId)
c.SendResponse(r, err, resp)
}

// ImportHostResp 导入主机参数
Expand All @@ -121,8 +121,7 @@ type ImportHostResp struct {
}

func (p ImportMachParam) transParamToBytes() (lableJson json.RawMessage, err error) {
// lableJson = []byte("{}")
lableJson, err = json.Marshal(cmutil.CleanStrMap(p.Labels))
lableJson, err = json.Marshal(p.Labels)
if err != nil {
logger.Error(fmt.Sprintf("ConverLableToJsonStr Failed,Error:%s", err.Error()))
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ func (c *LableHandler) Edit(r *rf.Context) {
logger.Error(fmt.Sprintf("Preare Error %s", err.Error()))
return
}
requestId := r.GetString("request_id")
// requestId := r.GetString("request_id")
lableJson, err := cmutil.ConverMapToJsonStr(cmutil.CleanStrMap(input.Labels))
if err != nil {
logger.Error(fmt.Sprintf("ConverLableToJsonStr Failed,Error:%s", err.Error()))
c.SendResponse(r, err, nil, requestId)
c.SendResponse(r, err, nil)
return
}
if len(input.BkHostIds) == 0 {
c.SendResponse(r, nil, nil, requestId)
c.SendResponse(r, nil, nil)
return
}
err = model.DB.Self.Table(model.TbRpDetailName()).Where("bk_host_id in ? ", input.BkHostIds).Update("label",
lableJson).
Error
if err != nil {
logger.Error(fmt.Sprintf("Update Lable Failed %s", err.Error()))
c.SendResponse(r, err, nil, requestId)
c.SendResponse(r, err, nil)
return
}
c.SendResponse(r, nil, nil, requestId)
c.SendResponse(r, nil, nil)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type MachineResourceGetterInputParam struct {
City []string `json:"city"`
SubZoneIds []string `json:"subzone_ids"`
DeviceClass []string `json:"device_class"`
Labels map[string]string `json:"labels"`
Labels []string `json:"labels"`
Hosts []string `json:"hosts"`
BkCloudIds []int `json:"bk_cloud_ids"`
RsType string `json:"resource_type"`
Expand Down Expand Up @@ -62,29 +62,28 @@ func (c *MachineResourceHandler) List(r *rf.Context) {
if c.Prepare(r, &input) != nil {
return
}
requestId := r.GetString("request_id")
if err := input.paramCheck(); err != nil {
c.SendResponse(r, errno.ErrErrInvalidParam.AddErr(err), nil, requestId)
c.SendResponse(r, errno.ErrErrInvalidParam.AddErr(err), nil)
return
}
db := model.DB.Self.Table(model.TbRpDetailName())
if err := input.queryBs(db); err != nil {
c.SendResponse(r, err, requestId, err.Error())
c.SendResponse(r, err, err.Error())
return
}
if err := db.Count(&count).Error; err != nil {
c.SendResponse(r, err, requestId, err.Error())
c.SendResponse(r, err, err.Error())
return
}
if input.Limit > 0 {
db = db.Offset(input.Offset).Limit(input.Limit)
}
var data []model.TbRpDetail
if err := db.Find(&data).Error; err != nil {
c.SendResponse(r, errno.ErrDBQuery.AddErr(err), requestId, err.Error())
c.SendResponse(r, errno.ErrDBQuery.AddErr(err), err.Error())
return
}
c.SendResponse(r, nil, map[string]interface{}{"details": data, "count": count}, requestId)
c.SendResponse(r, nil, map[string]interface{}{"details": data, "count": count})
}

func (c *MachineResourceGetterInputParam) paramCheck() (err error) {
Expand Down Expand Up @@ -193,7 +192,9 @@ func (c *MachineResourceGetterInputParam) queryBs(db *gorm.DB) (err error) {
if len(c.SubZoneIds) > 0 {
db.Where(" sub_zone_id in (?) ", c.SubZoneIds)
}

if len(c.Labels) > 0 {
db.Where(model.JSONQuery("labels").JointOrContains(c.Labels))
}
if cmutil.IsNotEmpty(c.OsType) {
db.Where("os_type = ?", c.OsType)
}
Expand All @@ -203,19 +204,19 @@ func (c *MachineResourceGetterInputParam) queryBs(db *gorm.DB) (err error) {

// ListAll TODO
func (c *MachineResourceHandler) ListAll(r *rf.Context) {
requestId := r.GetString("request_id")
// requestId := r.GetString("request_id")
var data []model.TbRpDetail
db := model.DB.Self.Table(model.TbRpDetailName()).Where("status in (?)", []string{model.Unused, model.Prepoccupied,
model.Preselected})
err := db.Scan(&data).Error
if err != nil {
c.SendResponse(r, err, requestId, err.Error())
c.SendResponse(r, err, err.Error())
return
}
var count int64
if err := db.Count(&count).Error; err != nil {
c.SendResponse(r, err, requestId, err.Error())
c.SendResponse(r, err, err.Error())
return
}
c.SendResponse(r, nil, map[string]interface{}{"details": data, "count": count}, requestId)
c.SendResponse(r, nil, map[string]interface{}{"details": data, "count": count})
}
Loading

0 comments on commit 0100106

Please sign in to comment.