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 7a6dde704 refactor(admin): refactor admin consumers/plugin_metadata
(#8820)
7a6dde704 is described below
commit 7a6dde704e3815cf311733e982821f62a2b04c00
Author: dongjunduo <[email protected]>
AuthorDate: Tue Feb 28 09:23:10 2023 +0800
refactor(admin): refactor admin consumers/plugin_metadata (#8820)
---
apisix/admin/consumers.lua | 88 +++++-----------------------------------
apisix/admin/init.lua | 2 +
apisix/admin/plugin_metadata.lua | 76 ++++------------------------------
apisix/admin/resource.lua | 47 +++++++++++++--------
4 files changed, 51 insertions(+), 162 deletions(-)
diff --git a/apisix/admin/consumers.lua b/apisix/admin/consumers.lua
index 7ab0ec1e2..84485231f 100644
--- a/apisix/admin/consumers.lua
+++ b/apisix/admin/consumers.lua
@@ -16,25 +16,13 @@
--
local core = require("apisix.core")
local plugins = require("apisix.admin.plugins")
-local utils = require("apisix.admin.utils")
+local resource = require("apisix.admin.resource")
local plugin = require("apisix.plugin")
local pairs = pairs
-local _M = {
- version = 0.1,
- need_v3_filter = true,
-}
-
-local function check_conf(username, conf)
- -- core.log.error(core.json.encode(conf))
- if not conf then
- return nil, {error_msg = "missing configurations"}
- end
-
- core.log.info("schema: ", core.json.delay_encode(core.schema.consumer))
- core.log.info("conf : ", core.json.delay_encode(conf))
- local ok, err = core.schema.check(core.schema.consumer, conf)
+local function check_conf(username, conf, need_username, schema)
+ local ok, err = core.schema.check(schema, conf)
if not ok then
return nil, {error_msg = "invalid configuration: " .. err}
end
@@ -82,66 +70,10 @@ local function check_conf(username, conf)
end
-function _M.put(username, conf)
- local consumer_name, err = check_conf(username, conf)
- if not consumer_name then
- return 400, err
- end
-
- local key = "/consumers/" .. consumer_name
- core.log.info("key: ", key)
-
- local ok, err = utils.inject_conf_with_prev_conf("consumer", key, conf)
- if not ok then
- return 503, {error_msg = err}
- end
-
- local res, err = core.etcd.set(key, conf)
- if not res then
- core.log.error("failed to put consumer[", key, "]: ", err)
- return 503, {error_msg = err}
- end
-
- return res.status, res.body
-end
-
-
-function _M.get(consumer_name)
- local key = "/consumers"
- if consumer_name then
- key = key .. "/" .. consumer_name
- end
-
- local res, err = core.etcd.get(key, not consumer_name)
- if not res then
- core.log.error("failed to get consumer[", key, "]: ", err)
- return 503, {error_msg = err}
- end
-
- utils.fix_count(res.body, consumer_name)
- return res.status, res.body
-end
-
-
-function _M.post(consumer_name, conf)
- return 405, {error_msg = "not supported `POST` method for consumer"}
-end
-
-
-function _M.delete(consumer_name)
- if not consumer_name then
- return 400, {error_msg = "missing consumer name"}
- end
-
- local key = "/consumers/" .. consumer_name
- local res, err = core.etcd.delete(key)
- if not res then
- core.log.error("failed to delete consumer[", key, "]: ", err)
- return 503, {error_msg = err}
- end
-
- return res.status, res.body
-end
-
-
-return _M
+return resource.new({
+ name = "consumers",
+ kind = "consumer",
+ schema = core.schema.consumer,
+ checker = check_conf,
+ unsupported_methods = {"post", "patch"}
+})
diff --git a/apisix/admin/init.lua b/apisix/admin/init.lua
index 1c139ef7f..5afbff096 100644
--- a/apisix/admin/init.lua
+++ b/apisix/admin/init.lua
@@ -213,6 +213,8 @@ local function run()
"services",
"consumer_groups",
"plugin_configs",
+ "consumers",
+ "plugin_metadata",
}
if core.table.array_find(refactored_resources, seg_res) then
code, data = resource[method](resource, seg_id, req_body,
seg_sub_path, uri_args)
diff --git a/apisix/admin/plugin_metadata.lua b/apisix/admin/plugin_metadata.lua
index 065b60c47..1387ca0ee 100644
--- a/apisix/admin/plugin_metadata.lua
+++ b/apisix/admin/plugin_metadata.lua
@@ -17,13 +17,10 @@
local pcall = pcall
local require = require
local core = require("apisix.core")
-local utils = require("apisix.admin.utils")
+local resource = require("apisix.admin.resource")
local encrypt_conf = require("apisix.plugin").encrypt_conf
local injected_mark = "injected metadata_schema"
-local _M = {
- need_v3_filter = true,
-}
local function validate_plugin(name)
@@ -48,10 +45,6 @@ local function check_conf(plugin_name, conf)
return nil, {error_msg = "invalid plugin name"}
end
- if not conf then
- return nil, {error_msg = "missing configurations"}
- end
-
if not plugin_object.metadata_schema then
plugin_object.metadata_schema = {
type = "object",
@@ -61,9 +54,6 @@ local function check_conf(plugin_name, conf)
end
local schema = plugin_object.metadata_schema
- core.log.info("schema: ", core.json.delay_encode(schema))
- core.log.info("conf: ", core.json.delay_encode(conf))
-
local ok, err
if schema['$comment'] == injected_mark
-- check_schema is not required. If missing, fallback to check schema
directly
@@ -84,60 +74,10 @@ local function check_conf(plugin_name, conf)
end
-function _M.put(plugin_name, conf)
- local plugin_name, err = check_conf(plugin_name, conf)
- if not plugin_name then
- return 400, err
- end
-
- local key = "/plugin_metadata/" .. plugin_name
- core.log.info("key: ", key)
- local res, err = core.etcd.set(key, conf)
- if not res then
- core.log.error("failed to put plugin metadata[", key, "]: ", err)
- return 503, {error_msg = err}
- end
-
- return res.status, res.body
-end
-
-
-function _M.get(key)
- local path = "/plugin_metadata"
- if key then
- path = path .. "/" .. key
- end
-
- local res, err = core.etcd.get(path, not key)
- if not res then
- core.log.error("failed to get metadata[", key, "]: ", err)
- return 503, {error_msg = err}
- end
-
- utils.fix_count(res.body, key)
- return res.status, res.body
-end
-
-
-function _M.post(key, conf)
- return 405, {error_msg = "not supported `POST` method for metadata"}
-end
-
-
-function _M.delete(key)
- if not key then
- return 400, {error_msg = "missing metadata key"}
- end
-
- local key = "/plugin_metadata/" .. key
- local res, err = core.etcd.delete(key)
- if not res then
- core.log.error("failed to delete metadata[", key, "]: ", err)
- return 503, {error_msg = err}
- end
-
- return res.status, res.body
-end
-
-
-return _M
+return resource.new({
+ name = "plugin_metadata",
+ kind = "plugin_metadata",
+ schema = core.schema.plugin_metadata,
+ checker = check_conf,
+ unsupported_methods = {"post", "patch"}
+})
diff --git a/apisix/admin/resource.lua b/apisix/admin/resource.lua
index 6746c055c..59689a2af 100644
--- a/apisix/admin/resource.lua
+++ b/apisix/admin/resource.lua
@@ -31,6 +31,12 @@ local mt = {
}
+local no_id_res = {
+ consumers = true,
+ plugin_metadata = true
+}
+
+
function _M:check_conf(id, conf, need_id)
-- check if missing configurations
if not conf then
@@ -38,20 +44,22 @@ function _M:check_conf(id, conf, need_id)
end
-- check id if need id
- id = id or conf.id
- if need_id and not id then
- return nil, {error_msg = "missing ".. self.kind .. " id"}
- end
+ if not no_id_res[self.name] then
+ id = id or conf.id
+ if need_id and not id then
+ return nil, {error_msg = "missing ".. self.kind .. " id"}
+ end
- if not need_id and id then
- return nil, {error_msg = "wrong ".. self.kind .. " id, do not need it"}
- end
+ if not need_id and id then
+ return nil, {error_msg = "wrong ".. self.kind .. " id, do not need
it"}
+ end
- if need_id and conf.id and tostring(conf.id) ~= tostring(id) then
- return nil, {error_msg = "wrong ".. self.kind .. " id"}
- end
+ if need_id and conf.id and tostring(conf.id) ~= tostring(id) then
+ return nil, {error_msg = "wrong ".. self.kind .. " id"}
+ end
- conf.id = id
+ conf.id = id
+ end
core.log.info("schema: ", core.json.delay_encode(self.schema))
core.log.info("conf : ", core.json.delay_encode(conf))
@@ -62,7 +70,11 @@ function _M:check_conf(id, conf, need_id)
if not ok then
return ok, err
else
- return need_id and id or true
+ if no_id_res[self.name] then
+ return ok
+ else
+ return need_id and id or true
+ end
end
end
@@ -121,16 +133,19 @@ function _M:put(id, conf, sub_path, args)
return 405, {error_msg = "not supported `PUT` method for " ..
self.kind}
end
- local id, err = self:check_conf(id, conf, true)
+ local need_id = not no_id_res[self.name]
+ local id, err = self:check_conf(id, conf, need_id)
if not id then
return 400, err
end
local key = "/" .. self.name .. "/" .. id
- local ok, err = utils.inject_conf_with_prev_conf(self.kind, key, conf)
- if not ok then
- return 503, {error_msg = err}
+ if self.name ~= "plugin_metadata" then
+ local ok, err = utils.inject_conf_with_prev_conf(self.kind, key, conf)
+ if not ok then
+ return 503, {error_msg = err}
+ end
end
local ttl = nil