Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance PATCH method, allow to update partial data. #1609

Merged
merged 5 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 7 additions & 29 deletions apisix/admin/global_rules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local core = require("apisix.core")
local schema_plugin = require("apisix.admin.plugins").check_schema
local type = type
local tostring = tostring
local table_util = require("apisix.utils.table-util")


local _M = {
Expand Down Expand Up @@ -104,19 +105,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 +132,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 = table_util.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
37 changes: 8 additions & 29 deletions apisix/admin/routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local upstreams = require("apisix.admin.upstreams")
local tostring = tostring
local type = type
local loadstring = loadstring
local table_util = require("apisix.utils.table-util")


local _M = {
Expand Down Expand Up @@ -194,19 +195,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 +225,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 = table_util.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
36 changes: 7 additions & 29 deletions apisix/admin/services.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ local tostring = tostring
local ipairs = ipairs
local tonumber = tonumber
local type = type
local table_util = require("apisix.utils.table-util")


local _M = {
Expand Down Expand Up @@ -177,19 +178,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 +205,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 = table_util.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
36 changes: 7 additions & 29 deletions apisix/admin/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local get_services = require("apisix.http.service").services
local tostring = tostring
local ipairs = ipairs
local type = type
local table_util = require("apisix.utils.table-util")


local _M = {
Expand Down Expand Up @@ -213,19 +214,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 +241,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 = table_util.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
43 changes: 43 additions & 0 deletions apisix/utils/table-util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local ngx = ngx
local pairs = pairs
local type = type

local _M = {}

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
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
origin[k] = nil
else
origin[k] = v
end
end

return origin
end

_M.merge = merge
nic-chen marked this conversation as resolved.
Show resolved Hide resolved

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