Skip to content

Commit

Permalink
fix(dbm-services): fix 资源导入云区域bug close #1588
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq authored and yksitu committed Oct 31, 2023
1 parent a1538ee commit 572246c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (

// ImportMachParam TODO
type ImportMachParam struct {
BkCloudId int `json:"bk_cloud_id"`
// ForBizs 业务标签,表示这个资源将来给ForBizs这个业务使用
ForBizs []int `json:"for_bizs"`
RsTypes []string `json:"resource_types"`
Expand All @@ -43,8 +42,9 @@ type ImportMachParam struct {

// HostBase TODO
type HostBase struct {
Ip string `json:"ip" binding:"required,ip"`
HostId int `json:"host_id" binding:"required"`
Ip string `json:"ip" binding:"required,ip"`
HostId int `json:"host_id" binding:"required"`
BkCloudId int `json:"bk_cloud_id"`
}

func (p ImportMachParam) getOperationInfo(requestId string, hostIds json.RawMessage,
Expand Down Expand Up @@ -72,6 +72,16 @@ func (p ImportMachParam) getIps() (ips []string) {
return
}

func (p ImportMachParam) getIpsByCloudId() (ipMap map[int][]string) {
ipMap = make(map[int][]string)
for _, v := range p.Hosts {
if !cmutil.IsEmpty(v.Ip) {
ipMap[v.BkCloudId] = append(ipMap[v.BkCloudId], v.Ip)
}
}
return
}

func (p ImportMachParam) getHostIds() (hostIds []int) {
for _, v := range p.Hosts {
if v.HostId > 0 {
Expand All @@ -83,17 +93,20 @@ func (p ImportMachParam) getHostIds() (hostIds []int) {

func (p *ImportMachParam) existCheck() (err error) {
var alreadyExistRs []model.TbRpDetail
err = model.DB.Self.Table(model.TbRpDetailName()).Where("bk_cloud_id = ? and ip in (?)", p.BkCloudId, p.getIps()).
Scan(&alreadyExistRs).Error
if err != nil {
return errno.ErrDBQuery.Add(err.Error())
}
if len(alreadyExistRs) > 0 {
errMsg := "already exist:\n "
for _, r := range alreadyExistRs {
errMsg += fmt.Sprintf(" bk_cloud_id:%d,ip:%s \n", r.BkCloudID, r.IP)
ipmap := p.getIpsByCloudId()
for cloudId, ips := range ipmap {
err = model.DB.Self.Table(model.TbRpDetailName()).Where("bk_cloud_id = ? and ip in (?)", cloudId, ips).
Scan(&alreadyExistRs).Error
if err != nil {
return errno.ErrDBQuery.Add(err.Error())
}
if len(alreadyExistRs) > 0 {
errMsg := "already exist:\n "
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(errMsg)
}
return nil
}
Expand Down Expand Up @@ -184,7 +197,14 @@ func Doimport(param ImportMachParam) (resp *ImportHostResp, err error) {
ccHostsInfo, notFoundHosts, derr = bk.BatchQueryHostsInfo(param.BkBizId, targetHosts)
}()
// get disk information in batch
diskResp, err = bk.GetDiskInfo(targetHosts, param.BkCloudId, param.BkBizId)
var ipList []bk.IPList
for _, host := range param.Hosts {
ipList = append(ipList, bk.IPList{
IP: host.Ip,
BkCloudID: host.BkCloudId,
})
}
diskResp, err = bk.GetDiskInfo(ipList, param.BkBizId)
if err != nil {
logger.Error("query host cc info failed %s", err.Error())
return resp, err
Expand Down Expand Up @@ -217,12 +237,12 @@ func Doimport(param ImportMachParam) (resp *ImportHostResp, err error) {
logger.Info("more info %v", ccHostsInfo)
for _, h := range ccHostsInfo {
delete(hostsMap, h.InnerIP)
el := transHostInfoToDbModule(h, param.BkCloudId, param.BkBizId, rstypes, bizJson, lableJson)
el := transHostInfoToDbModule(h, h.BkCloudId, param.BkBizId, rstypes, bizJson, lableJson)
el.SetMore(h.InnerIP, diskResp.IpLogContentMap)
// gse agent 1.0的 agent 是用 cloudid:ip
gseAgentId := h.BkAgentId
if cmutil.IsEmpty(gseAgentId) {
gseAgentId = fmt.Sprintf("%d:%s", param.BkCloudId, h.InnerIP)
gseAgentId = fmt.Sprintf("%d:%s", h.BkCloudId, h.InnerIP)
}
gseAgentIds = append(gseAgentIds, gseAgentId)
el.BkAgentId = gseAgentId
Expand Down
7 changes: 3 additions & 4 deletions dbm-services/common/db-resource/internal/svr/bk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ func getIpList(hosts []string, bk_cloud_id int) []IPList {
}

// GetDiskInfo TODO
func GetDiskInfo(hosts []string, bk_cloud_id, bk_biz_id int) (resp GetDiskResp, err error) {
iplist := getIpList(hosts, bk_cloud_id)
func GetDiskInfo(hosts []IPList, bk_biz_id int) (resp GetDiskResp, err error) {
jober := JobV3{
Client: EsbClient,
}
Expand All @@ -129,7 +128,7 @@ func GetDiskInfo(hosts []string, bk_cloud_id, bk_biz_id int) (resp GetDiskResp,
ScriptLanguage: 1,
AccountAlias: "root",
TargetServer: TargetServer{
IPList: iplist,
IPList: hosts,
},
},
)
Expand Down Expand Up @@ -174,7 +173,7 @@ func GetDiskInfo(hosts []string, bk_cloud_id, bk_biz_id int) (resp GetDiskResp,
BKBizId: bk_biz_id,
JobInstanceID: job.JobInstanceID,
StepInstanceID: job.StepInstanceID,
IPList: iplist,
IPList: hosts,
})
resp.IpLogContentMap = make(map[string]*ShellResCollection)
for _, d := range ipLogs.ScriptTaskLogs {
Expand Down
1 change: 1 addition & 0 deletions dbm-services/common/go-pubpkg/cc.v3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type Host struct {
BkCpu int `json:"bk_cpu,omitempty"`
BkMem int `json:"bk_mem,omitempty"`
BkDisk int `json:"bk_disk"`
BkCloudId int `json:"bk_cloud_id"`
BkAgentId string `json:"bk_agent_id,omitempty"`
SvrTypeName string `json:"svr_type_name"`
BKBSInfos []*CMDBInfo `json:"bk_bs_info"`
Expand Down
2 changes: 1 addition & 1 deletion helm-charts/bk-dbm/charts/db-resource/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: v2
appVersion: 0.0.1-alpha.47
appVersion: 0.0.1-alpha.48
description: A Helm chart for Kubernetes
name: db-resource
type: application
Expand Down

0 comments on commit 572246c

Please sign in to comment.