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

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new e6c6931  fix(admin): correct the resources' count field (#4385)
e6c6931 is described below

commit e6c69318e12b67996efadc6535ab937f7f0139c2
Author: 罗泽轩 <[email protected]>
AuthorDate: Tue Jun 8 16:54:29 2021 +0800

    fix(admin): correct the resources' count field (#4385)
    
    Here I use separate test files for the resources to ensure
    the number of resource is 0.
    Signed-off-by: spacewander <[email protected]>
---
 apisix/admin/consumers.lua        |  1 +
 apisix/admin/plugin_config.lua    |  1 +
 apisix/admin/proto.lua            |  1 +
 apisix/admin/routes.lua           |  1 +
 apisix/admin/services.lua         |  1 +
 apisix/admin/ssl.lua              |  1 +
 apisix/admin/stream_routes.lua    |  1 +
 apisix/admin/upstreams.lua        |  1 +
 apisix/admin/utils.lua            | 14 +++++++++
 docs/en/latest/getting-started.md |  2 +-
 docs/zh/latest/getting-started.md |  2 +-
 t/admin/consumers2.t              | 26 ++++++++++++++++
 t/admin/routes3.t                 | 63 +++++++++++++++++++++++++++++++++++++++
 t/admin/ssl3.t                    | 63 +++++++++++++++++++++++++++++++++++++++
 t/admin/upstream3.t               | 63 +++++++++++++++++++++++++++++++++++++++
 15 files changed, 239 insertions(+), 2 deletions(-)

diff --git a/apisix/admin/consumers.lua b/apisix/admin/consumers.lua
index 14bd5cb..504addf 100644
--- a/apisix/admin/consumers.lua
+++ b/apisix/admin/consumers.lua
@@ -97,6 +97,7 @@ function _M.get(consumer_name)
         return 500, {error_msg = err}
     end
 
+    utils.fix_count(res.body, consumer_name)
     return res.status, res.body
 end
 
diff --git a/apisix/admin/plugin_config.lua b/apisix/admin/plugin_config.lua
index f24a312..e3cca79 100644
--- a/apisix/admin/plugin_config.lua
+++ b/apisix/admin/plugin_config.lua
@@ -94,6 +94,7 @@ function _M.get(id)
         return 500, {error_msg = err}
     end
 
+    utils.fix_count(res.body, id)
     return res.status, res.body
 end
 
diff --git a/apisix/admin/proto.lua b/apisix/admin/proto.lua
index f9a4eb9..f7f7a91 100644
--- a/apisix/admin/proto.lua
+++ b/apisix/admin/proto.lua
@@ -92,6 +92,7 @@ function _M.get(id)
         return 500, {error_msg = err}
     end
 
+    utils.fix_count(res.body, id)
     return res.status, res.body
 end
 
diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua
index cad66fc..8de5cbf 100644
--- a/apisix/admin/routes.lua
+++ b/apisix/admin/routes.lua
@@ -202,6 +202,7 @@ function _M.get(id)
         return 500, {error_msg = err}
     end
 
+    utils.fix_count(res.body, id)
     return res.status, res.body
 end
 
diff --git a/apisix/admin/services.lua b/apisix/admin/services.lua
index 4b6e98e..99a6f4f 100644
--- a/apisix/admin/services.lua
+++ b/apisix/admin/services.lua
@@ -145,6 +145,7 @@ function _M.get(id)
         return 500, {error_msg = err}
     end
 
+    utils.fix_count(res.body, id)
     return res.status, res.body
 end
 
diff --git a/apisix/admin/ssl.lua b/apisix/admin/ssl.lua
index b24d014..b1c78c8 100644
--- a/apisix/admin/ssl.lua
+++ b/apisix/admin/ssl.lua
@@ -106,6 +106,7 @@ function _M.get(id)
         res.body.node.value.key = nil
     end
 
+    utils.fix_count(res.body, id)
     return res.status, res.body
 end
 
diff --git a/apisix/admin/stream_routes.lua b/apisix/admin/stream_routes.lua
index 2a00893..d8a5d42 100644
--- a/apisix/admin/stream_routes.lua
+++ b/apisix/admin/stream_routes.lua
@@ -115,6 +115,7 @@ function _M.get(id)
         return 500, {error_msg = err}
     end
 
+    utils.fix_count(res.body, id)
     return res.status, res.body
 end
 
diff --git a/apisix/admin/upstreams.lua b/apisix/admin/upstreams.lua
index d367ec3..9344f36 100644
--- a/apisix/admin/upstreams.lua
+++ b/apisix/admin/upstreams.lua
@@ -98,6 +98,7 @@ function _M.get(id)
         return 500, {error_msg = err}
     end
 
+    utils.fix_count(res.body, id)
     return res.status, res.body
 end
 
diff --git a/apisix/admin/utils.lua b/apisix/admin/utils.lua
index df44323..aa7a34f 100644
--- a/apisix/admin/utils.lua
+++ b/apisix/admin/utils.lua
@@ -16,6 +16,7 @@
 --
 local core    = require("apisix.core")
 local ngx_time = ngx.time
+local tonumber = tonumber
 
 
 local _M = {}
