fix: add secgroup filter opts

This commit is contained in:
Qu Xuan
2021-03-04 20:56:41 +08:00
parent 59fc9c2dea
commit 1ae3fbc749
3 changed files with 57 additions and 9 deletions
+5 -1
View File
@@ -149,7 +149,7 @@ type SSecgroupCreateInput struct {
type SecgroupListInput struct {
apis.SharableVirtualResourceListInput
ServerFilterListInput
ServerResourceInput
DBInstanceResourceInput
ELasticcacheResourceInput
@@ -177,6 +177,10 @@ type SecgroupListInput struct {
// default: all
// example: in
Direction string `json:"direction"`
RegionalFilterListInput
ManagedResourceListInput
}
type SecurityGroupCacheListInput struct {
+44 -2
View File
@@ -131,7 +131,7 @@ func (manager *SSecurityGroupManager) ListItemFilter(
filters = append(filters, sqlchemy.In(q.Field("id"), GuestsecgroupManager.Query("secgroup_id").Equals("guest_id", serverId).SubQuery()))
isAdmin := false
admin := (input.ServerFilterListInput.Admin != nil && *input.ServerFilterListInput.Admin)
admin := (input.Admin != nil && *input.Admin)
if consts.IsRbacEnabled() {
allowScope := policy.PolicyManager.AllowScope(userCred, consts.GetServiceType(), manager.KeywordPlural(), policy.PolicyActionList)
if allowScope == rbacutils.ScopeSystem || allowScope == rbacutils.ScopeDomain {
@@ -156,6 +156,22 @@ func (manager *SSecurityGroupManager) ListItemFilter(
q = q.In("id", sq.SubQuery())
}
if len(input.CloudregionId) > 0 || len(input.Providers) > 0 || len(input.Brands) > 0 || len(input.CloudaccountId) > 0 {
caches := SecurityGroupCacheManager.Query()
filter := api.SecurityGroupCacheListInput{
ManagedResourceListInput: input.ManagedResourceListInput,
RegionalFilterListInput: input.RegionalFilterListInput,
}
caches, err = SecurityGroupCacheManager.ListItemFilter(ctx, caches, userCred, filter)
if err != nil {
return nil, errors.Wrapf(err, "SecurityGroupCacheManager.ListItemFilter")
}
sq := caches.SubQuery()
q = q.Join(sq, sqlchemy.Equals(q.Field("id"), sq.Field("secgroup_id")))
}
// elastic cache
q, err = manager.ListItemElasticcacheFilter(ctx, q, userCred, input)
if err != nil {
@@ -258,11 +274,37 @@ func (manager *SSecurityGroupManager) OrderByExtraFields(
func (manager *SSecurityGroupManager) QueryDistinctExtraField(q *sqlchemy.SQuery, field string) (*sqlchemy.SQuery, error) {
var err error
q, err = manager.SSharableVirtualResourceBaseManager.QueryDistinctExtraField(q, field)
if err == nil {
return q, nil
}
switch field {
case "provider", "brand":
accountQuery := CloudaccountManager.Query(field, "id").Distinct().SubQuery()
providers := CloudproviderManager.Query("id", "cloudaccount_id").SubQuery()
caches := SecurityGroupCacheManager.Query("manager_id", "secgroup_id").SubQuery()
q.AppendField(accountQuery.Field(field)).Distinct()
q = q.Join(caches, sqlchemy.Equals(q.Field("id"), caches.Field("secgroup_id")))
q = q.Join(providers, sqlchemy.Equals(providers.Field("id"), caches.Field("manager_id")))
q = q.Join(accountQuery, sqlchemy.Equals(accountQuery.Field("id"), providers.Field("cloudaccount_id")))
return q, nil
case "region":
regionQuery := CloudregionManager.Query("name", "id").SubQuery()
caches := SecurityGroupCacheManager.Query("cloudregion_id", "secgroup_id").SubQuery()
q.AppendField(regionQuery.Field("name").Label("region")).Distinct()
q = q.Join(caches, sqlchemy.Equals(q.Field("id"), caches.Field("secgroup_id")))
q = q.Join(regionQuery, sqlchemy.Equals(caches.Field("cloudregion_id"), regionQuery.Field("id")))
return q, nil
case "account":
accountQuery := CloudaccountManager.Query("name", "id").Distinct().SubQuery()
providers := CloudproviderManager.Query("id", "cloudaccount_id").SubQuery()
caches := SecurityGroupCacheManager.Query("manager_id", "secgroup_id").SubQuery()
q.AppendField(accountQuery.Field("name").Label("account")).Distinct()
q = q.Join(caches, sqlchemy.Equals(q.Field("id"), caches.Field("secgroup_id")))
q = q.Join(providers, sqlchemy.Equals(providers.Field("id"), caches.Field("manager_id")))
q = q.Join(accountQuery, sqlchemy.Equals(accountQuery.Field("id"), providers.Field("cloudaccount_id")))
return q, nil
}
return q, httperrors.ErrNotFound
}
+8 -6
View File
@@ -29,12 +29,14 @@ import (
type SecgroupListOptions struct {
BaseListOptions
Equals string `help:"Secgroup ID or Name, filter secgroups whose rules equals the specified one"`
Server string `help:"Filter secgroups bound to specified server"`
Ip string `help:"Filter secgroup by ip"`
Ports string `help:"Filter secgroup by ports"`
Direction string `help:"Filter secgroup by ports" choices:"all|in|out"`
DBInstance string `help:"Filter secgroups bound to specified rds" json:"dbinstance"`
Equals string `help:"Secgroup ID or Name, filter secgroups whose rules equals the specified one"`
Server string `help:"Filter secgroups bound to specified server"`
Ip string `help:"Filter secgroup by ip"`
Ports string `help:"Filter secgroup by ports"`
Direction string `help:"Filter secgroup by ports" choices:"all|in|out"`
DBInstance string `help:"Filter secgroups bound to specified rds" json:"dbinstance"`
Cloudregion string `help:"Filter secgroups by region"`
Cloudaccount string `help:"Filter secgroups by account"`
}
func (opts *SecgroupListOptions) Params() (jsonutils.JSONObject, error) {