Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(admin): stream_routes/upstreams/protos/services/global_rules/consumer_groups/plugin_configs #8661

Merged
merged 7 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 13 additions & 142 deletions apisix/admin/consumer_group.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,15 @@
--
local core = require("apisix.core")
local consumers = require("apisix.consumer").consumers
local utils = require("apisix.admin.utils")
local resource = require("apisix.admin.resource")
local schema_plugin = require("apisix.admin.plugins").check_schema
local type = type
local tostring = tostring
local ipairs = ipairs


local _M = {
need_v3_filter = true,
}


local function check_conf(id, conf, need_id)
if not conf then
return nil, {error_msg = "missing configurations"}
end

id = id or conf.id
if need_id and not id then
return nil, {error_msg = "missing id"}
end

if not need_id and id then
return nil, {error_msg = "wrong id, do not need it"}
end

if need_id and conf.id and tostring(conf.id) ~= tostring(id) then
return nil, {error_msg = "wrong id"}
end

conf.id = id

core.log.info("conf: ", core.json.delay_encode(conf))
local ok, err = core.schema.check(core.schema.consumer_group, conf)
local function check_conf(id, conf, need_id, schema)
local ok, err = core.schema.check(schema, conf)
if not ok then
return nil, {error_msg = "invalid configuration: " .. err}
end
Expand All @@ -63,50 +38,7 @@ local function check_conf(id, conf, need_id)
end


function _M.put(id, conf)
local ok, err = check_conf(id, conf, true)
if not ok then
return 400, err
end

local key = "/consumer_groups/" .. id