@@ -57,4 +58,17 @@ function _M.inject_conf_with_prev_conf(kind, key, conf)
 end
 
 
+-- fix_count makes the "count" field returned by etcd reasonable
+function _M.fix_count(body, id)
+    if body.count then
+        if not id then
+            -- remove the count of placeholder (init_dir)
+            body.count = tonumber(body.count) - 1
+        else
+            body.count = tonumber(body.count)
+        end
+    end
+end
+
+
 return _M
diff --git a/docs/en/latest/getting-started.md 
b/docs/en/latest/getting-started.md
index 9679c16..be8d32d 100644
--- a/docs/en/latest/getting-started.md
+++ b/docs/en/latest/getting-started.md
@@ -90,7 +90,7 @@ We expect the following data to be returned:
 
 ```json
 {
-  "count":"1",
+  "count":1,
   "action":"get",
   "node":{
     "key":"/apisix/services",
diff --git a/docs/zh/latest/getting-started.md 
b/docs/zh/latest/getting-started.md
index 2153299..034db6e 100644
--- a/docs/zh/latest/getting-started.md
+++ b/docs/zh/latest/getting-started.md
@@ -86,7 +86,7 @@ $ curl "http://127.0.0.1:9080/apisix/admin/services/"; -H 
'X-API-KEY: edd1c9f0343
 
 ```json
 {
-  "count":"1",
+  "count":1,
   "action":"get",
   "node":{
     "key":"/apisix/services",
diff --git a/t/admin/consumers2.t b/t/admin/consumers2.t
index 2b86380..9ff34a8 100644
--- a/t/admin/consumers2.t
+++ b/t/admin/consumers2.t
@@ -125,3 +125,29 @@ __DATA__
     }
 --- response_body
 {"action":"delete","deleted":"1","key":"/apisix/consumers/jack","node":{}}
+
+
+
+=== TEST 4: list empty resources
+--- config
+    location /t {
+        content_by_lua_block {
+            local json = require("toolkit.json")
+            local t = require("lib.test_admin").test
+
+            local code, message, res = t('/apisix/admin/consumers',
+                ngx.HTTP_GET
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(message)
+                return
+            end
+
+            res = json.decode(res)
+            ngx.say(json.encode(res))
+        }
+    }
+--- response_body
+{"action":"get","count":0,"node":{"dir":true,"key":"/apisix/consumers","nodes":{}}}
diff --git a/t/admin/routes3.t b/t/admin/routes3.t
new file mode 100644
index 0000000..5938c14
--- /dev/null
+++ b/t/admin/routes3.t
@@ -0,0 +1,63 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+no_shuffle();
+log_level("info");
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if (!$block->no_error_log && !$block->error_log) {
+        $block->set_value("no_error_log", "[error]\n[alert]");
+    }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: list empty resources
+--- config
+    location /t {
+        content_by_lua_block {
+            local json = require("toolkit.json")
+            local t = require("lib.test_admin").test
+
+            local code, message, res = t('/apisix/admin/routes',
+                ngx.HTTP_GET
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(message)
+                return
+            end
+
+            res = json.decode(res)
+            ngx.say(json.encode(res))
+        }
+    }
+--- response_body
+{"action":"get","count":0,"node":{"dir":true,"key":"/apisix/routes","nodes":{}}}
diff --git a/t/admin/ssl3.t b/t/admin/ssl3.t
new file mode 100644
index 0000000..4fcce60
--- /dev/null
+++ b/t/admin/ssl3.t
@@ -0,0 +1,63 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+no_shuffle();
+log_level("info");
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if (!$block->no_error_log && !$block->error_log) {
+        $block->set_value("no_error_log", "[error]\n[alert]");
+    }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: list empty resources
+--- config
+    location /t {
+        content_by_lua_block {
+            local json = require("toolkit.json")
+            local t = require("lib.test_admin").test
+
+            local code, message, res = t('/apisix/admin/ssl',
+                ngx.HTTP_GET
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(message)
+                return
+            end
+
+            res = json.decode(res)
+            ngx.say(json.encode(res))
+        }
+    }
+--- response_body
+{"action":"get","count":0,"node":{"dir":true,"key":"/apisix/ssl","nodes":{}}}
diff --git a/t/admin/upstream3.t b/t/admin/upstream3.t
new file mode 100644
index 0000000..6dda9ad
--- /dev/null
+++ b/t/admin/upstream3.t
@@ -0,0 +1,63 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+no_shuffle();
+log_level("info");
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if (!$block->no_error_log && !$block->error_log) {
+        $block->set_value("no_error_log", "[error]\n[alert]");
+    }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: list empty resources
+--- config
+    location /t {
+        content_by_lua_block {
+            local json = require("toolkit.json")
+            local t = require("lib.test_admin").test
+
+            local code, message, res = t('/apisix/admin/upstreams',
+                ngx.HTTP_GET
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(message)
+                return
+            end
+
+            res = json.decode(res)
+            ngx.say(json.encode(res))
+        }
+    }
+--- response_body
+{"action":"get","count":0,"node":{"dir":true,"key":"/apisix/upstreams","nodes":{}}}

Reply via email to