fix: 支持多项目安全组规则过滤

This commit is contained in:
Qu Xuan
2020-07-23 20:36:59 +08:00
parent b627985733
commit ec16b2abbd
3 changed files with 20 additions and 8 deletions
+8 -8
View File
@@ -25,14 +25,14 @@ import (
func init() {
type SecGroupRulesListOptions struct {
options.BaseListOptions
Secgroup string `help:"Secgroup ID or Name"`
SecgroupName string `help:"Search rules by fuzzy secgroup name"`
Project string `help:"Filter rules by project"`
Direction string `help:"filter Direction of rule" choices:"in|out"`
Protocol string `help:"filter Protocol of rule" choices:"any|tcp|udp|icmp"`
Action string `help:"filter Actin of rule" choices:"allow|deny"`
Ports string `help:"filter Ports of rule"`
Ip string `help:"filter cidr of rule"`
Secgroup string `help:"Secgroup ID or Name"`
SecgroupName string `help:"Search rules by fuzzy secgroup name"`
Projects []string `help:"Filter rules by project"`
Direction string `help:"filter Direction of rule" choices:"in|out"`
Protocol string `help:"filter Protocol of rule" choices:"any|tcp|udp|icmp"`
Action string `help:"filter Actin of rule" choices:"allow|deny"`
Ports string `help:"filter Ports of rule"`
Ip string `help:"filter cidr of rule"`
}
R(&SecGroupRulesListOptions{}, "secgroup-rule-list", "List all security group", func(s *mcclient.ClientSession, args *SecGroupRulesListOptions) error {
+2
View File
@@ -174,6 +174,8 @@ type SecurityGroupRuleListInput struct {
apis.ResourceBaseListInput
SecgroupFilterListInput
Projects []string `json:"projects"`
// 以direction字段过滤安全组规则
Direction string `json:"direction"`
// 以action字段过滤安全组规则
+10
View File
@@ -176,6 +176,16 @@ func (manager *SSecurityGroupRuleManager) ListItemFilter(
if err != nil {
return nil, errors.Wrap(err, "SSecurityGroupResourceBaseManager.ListItemFilter")
}
if len(query.Projects) > 0 {
sq := SecurityGroupManager.Query("id")
tenants := db.TenantCacheManager.GetTenantQuery().SubQuery()
subq := tenants.Query(tenants.Field("id")).Filter(sqlchemy.OR(
sqlchemy.In(tenants.Field("id"), query.Projects),
sqlchemy.In(tenants.Field("name"), query.Projects),
)).SubQuery()
sq = sq.In("tenant_id", subq)
sql = sql.In("secgroup_id", sq.SubQuery())
}
if len(query.Direction) > 0 {
sql = sql.Equals("direction", query.Direction)
}