Skip to content

Commit

Permalink
fix 666
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq committed Nov 1, 2024
1 parent 1e60938 commit 87d0be4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type ImportMachParam struct {
BkBizId int `json:"bk_biz_id" binding:"number"`
Hosts []HostBase `json:"hosts" binding:"gt=0,dive,required"`
Labels []string `json:"labels"`
// return_resource 如果为ture,则资源导入的时候 检查主机是否存在,存在的话 就把预占用标记改成空闲。 如果主机不存在 则正常导入
ReturnResource bool `json:"return_resource"`
apply.ActionInfo
}

Expand Down Expand Up @@ -98,7 +100,7 @@ func (c *MachineResourceHandler) Import(r *rf.Context) {
c.SendResponse(r, errno.RepeatedIpExistSystem.Add(err.Error()), err.Error())
return
}
resp, err := Doimport(input)
resp, err := Doimport(input, c.RequestId)
if err != nil {
logger.Error(fmt.Sprintf("ImportByIps failed %s", err.Error()))
c.SendResponse(r, err, err.Error())
Expand Down Expand Up @@ -136,15 +138,90 @@ func (p ImportMachParam) getJobIpList() (ipList []bk.IPList) {
})
}

// Doimport 导入主机获取主机信息
func Doimport(param ImportMachParam) (resp *ImportHostResp, err error) {
// resetPrepoccupiedResource 重置选中确认的资源改成unused
func resetPrepoccupiedResource(param ImportMachParam, requestId string) (resetIpList []string, err error) {
ipGroupbyCloudIdMap := param.getIpsByCloudId()
var allprepoccupiedRsList []model.TbRpDetail
for cloudId, ips := range ipGroupbyCloudIdMap {
var prepoccupiedRsList []model.TbRpDetail
err = model.DB.Self.Table(model.TbRpDetailName()).Where("bk_cloud_id = ? and ip in (?) and status = ? ", cloudId, ips,
model.Prepoccupied).Scan(&prepoccupiedRsList).Error
if err != nil {
return nil, errno.ErrDBQuery.Add(err.Error())
}
allprepoccupiedRsList = append(allprepoccupiedRsList, prepoccupiedRsList...)
}
if len(allprepoccupiedRsList) == 0 {
logger.Info("resources without preoccupied status need to be processed")
return
}
defer func() {
var desc string
status := model.StatusSuccess
if err != nil {
status = model.StatusFailed
desc = fmt.Sprintf("failed to reset prepoccupied status, error:%s", err.Error())
}
ipListBytes, errx := json.Marshal(resetIpList)
if errx != nil {
desc += "failed to serialize ipList"
logger.Error("json marshal failed %s", errx.Error())
}
// insert operation log
model.DB.Self.Table(model.TbRpOperationInfoTableName()).Create(&model.TbRpOperationInfo{
RequestID: requestId,
TotalCount: len(allprepoccupiedRsList),
IpList: ipListBytes,
OperationType: "RESET_PREPOCUPED_TO_UNUSED",
Operator: param.ActionInfo.Operator,
BillId: param.ActionInfo.BillId,
Description: desc,
Status: status,
CreateTime: time.Now(),
UpdateTime: time.Now(),
})
}()
// get all primary key
ids := lo.Map(allprepoccupiedRsList, func(item model.TbRpDetail, _ int) int {
return item.ID
})
resetIpList = lo.Map(allprepoccupiedRsList, func(item model.TbRpDetail, _ int) string {
return item.IP
})
db := model.DB.Self.Table(model.TbRpDetailName()).Where("id in (?) and status = ? ", ids, model.Prepoccupied).
Update("status", model.Unused)
if err = db.Error; err != nil {
logger.Error("reset prepoccupied status failed %s", err.Error())
return
}
if db.RowsAffected != int64(len(allprepoccupiedRsList)) {
logger.Error("reset prepoccupied status failed %d rows affected", db.RowsAffected)
return
}
return
}

// Doimport TODO
func Doimport(param ImportMachParam, requestId string) (resp *ImportHostResp, err error) {
var ccHostsInfo []*cc.Host
var derr error
var diskResp bk.GetDiskResp
var notFoundHosts, gseAgentIds []string
var elems []model.TbRpDetail
resp = &ImportHostResp{}
targetHosts := lo.Uniq(param.getIps())

if param.ReturnResource {
resetIpList, errx := resetPrepoccupiedResource(param, requestId)
if errx != nil {
return resp, errx
}
subtargetHosts, _ := lo.Difference(targetHosts, resetIpList)
targetHosts = subtargetHosts
}
if len(targetHosts) == 0 {
return resp, nil
}
ccHostsInfo, notFoundHosts, derr = bk.BatchQueryHostsInfo(param.BkBizId, targetHosts)
if derr != nil {
logger.Error("query cc info from cmdb failed %s", derr.Error())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ const (
Imported = "imported"
)

const (
// StatusSuccess operator success
StatusSuccess = "success"
// StatusFailed failed
StatusFailed = "failed"
)

// TbRpOperationInfo 资源池操作记录表
// nolint
type TbRpOperationInfo struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (o *SearchContext) PickInstance() (picker *PickerObject, err error) {
// MatchLables match lables
func (o *SearchContext) MatchLables(db *gorm.DB) {
if len(o.Labels) > 0 {
db.Where(model.JSONQuery("labels").JointOrContains(o.Labels))
db.Where(model.JSONQuery("label").JointOrContains(o.Labels))
} else {
// 如果请求没有标签, 只能匹配没有标签的资源
db.Where(" JSON_TYPE(label) = 'NULL' OR JSON_LENGTH(label) < 1 ")
Expand Down
2 changes: 1 addition & 1 deletion dbm-services/common/db-resource/internal/svr/bk/cc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestReserverCC(t *testing.T) {
RsType: "MySQL",
Hosts: hosts,
}
importResp, err := manage.Doimport(param)
importResp, err := manage.Doimport(param, "")
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 87d0be4

Please sign in to comment.