From 7a1287a62aebb49c76c439be52835da940cb0ce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B3=BD=E8=BD=A9?= Date: Fri, 6 Aug 2021 21:47:09 +0800 Subject: [PATCH] fix(admin): inject updatetime when the requst is PATCH with sub path (#4765) * fix(admin): inject updatetime when the requst is PATCH with sub path Fix #4763 Signed-off-by: spacewander * fix typo error Signed-off-by: spacewander --- apisix/admin/global_rules.lua | 4 ++-- apisix/admin/plugin_config.lua | 4 ++-- apisix/admin/routes.lua | 4 ++-- apisix/admin/services.lua | 4 ++-- apisix/admin/ssl.lua | 5 ++-- apisix/admin/upstreams.lua | 4 ++-- apisix/admin/utils.lua | 11 +++++++-- t/admin/upstream4.t | 43 ++++++++++++++++++++++++++++++++++ 8 files changed, 64 insertions(+), 15 deletions(-) diff --git a/apisix/admin/global_rules.lua b/apisix/admin/global_rules.lua index 658ec570e57b..caccc911996e 100644 --- a/apisix/admin/global_rules.lua +++ b/apisix/admin/global_rules.lua @@ -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 diff --git a/apisix/admin/plugin_config.lua b/apisix/admin/plugin_config.lua index c7f7f44879b0..74302df09be0 100644 --- a/apisix/admin/plugin_config.lua +++ b/apisix/admin/plugin_config.lua @@ -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 diff --git a/apisix/admin/routes.lua b/apisix/admin/routes.lua index 8be47400c04c..bed0524e8384 100644 --- a/apisix/admin/routes.lua +++ b/apisix/admin/routes.lua @@ -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) diff --git a/apisix/admin/services.lua b/apisix/admin/services.lua index e57b55115493..faef65a4c804 100644 --- a/apisix/admin/services.lua +++ b/apisix/admin/services.lua @@ -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) diff --git a/apisix/admin/ssl.lua b/apisix/admin/ssl.lua index 5fd3235a66cd..fc16e1cff6c5 100644 --- a/apisix/admin/ssl.lua +++ b/apisix/admin/ssl.lua @@ -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) @@ -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) diff --git a/apisix/admin/upstreams.lua b/apisix/admin/upstreams.lua index 3d93c755f7de..8180cdb981f6 100644 --- a/apisix/admin/upstreams.lua +++ b/apisix/admin/upstreams.lua @@ -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) diff --git a/apisix/admin/utils.lua b/apisix/admin/utils.lua index aa7a34fb98c5..3ff695a473b6 100644 --- a/apisix/admin/utils.lua +++ b/apisix/admin/utils.lua @@ -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 diff --git a/t/admin/upstream4.t b/t/admin/upstream4.t index 49dbcb1a8e58..f3078a1ddc39 100644 --- a/t/admin/upstream4.t +++ b/t/admin/upstream4.t @@ -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") + } + }