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 2020/12/29 03:01:19 UTC

[apisix] branch master updated: feat: provide the ability to view plug-in types (#2886)

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 a54aad1  feat: provide the ability to view plug-in types (#2886)
a54aad1 is described below

commit a54aad1ad3a46ae990fd9fbeddbd1ebb57385597
Author: tzssangglass <tz...@gmail.com>
AuthorDate: Tue Dec 29 11:01:12 2020 +0800

    feat: provide the ability to view plug-in types (#2886)
    
    Fix #2308
---
 apisix/admin/plugins.lua |  15 ++++++-
 doc/admin-api.md         |  12 ++++++
 doc/zh-cn/admin-api.md   |  12 ++++++
 t/admin/plugins.t        | 107 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 145 insertions(+), 1 deletion(-)

diff --git a/apisix/admin/plugins.lua b/apisix/admin/plugins.lua
index 78211f0..fb803eb 100644
--- a/apisix/admin/plugins.lua
+++ b/apisix/admin/plugins.lua
@@ -23,6 +23,7 @@ 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 _M = {}
 
@@ -38,6 +39,19 @@ end
 
 
 function _M.get(name)
+    local arg = get_uri_args()
+    if arg and arg["all"] == "true" then
+        local all_attributes = plugin_get_all({
+            version = true,
+            priority = true,
+            schema = true,
+            metadata_schema = true,
+            consumer_schema = true,
+            type = true,
+        })
+        return 200, all_attributes
+    end
+
     if not name then
         return 400, {error_msg = "not found plugin name"}
     end
@@ -50,7 +64,6 @@ function _M.get(name)
         return 400, {error_msg = "failed to load plugin " .. name}
     end
 
-    local arg = get_uri_args()
     local json_schema = plugin.schema
     if arg and arg["schema_type"] == "consumer" then
         json_schema = plugin.consumer_schema
diff --git a/doc/admin-api.md b/doc/admin-api.md
index 0726e85..bb9e927 100644
--- a/doc/admin-api.md
+++ b/doc/admin-api.md
@@ -710,3 +710,15 @@ Content-Type: text/plain
 ```
 
 [Back to TOC](#Table-of-Contents)
+
+*API*:/apisix/admin/plugins/?all=true
+
+*Description*: all the attributes of all plugins, each plugin includes `name`, `priority`, `type`, `schema`, `consumer_schema` and `version`.
+
+> Request Methods:
+
+|Method      |Request URI|Request Body|Description        |
+|---------|-------------------------|--|------|
+|GET      |/apisix/admin/plugins/?all=true|NULL|Fetch resource|
+
+[Back to TOC](#Table-of-Contents)
diff --git a/doc/zh-cn/admin-api.md b/doc/zh-cn/admin-api.md
index 18397a5..03b1786 100644
--- a/doc/zh-cn/admin-api.md
+++ b/doc/zh-cn/admin-api.md
@@ -718,3 +718,15 @@ Content-Type: text/plain
 ```
 
 [Back to TOC](#目录)
+
+*地址*:/apisix/admin/plugins/?all=true
+
+*说明*: 所有插件的所有属性,每个插件包括 `name`, `priority`, `type`, `schema`, `consumer_schema` and `version`。
+
+> 请求方法:
+
+|Method   |请求 URI|请求 body|说明        |
+|---------|-------------------------|--|------|
+|GET      |/apisix/admin/plugins/?all=true|无|获取资源|
+
+[Back to TOC](#目录)
diff --git a/t/admin/plugins.t b/t/admin/plugins.t
index db19467..79ff4ee 100644
--- a/t/admin/plugins.t
+++ b/t/admin/plugins.t
@@ -162,3 +162,110 @@ plugins:
     }
 --- no_error_log
 [error]
+
+
+
+=== TEST 8: confirm the name, priority, schema, type and version of plugin
+--- 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/plugins/?all=true',
+                ngx.HTTP_GET
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(message)
+                return
+            end
+
+            res = json.decode(res)
+            for k, v in pairs(res) do
+                if k == "example-plugin" then
+                    ngx.say(json.encode(v))
+                end
+            end
+        }
+    }
+--- response_body eval
+qr/\{"metadata_schema":\{"additionalProperties":false,"properties":\{"ikey":\{"minimum":0,"type":"number"\},"skey":\{"type":"string"\}\},"required":\["ikey","skey"\],"type":"object"\},"priority":0,"schema":\{"properties":\{"i":\{"minimum":0,"type":"number"\},"ip":\{"type":"string"\},"port":\{"type":"integer"\},"s":\{"type":"string"\},"t":\{"minItems":1,"type":"array"\}\},"required":\["i"\],"type":"object"\},"version":0.1\}/
+--- no_error_log
+[error]
+
+
+
+=== TEST 9: confirm the plugin of auth type
+--- 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/plugins/?all=true',
+                ngx.HTTP_GET
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(message)
+                return
+            end
+
+            res = json.decode(res)
+            local auth_plugins = {}
+            for k, v in pairs(res) do
+                if v.type == "auth" then
+                    local plugin = {}
+                    plugin.name = k
+                    plugin.priority = v.priority
+                    table.insert(auth_plugins, plugin)
+                end
+            end
+
+            table.sort(auth_plugins, function(l, r)
+                return l.priority > r.priority
+            end)
+            ngx.say(json.encode(auth_plugins))
+        }
+    }
+--- response_body eval
+qr/\[\{"name":"wolf-rbac","priority":2555\},\{"name":"hmac-auth","priority":2530\},\{"name":"basic-auth","priority":2520\},\{"name":"jwt-auth","priority":2510\},\{"name":"key-auth","priority":2500\}\]/
+--- no_error_log
+[error]
+
+
+
+=== TEST 10: confirm the consumer_schema of plugin
+--- 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/plugins/?all=true',
+                ngx.HTTP_GET
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(message)
+                return
+            end
+
+            res = json.decode(res)
+            local consumer_schema
+            for k, v in pairs(res) do
+                if k == "basic-auth" then
+                    consumer_schema = v.consumer_schema
+                end
+            end
+            ngx.say(json.encode(consumer_schema))
+        }
+    }
+--- response_body eval
+qr/\{"additionalProperties":false,"properties":\{"password":\{"type":"string"\},"username":\{"type":"string"\}\},"required":\["username","password"\],"title":"work with consumer object","type":"object"\}/
+--- no_error_log
+[error]