This is an automated email from the ASF dual-hosted git repository.
monkeydluffy 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 26ba567a0 change: Improve results from admin API while GET all plugins
(#9580)
26ba567a0 is described below
commit 26ba567a05ff7d6db4abcd95d0ce439bb45a9202
Author: Ashish Tiwari <[email protected]>
AuthorDate: Wed Jul 19 07:28:31 2023 +0530
change: Improve results from admin API while GET all plugins (#9580)
---
apisix/admin/init.lua | 14 +++++++---
apisix/admin/plugins.lua | 65 ++++++++++++++++++++++++++++++++-------------
apisix/plugin.lua | 5 ++++
docs/en/latest/admin-api.md | 28 +++++++------------
docs/zh/latest/admin-api.md | 12 +++++++--
t/admin/plugins.t | 59 +++++++++++++++++++++++++++++++++++++---
t/admin/schema.t | 2 +-
7 files changed, 139 insertions(+), 46 deletions(-)
diff --git a/apisix/admin/init.lua b/apisix/admin/init.lua
index 3e72e4aaf..0d4ef9323 100644
--- a/apisix/admin/init.lua
+++ b/apisix/admin/init.lua
@@ -16,6 +16,7 @@
--
local require = require
local core = require("apisix.core")
+local get_uri_args = ngx.req.get_uri_args
local route = require("apisix.utils.router")
local plugin = require("apisix.plugin")
local v3_adapter = require("apisix.admin.v3_adapter")
@@ -254,9 +255,16 @@ end
local function get_plugins_list()
set_ctx_and_check_token()
-
- local plugins = resources.plugins.get_plugins_list()
- core.response.exit(200, plugins)
+ local args = get_uri_args()
+ local subsystem = args["subsystem"]
+ -- If subsystem is passed then it should be either http or stream.
+ -- If it is not passed/nil then http will be default.
+ subsystem = subsystem or "http"
+ if subsystem == "http" or subsystem == "stream" then
+ local plugins = resources.plugins.get_plugins_list(subsystem)
+ core.response.exit(200, plugins)
+ end
+ core.response.exit(400,"invalid subsystem passed")
end
-- Handle unsupported request methods for the virtual "reload" plugin
diff --git a/apisix/admin/plugins.lua b/apisix/admin/plugins.lua
index 26dcab719..201f8f3c9 100644
--- a/apisix/admin/plugins.lua
+++ b/apisix/admin/plugins.lua
@@ -18,11 +18,12 @@ local require = require
local core = require("apisix.core")
local check_schema = require("apisix.plugin").check_schema
local ipairs = ipairs
-local pcall = pcall
local table_sort = table.sort
local table_insert = table.insert
local get_uri_args = ngx.req.get_uri_args
local plugin_get_all = require("apisix.plugin").get_all
+local plugin_get_http = require("apisix.plugin").get
+local plugin_get_stream = require("apisix.plugin").get_stream
local encrypt_conf = require("apisix.plugin").encrypt_conf
local pairs = pairs
@@ -42,7 +43,15 @@ end
function _M.get(name)
local arg = get_uri_args()
- if arg and arg["all"] == "true" then
+ -- If subsystem is passed inside args then it should be oneOf: http /
stream.
+ local subsystem = arg["subsystem"] or "http"
+ if subsystem ~= "http" and subsystem ~= "stream" then
+ return 400, {error_msg = "unsupported subsystem: "..subsystem}
+ end
+
+ -- arg all to be deprecated
+ if (arg and arg["all"] == "true") then
+ core.log.warn("query parameter \"all\" will be deprecated soon.")
local http_plugins, stream_plugins = plugin_get_all({
version = true,
priority = true,
@@ -60,16 +69,18 @@ function _M.get(name)
return 200, http_plugins
end
- if not name then
- return 400, {error_msg = "not found plugin name"}
- end
+ local plugin
- local plugin_name = "apisix.plugins." .. name
+ if subsystem == "http" then
+ plugin = plugin_get_http(name)
+ else
+ plugin = plugin_get_stream(name)
+ end
- local ok, plugin = pcall(require, plugin_name)
- if not ok then
- core.log.warn("failed to load plugin [", name, "] err: ", plugin)
- return 400, {error_msg = "failed to load plugin " .. name}
+ if not plugin then
+ local err = "plugin not found in subsystem " .. subsystem
+ core.log.warn(err)
+ return 404, {error_msg = err}
end
local json_schema = plugin.schema
@@ -85,16 +96,34 @@ function _M.get(name)
end
-function _M.get_plugins_list()
- local plugins = core.config.local_conf().plugins
+function _M.get_plugins_list(subsystem)
+ local http_plugins
+ local stream_plugins
+ if subsystem == "http" then
+ http_plugins = core.config.local_conf().plugins
+ else
+ stream_plugins = core.config.local_conf().stream_plugins
+ end
+
local priorities = {}
local success = {}
- for i, name in ipairs(plugins) do
- local plugin_name = "apisix.plugins." .. name
- local ok, plugin = pcall(require, plugin_name)
- if ok and plugin.priority then
- priorities[name] = plugin.priority
- table_insert(success, name)
+ if http_plugins then
+ for i, name in ipairs(http_plugins) do
+ local plugin = plugin_get_http(name)
+ if plugin and plugin.priority then
+ priorities[name] = plugin.priority
+ table_insert(success, name)
+ end
+ end
+ end
+
+ if stream_plugins then
+ for i, name in ipairs(stream_plugins) do
+ local plugin = plugin_get_stream(name)
+ if plugin and plugin.priority then
+ priorities[name] = plugin.priority
+ table_insert(success, name)
+ end
end
end
diff --git a/apisix/plugin.lua b/apisix/plugin.lua
index 6468d8df7..bde2b89a5 100644
--- a/apisix/plugin.lua
+++ b/apisix/plugin.lua
@@ -775,6 +775,11 @@ function _M.get(name)
end
+function _M.get_stream(name)
+ return stream_local_plugins_hash and stream_local_plugins_hash[name]
+end
+
+
function _M.get_all(attrs)
local http_plugins = {}
local stream_plugins = {}
diff --git a/docs/en/latest/admin-api.md b/docs/en/latest/admin-api.md
index 5096684d0..88add7798 100644
--- a/docs/en/latest/admin-api.md
+++ b/docs/en/latest/admin-api.md
@@ -1345,6 +1345,14 @@ Plugin resource request address:
/apisix/admin/plugins/{plugin_name}
The Plugin ({plugin_name}) of the data structure.
+### Request Arguments
+
+| Name | Description | Default |
+| --------- | ----------------------------- | ------- |
+| subsystem | The subsystem of the Plugins. | http |
+
+The plugin can be filtered on subsystem so that the ({plugin_name}) is
searched in the subsystem passed through query params.
+
### Example API usage:
```shell
@@ -1357,7 +1365,7 @@ curl "http://127.0.0.1:9180/apisix/admin/plugins/list" \
```
```shell
-curl "http://127.0.0.1:9180/apisix/admin/plugins/key-auth" -H 'X-API-KEY:
edd1c9f034335f136f87ad84b625c8f1'
+curl "http://127.0.0.1:9180/apisix/admin/plugins/key-auth?subsystem=http" -H
'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1'
```
```json
@@ -1366,26 +1374,10 @@ curl
"http://127.0.0.1:9180/apisix/admin/plugins/key-auth" -H 'X-API-KEY: ed
:::tip
-You can use the `/apisix/admin/plugins?all=true` API to get all properties of
all plugins.
-
-Each Plugin has the attributes `name`, `priority`, `type`, `schema`,
`consumer_schema` and `version`.
-
-Defaults to only L7 Plugins. If you need to get attributes of L4 / Stream
Plugins, use `/apisix/admin/plugins?all=true&subsystem=stream`.
+You can use the `/apisix/admin/plugins?all=true` API to get all properties of
all plugins. This API will be deprecated soon.
:::
-### Request Methods
-
-| Method | Request URI | Request Body | Description
|
-| ------ | ------------------------------ | ------------ |
---------------------------------------- |
-| GET | /apisix/admin/plugins?all=true | NULL | Fetches all
attributes from all Plugins. |
-
-### Request Arguments
-
-| Name | Description | Default |
-| --------- | ----------------------------- | ------- |
-| subsystem | The subsystem of the Plugins. | http |
-
## Stream Route
Route used in the [Stream Proxy](./stream-proxy.md).
diff --git a/docs/zh/latest/admin-api.md b/docs/zh/latest/admin-api.md
index f2f4d1f81..18efb19c5 100644
--- a/docs/zh/latest/admin-api.md
+++ b/docs/zh/latest/admin-api.md
@@ -1346,6 +1346,14 @@ Content-Type: text/plain
Plugin 资源请求地址:/apisix/admin/plugins/{plugin_name}
+### 请求参数
+
+| 名称 | 描述 | 默认 |
+| --------- | -------------------------------------- | -------- |
+| subsystem | 插件子系统。 | http |
+
+可以在子系统上过滤插件,以便在通过查询参数传递的子系统中搜索 ({plugin_name})
+
### 请求方法 {#plugin-request-methods}
| 名称 | 请求 URI | 请求 body | 描述 |
@@ -1373,7 +1381,7 @@ Plugin 资源请求地址:/apisix/admin/plugins/{plugin_name}
- 获取指定插件的属性
```shell
- curl "http://127.0.0.1:9180/apisix/admin/plugins/key-auth" \
+ curl "http://127.0.0.1:9180/apisix/admin/plugins/key-auth?subsystem=http" \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1'
```
@@ -1385,7 +1393,7 @@ Plugin 资源请求地址:/apisix/admin/plugins/{plugin_name}
你可以使用 `/apisix/admin/plugins?all=true` 接口获取所有插件的所有属性,每个插件包括
`name`,`priority`,`type`,`schema`,`consumer_schema` 和 `version`。
-默认情况下,该接口只返回 L7 插件。如果你需要获取 L4 / Stream 插件,需要使用
`/apisix/admin/plugins?all=true&subsystem=stream`。
+您可以使用“/apisix/admin/plugins?all=true”获取所有插件的所有属性。这个 API 将很快被弃用
:::
diff --git a/t/admin/plugins.t b/t/admin/plugins.t
index ae7617dfd..53bb82df8 100644
--- a/t/admin/plugins.t
+++ b/t/admin/plugins.t
@@ -138,12 +138,12 @@ ext-plugin-post-resp
-=== TEST 2: wrong path
+=== TEST 2: invalid plugin
--- request
-GET /apisix/admin/plugins
---- error_code: 400
+GET /apisix/admin/plugins/asdf
+--- error_code: 404
--- response_body
-{"error_msg":"not found plugin name"}
+{"error_msg":"plugin not found in subsystem http"}
@@ -412,3 +412,54 @@ plugins:
}
--- response_body
{"batch-requests":"global","error-log-logger":"global","node-status":"global","server-info":"global"}
+
+
+
+=== TEST 13: check with wrong plugin subsystem
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+
+ local _, message, _ = t('/apisix/admin/plugins?subsystem=asdf',
+ ngx.HTTP_GET
+ )
+ ngx.say(message)
+ }
+ }
+--- response_body eval
+qr/\{"error_msg":"unsupported subsystem: asdf"\}/
+
+
+
+=== TEST 14: check with right plugin in wrong subsystem
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+
+ local _, message, _ =
t('/apisix/admin/plugins/http-logger?subsystem=stream',
+ ngx.HTTP_GET
+ )
+ ngx.say(message)
+ }
+ }
+--- response_body eval
+qr/\{"error_msg":"plugin not found in subsystem stream"\}/
+
+
+
+=== TEST 15: check with right plugin in right subsystem
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+
+ local _, _ , message =
t('/apisix/admin/plugins/http-logger?subsystem=http',
+ ngx.HTTP_GET
+ )
+ ngx.say(message)
+ }
+ }
+--- response_body eval
+qr/this is a mark for our injected plugin schema/
diff --git a/t/admin/schema.t b/t/admin/schema.t
index 35ac20187..7853addd3 100644
--- a/t/admin/schema.t
+++ b/t/admin/schema.t
@@ -112,7 +112,7 @@ qr/"required":\["count","time_window"\]/
=== TEST 8: get not exist plugin
--- request
GET /apisix/admin/schema/plugins/no-exist
---- error_code: 400
+--- error_code: 404