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 502bba0a5 feat: add plugin_metadata into control API (#7514)
502bba0a5 is described below

commit 502bba0a53bbbbf97bbe18771a6215332fb20898
Author: jinhua luo <[email protected]>
AuthorDate: Tue Jul 26 13:31:27 2022 +0800

    feat: add plugin_metadata into control API (#7514)
---
 apisix/control/v1.lua         |  34 ++++++++++++
 docs/en/latest/control-api.md |  37 +++++++++++++
 docs/zh/latest/control-api.md |  37 +++++++++++++
 t/control/plugin-metadata.t   | 117 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 225 insertions(+)

diff --git a/apisix/control/v1.lua b/apisix/control/v1.lua
index bbe457cd6..c6d1e0650 100644
--- a/apisix/control/v1.lua
+++ b/apisix/control/v1.lua
@@ -276,6 +276,28 @@ function _M.dump_service_info()
     return 200, info
 end
 
+function _M.dump_all_plugin_metadata()
+    local names = core.config.local_conf().plugins
+    local metadatas = core.table.new(0, #names)
+    for _, name in ipairs(names) do
+        local metadata = plugin.plugin_metadata(name)
+        if metadata then
+            core.table.insert(metadatas, metadata.value)
+        end
+    end
+    return 200, metadatas
+end
+
+function _M.dump_plugin_metadata()
+    local uri_segs = core.utils.split_uri(ngx_var.uri)
+    local name = uri_segs[4]
+    local metadata = plugin.plugin_metadata(name)
+    if not metadata then
+        return 404, {error_msg = str_format("plugin metadata[%s] not found", 
name)}
+    end
+    return 200, metadata.value
+end
+
 
 return {
     -- /v1/schema
@@ -337,5 +359,17 @@ return {
         methods = {"GET"},
         uris = {"/upstream/*"},
         handler = _M.dump_upstream_info,
+    },
+    -- /v1/plugin_metadatas
+    {
+        methods = {"GET"},
+        uris = {"/plugin_metadatas"},
+        handler = _M.dump_all_plugin_metadata,
+    },
+    -- /v1/plugin_metadata/*
+    {
+        methods = {"GET"},
+        uris = {"/plugin_metadata/*"},
+        handler = _M.dump_plugin_metadata,
     }
 }
diff --git a/docs/en/latest/control-api.md b/docs/en/latest/control-api.md
index 381a643fd..7dd55e24f 100644
--- a/docs/en/latest/control-api.md
+++ b/docs/en/latest/control-api.md
@@ -451,3 +451,40 @@ Dumps the Upstream with the specified `upstream_id`:
    "modifiedIndex":1225
 }
 ```
+
+### GET /v1/plugin_metadatas
+
+Introduced in [v3.0.0](https://github.com/apache/apisix/releases/tag/3.0.0).
+
+Dumps all plugin_metadatas:
+
+```json
+[
+    {
+        "log_format": {
+            "upstream_response_time": "$upstream_response_time"
+        },
+        "id": "file-logger"
+    },
+    {
+        "ikey": 1,
+        "skey": "val",
+        "id": "example-plugin"
+    }
+]
+```
+
+### GET /v1/plugin_metadata/{plugin_name}
+
+Introduced in [v3.0.0](https://github.com/apache/apisix/releases/tag/3.0.0).
+
+Dumps the metadata with the specified `plugin_name`:
+
+```json
+{
+    "log_format": {
+        "upstream_response_time": "$upstream_response_time"
+    },
+    "id": "file-logger"
+}
+```
diff --git a/docs/zh/latest/control-api.md b/docs/zh/latest/control-api.md
index 89792c430..541034b0c 100644
--- a/docs/zh/latest/control-api.md
+++ b/docs/zh/latest/control-api.md
@@ -208,3 +208,40 @@ APISIX 中一些插件添加了自己的 control API。如果你对他们感兴
 在 http 子系统中触发一次全量 GC
 
 注意,当你启用 stream proxy 时,APISIX 将为 stream 子系统运行另一个 Lua 虚拟机。它不会触发这个 Lua 虚拟机中的全量 
GC。
+
+### GET /v1/plugin_metadatas
+
+引入自 3.0.0 版本
+
+打印所有插件的元数据:
+
+```json
+[
+    {
+        "log_format": {
+            "upstream_response_time": "$upstream_response_time"
+        },
+        "id": "file-logger"
+    },
+    {
+        "ikey": 1,
+        "skey": "val",
+        "id": "example-plugin"
+    }
+]
+```
+
+### GET /v1/plugin_metadata/{plugin_name}
+
+引入自 3.0.0 版本
+
+打印指定插件的元数据:
+
+```json
+{
+    "log_format": {
+        "upstream_response_time": "$upstream_response_time"
+    },
+    "id": "file-logger"
+}
+```
diff --git a/t/control/plugin-metadata.t b/t/control/plugin-metadata.t
new file mode 100644
index 000000000..21e784186
--- /dev/null
+++ b/t/control/plugin-metadata.t
@@ -0,0 +1,117 @@
+#
+# 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';
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: add plugin metadatas
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code = t('/apisix/admin/plugin_metadata/example-plugin',
+                ngx.HTTP_PUT,
+                [[{
+                    "skey": "val",
+                    "ikey": 1
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+
+            local code = t('/apisix/admin/plugin_metadata/file-logger',
+                ngx.HTTP_PUT,
+                [[
+                {"log_format": {"upstream_response_time": 
"$upstream_response_time"}}
+                ]]
+                )
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+        }
+    }
+--- error_code: 200
+
+
+
+=== TEST 2: dump all plugin metadatas
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local _, _, res = t('/v1/plugin_metadatas', ngx.HTTP_GET)
+            local json = require("toolkit.json")
+            res = json.decode(res)
+            for _, metadata in ipairs(res) do
+                if metadata.id == "file-logger" then
+                    ngx.say("check log_format: ", 
metadata.log_format.upstream_response_time == "$upstream_response_time")
+                elseif metadata.id == "example-plugin" then
+                    ngx.say("check skey: ", metadata.skey == "val")
+                    ngx.say("check ikey: ", metadata.ikey == 1)
+                end
+            end
+        }
+    }
+--- response_body
+check log_format: true
+check skey: true
+check ikey: true
+
+
+
+=== TEST 3: dump file-logger metadata
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local _, _, res = t('/v1/plugin_metadata/file-logger', 
ngx.HTTP_GET)
+            local json = require("toolkit.json")
+            metadata = json.decode(res)
+            if metadata.id == "file-logger" then
+                ngx.say("check log_format: ", 
metadata.log_format.upstream_response_time == "$upstream_response_time")
+            end
+        }
+    }
+--- response_body
+check log_format: true
+
+
+
+=== TEST 4: plugin without metadata
+--- request
+GET /v1/plugin_metadata/batch-requests
+--- error_code: 404
+--- response_body
+{"error_msg":"plugin metadata[batch-requests] not found"}

Reply via email to