Skip to content

Commit

Permalink
Merge 8118ae5 into 0034c66
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-chen committed Jul 30, 2020
2 parents 0034c66 + 8118ae5 commit 08c1a91
Show file tree
Hide file tree
Showing 11 changed files with 1,041 additions and 120 deletions.
19 changes: 15 additions & 4 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 @@ -106,7 +107,7 @@ function _M.delete(id)
end


function _M.patch(id, conf)
function _M.patch(id, conf, sub_path)
if not id then
return 400, {error_msg = "missing global rule id"}
end
Expand All @@ -115,8 +116,10 @@ function _M.patch(id, conf)
return 400, {error_msg = "missing new configuration"}
end

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

local key = "/global_rules/" .. id
Expand All @@ -134,7 +137,15 @@ function _M.patch(id, conf)

local node_value = res_old.body.node.value

node_value = core.table.merge(node_value, conf);
if sub_path and sub_path ~= "" then
local code, err, node_val = table_util.patch(node_value, sub_path, conf);
node_value = node_val
if code then
return code, err
end
else
node_value = core.table.merge(node_value, conf);
end

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

Expand Down
20 changes: 15 additions & 5 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 @@ -196,7 +197,7 @@ function _M.delete(id)
end


function _M.patch(id, conf, args)
function _M.patch(id, conf, sub_path, args)
if not id then
return 400, {error_msg = "missing route id"}
end
Expand All @@ -205,8 +206,10 @@ function _M.patch(id, conf, args)
return 400, {error_msg = "missing new configuration"}
end

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

local key = "/routes"
Expand All @@ -226,10 +229,17 @@ function _M.patch(id, conf, args)
core.log.info("key: ", key, " old value: ",
core.json.delay_encode(res_old, true))


local node_value = res_old.body.node.value

node_value = core.table.merge(node_value, conf);
if sub_path and sub_path ~= "" then
local code, err, node_val = table_util.patch(node_value, sub_path, conf);
node_value = node_val
if code then
return code, err
end
else
node_value = core.table.merge(node_value, conf);
end

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

Expand Down
27 changes: 19 additions & 8 deletions apisix/admin/services.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ local upstreams = require("apisix.admin.upstreams")
local tostring = tostring
local ipairs = ipairs
local type = type
local table_util = require("apisix.utils.table-util")


local _M = {
Expand Down Expand Up @@ -177,7 +178,7 @@ function _M.delete(id)
end


function _M.patch(id, conf)
function _M.patch(id, conf, sub_path)
if not id then
return 400, {error_msg = "missing service id"}
end
Expand All @@ -186,8 +187,10 @@ function _M.patch(id, conf)
return 400, {error_msg = "missing new configuration"}
end

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

local key = "/services" .. "/" .. id
Expand All @@ -203,19 +206,27 @@ function _M.patch(id, conf)
core.log.info("key: ", key, " old value: ",
core.json.delay_encode(res_old, true))

local new_value = res_old.body.node.value
local node_value = res_old.body.node.value

new_value = core.table.merge(new_value, conf);
if sub_path and sub_path ~= "" then
local code, err, node_val = table_util.patch(node_value, sub_path, conf);
node_value = node_val
if code then
return code, err
end
else
node_value = core.table.merge(node_value, conf);
end

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

local id, err = check_conf(id, new_value, true)
local id, err = check_conf(id, node_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)
local res, err = core.etcd.set(key, node_value)
if not res then
core.log.error("failed to set new service[", key, "]: ", err)
return 500, {error_msg = err}
Expand Down
19 changes: 15 additions & 4 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 @@ -211,7 +212,7 @@ function _M.delete(id)
end


function _M.patch(id, conf)
function _M.patch(id, conf, sub_path)
if not id then
return 400, {error_msg = "missing upstream id"}
end
Expand All @@ -220,8 +221,10 @@ function _M.patch(id, conf)
return 400, {error_msg = "missing new configuration"}
end

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

local key = "/upstreams" .. "/" .. id
Expand All @@ -239,7 +242,15 @@ function _M.patch(id, conf)

local new_value = res_old.body.node.value

new_value = core.table.merge(new_value, conf);
if sub_path and sub_path ~= "" then
local code, err, node_val = table_util.patch(new_value, sub_path, conf);
new_value = node_val
if code then
return code, err
end
else
new_value = core.table.merge(new_value, conf);
end

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

Expand Down
58 changes: 58 additions & 0 deletions apisix/utils/table-util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--
-- 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 type = type
local core = require("apisix.core")
local table = core.utils.table
local utils = core.utils

local _M = {}


local function patch(node_value, sub_path, conf)
local sub_value = node_value
local sub_paths = 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: /"
.. 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

return nil, nil, node_value
end
_M.patch = patch


return _M

0 comments on commit 08c1a91

Please sign in to comment.