Merge pull request #5225 from tb365/automated-cherry-pick-of-#5224-upstream-release-3.1

Automated cherry pick of #5224: network query by ip fix
This commit is contained in:
yunion-ci-robot
2020-02-23 18:52:06 +08:00
committed by GitHub
7 changed files with 55 additions and 56 deletions
+2 -2
View File
@@ -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
)
+4 -2
View File
@@ -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=
+4
View File
@@ -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 {
+20 -44
View File
@@ -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
}
+2 -2
View File
@@ -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
+19 -6
View File
@@ -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")
+4
View File
@@ -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)
}