mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
feat(region): filter server ip_addrs by REGEXP
This commit is contained in:
@@ -90,7 +90,7 @@ require (
|
||||
yunion.io/x/ovsdb v0.0.0-20230306173834-f164f413a900
|
||||
yunion.io/x/pkg v1.0.1-0.20230912084455-1393f31347db
|
||||
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e
|
||||
yunion.io/x/sqlchemy v1.1.2-0.20231011060911-10811127ad1e
|
||||
yunion.io/x/sqlchemy v1.1.2-0.20231011085815-70c5a22668f2
|
||||
yunion.io/x/structarg v0.0.0-20220312084958-9c6c79c7d1c6
|
||||
)
|
||||
|
||||
|
||||
@@ -1193,7 +1193,7 @@ yunion.io/x/pkg v1.0.1-0.20230912084455-1393f31347db h1:yxiqu3C7wDnL3M8Nn54QmBp+
|
||||
yunion.io/x/pkg v1.0.1-0.20230912084455-1393f31347db/go.mod h1:ksCJVQ+DwKrJ5QBEoU8pzrDFfDaZVAFH/iJ6yQCYxJk=
|
||||
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 v1.1.2-0.20231011060911-10811127ad1e h1:iEaQ0UTB0CoBXeAvR6NjtZXiPTUAlVSlrsWyonNanEA=
|
||||
yunion.io/x/sqlchemy v1.1.2-0.20231011060911-10811127ad1e/go.mod h1:uuPVZEyEq3sWd5vf9VjGSy6lZzof22X87OEHw9sddJQ=
|
||||
yunion.io/x/sqlchemy v1.1.2-0.20231011085815-70c5a22668f2 h1:bRXgsqgsT37ifIK/NrkLazNHpBxOsjK5Tv5xxlD4Ap4=
|
||||
yunion.io/x/sqlchemy v1.1.2-0.20231011085815-70c5a22668f2/go.mod h1:uuPVZEyEq3sWd5vf9VjGSy6lZzof22X87OEHw9sddJQ=
|
||||
yunion.io/x/structarg v0.0.0-20220312084958-9c6c79c7d1c6 h1:WuWXhY3DvhdRTzWCJ/kwt3Ss6KIq7+KqJwb+esvNGwU=
|
||||
yunion.io/x/structarg v0.0.0-20220312084958-9c6c79c7d1c6/go.mod h1:EP6NSv2C0zzqBDTKumv8hPWLb3XvgMZDHQRfyuOrQng=
|
||||
|
||||
@@ -420,7 +420,7 @@ func (manager *SGuestManager) ListItemFilter(
|
||||
vipq := GroupguestManager.Query("guest_id")
|
||||
conditions := []sqlchemy.ICondition{}
|
||||
for _, ipAddr := range query.IpAddrs {
|
||||
conditions = append(conditions, sqlchemy.Contains(grpnets.Field("ip_addr"), ipAddr))
|
||||
conditions = append(conditions, sqlchemy.Regexp(grpnets.Field("ip_addr"), ipAddr))
|
||||
}
|
||||
vipq = vipq.Join(grpnets, sqlchemy.Equals(grpnets.Field("group_id"), vipq.Field("group_id"))).Filter(
|
||||
sqlchemy.OR(conditions...),
|
||||
@@ -429,7 +429,7 @@ func (manager *SGuestManager) ListItemFilter(
|
||||
grpeips := ElasticipManager.Query().Equals("associate_type", api.EIP_ASSOCIATE_TYPE_INSTANCE_GROUP).SubQuery()
|
||||
conditions = []sqlchemy.ICondition{}
|
||||
for _, ipAddr := range query.IpAddrs {
|
||||
conditions = append(conditions, sqlchemy.Contains(grpeips.Field("ip_addr"), ipAddr))
|
||||
conditions = append(conditions, sqlchemy.Regexp(grpeips.Field("ip_addr"), ipAddr))
|
||||
}
|
||||
vipeipq := GroupguestManager.Query("guest_id")
|
||||
vipeipq = vipeipq.Join(grpeips, sqlchemy.Equals(grpeips.Field("associate_id"), vipeipq.Field("group_id"))).Filter(
|
||||
@@ -439,14 +439,14 @@ func (manager *SGuestManager) ListItemFilter(
|
||||
gnQ := GuestnetworkManager.Query("guest_id")
|
||||
conditions = []sqlchemy.ICondition{}
|
||||
for _, ipAddr := range query.IpAddrs {
|
||||
conditions = append(conditions, sqlchemy.Contains(gnQ.Field("ip_addr"), ipAddr))
|
||||
conditions = append(conditions, sqlchemy.Regexp(gnQ.Field("ip_addr"), ipAddr))
|
||||
}
|
||||
gn := gnQ.Filter(sqlchemy.OR(conditions...))
|
||||
|
||||
guestEipQ := ElasticipManager.Query("associate_id").Equals("associate_type", api.EIP_ASSOCIATE_TYPE_SERVER)
|
||||
conditions = []sqlchemy.ICondition{}
|
||||
for _, ipAddr := range query.IpAddrs {
|
||||
conditions = append(conditions, sqlchemy.Contains(guestEipQ.Field("ip_addr"), ipAddr))
|
||||
conditions = append(conditions, sqlchemy.Regexp(guestEipQ.Field("ip_addr"), ipAddr))
|
||||
}
|
||||
guestEip := guestEipQ.Filter(sqlchemy.OR(conditions...))
|
||||
|
||||
@@ -454,7 +454,7 @@ func (manager *SGuestManager) ListItemFilter(
|
||||
conditions = []sqlchemy.ICondition{}
|
||||
for _, ipAddr := range query.IpAddrs {
|
||||
conditions = append(conditions, sqlchemy.AND(
|
||||
sqlchemy.Contains(metadataQ.Field("value"), ipAddr),
|
||||
sqlchemy.Regexp(metadataQ.Field("value"), ipAddr),
|
||||
sqlchemy.Equals(metadataQ.Field("key"), "sync_ips"),
|
||||
sqlchemy.Equals(metadataQ.Field("obj_type"), "server"),
|
||||
))
|
||||
|
||||
Vendored
+1
-1
@@ -1586,7 +1586,7 @@ yunion.io/x/pkg/utils
|
||||
# yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e
|
||||
## explicit; go 1.12
|
||||
yunion.io/x/s3cli
|
||||
# yunion.io/x/sqlchemy v1.1.2-0.20231011060911-10811127ad1e
|
||||
# yunion.io/x/sqlchemy v1.1.2-0.20231011085815-70c5a22668f2
|
||||
## explicit; go 1.17
|
||||
yunion.io/x/sqlchemy
|
||||
yunion.io/x/sqlchemy/backends
|
||||
|
||||
+1
-1
@@ -56,7 +56,7 @@ type IBackend interface {
|
||||
//
|
||||
CaseInsensitiveLikeString() string
|
||||
//
|
||||
CaseInsensitiveRegexpString() string
|
||||
RegexpWhereClause(cond *SRegexpConition) string
|
||||
//
|
||||
UnionAllString() string
|
||||
//
|
||||
|
||||
+11
@@ -15,6 +15,7 @@
|
||||
package clickhouse
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -46,6 +47,16 @@ func (click *SClickhouseBackend) CaseInsensitiveLikeString() string {
|
||||
return "ILIKE"
|
||||
}
|
||||
|
||||
func (click *SClickhouseBackend) RegexpWhereClause(cond *sqlchemy.SRegexpConition) string {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString("match(")
|
||||
buf.WriteString(cond.GetLeft().Reference())
|
||||
buf.WriteString(", ")
|
||||
buf.WriteString(sqlchemy.VarConditionWhereClause(cond.GetRight()))
|
||||
buf.WriteString(")")
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// CanUpdate returns wether the backend supports update
|
||||
func (click *SClickhouseBackend) CanUpdate() bool {
|
||||
return true
|
||||
|
||||
+2
-2
@@ -60,8 +60,8 @@ func (bb *SBaseBackend) CaseInsensitiveLikeString() string {
|
||||
return "LIKE"
|
||||
}
|
||||
|
||||
func (bb *SBaseBackend) CaseInsensitiveRegexpString() string {
|
||||
return "REGEXP"
|
||||
func (bb *SBaseBackend) RegexpWhereClause(cond *SRegexpConition) string {
|
||||
return tupleConditionWhereClause(&cond.STupleCondition, SQL_OP_REGEXP)
|
||||
}
|
||||
|
||||
func (bb *SBaseBackend) UnionAllString() string {
|
||||
|
||||
+13
-6
@@ -336,7 +336,7 @@ func tupleConditionWhereClause(t *STupleCondition, op string) string {
|
||||
buf.WriteByte(' ')
|
||||
buf.WriteString(op)
|
||||
buf.WriteByte(' ')
|
||||
buf.WriteString(varConditionWhereClause(t.right))
|
||||
buf.WriteString(VarConditionWhereClause(t.right))
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
@@ -359,7 +359,7 @@ func questionMark(count int) string {
|
||||
}
|
||||
}
|
||||
|
||||
func varConditionWhereClause(v interface{}) string {
|
||||
func VarConditionWhereClause(v interface{}) string {
|
||||
switch q := v.(type) {
|
||||
case IQueryField:
|
||||
return q.Reference()
|
||||
@@ -393,6 +393,14 @@ func NewTupleCondition(l IQueryField, r interface{}) STupleCondition {
|
||||
return STupleCondition{left: l, right: r}
|
||||
}
|
||||
|
||||
func (t *STupleCondition) GetLeft() IQueryField {
|
||||
return t.left
|
||||
}
|
||||
|
||||
func (t *STupleCondition) GetRight() interface{} {
|
||||
return t.right
|
||||
}
|
||||
|
||||
// Variables implementation of STupleCondition for ICondition
|
||||
func (t *STupleCondition) Variables() []interface{} {
|
||||
return varConditionVariables(t.right)
|
||||
@@ -410,7 +418,7 @@ type SInCondition struct {
|
||||
}
|
||||
|
||||
func inConditionWhereClause(t *STupleCondition, op string) string {
|
||||
v := varConditionWhereClause(t.right)
|
||||
v := VarConditionWhereClause(t.right)
|
||||
if len(v) != 0 {
|
||||
return tupleConditionWhereClause(t, op)
|
||||
}
|
||||
@@ -499,8 +507,7 @@ type SRegexpConition struct {
|
||||
|
||||
// WhereClause implementation for SRegexpConition for ICondition
|
||||
func (t *SRegexpConition) WhereClause() string {
|
||||
// op := t.left.database().backend.CaseInsensitiveRegexpString()
|
||||
return tupleConditionWhereClause(&t.STupleCondition, SQL_OP_REGEXP)
|
||||
return t.left.database().backend.RegexpWhereClause(t)
|
||||
}
|
||||
|
||||
// Regexp SQL operator
|
||||
@@ -665,7 +672,7 @@ type SBetweenCondition struct {
|
||||
// WhereClause implementation of SBetweenCondition for ICondition
|
||||
func (t *SBetweenCondition) WhereClause() string {
|
||||
ret := tupleConditionWhereClause(&t.STupleCondition, SQL_OP_BETWEEN)
|
||||
return fmt.Sprintf("%s AND %s", ret, varConditionWhereClause(t.right2))
|
||||
return fmt.Sprintf("%s AND %s", ret, VarConditionWhereClause(t.right2))
|
||||
}
|
||||
|
||||
// Between SQL operator
|
||||
|
||||
+6
@@ -49,6 +49,12 @@ func (tq *SQuery) Like(f string, v string) *SQuery {
|
||||
return tq.Filter(cond)
|
||||
}
|
||||
|
||||
// Regexp filters query with a regexp condition
|
||||
func (tq *SQuery) Regexp(f string, v string) *SQuery {
|
||||
cond := Regexp(tq.Field(f), v)
|
||||
return tq.Filter(cond)
|
||||
}
|
||||
|
||||
// Contains filters query with a contains condition
|
||||
func (tq *SQuery) Contains(f string, v string) *SQuery {
|
||||
cond := Contains(tq.Field(f), v)
|
||||
|
||||
Reference in New Issue
Block a user