Skip to content

Commit

Permalink
feat: http实例查询接口支持多个metadata条件查询 (#1353)
Browse files Browse the repository at this point in the history
* feat: http实例查询接口支持多个metadata条件查询

* test: update test

* test: update test
  • Loading branch information
WTIFS committed May 23, 2024
1 parent 4d5f642 commit 5330d65
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
7 changes: 5 additions & 2 deletions apiserver/httpserver/utils/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,13 @@ func ParseQueryParams(req *restful.Request) map[string]string {
queryParams := make(map[string]string)
for key, value := range req.Request.URL.Query() {
if len(value) > 0 {
queryParams[key] = value[0] // 暂时默认只支持一个查询
if key == "keys" || key == "values" {
queryParams[key] = strings.Join(value, ",")
} else {
queryParams[key] = value[0] // 暂时默认只支持一个查询
}
}
}

return queryParams
}

Expand Down
13 changes: 13 additions & 0 deletions apiserver/httpserver/utils/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package utils

import (
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -100,3 +101,15 @@ func Test_ParseJsonBody(t *testing.T) {
}

}

func TestParseQueryParams(t *testing.T) {
hreq, _ := http.NewRequest(http.MethodGet, "http://localhost:8090/naming/v1/instances?namespace=default&service=mysql&healthy=true&isolate=false&keys=region&values=cn&keys=zone&values=1a&keys=version&values=v1.0.0&keys=environment&values=prod", nil)
req := restful.NewRequest(hreq)
queryParams := ParseQueryParams(req)
assert.Equal(t, queryParams["namespace"], "default")
assert.Equal(t, queryParams["service"], "mysql")
assert.Equal(t, queryParams["healthy"], "true")
assert.Equal(t, queryParams["isolate"], "false")
assert.Equal(t, queryParams["keys"], "region,zone,version,environment")
assert.Equal(t, queryParams["values"], "cn,1a,v1.0.0,prod")
}
13 changes: 12 additions & 1 deletion service/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"strconv"
"strings"
"time"

"github.com/gogo/protobuf/jsonpb"
Expand Down Expand Up @@ -1237,7 +1238,17 @@ func preGetInstances(query map[string]string) (map[string]string, map[string]str
apimodel.Code_InvalidQueryInsParameter, "instance metadata key and value must be both provided")
}
if metaKeyAvail {
metaFilter = map[string]string{metaKey: metaValue}
metaFilter = map[string]string{}
keys := strings.Split(metaKey, ",")
values := strings.Split(metaValue, ",")
if len(keys) == len(values) {
for i := range keys {
metaFilter[keys[i]] = values[i]
}
} else {
return nil, nil, api.NewBatchQueryResponseWithMsg(
apimodel.Code_InvalidQueryInsParameter, "instance metadata key and value length are different")
}
}

// 以healthy为准
Expand Down
18 changes: 18 additions & 0 deletions service/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ func TestListInstances1(t *testing.T) {
_ = discoverSuit.DiscoverServer().Cache().TestUpdate()

checkAmountAndSize(t, discoverSuit.DiscoverServer().GetInstances(discoverSuit.DefaultCtx, query), 1, 1)

// 使用共同的元数据查询,返回两个实例
query = map[string]string{
"service": serviceResp.GetName().GetValue(),
Expand All @@ -939,7 +940,24 @@ func TestListInstances1(t *testing.T) {
"values": "1111",
}
checkAmountAndSize(t, discoverSuit.DiscoverServer().GetInstances(discoverSuit.DefaultCtx, query), 2, 2)

query = map[string]string{
"service": serviceResp.GetName().GetValue(),
"namespace": serviceResp.GetNamespace().GetValue(),
"keys": "my-meta-a1,smy-xmeta-h2",
"values": "1111,2222",
}
checkAmountAndSize(t, discoverSuit.DiscoverServer().GetInstances(discoverSuit.DefaultCtx, query), 2, 2)

// 使用不存在的元数据查询,返回零个实例
query = map[string]string{
"service": serviceResp.GetName().GetValue(),
"namespace": serviceResp.GetNamespace().GetValue(),
"keys": "my-meta-a1,smy-xmeta-h2",
"values": "1111,none",
}
checkAmountAndSize(t, discoverSuit.DiscoverServer().GetInstances(discoverSuit.DefaultCtx, query), 0, 0)

query = map[string]string{
"service": serviceResp.GetName().GetValue(),
"namespace": serviceResp.GetNamespace().GetValue(),
Expand Down

0 comments on commit 5330d65

Please sign in to comment.