From 0481a185994ce4a8f1290de2f2b649ce2e261ba0 Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Thu, 5 Jan 2023 03:08:44 +0000 Subject: [PATCH 01/22] refactor(admin): refactor resource routes --- apisix/admin/resource.lua | 184 ++++++++++++++++++++++++++++++++++++++ apisix/admin/routes.lua | 129 ++------------------------ 2 files changed, 191 insertions(+), 122 deletions(-) create mode 100644 apisix/admin/resource.lua diff --git a/apisix/admin/resource.lua b/apisix/admin/resource.lua new file mode 100644 index 000000000000..bb6497056289 --- /dev/null +++ b/apisix/admin/resource.lua @@ -0,0 +1,184 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +local core = require("apisix.core") +local utils = require("apisix.admin.utils") +local setmetatable = setmetatable + + +local _M = { + name = "", + kind = "", + version = 0.2, + need_v3_filter = true, +} + + +local mt = { + __index = _M, + __tostring = function(self) + return "resource name: " .. (_M.name) + end +} + + +function _M.new(name, kind) + return setmetatable({ + name = name, + kind = kind + }, mt) +end + + +function _M.get(id) + local key = "/" .. _M.name + 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 " .. _M.kind .. "[", key, "] from etcd: ", err) + return 503, {error_msg = err} + end + + utils.fix_count(res.body, id) + return res.status, res.body +end + + +function _M.post(check_conf, id, conf, sub_path, args) + local id, err = check_conf(id, conf, false) + if not id then + return 400, err + end + + local key = "/" .. _M.name + utils.inject_timestamp(conf) + local res, err = core.etcd.push(key, conf, args.ttl) + if not res then + core.log.error("failed to post " .. _M.kind .. "[", key, "] to etcd: ", err) + return 503, {error_msg = err} + end + + return res.status, res.body +end + + +function _M.put(check_conf, id, conf, sub_path, args) + local id, err = check_conf(id, conf, true) + if not id then + return 400, err + end + + local key = "/" .. _M.name .. "/" .. id + + local ok, err = utils.inject_conf_with_prev_conf(_M.kind, key, conf) + if not ok then + return 503, {error_msg = err} + end + + local res, err = core.etcd.set(key, conf, args.ttl) + if not res then + core.log.error("failed to put " .. _M.kind .. "[", key, "] to etcd: ", 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 " .. _M.kind .. " id"} + end + + local key = "/" .. _M.name .. "/" .. id + local res, err = core.etcd.delete(key) + if not res then + core.log.error("failed to delete " .. _M.kind .. "[", key, "] in etcd: ", err) + return 503, {error_msg = err} + end + + return res.status, res.body +end + + +function _M.patch(check_conf, id, conf, sub_path, args) + if not id then + return 400, {error_msg = "missing " .. _M.kind .. " id"} + end + + if conf == nil 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 = "/" .. _M.name + if id then + key = key .. "/" .. id + end + + local res_old, err = core.etcd.get(key) + if not res_old then + core.log.error("failed to get " .. _M.kind .. " [", key, "] in etcd: ", 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 id, err = check_conf(id, node_value, true) + if not id then + return 400, err + end + + local res, err = core.etcd.atomic_set(key, node_value, args.ttl, modified_index) + if not res then + core.log.error("failed to set new ".. _M.kind .."[", key, "] to etcd: ", err) + return 503, {error_msg = err} + end + + return res.status, res.body +end + + +return _M diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index 4cd36b385146..91be05e97712 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -18,15 +18,14 @@ local expr = require("resty.expr.v1") local core = require("apisix.core") local apisix_upstream = require("apisix.upstream") local schema_plugin = require("apisix.admin.plugins").check_schema -local utils = require("apisix.admin.utils") +local resource = require("apisix.admin.resource") local tostring = tostring local type = type local loadstring = loadstring local _M = { - version = 0.2, - need_v3_filter = true, + res = resource.new("routes", "route") } @@ -169,141 +168,27 @@ end function _M.put(id, conf, sub_path, args) - local id, err = check_conf(id, conf, true) - if not id then - return 400, err - end - - local key = "/routes/" .. id - - local ok, err = utils.inject_conf_with_prev_conf("route", key, conf) - if not ok then - return 503, {error_msg = err} - end - - local res, err = core.etcd.set(key, conf, args.ttl) - if not res then - core.log.error("failed to put route[", key, "] to etcd: ", err) - return 503, {error_msg = err} - end - - return res.status, res.body + return _M.res.kind(check_conf, id, conf, sub_path, args) end function _M.get(id) - local key = "/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 route[", key, "] from etcd: ", err) - return 503, {error_msg = err} - end - - utils.fix_count(res.body, id) - return res.status, res.body + return _M.res.get(id) end function _M.post(id, conf, sub_path, args) - local id, err = check_conf(id, conf, false) - if not id then - return 400, err - end - - local key = "/routes" - utils.inject_timestamp(conf) - local res, err = core.etcd.push(key, conf, args.ttl) - if not res then - core.log.error("failed to post route[", key, "] to etcd: ", err) - return 503, {error_msg = err} - end - - return res.status, res.body + return _M.res.post(check_conf, id, conf, sub_path, args) end function _M.delete(id) - if not id then - return 400, {error_msg = "missing route id"} - end - - local key = "/routes/" .. id - -- core.log.info("key: ", key) - local res, err = core.etcd.delete(key) - if not res then - core.log.error("failed to delete route[", key, "] in etcd: ", err) - return 503, {error_msg = err} - end - - return res.status, res.body + return _M.res.delete(id) end function _M.patch(id, conf, sub_path, args) - if not id then - return 400, {error_msg = "missing route id"} - end - - if conf == nil 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 = "/routes" - if id then - key = key .. "/" .. id - end - - local res_old, err = core.etcd.get(key) - if not res_old then - core.log.error("failed to get route [", key, "] in etcd: ", 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 id, err = check_conf(id, node_value, true) - if not id then - return 400, err - end - - local res, err = core.etcd.atomic_set(key, node_value, args.ttl, modified_index) - if not res then - core.log.error("failed to set new route[", key, "] to etcd: ", err) - return 503, {error_msg = err} - end - - return res.status, res.body + return _M.res.patch(check_conf, id, conf, sub_path, args) end From 3e4c285954319203e66991a1ab531ddf6979f731 Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Thu, 5 Jan 2023 03:20:47 +0000 Subject: [PATCH 02/22] rerun From 6656e1f073e9d5b53f4eacbab89e9c25b6c3bf4c Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Thu, 5 Jan 2023 03:38:07 +0000 Subject: [PATCH 03/22] add missing type func --- apisix/admin/resource.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/apisix/admin/resource.lua b/apisix/admin/resource.lua index bb6497056289..c99de6676d24 100644 --- a/apisix/admin/resource.lua +++ b/apisix/admin/resource.lua @@ -17,6 +17,7 @@ local core = require("apisix.core") local utils = require("apisix.admin.utils") local setmetatable = setmetatable +local type = type local _M = { From 0de1b72447ebcd7bfc4929e72ef1f09947e7bd4d Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Thu, 5 Jan 2023 03:45:53 +0000 Subject: [PATCH 04/22] fix wrong func name --- apisix/admin/routes.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index 91be05e97712..e2b9a62fb76a 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -168,7 +168,7 @@ end function _M.put(id, conf, sub_path, args) - return _M.res.kind(check_conf, id, conf, sub_path, args) + return _M.res.put(check_conf, id, conf, sub_path, args) end From fb5d19ad3981f602713ad3f57c4cae34341fe08f Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Thu, 5 Jan 2023 07:22:30 +0000 Subject: [PATCH 05/22] fix missing variables --- apisix/admin/routes.lua | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index e2b9a62fb76a..7bd85841bfc9 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -24,8 +24,12 @@ local type = type local loadstring = loadstring +local res = resource.new("routes", "route") + + local _M = { - res = resource.new("routes", "route") + version = res.version, + need_v3_filter = res.need_v3_filter, } @@ -168,27 +172,27 @@ end function _M.put(id, conf, sub_path, args) - return _M.res.put(check_conf, id, conf, sub_path, args) + return res.put(check_conf, id, conf, sub_path, args) end function _M.get(id) - return _M.res.get(id) + return res.get(id) end function _M.post(id, conf, sub_path, args) - return _M.res.post(check_conf, id, conf, sub_path, args) + return res.post(check_conf, id, conf, sub_path, args) end function _M.delete(id) - return _M.res.delete(id) + return res.delete(id) end function _M.patch(id, conf, sub_path, args) - return _M.res.patch(check_conf, id, conf, sub_path, args) + return res.patch(check_conf, id, conf, sub_path, args) end From 846be8eb87204d7b7495cbfb4a11beabe53ca9b1 Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Thu, 5 Jan 2023 07:41:47 +0000 Subject: [PATCH 06/22] revert routes.lua --- apisix/admin/routes.lua | 135 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 123 insertions(+), 12 deletions(-) diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index 7bd85841bfc9..c1d6016bc01d 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -18,18 +18,15 @@ local expr = require("resty.expr.v1") local core = require("apisix.core") local apisix_upstream = require("apisix.upstream") local schema_plugin = require("apisix.admin.plugins").check_schema -local resource = require("apisix.admin.resource") +local utils = require("apisix.admin.utils") local tostring = tostring local type = type local loadstring = loadstring -local res = resource.new("routes", "route") - - local _M = { - version = res.version, - need_v3_filter = res.need_v3_filter, + version = 0.2, + need_v3_filter = true, } @@ -172,28 +169,142 @@ end function _M.put(id, conf, sub_path, args) - return res.put(check_conf, id, conf, sub_path, args) + local id, err = check_conf(id, conf, true) + if not id then + return 400, err + end + + local key = "/routes/" .. id + + local ok, err = utils.inject_conf_with_prev_conf("route", key, conf) + if not ok then + return 503, {error_msg = err} + end + + local res, err = core.etcd.set(key, conf, args.ttl) + if not res then + core.log.error("failed to put route[", key, "] to etcd: ", err) + return 503, {error_msg = err} + end + + return res.status, res.body end function _M.get(id) - return res.get(id) + local key = "/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 route[", key, "] from etcd: ", err) + return 503, {error_msg = err} + end + + utils.fix_count(res.body, id) + return res.status, res.body end function _M.post(id, conf, sub_path, args) - return res.post(check_conf, id, conf, sub_path, args) + local id, err = check_conf(id, conf, false) + if not id then + return 400, err + end + + local key = "/routes" + utils.inject_timestamp(conf) + local res, err = core.etcd.push(key, conf, args.ttl) + if not res then + core.log.error("failed to post route[", key, "] to etcd: ", err) + return 503, {error_msg = err} + end + + return res.status, res.body end function _M.delete(id) - return res.delete(id) + if not id then + return 400, {error_msg = "missing route id"} + end + + local key = "/routes/" .. id + -- core.log.info("key: ", key) + local res, err = core.etcd.delete(key) + if not res then + core.log.error("failed to delete route[", key, "] in etcd: ", err) + return 503, {error_msg = err} + end + + return res.status, res.body end function _M.patch(id, conf, sub_path, args) - return res.patch(check_conf, id, conf, sub_path, args) + if not id then + return 400, {error_msg = "missing route id"} + end + + if conf == nil 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 = "/routes" + if id then + key = key .. "/" .. id + end + + local res_old, err = core.etcd.get(key) + if not res_old then + core.log.error("failed to get route [", key, "] in etcd: ", 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 id, err = check_conf(id, node_value, true) + if not id then + return 400, err + end + + local res, err = core.etcd.atomic_set(key, node_value, args.ttl, modified_index) + if not res then + core.log.error("failed to set new route[", key, "] to etcd: ", err) + return 503, {error_msg = err} + end + + return res.status, res.body end -return _M +return _M \ No newline at end of file From 58b5ad35f24a036101d2f3327924d64dd3648231 Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Thu, 5 Jan 2023 07:52:50 +0000 Subject: [PATCH 07/22] only refactor routes.get --- apisix/admin/routes.lua | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index c1d6016bc01d..7c42f6db200d 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -17,6 +17,7 @@ local expr = require("resty.expr.v1") local core = require("apisix.core") local apisix_upstream = require("apisix.upstream") +local resource = require("apisix.admin.resource") local schema_plugin = require("apisix.admin.plugins").check_schema local utils = require("apisix.admin.utils") local tostring = tostring @@ -24,6 +25,9 @@ local type = type local loadstring = loadstring +local handler = resource.new("routes", "route") + + local _M = { version = 0.2, need_v3_filter = true, @@ -192,19 +196,7 @@ end function _M.get(id) - local key = "/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 route[", key, "] from etcd: ", err) - return 503, {error_msg = err} - end - - utils.fix_count(res.body, id) - return res.status, res.body + return handler.get(id) end @@ -307,4 +299,4 @@ function _M.patch(id, conf, sub_path, args) end -return _M \ No newline at end of file +return _M From 38d7e226e877f4aba4033cb813668b19b4e9f77d Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Thu, 5 Jan 2023 08:16:43 +0000 Subject: [PATCH 08/22] get the error key from unit test --- apisix/admin/resource.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/admin/resource.lua b/apisix/admin/resource.lua index c99de6676d24..361042d58a95 100644 --- a/apisix/admin/resource.lua +++ b/apisix/admin/resource.lua @@ -53,7 +53,7 @@ function _M.get(id) local res, err = core.etcd.get(key, not id) if not res then core.log.error("failed to get " .. _M.kind .. "[", key, "] from etcd: ", err) - return 503, {error_msg = err} + return 503, {error_msg = err .. " key = " .. key} end utils.fix_count(res.body, id) From 4feeb3162bff56a93bf5f73b3a72871cfbff7e53 Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Thu, 5 Jan 2023 08:53:03 +0000 Subject: [PATCH 09/22] revert routes.lua --- apisix/admin/routes.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index 7c42f6db200d..6672539abd90 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -196,7 +196,19 @@ end function _M.get(id) - return handler.get(id) + local key = "/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 route[", key, "] from etcd: ", err) + return 503, {error_msg = err} + end + + utils.fix_count(res.body, id) + return res.status, res.body end From e08a69daf8b5c30b42c4f02d178dc29676bed75b Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Thu, 5 Jan 2023 12:14:38 +0000 Subject: [PATCH 10/22] fix ci error --- apisix/admin/resource.lua | 84 +++++++++++++++----------- apisix/admin/routes.lua | 124 ++------------------------------------ 2 files changed, 55 insertions(+), 153 deletions(-) diff --git a/apisix/admin/resource.lua b/apisix/admin/resource.lua index 361042d58a95..a6bd69cebdc7 100644 --- a/apisix/admin/resource.lua +++ b/apisix/admin/resource.lua @@ -21,39 +21,26 @@ local type = type local _M = { - name = "", - kind = "", version = 0.2, need_v3_filter = true, } local mt = { - __index = _M, - __tostring = function(self) - return "resource name: " .. (_M.name) - end + __index = _M } -function _M.new(name, kind) - return setmetatable({ - name = name, - kind = kind - }, mt) -end - - -function _M.get(id) - local key = "/" .. _M.name +local function get(self, id) + local key = "/" .. self.name 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 " .. _M.kind .. "[", key, "] from etcd: ", err) - return 503, {error_msg = err .. " key = " .. key} + core.log.error("failed to get " .. self.kind .. "[", key, "] from etcd: ", err) + return 503, {error_msg = err} end utils.fix_count(res.body, id) @@ -61,17 +48,17 @@ function _M.get(id) end -function _M.post(check_conf, id, conf, sub_path, args) +local function post(self, check_conf, id, conf, sub_path, args) local id, err = check_conf(id, conf, false) if not id then return 400, err end - local key = "/" .. _M.name + local key = "/" .. self.name utils.inject_timestamp(conf) local res, err = core.etcd.push(key, conf, args.ttl) if not res then - core.log.error("failed to post " .. _M.kind .. "[", key, "] to etcd: ", err) + core.log.error("failed to post " .. self.kind .. "[", key, "] to etcd: ", err) return 503, {error_msg = err} end @@ -79,22 +66,22 @@ function _M.post(check_conf, id, conf, sub_path, args) end -function _M.put(check_conf, id, conf, sub_path, args) +local function put(self, check_conf, id, conf, sub_path, args) local id, err = check_conf(id, conf, true) if not id then return 400, err end - local key = "/" .. _M.name .. "/" .. id + local key = "/" .. self.name .. "/" .. id - local ok, err = utils.inject_conf_with_prev_conf(_M.kind, key, conf) + local ok, err = utils.inject_conf_with_prev_conf(self.kind, key, conf) if not ok then return 503, {error_msg = err} end local res, err = core.etcd.set(key, conf, args.ttl) if not res then - core.log.error("failed to put " .. _M.kind .. "[", key, "] to etcd: ", err) + core.log.error("failed to put " .. self.kind .. "[", key, "] to etcd: ", err) return 503, {error_msg = err} end @@ -102,15 +89,15 @@ function _M.put(check_conf, id, conf, sub_path, args) end -function _M.delete(id) +local function delete(self, id) if not id then - return 400, {error_msg = "missing " .. _M.kind .. " id"} + return 400, {error_msg = "missing " .. self.kind .. " id"} end - local key = "/" .. _M.name .. "/" .. id + local key = "/" .. self.name .. "/" .. id local res, err = core.etcd.delete(key) if not res then - core.log.error("failed to delete " .. _M.kind .. "[", key, "] in etcd: ", err) + core.log.error("failed to delete " .. self.kind .. "[", key, "] in etcd: ", err) return 503, {error_msg = err} end @@ -118,9 +105,9 @@ function _M.delete(id) end -function _M.patch(check_conf, id, conf, sub_path, args) +local function patch(self, check_conf, id, conf, sub_path, args) if not id then - return 400, {error_msg = "missing " .. _M.kind .. " id"} + return 400, {error_msg = "missing " .. self.kind .. " id"} end if conf == nil then @@ -133,14 +120,14 @@ function _M.patch(check_conf, id, conf, sub_path, args) end end - local key = "/" .. _M.name + local key = "/" .. self.name if id then key = key .. "/" .. id end local res_old, err = core.etcd.get(key) if not res_old then - core.log.error("failed to get " .. _M.kind .. " [", key, "] in etcd: ", err) + core.log.error("failed to get " .. self.kind .. " [", key, "] in etcd: ", err) return 503, {error_msg = err} end @@ -174,7 +161,7 @@ function _M.patch(check_conf, id, conf, sub_path, args) local res, err = core.etcd.atomic_set(key, node_value, args.ttl, modified_index) if not res then - core.log.error("failed to set new ".. _M.kind .."[", key, "] to etcd: ", err) + core.log.error("failed to set new ".. self.kind .."[", key, "] to etcd: ", err) return 503, {error_msg = err} end @@ -182,4 +169,33 @@ function _M.patch(check_conf, id, conf, sub_path, args) end +function _M.new(name, kind) + local obj = setmetatable({ + name = name, + kind = kind + }, mt) + + function obj.get(id) + return get(obj, id) + end + + function obj.post(check_conf, id, conf, sub_path, args) + return post(obj, check_conf, id, conf, sub_path, args) + end + + function obj.put(check_conf, id, conf, sub_path, args) + return put(obj, check_conf, id, conf, sub_path, args) + end + + function obj.delete(id) + return delete(obj, id) + end + + function obj.patch(check_conf, id, conf, sub_path, args) + return patch(obj, check_conf, id, conf, sub_path, args) + end + return obj +end + + return _M diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index 6672539abd90..e1581c4aedac 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -173,141 +173,27 @@ end function _M.put(id, conf, sub_path, args) - local id, err = check_conf(id, conf, true) - if not id then - return 400, err - end - - local key = "/routes/" .. id - - local ok, err = utils.inject_conf_with_prev_conf("route", key, conf) - if not ok then - return 503, {error_msg = err} - end - - local res, err = core.etcd.set(key, conf, args.ttl) - if not res then - core.log.error("failed to put route[", key, "] to etcd: ", err) - return 503, {error_msg = err} - end - - return res.status, res.body + return handler.put(id, conf, sub_path, args) end function _M.get(id) - local key = "/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 route[", key, "] from etcd: ", err) - return 503, {error_msg = err} - end - - utils.fix_count(res.body, id) - return res.status, res.body + return handler.get(id) end function _M.post(id, conf, sub_path, args) - local id, err = check_conf(id, conf, false) - if not id then - return 400, err - end - - local key = "/routes" - utils.inject_timestamp(conf) - local res, err = core.etcd.push(key, conf, args.ttl) - if not res then - core.log.error("failed to post route[", key, "] to etcd: ", err) - return 503, {error_msg = err} - end - - return res.status, res.body + return handler.post(id, conf, sub_path, args) end function _M.delete(id) - if not id then - return 400, {error_msg = "missing route id"} - end - - local key = "/routes/" .. id - -- core.log.info("key: ", key) - local res, err = core.etcd.delete(key) - if not res then - core.log.error("failed to delete route[", key, "] in etcd: ", err) - return 503, {error_msg = err} - end - - return res.status, res.body + return handler.delete(id) end function _M.patch(id, conf, sub_path, args) - if not id then - return 400, {error_msg = "missing route id"} - end - - if conf == nil 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 = "/routes" - if id then - key = key .. "/" .. id - end - - local res_old, err = core.etcd.get(key) - if not res_old then - core.log.error("failed to get route [", key, "] in etcd: ", 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 id, err = check_conf(id, node_value, true) - if not id then - return 400, err - end - - local res, err = core.etcd.atomic_set(key, node_value, args.ttl, modified_index) - if not res then - core.log.error("failed to set new route[", key, "] to etcd: ", err) - return 503, {error_msg = err} - end - - return res.status, res.body + return handler.patch(id, conf, sub_path, args) end From 8de708d3c2ebcd4ccea64765dc31f14af68c07f8 Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Thu, 5 Jan 2023 12:21:39 +0000 Subject: [PATCH 11/22] remove useless module --- apisix/admin/routes.lua | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index e1581c4aedac..b8830431758a 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -19,7 +19,6 @@ local core = require("apisix.core") local apisix_upstream = require("apisix.upstream") local resource = require("apisix.admin.resource") local schema_plugin = require("apisix.admin.plugins").check_schema -local utils = require("apisix.admin.utils") local tostring = tostring local type = type local loadstring = loadstring @@ -173,7 +172,7 @@ end function _M.put(id, conf, sub_path, args) - return handler.put(id, conf, sub_path, args) + return handler.put(check_conf, id, conf, sub_path, args) end @@ -183,7 +182,7 @@ end function _M.post(id, conf, sub_path, args) - return handler.post(id, conf, sub_path, args) + return handler.post(check_conf, id, conf, sub_path, args) end @@ -193,7 +192,7 @@ end function _M.patch(id, conf, sub_path, args) - return handler.patch(id, conf, sub_path, args) + return handler.patch(check_conf, id, conf, sub_path, args) end From 0618478d72e41b411a43a024e58175190439b5fb Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Fri, 6 Jan 2023 08:13:00 +0000 Subject: [PATCH 12/22] refactor resource.lua --- apisix/admin/init.lua | 9 ++++-- apisix/admin/resource.lua | 59 +++++++++++++-------------------------- apisix/admin/routes.lua | 35 ++--------------------- 3 files changed, 28 insertions(+), 75 deletions(-) diff --git a/apisix/admin/init.lua b/apisix/admin/init.lua index 3ed8d362e37f..15c4ee6cb36d 100644 --- a/apisix/admin/init.lua +++ b/apisix/admin/init.lua @@ -198,8 +198,13 @@ local function run() end end - local code, data = resource[method](seg_id, req_body, seg_sub_path, - uri_args) + local code, data + if seg_res == "routes" 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) + end + if code then if method == "get" and plugin.enable_data_encryption then if seg_res == "consumers" then diff --git a/apisix/admin/resource.lua b/apisix/admin/resource.lua index a6bd69cebdc7..251a483c4a8d 100644 --- a/apisix/admin/resource.lua +++ b/apisix/admin/resource.lua @@ -31,7 +31,7 @@ local mt = { } -local function get(self, id) +function _M:get(id) local key = "/" .. self.name if id then key = key .. "/" .. id @@ -39,7 +39,7 @@ local function get(self, id) local res, err = core.etcd.get(key, not id) if not res then - core.log.error("failed to get " .. self.kind .. "[", key, "] from etcd: ", err) + core.log.error("failed to get ", self.kind, "[", key, "] from etcd: ", err) return 503, {error_msg = err} end @@ -48,8 +48,8 @@ local function get(self, id) end -local function post(self, check_conf, id, conf, sub_path, args) - local id, err = check_conf(id, conf, false) +function _M:post(id, conf, sub_path, args) + local id, err = self.check_conf(id, conf, false) if not id then return 400, err end @@ -58,7 +58,7 @@ local function post(self, check_conf, id, conf, sub_path, args) utils.inject_timestamp(conf) local res, err = core.etcd.push(key, conf, args.ttl) if not res then - core.log.error("failed to post " .. self.kind .. "[", key, "] to etcd: ", err) + core.log.error("failed to post ", self.kind, "[", key, "] to etcd: ", err) return 503, {error_msg = err} end @@ -66,8 +66,8 @@ local function post(self, check_conf, id, conf, sub_path, args) end -local function put(self, check_conf, id, conf, sub_path, args) - local id, err = check_conf(id, conf, true) +function _M:put(id, conf, sub_path, args) + local id, err = self.check_conf(id, conf, true) if not id then return 400, err end @@ -81,7 +81,7 @@ local function put(self, check_conf, id, conf, sub_path, args) local res, err = core.etcd.set(key, conf, args.ttl) if not res then - core.log.error("failed to put " .. self.kind .. "[", key, "] to etcd: ", err) + core.log.error("failed to put ", self.kind, "[", key, "] to etcd: ", err) return 503, {error_msg = err} end @@ -89,7 +89,7 @@ local function put(self, check_conf, id, conf, sub_path, args) end -local function delete(self, id) +function _M:delete(id) if not id then return 400, {error_msg = "missing " .. self.kind .. " id"} end @@ -97,7 +97,7 @@ local function delete(self, id) local key = "/" .. self.name .. "/" .. id local res, err = core.etcd.delete(key) if not res then - core.log.error("failed to delete " .. self.kind .. "[", key, "] in etcd: ", err) + core.log.error("failed to delete ", self.kind, "[", key, "] in etcd: ", err) return 503, {error_msg = err} end @@ -105,7 +105,7 @@ local function delete(self, id) end -local function patch(self, check_conf, id, conf, sub_path, args) +function _M:patch(id, conf, sub_path, args) if not id then return 400, {error_msg = "missing " .. self.kind .. " id"} end @@ -127,15 +127,14 @@ local function patch(self, check_conf, id, conf, sub_path, args) local res_old, err = core.etcd.get(key) if not res_old then - core.log.error("failed to get " .. self.kind .. " [", key, "] in etcd: ", err) + core.log.error("failed to get ", self.kind, " [", key, "] in etcd: ", 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)) + 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 @@ -154,14 +153,14 @@ local function patch(self, check_conf, id, conf, sub_path, args) core.log.info("new conf: ", core.json.delay_encode(node_value, true)) - local id, err = check_conf(id, node_value, true) + local id, err = self.check_conf(id, node_value, true) if not id then return 400, err end local res, err = core.etcd.atomic_set(key, node_value, args.ttl, modified_index) if not res then - core.log.error("failed to set new ".. self.kind .."[", key, "] to etcd: ", err) + core.log.error("failed to set new ", self.kind, "[", key, "] to etcd: ", err) return 503, {error_msg = err} end @@ -169,32 +168,12 @@ local function patch(self, check_conf, id, conf, sub_path, args) end -function _M.new(name, kind) - local obj = setmetatable({ +function _M.new(name, kind, check_conf) + return setmetatable({ name = name, - kind = kind + kind = kind, + check_conf = check_conf }, mt) - - function obj.get(id) - return get(obj, id) - end - - function obj.post(check_conf, id, conf, sub_path, args) - return post(obj, check_conf, id, conf, sub_path, args) - end - - function obj.put(check_conf, id, conf, sub_path, args) - return put(obj, check_conf, id, conf, sub_path, args) - end - - function obj.delete(id) - return delete(obj, id) - end - - function obj.patch(check_conf, id, conf, sub_path, args) - return patch(obj, check_conf, id, conf, sub_path, args) - end - return obj end diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index b8830431758a..8e59e36c8e25 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -24,13 +24,7 @@ local type = type local loadstring = loadstring -local handler = resource.new("routes", "route") - - -local _M = { - version = 0.2, - need_v3_filter = true, -} +local _M = {} local function check_conf(id, conf, need_id) @@ -171,29 +165,4 @@ local function check_conf(id, conf, need_id) end -function _M.put(id, conf, sub_path, args) - return handler.put(check_conf, id, conf, sub_path, args) -end - - -function _M.get(id) - return handler.get(id) -end - - -function _M.post(id, conf, sub_path, args) - return handler.post(check_conf, id, conf, sub_path, args) -end - - -function _M.delete(id) - return handler.delete(id) -end - - -function _M.patch(id, conf, sub_path, args) - return handler.patch(check_conf, id, conf, sub_path, args) -end - - -return _M +return resource.new("routes", "route", check_conf) From 9ecbcc97bc80ddc9c7bde57084be0404b701e84e Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Fri, 6 Jan 2023 08:17:19 +0000 Subject: [PATCH 13/22] remove useless _M --- apisix/admin/routes.lua | 3 --- 1 file changed, 3 deletions(-) diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index 8e59e36c8e25..a7ab9c862306 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -24,9 +24,6 @@ local type = type local loadstring = loadstring -local _M = {} - - local function check_conf(id, conf, need_id) if not conf then return nil, {error_msg = "missing configurations"} From efdbcc1bb2d955d9383830ae14981a56b957d504 Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Fri, 6 Jan 2023 08:58:56 +0000 Subject: [PATCH 14/22] remove useless resource field `version` --- apisix/admin/resource.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/apisix/admin/resource.lua b/apisix/admin/resource.lua index 251a483c4a8d..eb1be98081d9 100644 --- a/apisix/admin/resource.lua +++ b/apisix/admin/resource.lua @@ -21,7 +21,6 @@ local type = type local _M = { - version = 0.2, need_v3_filter = true, } From a178ff249ebfd9b15feeaa03efca5821f39dc6af Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Mon, 9 Jan 2023 03:37:13 +0000 Subject: [PATCH 15/22] replace resource.new multiple params to opt --- apisix/admin/resource.lua | 52 +++++++++++++++++++++++++++++++++++---- apisix/admin/routes.lua | 34 +++++-------------------- 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/apisix/admin/resource.lua b/apisix/admin/resource.lua index eb1be98081d9..c0fa0bca400a 100644 --- a/apisix/admin/resource.lua +++ b/apisix/admin/resource.lua @@ -30,6 +30,47 @@ local mt = { } +local function check_conf_id(id, conf, need_id) + 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 +end + + +function _M:check_conf(id, conf, need_id) + -- check if missing configurations + if not conf then + return nil, {error_msg = "missing configurations"} + end + + -- check id if need id + check_conf_id(id, conf, need_id) + + core.log.info("schema: ", core.json.delay_encode(self.schema)) + core.log.info("conf : ", core.json.delay_encode(conf)) + + -- check schema + local ok, err = core.schema.check(self.schema, conf) + if not ok then + return nil, {error_msg = "invalid configuration: " .. err} + end + + -- check self validation + self.check_conf_self(id, conf, need_id) +end + + function _M:get(id) local key = "/" .. self.name if id then @@ -142,7 +183,7 @@ function _M:patch(id, conf, sub_path, args) local code, err, node_val = core.table.patch(node_value, sub_path, conf) node_value = node_val if code then - return code, err + return code, {error_msg = err} end utils.inject_timestamp(node_value, nil, true) else @@ -167,11 +208,12 @@ function _M:patch(id, conf, sub_path, args) end -function _M.new(name, kind, check_conf) +function _M.new(opt) return setmetatable({ - name = name, - kind = kind, - check_conf = check_conf + name = opt.name, + kind = opt.kind, + schema = opt.schema, + check_conf_self = opt.check_conf_self, }, mt) end diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index a7ab9c862306..90690e9aa937 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -25,28 +25,6 @@ local loadstring = loadstring 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.route)) - core.log.info("conf : ", core.json.delay_encode(conf)) - if conf.host and conf.hosts then return nil, {error_msg = "only one of host or hosts is allowed"} end @@ -56,11 +34,6 @@ local function check_conf(id, conf, need_id) .. "allowed"} end - local ok, err = core.schema.check(core.schema.route, conf) - if not ok then - return nil, {error_msg = "invalid configuration: " .. err} - end - local upstream_conf = conf.upstream if upstream_conf then local ok, err = apisix_upstream.check_upstream_conf(upstream_conf) @@ -162,4 +135,9 @@ local function check_conf(id, conf, need_id) end -return resource.new("routes", "route", check_conf) +return resource.new({ + name = "routes", + kind = "route", + schema = core.schema.route, + check_conf_self = check_conf +}) From 3fa4fbaef60166cdcdd9c2807d2843495db116ff Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Mon, 9 Jan 2023 03:42:54 +0000 Subject: [PATCH 16/22] add missing `local` --- apisix/admin/resource.lua | 1 + apisix/admin/routes.lua | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/apisix/admin/resource.lua b/apisix/admin/resource.lua index c0fa0bca400a..89f451284d58 100644 --- a/apisix/admin/resource.lua +++ b/apisix/admin/resource.lua @@ -17,6 +17,7 @@ local core = require("apisix.core") local utils = require("apisix.admin.utils") local setmetatable = setmetatable +local tostring = tostring local type = type diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index 90690e9aa937..6a9b6bbf7206 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -19,12 +19,13 @@ local core = require("apisix.core") local apisix_upstream = require("apisix.upstream") local resource = require("apisix.admin.resource") local schema_plugin = require("apisix.admin.plugins").check_schema -local tostring = tostring local type = type local loadstring = loadstring local function check_conf(id, conf, need_id) + local ok, err + if conf.host and conf.hosts then return nil, {error_msg = "only one of host or hosts is allowed"} end @@ -101,7 +102,7 @@ local function check_conf(id, conf, need_id) end if conf.vars then - ok, err = expr.new(conf.vars) + local ok, err = expr.new(conf.vars) if not ok then return nil, {error_msg = "failed to validate the 'vars' expression: " .. err} end From 9130c7f6130d91d5b799d4c12e447b21de9c70cb Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Mon, 9 Jan 2023 03:44:55 +0000 Subject: [PATCH 17/22] remove useless variables --- apisix/admin/routes.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index 6a9b6bbf7206..88a55d54c165 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -24,8 +24,6 @@ local loadstring = loadstring local function check_conf(id, conf, need_id) - local ok, err - if conf.host and conf.hosts then return nil, {error_msg = "only one of host or hosts is allowed"} end From 963645d01c271c6d2348fd15076aad48c6c7ed92 Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Mon, 9 Jan 2023 05:13:41 +0000 Subject: [PATCH 18/22] fix no return error --- apisix/admin/resource.lua | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/apisix/admin/resource.lua b/apisix/admin/resource.lua index 89f451284d58..9607b55c0774 100644 --- a/apisix/admin/resource.lua +++ b/apisix/admin/resource.lua @@ -44,8 +44,8 @@ local function check_conf_id(id, conf, need_id) if need_id and conf.id and tostring(conf.id) ~= tostring(id) then return nil, {error_msg = "wrong id"} end - conf.id = id + return id, nil end @@ -56,19 +56,22 @@ function _M:check_conf(id, conf, need_id) end -- check id if need id - check_conf_id(id, conf, need_id) + local ok, err = check_conf_id(id, conf, need_id) + if not ok then + return nil, err + end core.log.info("schema: ", core.json.delay_encode(self.schema)) core.log.info("conf : ", core.json.delay_encode(conf)) -- check schema - local ok, err = core.schema.check(self.schema, conf) + ok, err = core.schema.check(self.schema, conf) if not ok then return nil, {error_msg = "invalid configuration: " .. err} end -- check self validation - self.check_conf_self(id, conf, need_id) + return self.check_conf_self(id, conf, need_id) end @@ -90,7 +93,7 @@ end function _M:post(id, conf, sub_path, args) - local id, err = self.check_conf(id, conf, false) + local id, err = self:check_conf(id, conf, false) if not id then return 400, err end @@ -108,7 +111,7 @@ end function _M:put(id, conf, sub_path, args) - local id, err = self.check_conf(id, conf, true) + local id, err = self:check_conf(id, conf, true) if not id then return 400, err end @@ -194,7 +197,7 @@ function _M:patch(id, conf, sub_path, args) core.log.info("new conf: ", core.json.delay_encode(node_value, true)) - local id, err = self.check_conf(id, node_value, true) + local id, err = self:check_conf(id, node_value, true) if not id then return 400, err end From 67bc68d317138a05b38b624ab876e5def5abbf96 Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Mon, 9 Jan 2023 06:14:39 +0000 Subject: [PATCH 19/22] fix check id error --- apisix/admin/resource.lua | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/apisix/admin/resource.lua b/apisix/admin/resource.lua index 9607b55c0774..0dd529768b14 100644 --- a/apisix/admin/resource.lua +++ b/apisix/admin/resource.lua @@ -31,7 +31,13 @@ local mt = { } -local function check_conf_id(id, conf, need_id) +function _M:check_conf(id, conf, need_id) + -- check if missing configurations + if not conf then + return nil, {error_msg = "missing configurations"} + end + + -- check id if need id id = id or conf.id if need_id and not id then return nil, {error_msg = "missing id"} @@ -44,28 +50,14 @@ local function check_conf_id(id, conf, need_id) if need_id and conf.id and tostring(conf.id) ~= tostring(id) then return nil, {error_msg = "wrong id"} end - conf.id = id - return id, nil -end - - -function _M:check_conf(id, conf, need_id) - -- check if missing configurations - if not conf then - return nil, {error_msg = "missing configurations"} - end - -- check id if need id - local ok, err = check_conf_id(id, conf, need_id) - if not ok then - return nil, err - end + conf.id = id core.log.info("schema: ", core.json.delay_encode(self.schema)) core.log.info("conf : ", core.json.delay_encode(conf)) -- check schema - ok, err = core.schema.check(self.schema, conf) + local ok, err = core.schema.check(self.schema, conf) if not ok then return nil, {error_msg = "invalid configuration: " .. err} end From bf40a1136d5e60918f14380f9495737402d7038b Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Mon, 9 Jan 2023 06:52:39 +0000 Subject: [PATCH 20/22] fix missing error message --- apisix/admin/resource.lua | 17 ++++++----------- apisix/admin/routes.lua | 2 +- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/apisix/admin/resource.lua b/apisix/admin/resource.lua index 0dd529768b14..f317c7942700 100644 --- a/apisix/admin/resource.lua +++ b/apisix/admin/resource.lua @@ -40,15 +40,15 @@ function _M:check_conf(id, conf, need_id) -- check id if need id id = id or conf.id if need_id and not id then - return nil, {error_msg = "missing id"} + return nil, {error_msg = "missing ".. self.kind .. " id"} end if not need_id and id then - return nil, {error_msg = "wrong id, do not need it"} + 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 id"} + return nil, {error_msg = "wrong ".. self.kind .. " id"} end conf.id = id @@ -62,8 +62,8 @@ function _M:check_conf(id, conf, need_id) return nil, {error_msg = "invalid configuration: " .. err} end - -- check self validation - return self.check_conf_self(id, conf, need_id) + -- check the resource own rules + return self.checker(id, conf, need_id) end @@ -205,12 +205,7 @@ end function _M.new(opt) - return setmetatable({ - name = opt.name, - kind = opt.kind, - schema = opt.schema, - check_conf_self = opt.check_conf_self, - }, mt) + return setmetatable(opt, mt) end diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index 88a55d54c165..09506bfb91ea 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -138,5 +138,5 @@ return resource.new({ name = "routes", kind = "route", schema = core.schema.route, - check_conf_self = check_conf + checker = check_conf }) From 7cc9f61980906ed57e82a998c9c7b987fd47d017 Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Mon, 9 Jan 2023 09:06:08 +0000 Subject: [PATCH 21/22] move schema check to the resource own --- apisix/admin/resource.lua | 9 --------- apisix/admin/routes.lua | 11 +++++++++-- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/apisix/admin/resource.lua b/apisix/admin/resource.lua index f317c7942700..6ad71c28245f 100644 --- a/apisix/admin/resource.lua +++ b/apisix/admin/resource.lua @@ -53,15 +53,6 @@ function _M:check_conf(id, conf, need_id) conf.id = id - core.log.info("schema: ", core.json.delay_encode(self.schema)) - core.log.info("conf : ", core.json.delay_encode(conf)) - - -- check schema - local ok, err = core.schema.check(self.schema, conf) - if not ok then - return nil, {error_msg = "invalid configuration: " .. err} - end - -- check the resource own rules return self.checker(id, conf, need_id) end diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index 09506bfb91ea..faf0a555b43e 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -24,6 +24,9 @@ local loadstring = loadstring local function check_conf(id, conf, need_id) + core.log.info("schema: ", core.json.delay_encode(core.schema.route)) + core.log.info("conf : ", core.json.delay_encode(conf)) + if conf.host and conf.hosts then return nil, {error_msg = "only one of host or hosts is allowed"} end @@ -33,6 +36,11 @@ local function check_conf(id, conf, need_id) .. "allowed"} end + local ok, err = core.schema.check(core.schema.route, conf) + if not ok then + return nil, {error_msg = "invalid configuration: " .. err} + end + local upstream_conf = conf.upstream if upstream_conf then local ok, err = apisix_upstream.check_upstream_conf(upstream_conf) @@ -100,7 +108,7 @@ local function check_conf(id, conf, need_id) end if conf.vars then - local ok, err = expr.new(conf.vars) + ok, err = expr.new(conf.vars) if not ok then return nil, {error_msg = "failed to validate the 'vars' expression: " .. err} end @@ -137,6 +145,5 @@ end return resource.new({ name = "routes", kind = "route", - schema = core.schema.route, checker = check_conf }) From fce78a619263cec1aa1246c853bf828a307949bd Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Tue, 10 Jan 2023 07:09:03 +0000 Subject: [PATCH 22/22] move log to common --- apisix/admin/resource.lua | 5 ++++- apisix/admin/routes.lua | 8 +++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/apisix/admin/resource.lua b/apisix/admin/resource.lua index 6ad71c28245f..c4aa21b3504d 100644 --- a/apisix/admin/resource.lua +++ b/apisix/admin/resource.lua @@ -53,8 +53,11 @@ function _M:check_conf(id, conf, need_id) conf.id = id + core.log.info("schema: ", core.json.delay_encode(self.schema)) + core.log.info("conf : ", core.json.delay_encode(conf)) + -- check the resource own rules - return self.checker(id, conf, need_id) + return self.checker(id, conf, need_id, self.schema) end diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index faf0a555b43e..06f27fa0c388 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -23,10 +23,7 @@ local type = type local loadstring = loadstring -local function check_conf(id, conf, need_id) - core.log.info("schema: ", core.json.delay_encode(core.schema.route)) - core.log.info("conf : ", core.json.delay_encode(conf)) - +local function check_conf(id, conf, need_id, schema) if conf.host and conf.hosts then return nil, {error_msg = "only one of host or hosts is allowed"} end @@ -36,7 +33,7 @@ local function check_conf(id, conf, need_id) .. "allowed"} end - local ok, err = core.schema.check(core.schema.route, conf) + local ok, err = core.schema.check(schema, conf) if not ok then return nil, {error_msg = "invalid configuration: " .. err} end @@ -145,5 +142,6 @@ end return resource.new({ name = "routes", kind = "route", + schema = core.schema.route, checker = check_conf })