Skip to content

Commit

Permalink
fix: 分开统计安全组关联虚拟机数量
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu Xuan committed Jul 7, 2020
1 parent acdc1da commit a997cf4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
11 changes: 9 additions & 2 deletions pkg/apis/compute/secgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,16 @@ type SecgroupDetails struct {
SSecurityGroup

// 关联云主机数量
GuestCnt int `json:"guest_cnt"`
GuestCnt int `json:"guest_cnt,allowempty"`

// 关联此安全组的云主机is_system为true数量
SystemGuestCnt int `json:"system_guest_cnt,allowempty"`

// admin_secgrp_id为此安全组的云主机数量
AdminGuestCnt int `json:"admin_guest_cnt,allowempty"`

// 安全组缓存数量
CacheCnt int `json:"cache_cnt"`
CacheCnt int `json:"cache_cnt,allowempty"`
// 规则信息
Rules []SSecurityGroupRule `json:"rules"`
// 入方向规则信息
Expand Down
35 changes: 24 additions & 11 deletions pkg/compute/models/secgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,27 @@ func (manager *SSecurityGroupManager) FetchCustomizeColumns(
return rows
}

guestMaps := map[string]int{}
adminGuestMaps := map[string]int{}
systemGuestMaps := map[string]int{}
normalGuestMaps := map[string]int{}
for i := range guests {
if _, ok := guestMaps[guests[i].SecgrpId]; !ok {
guestMaps[guests[i].SecgrpId] = 0
if guests[i].IsSystem {
if _, ok := systemGuestMaps[guests[i].SecgrpId]; !ok {
systemGuestMaps[guests[i].SecgrpId] = 0
}
systemGuestMaps[guests[i].SecgrpId]++
} else {
if _, ok := normalGuestMaps[guests[i].SecgrpId]; !ok {
normalGuestMaps[guests[i].SecgrpId] = 0
}
normalGuestMaps[guests[i].SecgrpId]++
}
guestMaps[guests[i].SecgrpId]++
if _, ok := guestMaps[guests[i].AdminSecgrpId]; !ok {
guestMaps[guests[i].AdminSecgrpId] = 0
if len(guests[i].AdminSecgrpId) > 0 {
if _, ok := adminGuestMaps[guests[i].AdminSecgrpId]; !ok {
adminGuestMaps[guests[i].AdminSecgrpId] = 0
}
adminGuestMaps[guests[i].AdminSecgrpId]++
}
guestMaps[guests[i].AdminSecgrpId]++
}

guestSecgroups := []SGuestsecgroup{}
Expand All @@ -339,10 +350,10 @@ func (manager *SSecurityGroupManager) FetchCustomizeColumns(
}

for i := range guestSecgroups {
if _, ok := guestMaps[guestSecgroups[i].SecgroupId]; !ok {
guestMaps[guestSecgroups[i].SecgroupId] = 0
if _, ok := normalGuestMaps[guestSecgroups[i].SecgroupId]; !ok {
normalGuestMaps[guestSecgroups[i].SecgroupId] = 0
}
guestMaps[guestSecgroups[i].SecgroupId]++
normalGuestMaps[guestSecgroups[i].SecgroupId]++
}

rules := []SSecurityGroupRule{}
Expand Down Expand Up @@ -390,7 +401,9 @@ func (manager *SSecurityGroupManager) FetchCustomizeColumns(
rows[i].InRules = _inRules
rows[i].OutRules = _outRules
rows[i].CacheCnt, _ = cacheMaps[secgroupIds[i]]
rows[i].GuestCnt, _ = guestMaps[secgroupIds[i]]
rows[i].GuestCnt, _ = normalGuestMaps[secgroupIds[i]]
rows[i].AdminGuestCnt, _ = adminGuestMaps[secgroupIds[i]]
rows[i].SystemGuestCnt, _ = systemGuestMaps[secgroupIds[i]]
}

return rows
Expand Down

0 comments on commit a997cf4

Please sign in to comment.