This is an automated email from the ASF dual-hosted git repository. dabue 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 2996416 feat: Add create_time and update_time in global_rule schema (#3201) 2996416 is described below commit 2996416d603cd6b76b0b9dc9fede220edebebc13 Author: Joey <majunj...@gmail.com> AuthorDate: Thu Jan 7 13:01:09 2021 +0800 feat: Add create_time and update_time in global_rule schema (#3201) * feat: Add create_time and update_time in global_rule schema Signed-off-by: imjoey <majunj...@gmail.com> * Add missing time injection and test cases for that Signed-off-by: imjoey <majunj...@gmail.com> * Revert previous delete test cases for strip_etcd_resp Signed-off-by: imjoey <majunj...@gmail.com> --- apisix/admin/global_rules.lua | 9 +++++++++ apisix/schema_def.lua | 4 +++- t/admin/global-rules.t | 37 +++++++++++++++++++++++++++++++++++++ t/admin/schema.t | 10 ++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/apisix/admin/global_rules.lua b/apisix/admin/global_rules.lua index 997f444..d15218f 100644 --- a/apisix/admin/global_rules.lua +++ b/apisix/admin/global_rules.lua @@ -15,6 +15,7 @@ -- limitations under the License. -- local core = require("apisix.core") +local utils = require("apisix.admin.utils") local schema_plugin = require("apisix.admin.plugins").check_schema local type = type local tostring = tostring @@ -68,6 +69,12 @@ function _M.put(id, conf) end local key = "/global_rules/" .. id + + local ok, err = utils.inject_conf_with_prev_conf("route", key, conf) + if not ok then + return 500, {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) @@ -149,6 +156,8 @@ function _M.patch(id, conf, sub_path) core.log.info("new conf: ", core.json.delay_encode(node_value, true)) + utils.inject_timestamp(node_value, nil, conf) + local ok, err = check_conf(id, node_value, true) if not ok then return 400, err diff --git a/apisix/schema_def.lua b/apisix/schema_def.lua index 4992ea9..a4fedd0 100644 --- a/apisix/schema_def.lua +++ b/apisix/schema_def.lua @@ -688,7 +688,9 @@ _M.global_rule = { type = "object", properties = { id = id_schema, - plugins = plugins_schema + plugins = plugins_schema, + create_time = timestamp_def, + update_time = timestamp_def }, required = {"plugins"}, additionalProperties = false, diff --git a/t/admin/global-rules.t b/t/admin/global-rules.t index ee5c4a9..564f2b4 100644 --- a/t/admin/global-rules.t +++ b/t/admin/global-rules.t @@ -31,6 +31,7 @@ __DATA__ location /t { content_by_lua_block { local t = require("lib.test_admin").test + local etcd = require("apisix.core.etcd") local code, body = t('/apisix/admin/global_rules/1', ngx.HTTP_PUT, [[{ @@ -63,6 +64,12 @@ __DATA__ ngx.status = code ngx.say(body) + + local res = assert(etcd.get('/global_rules/1')) + local create_time = res.body.node.value.create_time + assert(create_time ~= nil, "create_time is nil") + local update_time = res.body.node.value.update_time + assert(update_time ~= nil, "update_time is nil") } } --- request @@ -164,6 +171,14 @@ passed location /t { content_by_lua_block { local t = require("lib.test_admin").test + local etcd = require("apisix.core.etcd") + local res = assert(etcd.get('/global_rules/1')) + local prev_create_time = res.body.node.value.create_time + assert(prev_create_time ~= nil, "create_time is nil") + local prev_update_time = res.body.node.value.update_time + assert(prev_update_time ~= nil, "update_time is nil") + ngx.sleep(1) + local code, body = t('/apisix/admin/global_rules/1', ngx.HTTP_PATCH, [[{ @@ -195,6 +210,13 @@ passed ngx.status = code ngx.say(body) + + local res = assert(etcd.get('/global_rules/1')) + local create_time = res.body.node.value.create_time + assert(prev_create_time == create_time, "create_time mismatched") + local update_time = res.body.node.value.update_time + assert(update_time ~= nil, "update_time is nil") + assert(prev_update_time ~= update_time, "update_time should be changed") } } --- request @@ -211,6 +233,14 @@ passed location /t { content_by_lua_block { local t = require("lib.test_admin").test + local etcd = require("apisix.core.etcd") + local res = assert(etcd.get('/global_rules/1')) + local prev_create_time = res.body.node.value.create_time + assert(prev_create_time ~= nil, "create_time is nil") + local prev_update_time = res.body.node.value.update_time + assert(prev_update_time ~= nil, "update_time is nil") + ngx.sleep(1) + local code, body = t('/apisix/admin/global_rules/1/plugins', ngx.HTTP_PATCH, [[{ @@ -241,6 +271,13 @@ passed ngx.status = code ngx.say(body) + + local res = assert(etcd.get('/global_rules/1')) + local create_time = res.body.node.value.create_time + assert(prev_create_time == create_time, "create_time mismatched") + local update_time = res.body.node.value.update_time + assert(update_time ~= nil, "update_time is nil") + assert(prev_update_time ~= update_time, "update_time should be changed") } } --- request diff --git a/t/admin/schema.t b/t/admin/schema.t index 021f59d..0a8896d 100644 --- a/t/admin/schema.t +++ b/t/admin/schema.t @@ -281,3 +281,13 @@ GET /apisix/admin/schema/plugins/node-status qr/"disable":\{"type":"boolean"\}/ --- no_error_log [error] + + + +=== TEST 15: get global_rule schema to check if it contains `create_time` and `update_time` +--- request +GET /apisix/admin/schema/global_rule +--- response_body eval +qr/("update_time":\{"type":"integer"\}.*"create_time":\{"type":"integer"\}|"create_time":\{"type":"integer"\}.*"update_time":\{"type":"integer"\})/ +--- no_error_log +[error]