From 530b2a1422d9980b41b86702c49c46b0974a4141 Mon Sep 17 00:00:00 2001 From: TangBin Date: Fri, 21 Feb 2020 18:53:20 +0800 Subject: [PATCH] network query by ip fix --- go.mod | 4 +- go.sum | 6 +- pkg/apis/compute/network.go | 4 ++ pkg/compute/models/networks.go | 64 ++++++------------- vendor/modules.txt | 4 +- .../x/pkg/util/reflectutils/reflectutils.go | 25 ++++++-- vendor/yunion.io/x/sqlchemy/functions.go | 4 ++ 7 files changed, 55 insertions(+), 56 deletions(-) diff --git a/go.mod b/go.mod index b2b5ad0aa0..2829fc6cf5 100644 --- a/go.mod +++ b/go.mod @@ -137,9 +137,9 @@ require ( yunion.io/x/executor v0.0.0-20191202093616-92e2e6119257 yunion.io/x/jsonutils v0.0.0-20200113074440-9297fd00ba07 yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d - yunion.io/x/pkg v0.0.0-20200103043034-27c6f82160fa + yunion.io/x/pkg v0.0.0-20200221023330-f129027c3b04 yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e - yunion.io/x/sqlchemy v0.0.0-20200119074814-09089d67c78d + yunion.io/x/sqlchemy v0.0.0-20200221103553-6a98f7f8ab92 yunion.io/x/structarg v0.0.0-20190809075558-115bed041de3 ) diff --git a/go.sum b/go.sum index c0cc9718a6..4f7f24b8e3 100644 --- a/go.sum +++ b/go.sum @@ -677,9 +677,11 @@ yunion.io/x/pkg v0.0.0-20190620104149-945c25821dbf/go.mod h1:t6rEGG2sQ4J7DhFxSZV yunion.io/x/pkg v0.0.0-20190628082551-f4033ba2ea30/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= yunion.io/x/pkg v0.0.0-20200103043034-27c6f82160fa h1:+7zYi8MhaOW/53/7FOERnhQqAU4UhgaOVIS+AMzTKNU= yunion.io/x/pkg v0.0.0-20200103043034-27c6f82160fa/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= +yunion.io/x/pkg v0.0.0-20200221023330-f129027c3b04 h1:d2TZwtGkCpq+6rM/EfJQ0so+QjIdNhgMC4S+29NIcEA= +yunion.io/x/pkg v0.0.0-20200221023330-f129027c3b04/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e h1:v+EzIadodSwkdZ/7bremd7J8J50Cise/HCylsOJngmo= yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e/go.mod h1:0iFKpOs1y4lbCxeOmq3Xx/0AcQoewVPwj62eRluioEo= -yunion.io/x/sqlchemy v0.0.0-20200119074814-09089d67c78d h1:z9gliVPaZir4qJ9/E1ItnqCZeMq1thGbcuRwYeSKgQM= -yunion.io/x/sqlchemy v0.0.0-20200119074814-09089d67c78d/go.mod h1:FTdwPdGhMgh4E+UFXc9klI1Ok34fMuybTT+jLhOaIjI= +yunion.io/x/sqlchemy v0.0.0-20200221103553-6a98f7f8ab92 h1:Iz70/alKMAW3KeePhmExuhWsYw1MGTcMr5ewAL5lj1I= +yunion.io/x/sqlchemy v0.0.0-20200221103553-6a98f7f8ab92/go.mod h1:FTdwPdGhMgh4E+UFXc9klI1Ok34fMuybTT+jLhOaIjI= yunion.io/x/structarg v0.0.0-20190809075558-115bed041de3 h1:bfC8EhXYvyGYldRWlzxiCM39Zfj3s3+zham9mW2h2LE= yunion.io/x/structarg v0.0.0-20190809075558-115bed041de3/go.mod h1:EP6NSv2C0zzqBDTKumv8hPWLb3XvgMZDHQRfyuOrQng= diff --git a/pkg/apis/compute/network.go b/pkg/apis/compute/network.go index 42f35b6167..286c053a7f 100644 --- a/pkg/apis/compute/network.go +++ b/pkg/apis/compute/network.go @@ -59,6 +59,10 @@ type NetworkListInput struct { UsableResourceListInput WireFilterListInput + + // description: search ip address in network. + // example: 10.168.222.1 + Ip string `json:"ip"` } type NetworkCreateInput struct { diff --git a/pkg/compute/models/networks.go b/pkg/compute/models/networks.go index f6c1dad026..a43ac94910 100644 --- a/pkg/compute/models/networks.go +++ b/pkg/compute/models/networks.go @@ -1673,50 +1673,6 @@ func parseIpToIntArray(ip string) ([]int, error) { return ipIa, nil } -func (manager *SNetworkManager) CustomizeFilterList(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (*db.CustomizeListFilters, error) { - filters := db.NewCustomizeListFilters() - - if query.Contains("ip") { - // ipv4 only - ip, err := query.GetString("ip") - if err != nil { - return nil, httperrors.NewInputParameterError("Get ip fail") - } - ipIa, err := parseIpToIntArray(ip) - if err != nil { - return nil, err - } - - ipFilter := func(obj jsonutils.JSONObject) (bool, error) { - guestIpStart, err := obj.GetString("guest_ip_start") - if err != nil { - return false, httperrors.NewInternalServerError("Get guest ip start error %s", err) - } - guestIpEnd, err := obj.GetString("guest_ip_end") - if err != nil { - return false, httperrors.NewInternalServerError("Get guest ip end error %s", err) - } - ipStartIa, err := parseIpToIntArray(guestIpStart) - if err != nil { - return false, httperrors.NewInternalServerError("Parse guest ip start error %s", err) - } - ipEndIa, err := parseIpToIntArray(guestIpEnd) - if err != nil { - return false, httperrors.NewInternalServerError("Parse guest ip end error %s", err) - } - for i := 0; i < len(ipIa); i++ { - if ipIa[i] < ipStartIa[i] || ipIa[i] > ipEndIa[i] { - return false, nil - } - } - return true, nil - } - - filters.Append(ipFilter) - } - return filters, nil -} - // IP子网列表 func (manager *SNetworkManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, input api.NetworkListInput) (*sqlchemy.SQuery, error) { var err error @@ -1845,6 +1801,26 @@ func (manager *SNetworkManager) ListItemFilter(ctx context.Context, q *sqlchemy. q = q.Filter(sqlchemy.In(q.Field("wire_id"), sq.SubQuery())) } + if len(input.Ip) > 0 { + ipIa, err := parseIpToIntArray(input.Ip) + if err != nil { + return nil, err + } + + ipSa := []string{"0", "0", "0", "0"} + for i := range ipIa { + ipSa[i] = strconv.Itoa(ipIa[i]) + } + fullIp := strings.Join(ipSa, ".") + + ipField := sqlchemy.INET_ATON(sqlchemy.NewStringField(fullIp)) + ipStart := sqlchemy.INET_ATON(q.Field("guest_ip_start")) + ipEnd := sqlchemy.INET_ATON(q.Field("guest_ip_end")) + + ipCondtion := sqlchemy.OR(sqlchemy.Between(ipField, ipStart, ipEnd), sqlchemy.Contains(q.Field("guest_ip_start"), input.Ip), sqlchemy.Contains(q.Field("guest_ip_end"), input.Ip)) + q = q.Filter(ipCondtion) + } + return q, nil } diff --git a/vendor/modules.txt b/vendor/modules.txt index 10c9c4f014..afac990ad6 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -815,7 +815,7 @@ yunion.io/x/jsonutils # yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d yunion.io/x/log yunion.io/x/log/hooks -# yunion.io/x/pkg v0.0.0-20200103043034-27c6f82160fa +# yunion.io/x/pkg v0.0.0-20200221023330-f129027c3b04 yunion.io/x/pkg/errors yunion.io/x/pkg/gotypes yunion.io/x/pkg/prettytable @@ -847,7 +847,7 @@ yunion.io/x/pkg/util/workqueue yunion.io/x/pkg/utils # yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e yunion.io/x/s3cli -# yunion.io/x/sqlchemy v0.0.0-20200119074814-09089d67c78d +# yunion.io/x/sqlchemy v0.0.0-20200221103553-6a98f7f8ab92 yunion.io/x/sqlchemy # yunion.io/x/structarg v0.0.0-20190809075558-115bed041de3 yunion.io/x/structarg diff --git a/vendor/yunion.io/x/pkg/util/reflectutils/reflectutils.go b/vendor/yunion.io/x/pkg/util/reflectutils/reflectutils.go index 72de921aca..4d61d9d46a 100644 --- a/vendor/yunion.io/x/pkg/util/reflectutils/reflectutils.go +++ b/vendor/yunion.io/x/pkg/util/reflectutils/reflectutils.go @@ -160,12 +160,11 @@ func ExpandInterface(val interface{}) []interface{} { // tagetType must not be a pointer func getAnonymouStructPointer(structValue reflect.Value, targetType reflect.Type) interface{} { structType := structValue.Type() + if structType == targetType { + return structValue.Addr().Interface() + } for i := 0; i < structValue.NumField(); i += 1 { fieldType := structType.Field(i) - if fieldType.Type == targetType { - val := structValue.Field(i) // val is not a pointer - return val.Addr().Interface() - } if fieldType.Anonymous && fieldType.Type.Kind() == reflect.Struct { ptr := getAnonymouStructPointer(structValue.Field(i), targetType) if ptr != nil { @@ -177,12 +176,26 @@ func getAnonymouStructPointer(structValue reflect.Value, targetType reflect.Type } func FindAnonymouStructPointer(data interface{}, targetPtr interface{}) error { - targetValue := reflect.ValueOf(targetPtr).Elem() + targetValue := reflect.ValueOf(targetPtr) + if targetValue.Kind() != reflect.Ptr { + return fmt.Errorf("target must be a pointer to pointer") + } + targetValue = targetValue.Elem() if targetValue.Kind() != reflect.Ptr { return fmt.Errorf("target must be a pointer to pointer") } targetType := targetValue.Type().Elem() - structValue := reflect.Indirect(reflect.ValueOf(data)) + if targetType.Kind() != reflect.Struct { + return fmt.Errorf("target type must be a struct") + } + structValue := reflect.ValueOf(data) + if structValue.Kind() != reflect.Ptr { + return fmt.Errorf("data type must be a pointer to struct") + } + structValue = reflect.ValueOf(data).Elem() + if structValue.Kind() != reflect.Struct { + return fmt.Errorf("data type must be a pointer to struct") + } ptr := getAnonymouStructPointer(structValue, targetType) if ptr == nil { return fmt.Errorf("no anonymous struct found") diff --git a/vendor/yunion.io/x/sqlchemy/functions.go b/vendor/yunion.io/x/sqlchemy/functions.go index 26ead1dd72..b5c5ac87b2 100644 --- a/vendor/yunion.io/x/sqlchemy/functions.go +++ b/vendor/yunion.io/x/sqlchemy/functions.go @@ -170,3 +170,7 @@ func AND_Val(name string, field IQueryField, v interface{}) IQueryField { rightStr := fmt.Sprintf("&%v", v) return NewFunctionField(name, "%s"+rightStr, field) } + +func INET_ATON(field IQueryField) IQueryField { + return NewFunctionField("", `INET_ATON(%s)`, field) +}