Skip to content

Commit

Permalink
feat(admin) targets can be deleted by their ID
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed Mar 30, 2017
1 parent 476d6ab commit e05f20f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
19 changes: 19 additions & 0 deletions kong/api/crud_helpers.lua
Expand Up @@ -59,6 +59,25 @@ function _M.find_upstream_by_name_or_id(self, dao_factory, helpers)
end
end

function _M.find_target_by_target_or_id(self, dao_factory, helpers)
local filter_keys = {
upstream_id = self.upstream.id,
[utils.is_valid_uuid(self.params.target_or_id) and "id" or "target"] = self.params.target_or_id
}
self.params.target_or_id = nil

local rows, err = dao_factory.targets:find_all(filter_keys)
if err then
return helpers.yield_error(err)
end

-- We know target and id are unique, so if we have a row, it must be the only one
self.target = rows[1]
if not self.target then
return helpers.responses.send_HTTP_NOT_FOUND()
end
end

function _M.paginated_set(self, dao_collection)
local size = self.params.size and tonumber(self.params.size) or 100
local offset = self.params.offset and ngx.decode_base64(self.params.offset)
Expand Down
5 changes: 3 additions & 2 deletions kong/api/routes/upstreams.lua
Expand Up @@ -167,17 +167,18 @@ return {
end
},

["/upstreams/:name_or_id/targets/:target"] = {
["/upstreams/:name_or_id/targets/:target_or_id"] = {
before = function(self, dao_factory, helpers)
crud.find_upstream_by_name_or_id(self, dao_factory, helpers)
crud.find_target_by_target_or_id(self, dao_factory, helpers)
end,

DELETE = function(self, dao_factory)
clean_history(self.upstream.id, dao_factory)

-- this is just a wrapper around POSTing a new target with weight=0
local data, err = dao_factory.targets:insert({
target = self.params.target,
target = self.target.target,
upstream_id = self.upstream.id,
weight = 0,
})
Expand Down
29 changes: 28 additions & 1 deletion spec/02-integration/03-admin_api/09-targets_routes_spec.lua
Expand Up @@ -345,7 +345,7 @@ describe("Admin API", function()
})
end)

it("acts as a sugar method to POST a target with 0 weight", function()
it("acts as a sugar method to POST a target with 0 weight (by target)", function()
local res = assert(client:send {
method = "DELETE",
path = "/upstreams/" .. upstream_name4 .. "/targets/" .. target.target
Expand All @@ -371,6 +371,33 @@ describe("Admin API", function()
assert.equal(1, json.total)
assert.equal("api-1:80", json.data[1].target)
end)

it("acts as a sugar method to POST a target with 0 weight (by id)", function()
local res = assert(client:send {
method = "DELETE",
path = "/upstreams/" .. upstream_name4 .. "/targets/" .. target.id
})
assert.response(res).has.status(204)

local targets = assert(client:send {
method = "GET",
path = "/upstreams/" .. upstream_name4 .. "/targets/",
})
assert.response(targets).has.status(200)
local json = assert.response(targets).has.jsonbody()
assert.equal(3, #json.data)
assert.equal(3, json.total)

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

0 comments on commit e05f20f

Please sign in to comment.