Skip to content

Commit

Permalink
feat(admin) add /consumers/:id/plugins routes
Browse files Browse the repository at this point in the history
This PR needs #2726 or the api route tests will fail.

Closes #2336
  • Loading branch information
kikito committed Jul 24, 2017
1 parent dbe04cd commit d47d1a5
Show file tree
Hide file tree
Showing 6 changed files with 528 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
- rate-limiting/response-ratelimiting: Optionally hide informative response
headers.
[#2087](https://github.com/Mashape/kong/pull/2087)
- New endpoints `/consumers/:username_or_id/plugins` and
`/consumers/:username_or_id/plugins/:id`
[#2714](https://github.com/Mashape/kong/pull/2714)
- The endpoint `/apis/:api_name_or_id/plugins/:plugin_name_or_id` now accepts
the plugin name as well for the last parameter.
[#2252](https://github.com/Mashape/kong/pull/2252)
Expand Down
18 changes: 6 additions & 12 deletions kong/api/crud_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,18 @@ function _M.find_api_by_name_or_id(self, dao_factory, helpers)
end
end

-- this function will lookup a plugin by name or id, but REQUIRES
-- also the api to be specified by name or id
function _M.find_plugin_by_name_or_id(self, dao_factory, helpers)
_M.find_api_by_name_or_id(self, dao_factory, helpers)

local rows, err = _M.find_by_id_or_field(dao_factory.plugins, { api_id = self.api.id },
self.params.plugin_name_or_id, "name")
function _M.find_plugin_by_id(self, dao_factory, filter, helpers)
filter = filter or {}
filter.id = self.params.id

local rows, err = dao_factory.plugins:find_all(filter)
if err then
return helpers.yield_error(err)
elseif not rows[1] then
return helpers.responses.send_HTTP_NOT_FOUND()
end
self.params.plugin_name_or_id = nil

-- We know combi of api+plugin is unique for plugins, hence if we have a row, it must be the only one
self.plugin = rows[1]
if not self.plugin then
return helpers.responses.send_HTTP_NOT_FOUND()
end
end

function _M.find_consumer_by_username_or_id(self, dao_factory, helpers)
Expand Down
5 changes: 4 additions & 1 deletion kong/api/routes/apis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ return {

["/apis/:api_name_or_id/plugins/:plugin_name_or_id"] = {
before = function(self, dao_factory, helpers)
crud.find_plugin_by_name_or_id(self, dao_factory, helpers)
crud.find_api_by_name_or_id(self, dao_factory, helpers)

crud.find_plugin_by_name_or_id(self, dao_factory,
{ api_id = self.api.id }, helpers)
end,

GET = function(self, dao_factory, helpers)
Expand Down
42 changes: 41 additions & 1 deletion kong/api/routes/consumers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,45 @@ return {
DELETE = function(self, dao_factory)
crud.delete(self.consumer, dao_factory.consumers)
end
}
},

["/consumers/:username_or_id/plugins/"] = {
before = function(self, dao_factory, helpers)
self.params.username_or_id = ngx.unescape_uri(self.params.username_or_id)
crud.find_consumer_by_username_or_id(self, dao_factory, helpers)
self.params.consumer_id = self.consumer.id
end,

GET = function(self, dao_factory)
crud.paginated_set(self, dao_factory.plugins)
end,

POST = function(self, dao_factory)
crud.post(self.params, dao_factory.plugins)
end,

PUT = function(self, dao_factory)
crud.put(self.params, dao_factory.plugins)
end
},

["/consumers/:username_or_id/plugins/:id"] = {
before = function(self, dao_factory, helpers)
self.params.username_or_id = ngx.unescape_uri(self.params.username_or_id)
crud.find_consumer_by_username_or_id(self, dao_factory, helpers)
crud.find_plugin_by_id(self, dao_factory, {consumer_id = self.consumer.id}, helpers)
end,

GET = function(self, dao_factory, helpers)
return helpers.responses.send_HTTP_OK(self.plugin)
end,

PATCH = function(self, dao_factory)
crud.patch(self.params, dao_factory.plugins, self.plugin)
end,

DELETE = function(self, dao_factory)
crud.delete(self.plugin, dao_factory.plugins)
end
},
}
Loading

0 comments on commit d47d1a5

Please sign in to comment.