diff --git a/cmd/climc/shell/secgrouprules.go b/cmd/climc/shell/secgrouprules.go index b326bd4d23..c57d12e6a7 100644 --- a/cmd/climc/shell/secgrouprules.go +++ b/cmd/climc/shell/secgrouprules.go @@ -25,33 +25,18 @@ import ( func init() { type SecGroupRulesListOptions struct { options.BaseListOptions - Secgroup string `help:"Secgroup ID or Name"` - 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"` + 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"` } R(&SecGroupRulesListOptions{}, "secgroup-rule-list", "List all security group", func(s *mcclient.ClientSession, args *SecGroupRulesListOptions) error { - var params *jsonutils.JSONDict - { - var err error - params, err = args.BaseListOptions.Params() - if err != nil { - return err - - } - } - if len(args.Secgroup) > 0 { - params.Add(jsonutils.NewString(args.Secgroup), "secgroup") - } - if len(args.Direction) > 0 { - params.Add(jsonutils.NewString(args.Direction), "direction") - } - if len(args.Protocol) > 0 { - params.Add(jsonutils.NewString(args.Protocol), "protocol") - } - if len(args.Action) > 0 { - params.Add(jsonutils.NewString(args.Action), "action") + params, err := options.ListStructToParams(args) + if err != nil { + return err } result, err := modules.SecGroupRules.List(s, params) if err != nil { diff --git a/pkg/apis/compute/secgroup.go b/pkg/apis/compute/secgroup.go index 376031b07b..c3e433de29 100644 --- a/pkg/apis/compute/secgroup.go +++ b/pkg/apis/compute/secgroup.go @@ -189,6 +189,11 @@ type SecgroupResourceInput struct { // Deprecated // filter by secgroup_id SecgroupId string `json:"secgroup_id" "yunion:deprecated-by":"secgroup"` + + // 模糊匹配安全组规则名称 + SecgroupName string `json:"secgroup_name"` + + apis.ProjectizedResourceInput } type SecgroupFilterListInput struct { diff --git a/pkg/compute/models/secgroupresource.go b/pkg/compute/models/secgroupresource.go index 47f5f0f91d..f88125e600 100644 --- a/pkg/compute/models/secgroupresource.go +++ b/pkg/compute/models/secgroupresource.go @@ -114,6 +114,18 @@ func (manager *SSecurityGroupResourceBaseManager) ListItemFilter( } q = q.Equals("secgroup_id", secgrpObj.GetId()) } + if len(query.SecgroupName) > 0 { + sq := SecurityGroupManager.Query("id").Like("name", "%"+query.SecgroupName+"%") + q = q.In("secgroup_id", sq.SubQuery()) + } + if len(query.Project) > 0 { + tenant, err := db.TenantCacheManager.FetchTenantByIdOrName(ctx, query.Project) + if err != nil { + return nil, httperrors.NewResourceNotFoundError2("projects", query.Project) + } + sq := SecurityGroupManager.Query("id").Equals("tenant_id", tenant.Id) + q = q.In("secgroup_id", sq.SubQuery()) + } return q, nil } diff --git a/pkg/compute/models/secgrouprules.go b/pkg/compute/models/secgrouprules.go index 84aa1f6f42..a65af40444 100644 --- a/pkg/compute/models/secgrouprules.go +++ b/pkg/compute/models/secgrouprules.go @@ -195,6 +195,7 @@ func (manager *SSecurityGroupRuleManager) ListItemFilter( if len(query.Protocol) > 0 { sql = sql.Equals("protocol", query.Protocol) } + sql = sql.GroupBy("secgroup_id") return sql, nil } diff --git a/pkg/mcclient/modules/mod_secgrouprules.go b/pkg/mcclient/modules/mod_secgrouprules.go index ef8c740db6..e0583a1738 100644 --- a/pkg/mcclient/modules/mod_secgrouprules.go +++ b/pkg/mcclient/modules/mod_secgrouprules.go @@ -24,7 +24,7 @@ func init() { SecGroupRules = NewComputeManager("secgrouprule", "secgrouprules", []string{"ID", "Name", "Direction", "Action", "Protocol", "Ports", "Priority", - "Cidr", "Description"}, + "Cidr", "Secgroup", "Tenant", "Description"}, []string{"SecGroups"}) registerCompute(&SecGroupRules)