diff --git a/cmd/climc/shell/secgrouprules.go b/cmd/climc/shell/secgrouprules.go index 92f10f9ee4..7d3f6df5a3 100644 --- a/cmd/climc/shell/secgrouprules.go +++ b/cmd/climc/shell/secgrouprules.go @@ -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 { diff --git a/pkg/apis/compute/secgroup.go b/pkg/apis/compute/secgroup.go index 02af2d7710..eae5a8817d 100644 --- a/pkg/apis/compute/secgroup.go +++ b/pkg/apis/compute/secgroup.go @@ -174,6 +174,8 @@ type SecurityGroupRuleListInput struct { apis.ResourceBaseListInput SecgroupFilterListInput + Projects []string `json:"projects"` + // 以direction字段过滤安全组规则 Direction string `json:"direction"` // 以action字段过滤安全组规则 diff --git a/pkg/compute/models/secgrouprules.go b/pkg/compute/models/secgrouprules.go index 06e910bfb7..333a5135c8 100644 --- a/pkg/compute/models/secgrouprules.go +++ b/pkg/compute/models/secgrouprules.go @@ -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) }