Merge pull request #7174 from ioito/hotfix/qx-secgroup-rule-export

fix: 添加secgroup rule过滤参数
This commit is contained in:
Zexi Li
2020-07-16 11:18:40 +08:00
committed by GitHub
5 changed files with 28 additions and 25 deletions
+9 -24
View File
@@ -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 {
+5
View File
@@ -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 {
+12
View File
@@ -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
}
+1
View File
@@ -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
}
+1 -1
View File
@@ -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)