@@ -87,14 +87,16 @@ func NewInstOperation(client apimachinery.ClientSetInterface, lang language.CCLa
8787
8888// ObjectWithInsts a struct include object msg and insts array
8989type ObjectWithInsts struct {
90+ AssoID int64
91+ instID int64
9092 Object metadata.Object
91- Insts [] mapstr.MapStr
93+ Inst mapstr.MapStr
9294}
9395
9496// ObjectAssoPair a struct include object msg and association
9597type ObjectAssoPair struct {
96- Object metadata.Object
97- AssocName string
98+ Object metadata.Object
99+ AssocNames [] string
98100}
99101
100102// ConditionItem subcondition
@@ -1048,7 +1050,7 @@ func (c *commonInst) findInstTopo(kit *rest.Kit, objID string, instID int64, nee
10481050
10491051 tmpResults := map [string ]* metadata.CommonInstTopo {}
10501052
1051- topoInsts , relation , err := c .getAssociatedObjectWithInsts (kit , objID , instID , needChild )
1053+ topoInsts , err := c .getAssociatedObjectWithInsts (kit , objID , instID , needChild )
10521054 if err != nil {
10531055 return 0 , nil , err
10541056 }
@@ -1066,32 +1068,31 @@ func (c *commonInst) findInstTopo(kit *rest.Kit, objID string, instID int64, nee
10661068 tmpResults [object .ObjectID ] = commonInst
10671069 }
10681070
1069- commonInst .Count = commonInst .Count + len (topoInst .Insts )
1071+ commonInst .Count = commonInst .Count + len (topoInst .Inst )
10701072
1071- for _ , inst := range topoInst .Insts {
1073+ inst := topoInst .Inst
10721074
1073- id , err := inst .Int64 (metadata .GetInstIDFieldByObjID (object .ObjectID ))
1074- if err != nil {
1075- return 0 , nil , err
1076- }
1077-
1078- name , err := inst .String (metadata .GetInstNameFieldName (object .ObjectID ))
1079- if err != nil {
1080- return 0 , nil , err
1081- }
1075+ id , err := inst .Int64 (metadata .GetInstIDFieldByObjID (object .ObjectID ))
1076+ if err != nil {
1077+ return 0 , nil , err
1078+ }
10821079
1083- instAsst := metadata.InstNameAsst {
1084- ID : strconv .Itoa (int (id )),
1085- InstID : id ,
1086- InstName : name ,
1087- ObjectName : object .ObjectName ,
1088- ObjIcon : object .ObjIcon ,
1089- ObjID : object .ObjectID ,
1090- AssoID : relation [id ],
1091- }
1080+ name , err := inst .String (metadata .GetInstNameFieldName (object .ObjectID ))
1081+ if err != nil {
1082+ return 0 , nil , err
1083+ }
10921084
1093- tmpResults [object .ObjectID ].Children = append (tmpResults [object .ObjectID ].Children , instAsst )
1085+ instAsst := metadata.InstNameAsst {
1086+ ID : strconv .Itoa (int (id )),
1087+ InstID : id ,
1088+ InstName : name ,
1089+ ObjectName : object .ObjectName ,
1090+ ObjIcon : object .ObjIcon ,
1091+ ObjID : object .ObjectID ,
1092+ AssoID : topoInst .AssoID ,
10941093 }
1094+
1095+ tmpResults [object .ObjectID ].Children = append (tmpResults [object .ObjectID ].Children , instAsst )
10951096 }
10961097
10971098 results := make ([]* metadata.CommonInstTopo , 0 )
@@ -1443,7 +1444,7 @@ func (c *commonInst) innerHasHost(kit *rest.Kit, moduleIDs []int64) (bool, error
14431444// getAssociatedObjectWithInsts TODO
14441445// GetObjectWithInsts get object with insts, get parent or child depends on needChild
14451446func (c * commonInst ) getAssociatedObjectWithInsts (kit * rest.Kit , objID string , instID int64 , needChild bool ) (
1446- []* ObjectWithInsts , map [ int64 ] int64 , error ) {
1447+ []* ObjectWithInsts , error ) {
14471448
14481449 cond := mapstr .New ()
14491450 if needChild {
@@ -1455,16 +1456,15 @@ func (c *commonInst) getAssociatedObjectWithInsts(kit *rest.Kit, objID string, i
14551456 objPairs , err := c .searchAssoObjects (kit , needChild , cond )
14561457 if err != nil {
14571458 blog .Errorf ("failed to get the object(%s)'s parent, err: %v, rid: %s" , objID , err , kit .Rid )
1458- return nil , nil , err
1459+ return nil , err
14591460 }
14601461
1461- relation := make (map [int64 ]int64 )
14621462 result := make ([]* ObjectWithInsts , 0 )
14631463 for _ , objPair := range objPairs {
14641464
14651465 queryCond := & metadata.InstAsstQueryCondition {
14661466 Cond : metadata.QueryCondition {Condition : mapstr.MapStr {
1467- common .AssociationObjAsstIDField : objPair .AssocName ,
1467+ common .AssociationObjAsstIDField : mapstr. MapStr { common . BKDBIN : objPair .AssocNames } ,
14681468 }},
14691469 ObjID : objPair .Object .ObjectID ,
14701470 }
@@ -1482,7 +1482,7 @@ func (c *commonInst) getAssociatedObjectWithInsts(kit *rest.Kit, objID string, i
14821482 rsp , err := c .clientSet .CoreService ().Association ().ReadInstAssociation (kit .Ctx , kit .Header , queryCond )
14831483 if err != nil {
14841484 blog .Errorf ("search inst association failed , err: %v, rid: %s" , err , kit .Rid )
1485- return nil , nil , err
1485+ return nil , err
14861486 }
14871487
14881488 // found no noe inst association with this object and association info.
@@ -1499,9 +1499,17 @@ func (c *commonInst) getAssociatedObjectWithInsts(kit *rest.Kit, objID string, i
14991499 } else {
15001500 instID = item .InstID
15011501 }
1502- relation [instID ] = item .ID
15031502 instIDs = append (instIDs , instID )
1503+
1504+ inst := ObjectWithInsts {
1505+ AssoID : item .ID ,
1506+ instID : instID ,
1507+ Object : objPair .Object ,
1508+ Inst : nil ,
1509+ }
1510+ result = append (result , & inst )
15041511 }
1512+ instIDs = util .IntArrayUnique (instIDs )
15051513
15061514 innerCond := & metadata.QueryCondition {
15071515 Condition : mapstr.MapStr {objPair .Object .GetInstIDFieldName (): mapstr.MapStr {common .BKDBIN : instIDs }},
@@ -1511,18 +1519,31 @@ func (c *commonInst) getAssociatedObjectWithInsts(kit *rest.Kit, objID string, i
15111519 if objPair .Object .IsCommon () {
15121520 innerCond .Condition [common .BKObjIDField ] = objPair .Object .ObjectID
15131521 }
1514-
15151522 rspItems , err := c .FindInst (kit , objPair .Object .ObjectID , innerCond )
15161523 if err != nil {
1517- blog .Errorf ("failed to search the insts by the condition(%#v), err: %v, rid: %s" , innerCond , err , kit .Rid )
1518- return result , nil , err
1524+ blog .Errorf ("failed to search the inst by the condition(%#v), err: %v, rid: %s" , innerCond , err , kit .Rid )
1525+ return nil , err
15191526 }
15201527
1521- rstObj := & ObjectWithInsts {Object : objPair .Object , Insts : rspItems .Info }
1522- result = append (result , rstObj )
1528+ tempMap := make (map [int64 ]mapstr.MapStr )
1529+ for _ , info := range rspItems .Info {
1530+ id , err := info .Int64 (common .GetInstIDField (objPair .Object .ObjectID ))
1531+ if err != nil {
1532+ return nil , err
1533+ }
1534+ tempMap [id ] = info
1535+ }
1536+
1537+ for i , inst := range result {
1538+ if value , ok := tempMap [inst .instID ]; ok {
1539+ result [i ].Inst = value
1540+ continue
1541+ }
1542+ return nil , fmt .Errorf ("failed to find the inst (%v) by the condition(%#v)" , inst , queryCond )
1543+ }
15231544 }
15241545
1525- return result , relation , nil
1546+ return result , nil
15261547}
15271548
15281549func (c * commonInst ) searchAssoObjects (kit * rest.Kit , needChild bool , cond mapstr.MapStr ) ([]ObjectAssoPair ,
@@ -1544,18 +1565,27 @@ func (c *commonInst) searchAssoObjects(kit *rest.Kit, needChild bool, cond mapst
15441565 return make ([]ObjectAssoPair , 0 ), nil
15451566 }
15461567
1547- objAssoMap := make (map [string ]metadata. Association , 0 )
1568+ objAssoMap := make (map [string ][] string , len ( rsp . Info ) )
15481569 var objIDArray []string
15491570 for _ , asst := range rsp .Info {
15501571 if needChild {
15511572 objIDArray = append (objIDArray , asst .AsstObjID )
1552- objAssoMap [asst .AsstObjID ] = asst
1573+ if len (objAssoMap [asst .AsstObjID ]) == 0 {
1574+ objAssoMap [asst .AsstObjID ] = []string {asst .AssociationName }
1575+ continue
1576+ }
1577+ objAssoMap [asst .AsstObjID ] = append (objAssoMap [asst .AsstObjID ], asst .AssociationName )
15531578 } else {
15541579 objIDArray = append (objIDArray , asst .ObjectID )
1555- objAssoMap [asst .ObjectID ] = asst
1580+ if len (objAssoMap [asst .ObjectID ]) == 0 {
1581+ objAssoMap [asst .ObjectID ] = []string {asst .AssociationName }
1582+ continue
1583+ }
1584+ objAssoMap [asst .ObjectID ] = append (objAssoMap [asst .ObjectID ], asst .AssociationName )
15561585 }
15571586 }
15581587
1588+ objIDArray = util .StrArrayUnique (objIDArray )
15591589 queryCond := & metadata.QueryCondition {
15601590 Condition : mapstr.MapStr {metadata .ModelFieldObjectID : mapstr.MapStr {common .BKDBIN : objIDArray }},
15611591 Fields : []string {common .BKObjNameField , common .BKObjIDField , common .BKObjIconField },
@@ -1574,8 +1604,8 @@ func (c *commonInst) searchAssoObjects(kit *rest.Kit, needChild bool, cond mapst
15741604 pair := make ([]ObjectAssoPair , 0 )
15751605 for _ , object := range rspRst .Info {
15761606 pair = append (pair , ObjectAssoPair {
1577- Object : object ,
1578- AssocName : objAssoMap [object .ObjectID ]. AssociationName ,
1607+ Object : object ,
1608+ AssocNames : objAssoMap [object .ObjectID ],
15791609 })
15801610 }
15811611
0 commit comments