Skip to content

Commit

Permalink
refactor(admin): stream_routes/upstreams
Browse files Browse the repository at this point in the history
  • Loading branch information
An-DJ committed Jan 12, 2023
1 parent c9ed5d7 commit 71e9918
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 275 deletions.
7 changes: 6 additions & 1 deletion apisix/admin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,12 @@ local function run()
end

local code, data
if seg_res == "routes" then
local refactored_resources = {
"routes",
"stream_routes",
"upstreams",
}
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
49 changes: 46 additions & 3 deletions apisix/admin/resource.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ end


function _M:get(id)
if self.unsupported_methods and core.table.array_find(self.unsupported_methods, "get") then
return 405, {error_msg = "not supported `GET` method for " .. self.kind}
end

local key = "/" .. self.name
if id then
key = key .. "/" .. id
Expand All @@ -79,14 +83,24 @@ end


function _M:post(id, conf, sub_path, args)
if self.unsupported_methods and core.table.array_find(self.unsupported_methods, "post") then
return 405, {error_msg = "not supported `POST` method for " .. self.kind}
end

local id, err = self:check_conf(id, conf, false)
if not id then
return 400, err
end

local key = "/" .. self.name
utils.inject_timestamp(conf)
local res, err = core.etcd.push(key, conf, args.ttl)

local ttl = nil
if args then
ttl = args.ttl
end

local res, err = core.etcd.push(key, conf, ttl)
if not res then
core.log.error("failed to post ", self.kind, "[", key, "] to etcd: ", err)
return 503, {error_msg = err}
Expand All @@ -97,6 +111,10 @@ end


function _M:put(id, conf, sub_path, args)
if self.unsupported_methods and core.table.array_find(self.unsupported_methods, "put") then
return 405, {error_msg = "not supported `PUT` method for " .. self.kind}
end

local id, err = self:check_conf(id, conf, true)
if not id then
return 400, err
Expand All @@ -109,7 +127,12 @@ function _M:put(id, conf, sub_path, args)
return 503, {error_msg = err}
end

local res, err = core.etcd.set(key, conf, args.ttl)
local ttl = nil
if args then
ttl = args.ttl
end

local res, err = core.etcd.set(key, conf, ttl)
if not res then
core.log.error("failed to put ", self.kind, "[", key, "] to etcd: ", err)
return 503, {error_msg = err}
Expand All @@ -120,10 +143,21 @@ end


function _M:delete(id)
if self.unsupported_methods and core.table.array_find(self.unsupported_methods, "delete") then
return 405, {error_msg = "not supported `DELETE` method for " .. self.kind}
end

if not id then
return 400, {error_msg = "missing " .. self.kind .. " id"}
end

if self.delete_checker then
local code, err = self.delete_checker(id)
if err then
return code, err
end
end

local key = "/" .. self.name .. "/" .. id
local res, err = core.etcd.delete(key)
if not res then
Expand All @@ -136,6 +170,10 @@ end


function _M:patch(id, conf, sub_path, args)
if self.unsupported_methods and core.table.array_find(self.unsupported_methods, "patch") then
return 405, {error_msg = "not supported `PATCH` method for " .. self.kind}
end

if not id then
return 400, {error_msg = "missing " .. self.kind .. " id"}
end
Expand Down Expand Up @@ -188,7 +226,12 @@ function _M:patch(id, conf, sub_path, args)
return 400, err
end

local res, err = core.etcd.atomic_set(key, node_value, args.ttl, modified_index)
local ttl = nil
if args then
ttl = args.ttl
end

local res, err = core.etcd.atomic_set(key, node_value, ttl, modified_index)
if not res then
core.log.error("failed to set new ", self.kind, "[", key, "] to etcd: ", err)
return 503, {error_msg = err}
Expand Down
3 changes: 2 additions & 1 deletion apisix/admin/routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,6 @@ return resource.new({
name = "routes",
kind = "route",
schema = core.schema.route,
checker = check_conf
checker = check_conf,
unsupported_methods = {}
})
117 changes: 10 additions & 107 deletions apisix/admin/stream_routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,12 @@
-- limitations under the License.
--
local core = require("apisix.core")
local utils = require("apisix.admin.utils")
local resource = require("apisix.admin.resource")
local stream_route_checker = require("apisix.stream.router.ip_port").stream_route_checker
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 stream route id"}
end

if not need_id and id then
return nil, {error_msg = "wrong stream 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 stream route id"}
end

conf.id = id

core.log.info("schema: ", core.json.delay_encode(core.schema.stream_route))
core.log.info("conf : ", core.json.delay_encode(conf))
local ok, err = core.schema.check(core.schema.stream_route, 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 Down Expand Up @@ -79,79 +51,10 @@ local function check_conf(id, conf, need_id)
end


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

local key = "/stream_routes/" .. id

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

return res.status, res.body
end


function _M.get(id)
local key = "/stream_routes"
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 stream route[", key, "]: ", err)
return 503, {error_msg = err}
end

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


function _M.post(id, conf)
local id, err = check_conf(id, conf, false)
if not id then
return 400, err
end

local key = "/stream_routes"
utils.inject_timestamp(conf)
local res, err = core.etcd.push(key, conf)
if not res then
core.log.error("failed to post stream route[", key, "]: ", err)
return 503, {error_msg = err}
end

return res.status, res.body
end


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

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

return res.status, res.body
end


return _M
return resource.new({
name = "stream_routes",
kind = "stream route",
schema = core.schema.stream_route,
checker = check_conf,
unsupported_methods = {"patch"}
})
Loading

0 comments on commit 71e9918

Please sign in to comment.