local ok, err = utils.inject_conf_with_prev_conf("consumer_group", 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 group[", key, "]: ", err)
return 503, {error_msg = err}
end

return res.status, res.body
end


function _M.get(id)
local key = "/consumer_groups"
if id then
key = key .. "/" .. id
end
local res, err = core.etcd.get(key, not id)
if not res then
core.log.error("failed to get consumer group[", key, "]: ", err)
return 503, {error_msg = err}
end

utils.fix_count(res.body, id)
return res.status, res.body
end


function _M.delete(id)
if not id then
return 400, {error_msg = "missing consumer group id"}
end

local function delete_checker(id)
local consumers, consumers_ver = consumers()
if consumers_ver and consumers then
for _, consumer in ipairs(consumers) do
Expand All @@ -120,76 +52,15 @@ function _M.delete(id)
end
end

local key = "/consumer_groups/" .. id
local res, err = core.etcd.delete(key)
if not res then
core.log.error("failed to delete consumer group[", key, "]: ", err)
return 503, {error_msg = err}
end


return res.status, res.body
end


function _M.patch(id, conf, sub_path)
if not id then
return 400, {error_msg = "missing consumer group id"}
end

if not conf then
return 400, {error_msg = "missing new configuration"}
end

if not sub_path or sub_path == "" then
if type(conf) ~= "table" then
return 400, {error_msg = "invalid configuration"}
end
end

local key = "/consumer_groups/" .. id
local res_old, err = core.etcd.get(key)
if not res_old then
core.log.error("failed to get consumer group [", key, "]: ", err)
return 503, {error_msg = err}
end

if res_old.status ~= 200 then
return res_old.status, res_old.body
end
core.log.info("key: ", key, " old value: ",
core.json.delay_encode(res_old, true))

local node_value = res_old.body.node.value
local modified_index = res_old.body.node.modifiedIndex

if sub_path and sub_path ~= "" then
local code, err, node_val = core.table.patch(node_value, sub_path, conf)
node_value = node_val
if code then
return code, err
end
utils.inject_timestamp(node_value, nil, true)
else
node_value = core.table.merge(node_value, conf)
utils.inject_timestamp(node_value, nil, conf)
end

core.log.info("new conf: ", core.json.delay_encode(node_value, true))

local ok, err = check_conf(id, node_value, true)
if not ok then
return 400, err
end

local res, err = core.etcd.atomic_set(key, node_value, nil, modified_index)
if not res then
core.log.error("failed to set new consumer group[", key, "]: ", err)
return 503, {error_msg = err}
end

return res.status, res.body
return nil, nil
end


return _M
return resource.new({
name = "consumer_groups",
kind = "consumer group",
schema = core.schema.consumer_group,
checker = check_conf,
unsupported_methods = {"post"},
delete_checker = delete_checker
})
155 changes: 10 additions & 145 deletions apisix/admin/global_rules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,12 @@
-- limitations under the License.
--
local core = require("apisix.core")
local utils = require("apisix.admin.utils")
local resource = require("apisix.admin.resource")
local schema_plugin = require("apisix.admin.plugins").check_schema
local type = type
local tostring = tostring


local _M = {
version = 0.1,
need_v3_filter = true,
}


local function check_conf(id, conf, need_id)
if not conf then
return nil, {error_msg = "missing configurations"}
end

id = id or conf.id
if need_id and not id then
return nil, {error_msg = "missing route id"}
end

if not need_id and id then
return nil, {error_msg = "wrong route id, do not need it"}
end

if need_id and conf.id and tostring(conf.id) ~= tostring(id) then
return nil, {error_msg = "wrong route id"}
end

conf.id = id

core.log.info("schema: ", core.json.delay_encode(core.schema.global_rule))
core.log.info("conf : ", core.json.delay_encode(conf))
local ok, err = core.schema.check(core.schema.global_rule, conf)
local function check_conf(id, conf, need_id, schema)
local ok, err = core.schema.check(schema, conf)
if not ok then
return nil, {error_msg = "invalid configuration: " .. err}
end
Expand All @@ -63,116 +34,10 @@ local function check_conf(id, conf, need_id)
end


function _M.put(id, conf)
local ok, err = check_conf(id, conf, true)
if not ok then
return 400, err
end

local key = "/global_rules/" .. id

local ok, err = utils.inject_conf_with_prev_conf("global_rule", 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 global rule[", key, "]: ", err)
return 503, {error_msg = err}
end

return res.status, res.body
end


function _M.get(id)
local key = "/global_rules"
if id then
key = key .. "/" .. id
end
local res, err = core.etcd.get(key, not id)
if not res then
core.log.error("failed to get global rule[", key, "]: ", err)
return 503, {error_msg = err}
end

utils.fix_count(res.body, id)
return res.status, res.body
end


function _M.delete(id)
local key = "/global_rules/" .. id
-- core.log.info("key: ", key)
local res, err = core.etcd.delete(key)
if not res then
core.log.error("failed to delete global rule[", key, "]: ", err)
return 503, {error_msg = err}
end

return res.status, res.body
end


function _M.patch(id, conf, sub_path)
if not id then
return 400, {error_msg = "missing global rule id"}
end

if not conf then
return 400, {error_msg = "missing new configuration"}
end

if not sub_path or sub_path == "" then
if type(conf) ~= "table" then
return 400, {error_msg = "invalid configuration"}
end
end

local key = "/global_rules/" .. id
local res_old, err = core.etcd.get(key)
if not res_old then
core.log.error("failed to get global rule [", key, "]: ", err)
return 503, {error_msg = err}
end

if res_old.status ~= 200 then
return res_old.status, res_old.body
end
core.log.info("key: ", key, " old value: ",
core.json.delay_encode(res_old, true))

local node_value = res_old.body.node.value
local modified_index = res_old.body.node.modifiedIndex

if sub_path and sub_path ~= "" then
local code, err, node_val = core.table.patch(node_value, sub_path, conf)
node_value = node_val
if code then
return code, err
end
utils.inject_timestamp(node_value, nil, true)
else
node_value = core.table.merge(node_value, conf)
utils.inject_timestamp(node_value, nil, conf)
end

core.log.info("new conf: ", core.json.delay_encode(node_value, true))

local ok, err = check_conf(id, node_value, true)
if not ok then
return 400, err
end

local res, err = core.etcd.atomic_set(key, node_value, nil, modified_index)
if not res then
core.log.error("failed to set new global rule[", key, "]: ", err)
return 503, {error_msg = err}
end

return res.status, res.body
end


return _M
return resource.new({
name = "global_rules",
kind = "global rule",
schema = core.schema.global_rule,
checker = check_conf,
unsupported_methods = {"post"}
})
12 changes: 11 additions & 1 deletion apisix/admin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,17 @@ local function run()
end

local code, data
if seg_res == "routes" then
local refactored_resources = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

directly named as resources?

Copy link
Contributor Author

@An-DJ An-DJ Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There has been a global variable named resources already. 😭 (https://github.com/apache/apisix/blob/master/apisix/admin/init.lua#L46)

Maybe we can rename that variable after all resources are refactored.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

"routes",
"stream_routes",
"upstreams",
"protos",
"global_rules",
"services",
"consumer_groups",
"plugin_configs",
}
if core.table.array_find(refactored_resources, seg_res) then
code, data = resource[method](resource, seg_id, req_body, seg_sub_path, uri_args)
else
code, data = resource[method](seg_id, req_body, seg_sub_path, uri_args)
Expand Down
Loading