Skip to content

Commit

Permalink
feat: Add create_time and update_time in global_rule schema (#3201)
Browse files Browse the repository at this point in the history
* feat: Add create_time and update_time in global_rule schema

Signed-off-by: imjoey <majunjiev@gmail.com>

* Add missing time injection and test cases for that

Signed-off-by: imjoey <majunjiev@gmail.com>

* Revert previous delete test cases for strip_etcd_resp

Signed-off-by: imjoey <majunjiev@gmail.com>
  • Loading branch information
imjoey authored Jan 7, 2021
1 parent 7b5d2ea commit 2996416
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
9 changes: 9 additions & 0 deletions apisix/admin/global_rules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion apisix/schema_def.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions t/admin/global-rules.t
Original file line number Diff line number Diff line change
Expand Up @@ -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,
[[{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
[[{
Expand Down Expand Up @@ -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
Expand All @@ -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,
[[{
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions t/admin/schema.t
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit 2996416

Please # to comment.