Skip to content

Commit

Permalink
feat(admin api): enhance PATCH method, allow to update partial data. (
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-chen authored and SaberMaster committed Jun 30, 2020
1 parent eddcb9e commit 1f1cc89
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 159 deletions.
35 changes: 6 additions & 29 deletions apisix/admin/global_rules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,19 @@ function _M.delete(id)
end


function _M.patch(id, conf, sub_path)
function _M.patch(id, conf)
if not id then
return 400, {error_msg = "missing global rule id"}
end

if not sub_path then
return 400, {error_msg = "missing sub-path"}
end

if not conf then
return 400, {error_msg = "missing new configuration"}
end

if type(conf) ~= "table" then
return 400, {error_msg = "invalid configuration"}
end

local key = "/global_rules/" .. id
local res_old, err = core.etcd.get(key)
if not res_old then
Expand All @@ -131,32 +131,9 @@ function _M.patch(id, conf, sub_path)
core.json.delay_encode(res_old, true))

local node_value = res_old.body.node.value
local sub_value = node_value
local sub_paths = core.utils.split_uri(sub_path)
for i = 1, #sub_paths - 1 do
local sub_name = sub_paths[i]
if sub_value[sub_name] == nil then
sub_value[sub_name] = {}
end

sub_value = sub_value[sub_name]
node_value = core.table.merge(node_value, conf);

if type(sub_value) ~= "table" then
return 400, "invalid sub-path: /"
.. core.table.concat(sub_paths, 1, i)
end
end

if type(sub_value) ~= "table" then
return 400, "invalid sub-path: /" .. sub_path
end

