Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions apisix/resource.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,21 @@ end

function _M.get_nodes_ver(resource_path)
local res_conf = fetch_latest_conf(resource_path)
if not res_conf then
return nil
end

local upstream = res_conf.value.upstream or res_conf.value
return upstream._nodes_ver
end


function _M.set_nodes_ver_and_nodes(resource_path, nodes_ver, nodes)
local res_conf = fetch_latest_conf(resource_path)
if not res_conf then
return
end

local upstream = res_conf.value.upstream or res_conf.value
upstream._nodes_ver = nodes_ver
upstream.nodes = nodes
Expand Down
61 changes: 61 additions & 0 deletions t/core/resource.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#
# 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.
#
use t::APISIX 'no_plan';

run_tests;

__DATA__

=== TEST 1: get_nodes_ver returns nil when the resource does not exist
--- config
location /t {
content_by_lua_block {
local resource = require("apisix.resource")
ngx.say(resource.get_nodes_ver("/apisix/upstreams/missing-resource"))
}
}
--- request
GET /t
--- response_body
nil
--- error_log
resource not found: missing-resource in /upstreams
--- no_error_log
[error]
[alert]



=== TEST 2: set_nodes_ver_and_nodes does not fail when the resource does not exist
--- config
location /t {
content_by_lua_block {
local resource = require("apisix.resource")
resource.set_nodes_ver_and_nodes("/apisix/upstreams/missing-resource", 1,
{["127.0.0.1:1980"] = 1})
ngx.say("passed")
}
}
--- request
GET /t
--- response_body
passed
--- error_log
resource not found: missing-resource in /upstreams
--- no_error_log
[error]
[alert]
Loading