Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(admin) targets can be deleted by their ID #2304

Merged
merged 2 commits into from
Mar 31, 2017
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
23 changes: 23 additions & 0 deletions kong/api/crud_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ function _M.find_upstream_by_name_or_id(self, dao_factory, helpers)
end
end

-- this function will return the exact target if specified by `id`, or just
-- 'any target entry' if specified by target (= 'hostname:port')
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

-- if looked up by `target` property we can have multiple targets here, but
-- anyone will do as they all have the same 'target' field, so just pick
-- the first
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
Original file line number Diff line number Diff line change
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,
Copy link
Member Author

@subnetmarco subnetmarco Mar 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The target field name is confusing with the target entity, should be really called address ("Targets can have an address and a weight" as opposed to "Targets can have a target and a weight")

@Tieske

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a target object and a target field. Sounds like we need a breaking API change then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not worth it imo

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(also out of scope for the PR ;) )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this PR of course - just a consideration that it's a confusing naming.

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
Original file line number Diff line number Diff line change
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)