Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCB-2096 The configuration center supports fuzzy query #163

Merged
merged 4 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/validate/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ var defaultValidator = NewValidator()
const (
key = "key"
commonNameRegexString = `^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$`
getKeyRegexString = `^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$|^beginWith\([a-zA-Z0-9][a-zA-Z0-9_\-.]*\)$`
asciiRegexString = `^[\x00-\x7F]*$`
)

// custom validate rules
// please use different tag names from third party tags
var customRules = []*RegexValidateRule{
NewRule(key, commonNameRegexString, &Option{Min: 1, Max: 128}),
NewRule("getKey", commonNameRegexString, &Option{Max: 128}),
NewRule("getKey", getKeyRegexString, &Option{Max: 128}),
NewRule("commonName", commonNameRegexString, &Option{Min: 1, Max: 256}),
NewRule("valueType", `^$|^(ini|json|text|yaml|properties)$`, nil),
NewRule("kvStatus", `^$|^(enabled|disabled)$`, nil),
Expand Down
15 changes: 15 additions & 0 deletions pkg/validate/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,19 @@ func TestValidate(t *testing.T) {
Labels: map[string]string{string32 + "a": "a"},
}
assert.Error(t, validate.Validate(kvDoc))

ListKVRe := &model.ListKVRequest{Project: "a", Domain: "a",
Key: "beginWith(a)",
}
assert.NoError(t, validate.Validate(ListKVRe))

ListKVRe = &model.ListKVRequest{Project: "a", Domain: "a",
Key: "beginW(a)",
}
assert.Error(t, validate.Validate(ListKVRe))

ListKVRe = &model.ListKVRequest{Project: "a", Domain: "a",
Key: "beginW()",
}
assert.Error(t, validate.Validate(ListKVRe))
}
15 changes: 15 additions & 0 deletions server/resource/v1/kv_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,21 @@ func TestKVResource_List(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 1, len(result.Data))
})
t.Run("get one key, fuzzy match,should return 2 kv", func(t *testing.T) {
r, _ := http.NewRequest("GET", "/v1/kv_test/kie/kv?key=beginWith(time)", nil)
r.Header.Set("Content-Type", "application/json")
kvr := &v1.KVResource{}
c, err := restfultest.New(kvr, nil)
assert.NoError(t, err)
resp := httptest.NewRecorder()
c.ServeHTTP(resp, r)
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
result := &model.KVResponse{}
err = json.Unmarshal(body, result)
assert.NoError(t, err)
assert.Equal(t, 2, len(result.Data))
})
t.Run("get one key by service label should return 2 kv,delete one", func(t *testing.T) {
r, _ := http.NewRequest("GET", "/v1/kv_test/kie/kv?key=timeout&label=service:utService", nil)
r.Header.Set("Content-Type", "application/json")
Expand Down
12 changes: 12 additions & 0 deletions server/service/mongo/kv/kv_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package kv
import (
"context"
"fmt"
"regexp"
"strings"
"time"

"github.com/apache/servicecomb-kie/pkg/model"
Expand Down Expand Up @@ -108,12 +110,22 @@ func updateKeyValue(ctx context.Context, kv *model.KVDoc) error {

}

//Extract key values
func getValue(str string) string {
rex := regexp.MustCompile(`\(([^)]+)\)`)
res := rex.FindStringSubmatch(str)
return res[len(res)-1]
}

func findKV(ctx context.Context, domain string, project string, opts service.FindOptions) (*mongo.Cursor, int, error) {
collection := session.GetDB().Collection(session.CollectionKV)
ctx, _ = context.WithTimeout(ctx, opts.Timeout)
filter := bson.M{"domain": domain, "project": project}
if opts.Key != "" {
filter["key"] = opts.Key
if strings.HasPrefix(opts.Key, "beginWith") {
filter["key"] = bson.M{"$regex": getValue(opts.Key), "$options": "$i"}
}
}
if len(opts.Labels) != 0 {
for k, v := range opts.Labels {
Expand Down