local sub_name = sub_paths[#sub_paths]
if sub_name and sub_name ~= "" then
sub_value[sub_name] = conf
else
node_value = conf
end
core.log.info("new conf: ", core.json.delay_encode(node_value, true))

local ok, err = check_conf(id, node_value, true)
Expand Down
36 changes: 7 additions & 29 deletions apisix/admin/routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,19 @@ function _M.delete(id)
end


function _M.patch(id, conf, sub_path, args)
function _M.patch(id, conf, args)
if not id then
return 400, {error_msg = "missing route id"}
end

if not sub_path then
return 400, {error_msg = "missing sub-path"}
end

if not conf then
return 400, {error_msg = "missing new configuration"}
end

if type(conf) ~= "table" then
return 400, {error_msg = "invalid configuration"}
end

local key = "/routes"
if id then
key = key .. "/" .. id
Expand All @@ -224,33 +224,11 @@ function _M.patch(id, conf, sub_path, args)
core.log.info("key: ", key, " old value: ",
core.json.delay_encode(res_old, true))

local node_value = res_old.body.node.value
local sub_value = node_value
local sub_paths = core.utils.split_uri(sub_path)
for i = 1, #sub_paths - 1 do
local sub_name = sub_paths[i]
if sub_value[sub_name] == nil then
sub_value[sub_name] = {}
end

sub_value = sub_value[sub_name]

if type(sub_value) ~= "table" then
return 400, "invalid sub-path: /"
.. core.table.concat(sub_paths, 1, i)
end
end
local node_value = res_old.body.node.value

if type(sub_value) ~= "table" then
return 400, "invalid sub-path: /" .. sub_path
end
node_value = core.table.merge(node_value, conf);

local sub_name = sub_paths[#sub_paths]
if sub_name and sub_name ~= "" then
sub_value[sub_name] = conf
else
node_value = conf
end
core.log.info("new conf: ", core.json.delay_encode(node_value, true))

local id, err = check_conf(id, node_value, true)
Expand Down
35 changes: 6 additions & 29 deletions apisix/admin/services.lua
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,19 @@ function _M.delete(id)
end


function _M.patch(id, conf, sub_path)
function _M.patch(id, conf)
if not id then
return 400, {error_msg = "missing service id"}
end

if not sub_path then
return 400, {error_msg = "missing sub-path"}
end

if not conf then
return 400, {error_msg = "missing new configuration"}
end

if type(conf) ~= "table" then
return 400, {error_msg = "invalid configuration"}
end

local key = "/services" .. "/" .. id
local res_old, err = core.etcd.get(key)
if not res_old then
Expand All @@ -204,32 +204,9 @@ function _M.patch(id, conf, sub_path)
core.json.delay_encode(res_old, true))

local new_value = res_old.body.node.value
local sub_value = new_value
local sub_paths = core.utils.split_uri(sub_path)
for i = 1, #sub_paths - 1 do
local sub_name = sub_paths[i]
if sub_value[sub_name] == nil then
sub_value[sub_name] = {}
end

sub_value = sub_value[sub_name]
new_value = core.table.merge(new_value, conf);

if type(sub_value) ~= "table" then
return 400, "invalid sub-path: /"
.. core.table.concat(sub_paths, 1, i)
end
end

if type(sub_value) ~= "table" then
return 400, "invalid sub-path: /" .. sub_path
end

local sub_name = sub_paths[#sub_paths]
if sub_name and sub_name ~= "" then
sub_value[sub_name] = conf
else
new_value = conf
end
core.log.info("new value ", core.json.delay_encode(new_value, true))

local id, err = check_conf(id, new_value, true)
Expand Down
35 changes: 6 additions & 29 deletions apisix/admin/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,19 @@ function _M.delete(id)
end


function _M.patch(id, conf, sub_path)
function _M.patch(id, conf)
if not id then
return 400, {error_msg = "missing upstream id"}
end

if not sub_path then
return 400, {error_msg = "missing sub-path"}
end

if not conf then
return 400, {error_msg = "missing new configuration"}
end

if type(conf) ~= "table" then
return 400, {error_msg = "invalid configuration"}
end

local key = "/upstreams" .. "/" .. id
local res_old, err = core.etcd.get(key)
if not res_old then
Expand All @@ -240,32 +240,9 @@ function _M.patch(id, conf, sub_path)
core.json.delay_encode(res_old, true))

local new_value = res_old.body.node.value
local sub_value = new_value
local sub_paths = core.utils.split_uri(sub_path)
for i = 1, #sub_paths - 1 do
local sub_name = sub_paths[i]
if sub_value[sub_name] == nil then
sub_value[sub_name] = {}
end

sub_value = sub_value[sub_name]
new_value = core.table.merge(new_value, conf);

if type(sub_value) ~= "table" then
return 400, "invalid sub-path: /"
.. core.table.concat(sub_paths, 1, i)
end
end

if type(sub_value) ~= "table" then
return 400, "invalid sub-path: /" .. sub_path
end

local sub_name = sub_paths[#sub_paths]
if sub_name and sub_name ~= "" then
sub_value[sub_name] = conf
else
new_value = conf
end
core.log.info("new value ", core.json.delay_encode(new_value, true))

local id, err = check_conf(id, new_value, true)
Expand Down
19 changes: 19 additions & 0 deletions apisix/core/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,24 @@ local function deepcopy(orig)
end
_M.deepcopy = deepcopy

local ngx_null = ngx.null
local function merge(origin, extend)
for k,v in pairs(extend) do
if type(v) == "table" then
if type(origin[k] or false) == "table" then
merge(origin[k] or {}, extend[k] or {})
else
origin[k] = v
end
elseif v == ngx_null then
origin[k] = nil
else
origin[k] = v
end
end

return origin
end
_M.merge = merge

return _M
6 changes: 3 additions & 3 deletions doc/admin-api-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
|PUT |/apisix/admin/routes/{id}|{...}|根据 id 创建资源|
|POST |/apisix/admin/routes |{...}|创建资源,id 由后台服务自动生成|
|DELETE |/apisix/admin/routes/{id}||删除资源|
|PATCH |/apisix/admin/routes/{id}/{path}|{...}|修改已有 Route 的部分内容,其他不涉及部分会原样保留|
|PATCH |/apisix/admin/routes/{id}|{...}|修改已有 Route 的部分内容,其他不涉及部分会原样保留;如果你要删除某个属性,将该属性的值设置为null 即可删除|

> uri 请求参数:
Expand Down Expand Up @@ -187,7 +187,7 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f13
|PUT |/apisix/admin/services/{id}|{...}|根据 id 创建资源|
|POST |/apisix/admin/services |{...}|创建资源,id 由后台服务自动生成|
|DELETE |/apisix/admin/services/{id}||删除资源|
|PATCH |/apisix/admin/services/{id}/{path}|{...}|修改已有 Service 的部分内容,其他不涉及部分会原样保留|
|PATCH |/apisix/admin/services/{id}|{...}|修改已有 Service 的部分内容,其他不涉及部分会原样保留;如果你要删除某个属性,将该属性的值设置为null 即可删除|

> body 请求参数:
Expand Down Expand Up @@ -336,7 +336,7 @@ Date: Thu, 26 Dec 2019 08:17:49 GMT
|PUT |/apisix/admin/upstreams/{id}|{...}|根据 id 创建资源|
|POST |/apisix/admin/upstreams |{...}|创建资源,id 由后台服务自动生成|
|DELETE |/apisix/admin/upstreams/{id}||删除资源|
|PATCH |/apisix/admin/upstreams/{id}/{path}|{...}|修改已有 Route 的部分内容,其他不涉及部分会原样保留|
|PATCH |/apisix/admin/upstreams/{id}|{...}|修改已有 Route 的部分内容,其他不涉及部分会原样保留;如果你要删除某个属性,将该属性的值设置为null 即可删除|

> body 请求参数:
Expand Down
6 changes: 3 additions & 3 deletions doc/admin-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
|PUT |/apisix/admin/routes/{id}|{...}|Create resource by ID|
|POST |/apisix/admin/routes |{...}|Create resource, and ID is generated by server|
|DELETE |/apisix/admin/routes/{id}|NULL|Remove resource|
|PATCH |/apisix/admin/routes/{id}/{path}|{...}|Update targeted content|
|PATCH |/apisix/admin/routes/{id}|{...}|Update targeted content, if you want to remove an attribute, set the attribute value to null to remove|

> URI Request Parameters:
Expand Down Expand Up @@ -183,7 +183,7 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f13
|PUT |/apisix/admin/services/{id}|{...}|Create resource by ID|
|POST |/apisix/admin/services |{...}|Create resource, and ID is generated by server|
|DELETE |/apisix/admin/services/{id}|NULL|Remove resource|
|PATCH |/apisix/admin/routes/{id}/{path}|{...}|Update targeted content|
|PATCH |/apisix/admin/routes/{id}|{...}|Update targeted content, if you want to remove an attribute, set the attribute value to null to remove|

> Request Body Parameters:
Expand Down Expand Up @@ -328,7 +328,7 @@ Return response from etcd currently.
|PUT |/apisix/admin/upstreams/{id}|{...}|Create resource by ID|
|POST |/apisix/admin/upstreams |{...}|Create resource, and ID is generated by server|
|DELETE |/apisix/admin/upstreams/{id}|NULL|Remove resource|
|PATCH |/apisix/admin/upstreams/{id}/{path}|{...}|Update targeted content|
|PATCH |/apisix/admin/upstreams/{id}|{...}|Update targeted content, if you want to remove an attribute, set the attribute value to null to remove|

> Request Body Parameters:
Expand Down
5 changes: 3 additions & 2 deletions t/admin/global-rules.t
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,17 @@ passed
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/global_rules/1/plugins',
local code, body = t('/apisix/admin/global_rules/1',
ngx.HTTP_PATCH,
[[{
"plugins": {
"limit-count": {
"count": 3,
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr"
}
}]],
}}]],
[[{
"node": {
"value": {
Expand Down
Loading

0 comments on commit 1f1cc89

Please sign in to comment.