From 8fcce844f57c1db6ebd47a64b56d474805977d34 Mon Sep 17 00:00:00 2001 From: yuanruji Date: Mon, 4 Nov 2024 14:32:25 +0800 Subject: [PATCH] 4444 --- .../internal/controller/manage/rs_manage.go | 108 ++++++++---------- .../db-resource/internal/svr/apply/apply.go | 5 +- 2 files changed, 48 insertions(+), 65 deletions(-) diff --git a/dbm-services/common/db-resource/internal/controller/manage/rs_manage.go b/dbm-services/common/db-resource/internal/controller/manage/rs_manage.go index efc20f1908..9469c464ad 100644 --- a/dbm-services/common/db-resource/internal/controller/manage/rs_manage.go +++ b/dbm-services/common/db-resource/internal/controller/manage/rs_manage.go @@ -85,12 +85,9 @@ func (c *MachineResourceHandler) Delete(r *rf.Context) { // BatchUpdateMachineInput 批量编辑主机信息请求参数 type BatchUpdateMachineInput struct { - BkHostIds []int `json:"bk_host_ids" binding:"required,dive,gt=0" ` - ForBiz *int `json:"for_biz,omitempty"` - Labels []string `json:"labels"` - RsType *string `json:"resource_type,omitempty"` - RackId string `json:"rack_id"` - StorageDevice map[string]bk.DiskDetail `json:"storage_device"` + BkHostIds []int `json:"bk_host_ids" binding:"required,dive,gt=0" ` + RackId string `json:"rack_id"` + UpdateRsMeta } const ( @@ -102,48 +99,20 @@ const ( func (c *MachineResourceHandler) BatchUpdate(r *rf.Context) { var input BatchUpdateMachineInput var err error - var lableJson, storageJson []byte - - updateMap := make(map[string]interface{}) - if err = c.Prepare(r, &input); err != nil { logger.Error("Preare Error %s", err.Error()) return } - // update for biz - if input.ForBiz != nil { - updateMap["dedicated_biz"] = input.ForBiz - } - - // update resource type - if input.RsType != nil { - updateMap["rs_type"] = input.RsType - } - lableJson, err = json.Marshal(input.Labels) + updateMap, err := input.getUpdateMap() if err != nil { - logger.Error(fmt.Sprintf("ConverLableToJsonStr Failed,Error:%s", err.Error())) c.SendResponse(r, err, err.Error()) return } - updateMap["labels"] = lableJson - - // update disk - if len(input.StorageDevice) > 0 { - storageJson, err = json.Marshal(input.StorageDevice) - if err != nil { - logger.Error(fmt.Sprintf("conver resource types Failed,Error:%s", err.Error())) - c.SendResponse(r, err, err.Error()) - return - } - updateMap["storage_device"] = storageJson - } - // update rack id if cmutil.IsNotEmpty(input.RackId) { updateMap["rack_id"] = input.RackId } - // do update err = model.DB.Self.Table(model.TbRpDetailName()).Select("dedicated_biz", "rs_type", "storage_device", "rack_id", "labels"). @@ -152,7 +121,6 @@ func (c *MachineResourceHandler) BatchUpdate(r *rf.Context) { c.SendResponse(r, err, err.Error()) return } - // return respone c.SendResponse(r, nil, "ok") } @@ -164,13 +132,46 @@ type MachineResourceInputParam struct { // MachineResource 主机参数 type MachineResource struct { - BkHostID int `json:"bk_host_id" binding:"required"` - Labels []string `json:"labels"` + BkHostID int `json:"bk_host_id" binding:"required"` + UpdateRsMeta +} + +// UpdateRsMeta TODO +type UpdateRsMeta struct { + Labels *[]string `json:"labels"` ForBiz *int `json:"for_biz"` RsType *string `json:"resource_type"` StorageDevice map[string]bk.DiskDetail `json:"storage_device"` } +func (v UpdateRsMeta) getUpdateMap() (updateMap map[string]interface{}, err error) { + var lableJson, storageJson []byte + updateMap = make(map[string]interface{}) + if v.Labels != nil { + lableJson, err = json.Marshal(*v.Labels) + if err != nil { + logger.Error(fmt.Sprintf("ConverLableToJsonStr Failed,Error:%s", err.Error())) + return updateMap, err + } + updateMap["labels"] = lableJson + } + if v.ForBiz != nil { + updateMap["dedicated_biz"] = v.ForBiz + } + if v.RsType != nil { + updateMap["rs_type"] = v.RsType + } + if len(v.StorageDevice) > 0 { + storageJson, err = json.Marshal(v.StorageDevice) + if err != nil { + logger.Error(fmt.Sprintf("conver resource types Failed,Error:%s", err.Error())) + return updateMap, err + } + updateMap["storage_device"] = storageJson + } + return updateMap, err +} + // Update 编辑主机信息 func (c *MachineResourceHandler) Update(r *rf.Context) { var input MachineResourceInputParam @@ -181,32 +182,13 @@ func (c *MachineResourceHandler) Update(r *rf.Context) { logger.Debug(fmt.Sprintf("get params %v", input.Data)) tx := model.DB.Self.Begin() for _, v := range input.Data { - updateMap := make(map[string]interface{}) - if len(v.Labels) > 0 { - lableJson, err := json.Marshal(v.Labels) - if err != nil { - logger.Error(fmt.Sprintf("ConverLableToJsonStr Failed,Error:%s", err.Error())) - c.SendResponse(r, err, err.Error()) - return - } - updateMap["labels"] = lableJson - } - if v.ForBiz != nil { - updateMap["dedicated_biz"] = v.ForBiz - } - if v.RsType != nil { - updateMap["rs_type"] = v.RsType - } - if len(v.StorageDevice) > 0 { - storageJson, err := json.Marshal(v.StorageDevice) - if err != nil { - logger.Error(fmt.Sprintf("conver resource types Failed,Error:%s", err.Error())) - c.SendResponse(r, err, err.Error()) - return - } - updateMap["storage_device"] = storageJson + updateMap, err := v.getUpdateMap() + if err != nil { + logger.Error("conver resource types Failed,Error:%s", err.Error()) + c.SendResponse(r, err, err.Error()) + return } - err := tx.Model(&model.TbRpDetail{}).Table(model.TbRpDetailName()).Select("dedicated_biz", "rs_type", + err = tx.Model(&model.TbRpDetail{}).Table(model.TbRpDetailName()).Select("dedicated_biz", "rs_type", "labels").Where("bk_host_id=?", v.BkHostID).Updates(updateMap).Error if err != nil { tx.Rollback() diff --git a/dbm-services/common/db-resource/internal/svr/apply/apply.go b/dbm-services/common/db-resource/internal/svr/apply/apply.go index 453508c111..eacfc0869f 100644 --- a/dbm-services/common/db-resource/internal/svr/apply/apply.go +++ b/dbm-services/common/db-resource/internal/svr/apply/apply.go @@ -106,10 +106,11 @@ func RollBackAllInstanceUnused(ms []*PickerObject) { func (o *SearchContext) pickBase(db *gorm.DB) { // 如果指定了特殊资源,就只查询这些资源 if len(o.SpecialHostIds) > 0 { - db.Where("bk_host_id in (?) and status = ? ", o.SpecialHostIds, model.Unused) + db.Where("bk_host_id in (?) and status = ? and and gse_agent_status_code = ? ", o.SpecialHostIds, model.Unused, + bk.GSE_AGENT_OK) return } - db.Where(" bk_cloud_id = ? and status = ? and gse_agent_status_code = ? ", o.BkCloudId, model.Unused, bk.GSE_AGENT_OK) + db.Where("bk_cloud_id = ? and status = ? and gse_agent_status_code = ? ", o.BkCloudId, model.Unused, bk.GSE_AGENT_OK) o.MatchIntetionBkBiz(db) o.MatchRsType(db)