From 1600d6b81c8ded445356b01bf7a9ce3a5b188076 Mon Sep 17 00:00:00 2001 From: wtifs Date: Mon, 20 May 2024 16:11:51 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20http=E5=AE=9E=E4=BE=8B=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=8E=A5=E5=8F=A3=E6=94=AF=E6=8C=81=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?metadata=E6=9D=A1=E4=BB=B6=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apiserver/httpserver/utils/handler.go | 7 +++++-- service/instance.go | 13 ++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/apiserver/httpserver/utils/handler.go b/apiserver/httpserver/utils/handler.go index e8d1c91d9..f757a7d75 100644 --- a/apiserver/httpserver/utils/handler.go +++ b/apiserver/httpserver/utils/handler.go @@ -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 } diff --git a/service/instance.go b/service/instance.go index a8508d02a..693dbf468 100644 --- a/service/instance.go +++ b/service/instance.go @@ -22,6 +22,7 @@ import ( "errors" "fmt" "strconv" + "strings" "time" "github.com/gogo/protobuf/jsonpb" @@ -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为准 From eba32d51e7ab0beba80ff5a77b8dc5f7305bf39c Mon Sep 17 00:00:00 2001 From: wtifs Date: Thu, 23 May 2024 15:06:05 +0800 Subject: [PATCH 2/3] test: update test --- apiserver/httpserver/utils/handler_test.go | 13 +++++++++++++ service/instance_test.go | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/apiserver/httpserver/utils/handler_test.go b/apiserver/httpserver/utils/handler_test.go index a7080102f..8b4d69108 100644 --- a/apiserver/httpserver/utils/handler_test.go +++ b/apiserver/httpserver/utils/handler_test.go @@ -19,6 +19,7 @@ package utils import ( "fmt" + "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "strings" @@ -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") +} diff --git a/service/instance_test.go b/service/instance_test.go index a98b14ce6..b2266221c 100644 --- a/service/instance_test.go +++ b/service/instance_test.go @@ -935,8 +935,8 @@ func TestListInstances1(t *testing.T) { query = map[string]string{ "service": serviceResp.GetName().GetValue(), "namespace": serviceResp.GetNamespace().GetValue(), - "keys": "my-meta-a1", - "values": "1111", + "keys": "my-meta-a1,smy-xmeta-h2", + "values": "1111,2222", } checkAmountAndSize(t, discoverSuit.DiscoverServer().GetInstances(discoverSuit.DefaultCtx, query), 2, 2) // 使用不存在的元数据查询,返回零个实例 From af8d1f8c020355a4bb394fcd3ce722ad2d8e8628 Mon Sep 17 00:00:00 2001 From: wtifs Date: Thu, 23 May 2024 15:07:35 +0800 Subject: [PATCH 3/3] test: update test --- service/instance_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/service/instance_test.go b/service/instance_test.go index b2266221c..f1ca032a2 100644 --- a/service/instance_test.go +++ b/service/instance_test.go @@ -931,7 +931,16 @@ 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(), + "namespace": serviceResp.GetNamespace().GetValue(), + "keys": "my-meta-a1", + "values": "1111", + } + checkAmountAndSize(t, discoverSuit.DiscoverServer().GetInstances(discoverSuit.DefaultCtx, query), 2, 2) + query = map[string]string{ "service": serviceResp.GetName().GetValue(), "namespace": serviceResp.GetNamespace().GetValue(), @@ -939,7 +948,16 @@ func TestListInstances1(t *testing.T) { "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(),