mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #4799 from yousong/bugfix/yousong-lb
lb: support redirect type listener & rule
This commit is contained in:
@@ -98,6 +98,18 @@ func newInvalidChoiceError(key string, choices choices.Choices, choice string) e
|
||||
return newError(ERR_INVALID_CHOICE, "invalid %q, want %s, got %s", key, choices, choice)
|
||||
}
|
||||
|
||||
func newInvalidIntChoiceError(key string, choices []int64, choice int64) error {
|
||||
wantS := ""
|
||||
for i, c := range choices {
|
||||
if i > 0 {
|
||||
wantS += ", "
|
||||
}
|
||||
wantS += fmt.Sprintf("%d", c)
|
||||
}
|
||||
gotS := fmt.Sprintf("%d", choice)
|
||||
return newError(ERR_INVALID_CHOICE, "invalid %q, want %s, got %s", key, wantS, gotS)
|
||||
}
|
||||
|
||||
func newStringTooShortError(key string, got, want int) error {
|
||||
return newError(ERR_INVALID_LENGTH, "%q too short, got %d, min %s", key, got, want)
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
"net"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
@@ -158,11 +159,64 @@ func (v *ValidatorIPv4Prefix) Validate(data *jsonutils.JSONDict) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type ValidatorIntChoices struct {
|
||||
Validator
|
||||
choices []int64
|
||||
|
||||
Value int64
|
||||
}
|
||||
|
||||
func NewIntChoicesValidator(key string, choices []int64) *ValidatorIntChoices {
|
||||
v := &ValidatorIntChoices{
|
||||
Validator: Validator{Key: key},
|
||||
choices: choices,
|
||||
}
|
||||
v.SetParent(v)
|
||||
return v
|
||||
}
|
||||
|
||||
func (v *ValidatorIntChoices) has(i int64) bool {
|
||||
for _, c := range v.choices {
|
||||
if c == i {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (v *ValidatorIntChoices) Default(i int64) IValidator {
|
||||
if v.has(i) {
|
||||
v.Validator.Default(i)
|
||||
return v
|
||||
}
|
||||
panic("invalid default for " + v.Key)
|
||||
}
|
||||
|
||||
func (v *ValidatorIntChoices) getValue() interface{} {
|
||||
return v.Value
|
||||
}
|
||||
|
||||
func (v *ValidatorIntChoices) Validate(data *jsonutils.JSONDict) error {
|
||||
if err, isSet := v.Validator.validateEx(data); err != nil || !isSet {
|
||||
return err
|
||||
}
|
||||
i, err := v.value.Int()
|
||||
if err != nil {
|
||||
return newGeneralError(v.Key, err)
|
||||
}
|
||||
if !v.has(i) {
|
||||
return newInvalidIntChoiceError(v.Key, v.choices, i)
|
||||
}
|
||||
// in case it's stringified from v.value
|
||||
data.Set(v.Key, jsonutils.NewInt(i))
|
||||
v.Value = i
|
||||
return nil
|
||||
}
|
||||
|
||||
type ValidatorStringChoices struct {
|
||||
Validator
|
||||
Choices choices.Choices
|
||||
defaultVal string
|
||||
Value string
|
||||
Choices choices.Choices
|
||||
Value string
|
||||
}
|
||||
|
||||
func NewStringChoicesValidator(key string, choices choices.Choices) *ValidatorStringChoices {
|
||||
@@ -176,7 +230,8 @@ func NewStringChoicesValidator(key string, choices choices.Choices) *ValidatorSt
|
||||
|
||||
func (v *ValidatorStringChoices) Default(s string) IValidator {
|
||||
if v.Choices.Has(s) {
|
||||
return v.Validator.Default(s)
|
||||
v.Validator.Default(s)
|
||||
return v
|
||||
}
|
||||
panic("invalid default for " + v.Key)
|
||||
}
|
||||
@@ -204,11 +259,10 @@ func (v *ValidatorStringChoices) Validate(data *jsonutils.JSONDict) error {
|
||||
|
||||
type ValidatorStringMultiChoices struct {
|
||||
Validator
|
||||
Choices choices.Choices
|
||||
defaultVal string
|
||||
Value string
|
||||
sep string
|
||||
keepDup bool
|
||||
Choices choices.Choices
|
||||
Value string
|
||||
sep string
|
||||
keepDup bool
|
||||
}
|
||||
|
||||
func NewStringMultiChoicesValidator(key string, choices choices.Choices) *ValidatorStringMultiChoices {
|
||||
@@ -589,6 +643,76 @@ func NewDomainNameValidator(key string) *ValidatorDomainName {
|
||||
return v
|
||||
}
|
||||
|
||||
type ValidatorHostPort struct {
|
||||
ValidatorRegexp
|
||||
|
||||
optionalPort bool
|
||||
Domain string
|
||||
Port int
|
||||
Value string
|
||||
}
|
||||
|
||||
var regHostPort *regexp.Regexp
|
||||
|
||||
func init() {
|
||||
// guard against surprise
|
||||
exp := regutils.DOMAINNAME_REG.String()
|
||||
if exp != "" && exp[len(exp)-1] == '$' {
|
||||
exp = exp[:len(exp)-1]
|
||||
}
|
||||
exp += "(?::[0-9]{1,5})?"
|
||||
regHostPort = regexp.MustCompile(exp)
|
||||
}
|
||||
|
||||
func NewHostPortValidator(key string) *ValidatorHostPort {
|
||||
v := &ValidatorHostPort{
|
||||
ValidatorRegexp: *NewRegexpValidator(key, regHostPort),
|
||||
}
|
||||
v.SetParent(v)
|
||||
return v
|
||||
}
|
||||
|
||||
func (v *ValidatorHostPort) getValue() interface{} {
|
||||
return v.Value
|
||||
}
|
||||
|
||||
func (v *ValidatorHostPort) OptionalPort(optionalPort bool) *ValidatorHostPort {
|
||||
v.optionalPort = optionalPort
|
||||
return v
|
||||
}
|
||||
|
||||
func (v *ValidatorHostPort) Validate(data *jsonutils.JSONDict) error {
|
||||
err := v.ValidatorRegexp.Validate(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hostPort := v.ValidatorRegexp.Value
|
||||
if hostPort == "" && (v.optional || v.allowEmpty) {
|
||||
return nil
|
||||
}
|
||||
i := strings.IndexRune(hostPort, ':')
|
||||
if i < 0 {
|
||||
if v.optionalPort {
|
||||
v.Value = hostPort
|
||||
v.Domain = hostPort
|
||||
return nil
|
||||
}
|
||||
return newInvalidValueError(v.Key, "port missing")
|
||||
}
|
||||
portStr := hostPort[i+1:]
|
||||
port, err := strconv.ParseUint(portStr, 10, 16)
|
||||
if err != nil {
|
||||
return newInvalidValueError(v.Key, "bad port integer: "+err.Error())
|
||||
}
|
||||
if port <= 0 {
|
||||
return newInvalidValueError(v.Key, "negative port")
|
||||
}
|
||||
v.Value = hostPort
|
||||
v.Domain = hostPort[:i]
|
||||
v.Port = int(port)
|
||||
return nil
|
||||
}
|
||||
|
||||
type ValidatorURLPath struct {
|
||||
ValidatorRegexp
|
||||
}
|
||||
|
||||
@@ -52,6 +52,18 @@ func TestURLPathRegexp(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegHostPort(t *testing.T) {
|
||||
inputs := []string{
|
||||
"www.yunion.cn",
|
||||
"www.yunion.cn:9000",
|
||||
}
|
||||
for _, in := range inputs {
|
||||
if !regHostPort.Match([]byte(in)) {
|
||||
t.Errorf("should match: %q", in)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type C struct {
|
||||
Name string
|
||||
In string
|
||||
@@ -161,6 +173,73 @@ func TestStringChoicesValidator(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntChoicesValidator(t *testing.T) {
|
||||
choices := []int64{-1, 0, 100}
|
||||
cases := []*C{
|
||||
{
|
||||
Name: "missing non-optional",
|
||||
In: `{}`,
|
||||
Out: `{}`,
|
||||
Optional: false,
|
||||
Err: ERR_MISSING_KEY,
|
||||
ValueWant: int64(0),
|
||||
},
|
||||
{
|
||||
Name: "missing optional",
|
||||
In: `{}`,
|
||||
Out: `{}`,
|
||||
Optional: true,
|
||||
ValueWant: int64(0),
|
||||
},
|
||||
{
|
||||
Name: "missing with default",
|
||||
In: `{}`,
|
||||
Out: `{s: -1}`,
|
||||
Default: int64(-1),
|
||||
ValueWant: int64(-1),
|
||||
},
|
||||
{
|
||||
Name: "stringified",
|
||||
In: `{"s": "100"}`,
|
||||
Out: `{s: 100}`,
|
||||
ValueWant: int64(100),
|
||||
},
|
||||
{
|
||||
Name: "stringified invalid choice",
|
||||
In: `{"s": "101"}`,
|
||||
Out: `{"s": "101"}`,
|
||||
Err: ERR_INVALID_CHOICE,
|
||||
ValueWant: int64(0),
|
||||
},
|
||||
{
|
||||
Name: "good choice",
|
||||
In: `{"s": 0}`,
|
||||
Out: `{"s": 0}`,
|
||||
ValueWant: int64(0),
|
||||
},
|
||||
{
|
||||
Name: "bad choice",
|
||||
In: `{"s": 101}`,
|
||||
Out: `{"s": 101}`,
|
||||
Err: ERR_INVALID_CHOICE,
|
||||
ValueWant: int64(0),
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.Name, func(t *testing.T) {
|
||||
v := NewIntChoicesValidator("s", choices)
|
||||
if c.Default != nil {
|
||||
s := c.Default.(int64)
|
||||
v.Default(s)
|
||||
}
|
||||
if c.Optional {
|
||||
v.Optional(true)
|
||||
}
|
||||
testS(t, v, c)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringMultiChoicesValidator(t *testing.T) {
|
||||
type MultiChoicesC struct {
|
||||
*C
|
||||
@@ -487,6 +566,134 @@ func TestRegexValidator(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostPortValidator(t *testing.T) {
|
||||
type HostPortC struct {
|
||||
*C
|
||||
AllowEmpty bool
|
||||
OptionalPort bool
|
||||
}
|
||||
cases := []*HostPortC{
|
||||
{
|
||||
C: &C{
|
||||
Name: "missing non-optional",
|
||||
In: `{}`,
|
||||
Out: `{}`,
|
||||
Err: ERR_MISSING_KEY,
|
||||
ValueWant: "",
|
||||
},
|
||||
},
|
||||
{
|
||||
C: &C{
|
||||
Name: "missing optional",
|
||||
In: `{}`,
|
||||
Out: `{}`,
|
||||
Optional: true,
|
||||
ValueWant: "",
|
||||
},
|
||||
},
|
||||
{
|
||||
C: &C{
|
||||
Name: "missing with default",
|
||||
In: `{}`,
|
||||
Out: `{s: "example.com"}`,
|
||||
Default: "example.com",
|
||||
ValueWant: "example.com",
|
||||
},
|
||||
OptionalPort: true,
|
||||
},
|
||||
{
|
||||
C: &C{
|
||||
Name: "missing with default (has port)",
|
||||
In: `{}`,
|
||||
Out: `{s: "example.com:9000"}`,
|
||||
Default: "example.com:9000",
|
||||
ValueWant: "example.com:9000",
|
||||
},
|
||||
},
|
||||
{
|
||||
C: &C{
|
||||
Name: "valid",
|
||||
In: `{s: "a.example.com"}`,
|
||||
Out: `{s: "a.example.com"}`,
|
||||
ValueWant: "a.example.com",
|
||||
},
|
||||
OptionalPort: true,
|
||||
},
|
||||
{
|
||||
C: &C{
|
||||
Name: "valid (has port)",
|
||||
In: `{s: "a.example.com:9000"}`,
|
||||
Out: `{s: "a.example.com:9000"}`,
|
||||
ValueWant: "a.example.com:9000",
|
||||
},
|
||||
},
|
||||
{
|
||||
C: &C{
|
||||
Name: "valid (allow empty)",
|
||||
In: `{s: ""}`,
|
||||
Out: `{s: ""}`,
|
||||
ValueWant: "",
|
||||
},
|
||||
AllowEmpty: true,
|
||||
},
|
||||
{
|
||||
C: &C{
|
||||
Name: "invalid (domain)",
|
||||
In: `{s: "/.example.com:9000"}`,
|
||||
Out: `{s: "/.example.com:9000"}`,
|
||||
ValueWant: "",
|
||||
Err: ERR_INVALID_VALUE,
|
||||
},
|
||||
},
|
||||
{
|
||||
C: &C{
|
||||
Name: "invalid (port)",
|
||||
In: `{s: "/.example.com:65536"}`,
|
||||
Out: `{s: "/.example.com:65536"}`,
|
||||
ValueWant: "",
|
||||
Err: ERR_INVALID_VALUE,
|
||||
},
|
||||
},
|
||||
{
|
||||
C: &C{
|
||||
Name: "invalid (no port)",
|
||||
In: `{s: "a.example.com"}`,
|
||||
Out: `{s: "a.example.com"}`,
|
||||
ValueWant: "",
|
||||
Err: ERR_INVALID_VALUE,
|
||||
},
|
||||
},
|
||||
{
|
||||
C: &C{
|
||||
Name: "invalid (disallow empty)",
|
||||
In: `{s: ""}`,
|
||||
Out: `{s: ""}`,
|
||||
ValueWant: "",
|
||||
Err: ERR_INVALID_VALUE,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.Name, func(t *testing.T) {
|
||||
v := NewHostPortValidator("s")
|
||||
if c.Default != nil {
|
||||
i := c.Default.(string)
|
||||
v.Default(i)
|
||||
}
|
||||
if c.Optional {
|
||||
v.Optional(true)
|
||||
}
|
||||
if c.OptionalPort {
|
||||
v.OptionalPort(true)
|
||||
}
|
||||
if c.AllowEmpty {
|
||||
v.AllowEmpty(true)
|
||||
}
|
||||
testS(t, v, c.C)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIPv4Validator(t *testing.T) {
|
||||
var nilIP net.IP
|
||||
localIP := net.IPv4(127, 0, 0, 1).To4()
|
||||
|
||||
Reference in New Issue
Block a user