Skip to content

Commit

Permalink
optimize: 优化权限中心拉取主机资源信息的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
breezelxp committed Nov 17, 2020
1 parent c9a1f19 commit 212ff78
Showing 1 changed file with 52 additions and 37 deletions.
89 changes: 52 additions & 37 deletions src/scene_server/auth_server/logics/fetch_instance_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,47 +187,62 @@ func (lgc *Logics) FetchHostInfo(kit *rest.Kit, resourceType iam.TypeID, filter
hosts = append(hosts, hostArr...)
}

cloudIDList := make([]int64, 0)
hostIDList := make([]int64, 0)

for index, host := range hosts {
cloudID, err := util.GetInt64ByInterface(host[common.BKCloudIDField])
if err != nil {
blog.Errorf("parse cloud area id failed, err: %v, host: %+v", err, host)
return nil, kit.CCError.CCErrorf(common.CCErrCommParamsInvalid, common.BKCloudIDField)
}
cloudIDList[index] = cloudID

hostID, err := util.GetInt64ByInterface(host[common.BKHostIDField])
if err != nil {
blog.Errorf("parse host id failed, err: %v, host: %+v", err, host)
return nil, kit.CCError.CCErrorf(common.CCErrCommParamsInvalid, common.BKHostIDField)
}
hostIDList[index] = hostID
}

// get cloud area for display name use
var cloudMap map[int64]string
var err error
if hasName {
cloudIDs := make([]int64, len(hosts))
for index, host := range hosts {
cloudID, err := util.GetInt64ByInterface(host[common.BKCloudIDField])
if err != nil {
blog.Errorf("parse cloud area id failed, err: %v, host: %+v", err, host)
return nil, kit.CCError.CCErrorf(common.CCErrCommParamsInvalid, common.BKCloudIDField)
}

cloudIDs[index] = cloudID
}

cloudMap, err = lgc.getCloudNameMapByIDs(kit, cloudIDs)
cloudMap, err = lgc.getCloudNameMapByIDs(kit, cloudIDList)
if err != nil {
return nil, err
}
}

hostPathMap, err := lgc.getHostIamPath(kit, resourceType, hostIDList)
if err != nil {
return nil, err
}

// covert id and display_name field
for _, host := range hosts {
host[types.IDField] = util.GetStrByInterface(host[common.BKHostIDField])
hostID, err := util.GetInt64ByInterface(host[common.BKHostIDField])
if err != nil {
blog.Errorf("parse host id failed, err: %v, host: %+v", err, host)
return nil, kit.CCError.CCErrorf(common.CCErrCommParamsInvalid, common.BKHostIDField)
}
// add id field
host[types.IDField] = strconv.FormatInt(hostID, 10)

if hasName {
cloudID, err := util.GetInt64ByInterface(host[common.BKCloudIDField])
if err != nil {
blog.Errorf("parse cloud area id failed, err: %v, host: %+v", err, host)
return nil, kit.CCError.CCErrorf(common.CCErrCommParamsInvalid, common.BKCloudIDField)
}

host[types.NameField] = getHostDisplayName(util.GetStrByInterface(host[common.BKHostInnerIPField]), cloudMap[cloudID])
ip := util.GetStrByInterface(host[common.BKHostInnerIPField])
host[types.NameField] = getHostDisplayName(ip, cloudMap[cloudID])
}

if needPath {
host[sdktypes.IamPathKey], err = lgc.getHostIamPath(kit, resourceType, host)
if err != nil {
blog.ErrorJSON("getResourceIamPath failed, error: %s, instance: %s, rid: %s", err.Error(), host, kit.Rid)
return nil, err
}
host[sdktypes.IamPathKey] = hostPathMap[hostID]
}
}
return hosts, nil
Expand Down Expand Up @@ -261,56 +276,56 @@ func (lgc *Logics) getResourceIamPath(kit *rest.Kit, resourceType iam.TypeID, in
return iamPath, nil
}

func (lgc *Logics) getHostIamPath(kit *rest.Kit, resourceType iam.TypeID, host map[string]interface{}) ([]string, error) {
func (lgc *Logics) getHostIamPath(kit *rest.Kit, resourceType iam.TypeID, hostList []int64) (map[int64][]string, error) {
if resourceType != iam.Host {
return nil, kit.CCError.CCErrorf(common.CCErrCommParamsInvalid, common.BKResourceTypeField)
}

hostID, err := util.GetInt64ByInterface(host[common.BKHostIDField])
if err != nil {
blog.Errorf("hostID %v parse int failed, error: %s, rid: %s", host[common.BKHostIDField], err.Error(), kit.Rid)
return nil, err
}

// get host iam path, either in resource pool directory or in business TODO: support host in business module when topology is supported
defaultBizID, err := lgc.GetResourcePoolBizID(kit)
if err != nil {
return nil, err
}

req := &metadata.HostModuleRelationRequest{
HostIDArr: []int64{hostID},
HostIDArr: hostList,
Fields: []string{common.BKHostIDField, common.BKAppIDField, common.BKSetIDField, common.BKModuleIDField},
Page: metadata.BasePage{
Limit: common.BKNoLimit,
},
}
res, err := lgc.CoreAPI.CoreService().Host().GetHostModuleRelation(kit.Ctx, kit.Header, req)
if err != nil {
blog.Errorf("GetHostModuleRelation by host id %d failed, error: %s, rid: %s", hostID, err.Error(), kit.Rid)
blog.Errorf("GetHostModuleRelation by host id %v failed, err: %s, rid: %s", hostList, err, kit.Rid)
return nil, err
}

if !res.Result {
blog.Errorf("GetHostModuleRelation by host id %d failed, error code: %d, error message: %s, rid: %s", hostID, res.Code, res.ErrMsg, kit.Rid)
blog.Errorf("GetHostModuleRelation by host id %v failed, err: %v, rid: %s", hostList,
res.Code, res.ErrMsg, kit.Rid)
return nil, res.CCError()
}

if len(res.Data.Info) == 0 {
return nil, nil
return make(map[int64][]string), nil
}

relationDistinctMap := make(map[string]bool)
iamPath := make([]string, 0)
relationMap := make(map[int64][]string)
for _, relation := range res.Data.Info {
var path string
if relation.AppID == defaultBizID {
path = "/" + string(iam.SysResourcePoolDirectory) + "," + strconv.FormatInt(relation.ModuleID, 10) + "/"
} else {
path = "/" + string(iam.Business) + "," + strconv.FormatInt(relation.AppID, 10) + "/"
}
if !relationDistinctMap[path] {
relationDistinctMap[path] = true
iamPath = append(iamPath, path)

if _, exist := relationMap[relation.HostID]; !exist {
relationMap[relation.HostID] = make([]string, 0)
}

relationMap[relation.HostID] = append(relationMap[relation.HostID], path)

}
return iamPath, nil

return relationMap, nil
}

0 comments on commit 212ff78

Please sign in to comment.