You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by ju...@apache.org on 2020/10/15 01:34:53 UTC

[apisix-dashboard] branch refactor updated: feat: support search for resource list (#557)

This is an automated email from the ASF dual-hosted git repository.

juzhiyuan pushed a commit to branch refactor
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git


The following commit(s) were added to refs/heads/refactor by this push:
     new c3dcefb  feat: support search for resource list (#557)
c3dcefb is described below

commit c3dcefb45a20c41e7869ffaa3c22ae602add6f50
Author: nic-chen <33...@users.noreply.github.com>
AuthorDate: Thu Oct 15 09:34:45 2020 +0800

    feat: support search for resource list (#557)
    
    * feat: support search for resource list
    
    * fix ci
---
 api/internal/handler/consumer/consumer.go      |  2 +-
 api/internal/handler/consumer/consumer_test.go | 18 ++++++++++++++++
 api/internal/handler/route/route.go            | 10 ++++++++-
 api/internal/handler/route/route_test.go       | 30 ++++++++++++++++++++++++++
 api/internal/handler/service/service.go        |  5 ++++-
 api/internal/handler/service/service_test.go   | 24 +++++++++++++++++++++
 api/internal/handler/ssl/ssl.go                | 15 ++++++++++---
 api/internal/handler/ssl/ssl_test.go           | 20 +++++++++++++++++
 api/internal/handler/upstream/upstream.go      |  6 +++---
 9 files changed, 121 insertions(+), 9 deletions(-)

diff --git a/api/internal/handler/consumer/consumer.go b/api/internal/handler/consumer/consumer.go
index 372a765..1fdf3b3 100644
--- a/api/internal/handler/consumer/consumer.go
+++ b/api/internal/handler/consumer/consumer.go
@@ -79,7 +79,7 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) {
 	ret, err := h.consumerStore.List(store.ListInput{
 		Predicate: func(obj interface{}) bool {
 			if input.Username != "" {
-				return strings.Index(obj.(*entity.Consumer).Username, input.Username) > 0
+				return strings.Contains(obj.(*entity.Consumer).Username, input.Username)
 			}
 			return true
 		},
diff --git a/api/internal/handler/consumer/consumer_test.go b/api/internal/handler/consumer/consumer_test.go
index 3a3a43d..567c796 100644
--- a/api/internal/handler/consumer/consumer_test.go
+++ b/api/internal/handler/consumer/consumer_test.go
@@ -147,6 +147,24 @@ func TestConsumer(t *testing.T) {
 	dataPage2 := retPage2.(*store.ListOutput)
 	assert.Equal(t, len(dataPage2.Rows), 1)
 
+	//list search match
+	listInput3 := &ListInput{}
+	reqBody = `{"page_size": 1, "page": 1, "username": "pony"}`
+	json.Unmarshal([]byte(reqBody), listInput3)
+	ctx.SetInput(listInput3)
+	retPage, err := handler.List(ctx)
+	dataPage := retPage.(*store.ListOutput)
+	assert.Equal(t, len(dataPage.Rows), 1)
+
+	//list search not match
+	listInput4 := &ListInput{}
+	reqBody = `{"page_size": 1, "page": 1, "username": "not-exists"}`
+	json.Unmarshal([]byte(reqBody), listInput4)
+	ctx.SetInput(listInput4)
+	retPage, err = handler.List(ctx)
+	dataPage = retPage.(*store.ListOutput)
+	assert.Equal(t, len(dataPage.Rows), 0)
+
 	//delete consumer
 	inputDel := &BatchDelete{}
 	reqBody = `{"usernames": "jack"}`
diff --git a/api/internal/handler/route/route.go b/api/internal/handler/route/route.go
index 8f1b409..870eb06 100644
--- a/api/internal/handler/route/route.go
+++ b/api/internal/handler/route/route.go
@@ -93,6 +93,7 @@ func (h *Handler) Get(c droplet.Context) (interface{}, error) {
 
 type ListInput struct {
 	Name string `auto_read:"name,query"`
+	URI  string `auto_read:"uri,query"`
 	store.Pagination
 }
 
@@ -101,8 +102,15 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) {
 
 	ret, err := h.routeStore.List(store.ListInput{
 		Predicate: func(obj interface{}) bool {
+			if input.Name != "" && input.URI != "" {
+				return strings.Contains(obj.(*entity.Route).Name, input.Name) &&
+					strings.Contains(obj.(*entity.Route).URI, input.URI)
+			}
 			if input.Name != "" {
-				return strings.Index(obj.(*entity.Route).Name, input.Name) > 0
+				return strings.Contains(obj.(*entity.Route).Name, input.Name)
+			}
+			if input.URI != "" {
+				return strings.Contains(obj.(*entity.Route).URI, input.URI)
 			}
 			return true
 		},
diff --git a/api/internal/handler/route/route_test.go b/api/internal/handler/route/route_test.go
index 5c7d0e3..abed058 100644
--- a/api/internal/handler/route/route_test.go
+++ b/api/internal/handler/route/route_test.go
@@ -755,6 +755,36 @@ func TestRoute(t *testing.T) {
 	dataPage := retPage.(*store.ListOutput)
 	assert.Equal(t, len(dataPage.Rows), 1)
 
+	//list search match
+	listInput2 := &ListInput{}
+	reqBody = `{"page_size": 1, "page": 1, "name": "a", "uri": "index"}`
+	json.Unmarshal([]byte(reqBody), listInput2)
+	ctx.SetInput(listInput2)
+	retPage, err = handler.List(ctx)
+	assert.Nil(t, err)
+	dataPage = retPage.(*store.ListOutput)
+	assert.Equal(t, len(dataPage.Rows), 1)
+
+	//list search name not match
+	listInput3 := &ListInput{}
+	reqBody = `{"page_size": 1, "page": 1, "name": "not-exists", "uri": "index"}`
+	json.Unmarshal([]byte(reqBody), listInput3)
+	ctx.SetInput(listInput3)
+	retPage, err = handler.List(ctx)
+	assert.Nil(t, err)
+	dataPage = retPage.(*store.ListOutput)
+	assert.Equal(t, len(dataPage.Rows), 0)
+
+	//list search uri not match
+	listInput4 := &ListInput{}
+	reqBody = `{"page_size": 1, "page": 1, "name": "a", "uri": "not-exists"}`
+	json.Unmarshal([]byte(reqBody), listInput4)
+	ctx.SetInput(listInput4)
+	retPage, err = handler.List(ctx)
+	assert.Nil(t, err)
+	dataPage = retPage.(*store.ListOutput)
+	assert.Equal(t, len(dataPage.Rows), 0)
+
 	//delete test data
 	inputDel := &BatchDelete{}
 	reqBody = `{"ids": "1"}`
diff --git a/api/internal/handler/service/service.go b/api/internal/handler/service/service.go
index a85eda2..7ee3c0f 100644
--- a/api/internal/handler/service/service.go
+++ b/api/internal/handler/service/service.go
@@ -71,7 +71,7 @@ func (h *Handler) Get(c droplet.Context) (interface{}, error) {
 }
 
 type ListInput struct {
-	ID string `auto_read:"id,query"`
+	Name string `auto_read:"name,query"`
 	store.Pagination
 }
 
@@ -80,6 +80,9 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) {
 
 	ret, err := h.serviceStore.List(store.ListInput{
 		Predicate: func(obj interface{}) bool {
+			if input.Name != "" {
+				return strings.Contains(obj.(*entity.Service).Name, input.Name)
+			}
 			return true
 		},
 		PageSize:   input.PageSize,
diff --git a/api/internal/handler/service/service_test.go b/api/internal/handler/service/service_test.go
index ffce7fc..a433e80 100644
--- a/api/internal/handler/service/service_test.go
+++ b/api/internal/handler/service/service_test.go
@@ -85,6 +85,7 @@ func TestService(t *testing.T) {
 	service2 := &UpdateInput{}
 	service2.ID = "1"
 	reqBody = `{
+      "name": "test-service",
       "plugins": {
           "limit-count": {
               "count": 2,
@@ -107,6 +108,9 @@ func TestService(t *testing.T) {
 	_, err = handler.Update(ctx)
 	assert.Nil(t, err)
 
+  //sleep
+  time.Sleep(time.Duration(100) * time.Millisecond)
+
 	//list
 	listInput := &ListInput{}
 	reqBody = `{"page_size": 1, "page": 1}`
@@ -117,6 +121,26 @@ func TestService(t *testing.T) {
 	dataPage := retPage.(*store.ListOutput)
 	assert.Equal(t, len(dataPage.Rows), 1)
 
+	//list search match
+	listInput2 := &ListInput{}
+	reqBody = `{"page_size": 1, "page": 1, "name": "test"}`
+	json.Unmarshal([]byte(reqBody), listInput2)
+	ctx.SetInput(listInput2)
+	retPage, err = handler.List(ctx)
+	assert.Nil(t, err)
+	dataPage = retPage.(*store.ListOutput)
+	assert.Equal(t, len(dataPage.Rows), 1)
+
+	//list search not match
+	listInput3 := &ListInput{}
+	reqBody = `{"page_size": 1, "page": 1, "name": "not-exists"}`
+	json.Unmarshal([]byte(reqBody), listInput3)
+	ctx.SetInput(listInput3)
+	retPage, err = handler.List(ctx)
+	assert.Nil(t, err)
+	dataPage = retPage.(*store.ListOutput)
+	assert.Equal(t, len(dataPage.Rows), 0)
+
 	//delete test data
 	inputDel := &BatchDelete{}
 	reqBody = `{"ids": "1"}`
diff --git a/api/internal/handler/ssl/ssl.go b/api/internal/handler/ssl/ssl.go
index 494eaea..a7f5701 100644
--- a/api/internal/handler/ssl/ssl.go
+++ b/api/internal/handler/ssl/ssl.go
@@ -88,7 +88,7 @@ func (h *Handler) Get(c droplet.Context) (interface{}, error) {
 }
 
 type ListInput struct {
-	ID string `auto_read:"id,query"`
+	SNI string `auto_read:"sni,query"`
 	store.Pagination
 }
 
@@ -97,8 +97,17 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) {
 
 	ret, err := h.sslStore.List(store.ListInput{
 		Predicate: func(obj interface{}) bool {
-			if input.ID != "" {
-				return strings.Index(obj.(*entity.SSL).ID, input.ID) > 0
+			if input.SNI != "" {
+				if strings.Contains(obj.(*entity.SSL).Sni, input.SNI) {
+					return true
+				}
+				for _, str := range obj.(*entity.SSL).Snis {
+					result := strings.Contains(str, input.SNI)
+					if result {
+						return true
+					}
+				}
+				return false
 			}
 			return true
 		},
diff --git a/api/internal/handler/ssl/ssl_test.go b/api/internal/handler/ssl/ssl_test.go
index d50208d..53d26a8 100644
--- a/api/internal/handler/ssl/ssl_test.go
+++ b/api/internal/handler/ssl/ssl_test.go
@@ -90,6 +90,26 @@ func TestSSL(t *testing.T) {
 	dataPage := retPage.(*store.ListOutput)
 	assert.Equal(t, len(dataPage.Rows), 1)
 
+	//list search match
+	listInput2 := &ListInput{}
+	reqBody = `{"page_size": 1, "page": 1, "sni": "route"}`
+	json.Unmarshal([]byte(reqBody), listInput2)
+	ctx.SetInput(listInput2)
+	retPage, err = handler.List(ctx)
+	assert.Nil(t, err)
+	dataPage = retPage.(*store.ListOutput)
+	assert.Equal(t, len(dataPage.Rows), 1)
+
+	//list search not match
+	listInput3 := &ListInput{}
+	reqBody = `{"page_size": 1, "page": 1, "sni": "not-exists"}`
+	json.Unmarshal([]byte(reqBody), listInput3)
+	ctx.SetInput(listInput3)
+	retPage, err = handler.List(ctx)
+	assert.Nil(t, err)
+	dataPage = retPage.(*store.ListOutput)
+	assert.Equal(t, len(dataPage.Rows), 0)
+
 	//delete test data
 	inputDel := &BatchDelete{}
 	reqBody = `{"ids": "1"}`
diff --git a/api/internal/handler/upstream/upstream.go b/api/internal/handler/upstream/upstream.go
index 3727eb7..5071ff2 100644
--- a/api/internal/handler/upstream/upstream.go
+++ b/api/internal/handler/upstream/upstream.go
@@ -75,7 +75,7 @@ func (h *Handler) Get(c droplet.Context) (interface{}, error) {
 }
 
 type ListInput struct {
-	ID string `auto_read:"id,query"`
+	Name string `auto_read:"name,query"`
 	store.Pagination
 }
 
@@ -84,8 +84,8 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) {
 
 	ret, err := h.upstreamStore.List(store.ListInput{
 		Predicate: func(obj interface{}) bool {
-			if input.ID != "" {
-				return strings.Index(obj.(*entity.Upstream).ID, input.ID) > 0
+			if input.Name != "" {
+				return strings.Contains(obj.(*entity.Upstream).Name, input.Name)
 			}
 			return true
 		},