You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2021/09/10 05:53:50 UTC

[apisix] branch master updated: feat(control): add new dump route config api for control api (#5011)

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 0e8b866  feat(control): add new dump route config api for control api (#5011)
0e8b866 is described below

commit 0e8b8660f6f8b03f3286c9789c90119c8f1c0382
Author: Manjusaka <me...@manjusaka.me>
AuthorDate: Fri Sep 10 13:53:46 2021 +0800

    feat(control): add new dump route config api for control api (#5011)
---
 apisix/control/v1.lua         |  49 +++++++++++++++
 docs/en/latest/control-api.md |  78 +++++++++++++++++++++++
 t/control/routes.t            | 140 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 267 insertions(+)

diff --git a/apisix/control/v1.lua b/apisix/control/v1.lua
index 1f200f0..d80c295 100644
--- a/apisix/control/v1.lua
+++ b/apisix/control/v1.lua
@@ -156,6 +156,43 @@ function _M.get_health_checker()
     return 200, info
 end
 
+local function iter_add_get_routes_info(values, route_id)
+    local infos = {}
+    for _, route in core.config_util.iterate_values(values) do
+        local new_route = core.table.deepcopy(route)
+        if new_route.value.upstream and new_route.value.upstream.parent then
+            new_route.value.upstream.parent = nil
+        end
+        core.table.insert(infos, new_route)
+        -- check the roude id
+        if route_id and route.value.id == route_id then
+            return new_route
+        end
+    end
+    if not route_id then
+        return infos
+    end
+    return nil
+end
+
+function _M.dump_all_routes_info()
+    local routes = get_routes()
+    local infos, _ = iter_add_get_routes_info(routes, nil)
+    return 200, infos
+end
+
+function _M.dump_route_info()
+    local routes = get_routes()
+    local uri_segs = core.utils.split_uri(ngx_var.uri)
+    local route_id = uri_segs[4]
+    local route = iter_add_get_routes_info(routes, route_id)
+    if not route then
+        return 404, {error_msg = str_format("route[%s] not found", route_id)}
+    end
+    return 200, route
+end
+
+
 
 function _M.trigger_gc()
     -- TODO: find a way to trigger GC in the stream subsystem
@@ -189,4 +226,16 @@ return {
         uris = {"/gc"},
         handler = _M.trigger_gc,
     },
+    -- /v1/routes
+    {
+        methods = {"GET"},
+        uris = {"/routes"},
+        handler = _M.dump_all_routes_info,
+    },
+    --- /v1/
+    {
+        methods = {"GET"},
+        uris = {"/route/*"},
+        handler = _M.dump_route_info,
+    }
 }
diff --git a/docs/en/latest/control-api.md b/docs/en/latest/control-api.md
index 92355b5..27697cb 100644
--- a/docs/en/latest/control-api.md
+++ b/docs/en/latest/control-api.md
@@ -205,3 +205,81 @@ Introduced since `v2.8`.
 
 Trigger a full GC in the http subsystem.
 Note that when you enable stream proxy, APISIX will run another Lua VM for the stream subsystem. It won't trigger a full GC in this Lua VM .
+
+### Get /v1/routes
+
+Introduced since `v3.0`.
+
+Return all routes info in the format below:
+
+```json
+[
+  {
+    "update_count": 0,
+    "value": {
+      "priority": 0,
+      "uris": [
+        "/hello"
+      ],
+      "id": "1",
+      "upstream": {
+        "scheme": "http",
+        "pass_host": "pass",
+        "nodes": [
+          {
+            "port": 1980,
+            "host": "127.0.0.1",
+            "weight": 1
+          }
+        ],
+        "type": "roundrobin",
+        "hash_on": "vars"
+      },
+      "status": 1
+    },
+    "clean_handlers": {},
+    "has_domain": false,
+    "orig_modifiedIndex": 1631193445,
+    "modifiedIndex": 1631193445,
+    "key": "/routes/1"
+  }
+]
+```
+
+### Get /v1/route/{route_id}
+
+Introduced since `v3.0`.
+
+Return specific route info with **route_id** in the format below:
+
+```json
+{
+  "update_count": 0,
+  "value": {
+    "priority": 0,
+    "uris": [
+      "/hello"
+    ],
+    "id": "1",
+    "upstream": {
+      "scheme": "http",
+      "pass_host": "pass",
+      "nodes": [
+        {
+          "port": 1980,
+          "host": "127.0.0.1",
+          "weight": 1
+        }
+      ],
+      "type": "roundrobin",
+      "hash_on": "vars"
+    },
+    "status": 1
+  },
+  "clean_handlers": {},
+  "has_domain": false,
+  "orig_modifiedIndex": 1631193445,
+  "modifiedIndex": 1631193445,
+  "key": "/routes/1"
+}
+```
diff --git a/t/control/routes.t b/t/control/routes.t
new file mode 100644
index 0000000..180a690
--- /dev/null
+++ b/t/control/routes.t
@@ -0,0 +1,140 @@
+#
+# 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->yaml_config) {
+        my $yaml_config = <<_EOC_;
+apisix:
+    node_listen: 1984
+    config_center: yaml
+    enable_admin: false
+_EOC_
+
+        $block->set_value("yaml_config", $yaml_config);
+    }
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: routes
+--- apisix_yaml
+routes:
+  -
+    id: 1
+    uris:
+        - /hello
+    upstream:
+      nodes:
+        "127.0.0.1:1980": 1
+      type: roundrobin
+#END
+--- config
+    location /t {
+        content_by_lua_block {
+            local json = require("toolkit.json")
+            local t = require("lib.test_admin")
+            local code, body, res = t.test('/v1/routes',
+                ngx.HTTP_GET)
+            res = json.decode(res)
+            if res[1] then
+                local data = {}
+                data.uris = res[1].value.uris
+                data.upstream = res[1].value.upstream
+                ngx.say(json.encode(data))
+            end
+        }
+    }
+--- response_body
+{"upstream":{"hash_on":"vars","nodes":[{"host":"127.0.0.1","port":1980,"weight":1}],"pass_host":"pass","scheme":"http","type":"roundrobin"},"uris":["/hello"]}
+
+
+
+=== TEST 2: get route with id 1
+--- apisix_yaml
+routes:
+  -
+    id: 1
+    uris:
+        - /hello
+    upstream:
+      nodes:
+        "127.0.0.1:1980": 1
+      type: roundrobin
+#END
+--- config
+    location /t {
+        content_by_lua_block {
+            local json = require("toolkit.json")
+            local t = require("lib.test_admin")
+            local code, body, res = t.test('/v1/route/1',
+                ngx.HTTP_GET)
+            res = json.decode(res)
+            if res then
+                local data = {}
+                data.uris = res.value.uris
+                data.upstream = res.value.upstream
+                ngx.say(json.encode(data))
+            end
+        }
+    }
+--- response_body
+{"upstream":{"hash_on":"vars","nodes":[{"host":"127.0.0.1","port":1980,"weight":1}],"pass_host":"pass","scheme":"http","type":"roundrobin"},"uris":["/hello"]}
+
+
+
+=== TEST 3: routes with invalid id
+--- apisix_yaml
+routes:
+  -
+    id: 1
+    uris:
+        - /hello
+    upstream:
+      nodes:
+        "127.0.0.1:1980": 1
+      type: roundrobin
+#END
+--- config
+    location /t {
+        content_by_lua_block {
+            local json = require("toolkit.json")
+            local t = require("lib.test_admin")
+            local code, body, res = t.test('/v1/route/2',
+                ngx.HTTP_GET)
+            local data = {}
+            data.status = code
+            ngx.say(json.encode(data))
+            return
+        }
+    }
+--- response_body
+{"status":404}