Skip to content

Commit

Permalink
Merge pull request Kong#2230 from Mashape/feat/active-targets
Browse files Browse the repository at this point in the history
feat(admin) get a list of active targets per upstream
  • Loading branch information
p0pr0ck5 authored Mar 24, 2017
2 parents 3c7e76f + dc8cc25 commit 3bc8726
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
53 changes: 53 additions & 0 deletions kong/api/routes/upstreams.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local crud = require "kong.api.crud_helpers"
local app_helpers = require "lapis.application"
local responses = require "kong.tools.responses"

return {
["/upstreams/"] = {
Expand Down Expand Up @@ -107,4 +109,55 @@ return {
crud.post(self.params, dao_factory.targets)
end,
},

["/upstreams/:name_or_id/targets/active/"] = {
before = function(self, dao_factory, helpers)
crud.find_upstream_by_name_or_id(self, dao_factory, helpers)
self.params.upstream_id = self.upstream.id
end,

GET = function(self, dao_factory)
self.params.active = nil

local target_history, err = dao_factory.targets:find_all({
upstream_id = self.params.upstream_id,
})
if not target_history then
return app_helpers.yield_error(err)
end

--sort and walk based on target and creation time
for _, target in ipairs(target_history) do
target.order = target.target .. ":" ..
target.created_at .. ":" ..target.id
end
table.sort(target_history, function(a, b) return a.order > b.order end)

local ignored = {}
local found = {}
local found_n = 0

for _, entry in ipairs(target_history) do
if not found[entry.target] and not ignored[entry.target] then
if entry.weight ~= 0 then
entry.order = nil -- dont show our order key to the client
found_n = found_n + 1
found[found_n] = entry

else
ignored[entry.target] = true
end
end
end

-- for now lets not worry about rolling our own pagination
-- we also end up returning a "backwards" list of targets because
-- of how we sorted- do we care?
return responses.send_HTTP_OK {
total = found_n,
data = found,
}
end
},

}
61 changes: 61 additions & 0 deletions spec/02-integration/03-admin_api/09-targets_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,65 @@ describe("Admin API", function()
end)
end)
end)

describe("/upstreams/{upstream}/targets/active/", function()
describe("only shows active targets", function()
local upstream_name3 = "example.com"

before_each(function()
local upstream3 = assert(helpers.dao.upstreams:insert {
name = upstream_name3,
})

-- two target inserts, resulting in a 'down' target
assert(helpers.dao.targets:insert {
target = "api-1:80",
weight = 10,
upstream_id = upstream3.id,
})
assert(helpers.dao.targets:insert {
target = "api-1:80",
weight = 0,
upstream_id = upstream3.id,
})

-- three target inserts, resulting in an 'up' target
assert(helpers.dao.targets:insert {
target = "api-2:80",
weight = 10,
upstream_id = upstream3.id,
})
assert(helpers.dao.targets:insert {
target = "api-2:80",
weight = 0,
upstream_id = upstream3.id,
})
assert(helpers.dao.targets:insert {
target = "api-2:80",
weight = 10,
upstream_id = upstream3.id,
})

-- and an insert of a separate active target
assert(helpers.dao.targets:insert {
target = "api-3:80",
weight = 10,
upstream_id = upstream3.id,
})
end)

it("only shows active targets", function()
local res = assert(client:send {
method = "GET",
path = "/upstreams/" .. upstream_name3 .. "/targets/active/",
})
assert.response(res).has.status(200)
local json = assert.response(res).has.jsonbody()
assert.equal(2, #json.data)
assert.equal(2, json.total)
assert.equal("api-3:80", json.data[1].target)
assert.equal("api-2:80", json.data[2].target)
end)
end)
end)
end)

0 comments on commit 3bc8726

Please sign in to comment.