forked from Kong/kong
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(admin api) support CRUD on all core entities
Former-commit-id: 450f55d90c2247fab8bdf654525377b9cc4dc0c8
- Loading branch information
1 parent
6f9c97c
commit 7db809e
Showing
7 changed files
with
354 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
local responses = require "kong.tools.responses" | ||
local app_helpers = require "lapis.application" | ||
|
||
local _M = {} | ||
|
||
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) or nil | ||
|
||
self.params.size = nil | ||
self.params.offset = nil | ||
|
||
local data, err = dao_collection:find_by_keys(self.params, size, offset) | ||
if err then | ||
return app_helpers.yield_error(err) | ||
end | ||
|
||
local next_url | ||
if data.next_page then | ||
next_url = self:build_url(self.req.parsed_url.path, { | ||
port = self.req.parsed_url.port, | ||
query = ngx.encode_args({ | ||
offset = ngx.encode_base64(data.next_page), | ||
size = size | ||
}) | ||
}) | ||
data.next_page = nil | ||
end | ||
|
||
-- This check is required otherwise the response is going to be a | ||
-- JSON Object and not a JSON array. The reason is because an empty Lua array `{}` | ||
-- will not be translated as an empty array by cjson, but as an empty object. | ||
local result = #data == 0 and "{\"data\":[]}" or {data=data, ["next"]=next_url} | ||
|
||
return responses.send_HTTP_OK(result, type(result) ~= "table") | ||
end | ||
|
||
function _M.put(self, dao_collection) | ||
local new_entity, err | ||
if self.params.id then | ||
new_entity, err = dao_collection:update(self.params) | ||
if not err then | ||
return responses.send_HTTP_OK(new_entity) | ||
end | ||
else | ||
new_entity, err = dao_collection:insert(self.params) | ||
if not err then | ||
return responses.send_HTTP_CREATED(new_entity) | ||
end | ||
end | ||
|
||
if err then | ||
return app_helpers.yield_error(err) | ||
end | ||
end | ||
|
||
function _M.post(self, dao_collection) | ||
local data, err = dao_collection:insert(self.params) | ||
if err then | ||
return app_helpers.yield_error(err) | ||
else | ||
return responses.send_HTTP_CREATED(data) | ||
end | ||
end | ||
|
||
function _M.patch(params, dao_collection) | ||
local new_entity, err = dao_collection:update(params) | ||
if err then | ||
return app_helpers.yield_error(err) | ||
else | ||
return responses.send_HTTP_OK(new_entity) | ||
end | ||
end | ||
|
||
function _M.delete(entity_id, dao_collection) | ||
local ok, err = dao_collection:delete(entity_id) | ||
if not ok then | ||
if err then | ||
return app_helpers.yield_error(err) | ||
else | ||
return responses.send_HTTP_NOT_FOUND() | ||
end | ||
else | ||
return responses.send_HTTP_NO_CONTENT() | ||
end | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,73 @@ | ||
local validations = require("kong.dao.schemas") | ||
local validations = require "kong.dao.schemas" | ||
local crud = require "kong.api.crud_helpers" | ||
|
||
local function find_by_name_or_id(self, dao_factory, helpers) | ||
local fetch_keys = { | ||
[validations.is_valid_uuid(self.params.name_or_id) and "id" or "name"] = self.params.name_or_id | ||
} | ||
self.params.name_or_id = nil | ||
|
||
-- TODO: make the base_dao more flexible so we can query find_one with key/values | ||
-- https://github.com/Mashape/kong/issues/103 | ||
local data, err = dao_factory.apis:find_by_keys(fetch_keys) | ||
if err then | ||
return helpers.yield_error(err) | ||
end | ||
|
||
self.api = data[1] | ||
if not self.api then | ||
return helpers.responses.send_HTTP_NOT_FOUND() | ||
end | ||
end | ||
|
||
return { | ||
["/apis/"] = { | ||
GET = function(self, dao_factory, helpers) | ||
helpers.return_paginated_set(self, dao_factory.apis) | ||
GET = function(self, dao_factory) | ||
crud.paginated_set(self, dao_factory.apis) | ||
end, | ||
|
||
PUT = function(self, dao_factory, helpers) | ||
local new_api, err | ||
if self.params.id then | ||
new_api, err = dao_factory.apis:update(self.params) | ||
if not err then | ||
return helpers.responses.send_HTTP_OK(new_api) | ||
end | ||
else | ||
new_api, err = dao_factory.apis:insert(self.params) | ||
if not err then | ||
return helpers.responses.send_HTTP_CREATED(new_api) | ||
end | ||
end | ||
|
||
if err then | ||
return helpers.yield_error(err) | ||
end | ||
PUT = function(self, dao_factory) | ||
crud.put(self, dao_factory.apis) | ||
end, | ||
|
||
POST = function(self, dao_factory, helpers) | ||
local data, err = dao_factory.apis:insert(self.params) | ||
if err then | ||
return helpers.yield_error(err) | ||
else | ||
return helpers.responses.send_HTTP_CREATED(data) | ||
end | ||
POST = function(self, dao_factory) | ||
crud.post(self, dao_factory.apis) | ||
end | ||
}, | ||
|
||
["/apis/:name_or_id"] = { | ||
before = function(self, dao_factory, helpers) | ||
local fetch_keys = { | ||
[validations.is_valid_uuid(self.params.name_or_id) and "id" or "name"] = self.params.name_or_id | ||
} | ||
self.params.name_or_id = nil | ||
|
||
-- TODO: make the base_dao more flexible so we can query find_one with key/values | ||
-- https://github.com/Mashape/kong/issues/103 | ||
local data, err = dao_factory.apis:find_by_keys(fetch_keys) | ||
if err then | ||
return helpers.yield_error(err) | ||
end | ||
|
||
self.api = data[1] | ||
if not self.api then | ||
return helpers.responses.send_HTTP_NOT_FOUND() | ||
end | ||
end, | ||
before = find_by_name_or_id, | ||
|
||
GET = function(self, dao_factory, helpers) | ||
return helpers.responses.send_HTTP_OK(self.api) | ||
end, | ||
|
||
PATCH = function(self, dao_factory, helpers) | ||
PATCH = function(self, dao_factory) | ||
self.params.id = self.api.id | ||
crud.patch(self.params, dao_factory.apis) | ||
end, | ||
|
||
local new_api, err = dao_factory.apis:update(self.params) | ||
if err then | ||
return helpers.yield_error(err) | ||
else | ||
return helpers.responses.send_HTTP_OK(new_api) | ||
end | ||
DELETE = function(self, dao_factory) | ||
crud.delete(self.api.id, dao_factory.apis) | ||
end | ||
}, | ||
|
||
["/apis/:name_or_id/plugins/"] = { | ||
before = function(self, dao_factory, helpers) | ||
find_by_name_or_id(self, dao_factory, helpers) | ||
self.params.api_id = self.api.id | ||
end, | ||
|
||
GET = function(self, dao_factory) | ||
crud.paginated_set(self, dao_factory.plugins_configurations) | ||
end, | ||
|
||
POST = function(self, dao_factory, helpers) | ||
crud.post(self, dao_factory.plugins_configurations) | ||
end, | ||
|
||
PUT = function(self, dao_factory, helpers) | ||
crud.put(self, dao_factory.plugins_configurations) | ||
end | ||
} | ||
} | ||
|
Oops, something went wrong.