Skip to content

Commit

Permalink
fix(admin): inject updatetime when the requst is PATCH with sub path (#…
Browse files Browse the repository at this point in the history
…4765)

* fix(admin): inject updatetime when the requst is PATCH with sub path

Fix #4763

Signed-off-by: spacewander <spacewanderlzx@gmail.com>

* fix typo error

Signed-off-by: spacewander <spacewanderlzx@gmail.com>
  • Loading branch information
spacewander authored Aug 6, 2021
1 parent 391a2d3 commit 7a1287a
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 15 deletions.
4 changes: 2 additions & 2 deletions apisix/admin/global_rules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ function _M.patch(id, conf, sub_path)
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))

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: 2 additions & 2 deletions apisix/admin/plugin_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ function _M.patch(id, conf, sub_path)
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))

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: 2 additions & 2 deletions apisix/admin/routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ function _M.patch(id, conf, sub_path, args)
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

utils.inject_timestamp(node_value, nil, conf)

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

local id, err = check_conf(id, node_value, true)
Expand Down
4 changes: 2 additions & 2 deletions apisix/admin/services.lua
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ function _M.patch(id, conf, sub_path)
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

utils.inject_timestamp(node_value, nil, conf)

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

local id, err = check_conf(id, node_value, true)
Expand Down
5 changes: 2 additions & 3 deletions apisix/admin/ssl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ function _M.patch(id, conf, sub_path)
if code then
return code, err
end
utils.inject_timestamp(node_value, nil, true)
else
if conf.key then
conf.key = apisix_ssl.aes_encrypt_pkey(conf.key)
Expand All @@ -215,11 +216,9 @@ function _M.patch(id, conf, sub_path)
end

node_value = core.table.merge(node_value, conf);
utils.inject_timestamp(node_value, nil, conf)
end


utils.inject_timestamp(node_value, nil, conf)

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

local id, err = check_conf(id, node_value, true)
Expand Down
4 changes: 2 additions & 2 deletions apisix/admin/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ function _M.patch(id, conf, sub_path)
if code then
return code, err
end
utils.inject_timestamp(new_value, nil, true)
else
new_value = core.table.merge(new_value, conf);
utils.inject_timestamp(new_value, nil, conf)
end

utils.inject_timestamp(new_value, nil, conf)

core.log.info("new value ", core.json.delay_encode(new_value, true))

local id, err = check_conf(id, new_value, true)
Expand Down
11 changes: 9 additions & 2 deletions apisix/admin/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ local function inject_timestamp(conf, prev_conf, patch_conf)
end
end

-- For PATCH request, the modification is passed as 'patch_conf'
if not conf.update_time or (patch_conf and patch_conf.update_time == nil) then
if not conf.update_time or
-- For PATCH request, the modification is passed as 'patch_conf'
-- If the sub path is used, the 'patch_conf' will be a placeholder `true`
(patch_conf and (patch_conf == true or patch_conf.update_time == nil))
then
-- reset the update_time if:
-- 1. PATCH request, with sub path
-- 2. PATCH request, update_time not given
-- 3. Other request, update_time not given
conf.update_time = ngx_time()
end
end
Expand Down
43 changes: 43 additions & 0 deletions t/admin/upstream4.t
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,46 @@ passed
}
--- response_body
[delete] code: 200 message: passed
=== TEST 17: patch upstream with sub_path, the data is number
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin").test
local etcd = require("apisix.core.etcd")
local code, message = t('/apisix/admin/upstreams/1',
ngx.HTTP_PUT,
[[{
"nodes": {},
"type": "roundrobin"
}]]
)
if code >= 300 then
ngx.status = code
ngx.say(message)
return
end
local id = 1
local res = assert(etcd.get('/upstreams/' .. id))
local prev_create_time = res.body.node.value.create_time
local prev_update_time = res.body.node.value.update_time
ngx.sleep(1)
local code, message = t('/apisix/admin/upstreams/1/retries',
ngx.HTTP_PATCH,
json.encode(1)
)
if code >= 300 then
ngx.status = code
end
ngx.say(message)
local res = assert(etcd.get('/upstreams/' .. id))
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(prev_update_time ~= update_time, "update_time should be changed")
}
}

0 comments on commit 7a1287a

Please sign in to comment.