feature: supported PATCH method for service API. (#370)

* feature: supported `PATCH` method for `service` API.
This commit is contained in:
YuanSheng Wang 2019-08-05 12:51:21 +08:00 committed by GitHub
parent ae0bdfb6d8
commit 4850f91e82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 198 additions and 1 deletions

View File

@ -8,7 +8,7 @@ local type = type
local _M = {
version = 0.2,
version = 0.3,
}
@ -152,4 +152,75 @@ function _M.delete(id)
end
function _M.patch(id, conf, sub_path)
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
local key = "/services" .. "/" .. id
local res_old, err = core.etcd.get(key)
if not res_old then
core.log.error("failed to delete service[", key, "]: ", err)
return 500, {error_msg = err}
end
if res_old.status ~= 200 then
return res_old.status, res_old.body
end
core.log.info("key: ", key, " old value: ",
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]
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)
if not id then
return 400, err
end
-- TODO: this is not safe, we need to use compare-set
local res, err = core.etcd.set(key, new_value)
if not res then
core.log.error("failed to set new service[", key, "]: ", err)
return 500, {error_msg = err}
end
return res.status, res.body
end
return _M

View File

@ -609,3 +609,129 @@ GET /t
{"error_msg":"wrong service id, do not need it"}
--- no_error_log
[error]
=== TEST 19: patch service(whole)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/services/1/',
ngx.HTTP_PATCH,
[[{
"upstream": {
"nodes": {
"127.0.0.1:8080": 1
},
"type": "roundrobin"
},
"desc": "new 20 service"
}]],
[[{
"node": {
"value": {
"upstream": {
"nodes": {
"127.0.0.1:8080": 1
},
"type": "roundrobin"
},
"desc": "new 20 service"
},
"key": "/apisix/services/1"
},
"action": "set"
}]]
)
ngx.status = code
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 20: patch service(new desc)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/services/1/desc',
ngx.HTTP_PATCH,
'"new 19 service"',
[[{
"node": {
"value": {
"upstream": {
"nodes": {
"127.0.0.1:8080": 1
},
"type": "roundrobin"
},
"desc": "new 19 service"
},
"key": "/apisix/services/1"
},
"action": "set"
}]]
)
ngx.status = code
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 21: patch service(new nodes)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/services/1/upstream',
ngx.HTTP_PATCH,
[[{
"nodes": {
"127.0.0.1:8081": 3,
"127.0.0.1:8082": 4
},
"type": "roundrobin"
}]],
[[{
"node": {
"value": {
"upstream": {
"nodes": {
"127.0.0.1:8081": 3,
"127.0.0.1:8082": 4
},
"type": "roundrobin"
}
}
}
}]]
)
ngx.status = code
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]