Skip to content

Commit

Permalink
Merge pull request Kong#198 from Mashape/feat/cascade-delete
Browse files Browse the repository at this point in the history
[feature] cascade delete of plugins_configurations. fix Kong#107
  • Loading branch information
thibaultcha committed May 6, 2015
2 parents 6632e02 + d99654b commit d6b6198
Show file tree
Hide file tree
Showing 7 changed files with 384 additions and 107 deletions.
33 changes: 33 additions & 0 deletions kong/dao/cassandra/apis.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local BaseDao = require "kong.dao.cassandra.base_dao"
local constants = require "kong.constants"
local PluginsConfigurations = require "kong.dao.cassandra.plugins_configurations"

local SCHEMA = {
id = { type = constants.DATABASE_TYPES.ID },
Expand Down Expand Up @@ -50,4 +51,36 @@ function Apis:new(properties)
Apis.super.new(self, properties)
end

-- @override
function Apis:delete(api_id)
local ok, err = Apis.super.delete(self, api_id)
if not ok then
return err
end

-- delete all related plugins configurations
local plugins_dao = PluginsConfigurations(self._properties)
local query, args_keys, errors = plugins_dao:_build_where_query(plugins_dao._queries.select.query, {
api_id = api_id
})
if errors then
return nil, errors
end

for _, rows, page, err in plugins_dao:_execute_kong_query({query=query, args_keys=args_keys}, {api_id=api_id}, {auto_paging=true}) do
if err then
return nil, err
end

for _, row in ipairs(rows) do
local ok_del_plugin, err = plugins_dao:delete(row.id)
if not ok_del_plugin then
return nil, err
end
end
end

return ok
end

return Apis
62 changes: 39 additions & 23 deletions kong/dao/cassandra/base_dao.lua
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,33 @@ local function encode_cassandra_args(schema, t, args_keys)
return args_to_bind, errors
end

function BaseDao:_build_where_query(query, t)
local args_keys = {}
local where_str = ""
local errors

-- if t is an args_keys, compute a WHERE statement
if t and utils.table_size(t) > 0 then
local where = {}
for k, v in pairs(t) do
if self._schema[k] and self._schema[k].queryable or k == "id" then
table.insert(where, string.format("%s = ?", k))
table.insert(args_keys, k)
else
errors = utils.add_error(errors, k, k.." is not queryable.")
end
end

if errors then
return nil, nil, DaoError(errors, error_types.SCHEMA)
end

where_str = "WHERE "..table.concat(where, " AND ").." ALLOW FILTERING"
end

return string.format(query, where_str), args_keys
end

-- Get a statement from the cache or prepare it (and thus insert it in the cache).
-- The cache key will be the plain string query representation.
-- @param `kong_query` A kong query from the _queries property.
Expand Down Expand Up @@ -269,6 +296,14 @@ function BaseDao:_execute(statement, args, options, keyspace)
return nil, err
end

if options and options.auto_paging then
local _, rows, page, err = session:execute(statement, args, options)
for i, row in ipairs(rows) do
rows[i] = self:_unmarshall(row)
end
return _, rows, page, err
end

local results, err = session:execute(statement, args, options)
if err then
err = DaoError(err, error_types.DATABASE)
Expand Down Expand Up @@ -523,31 +558,12 @@ end
-- @param `paging_state` Start page from given offset. See lua-resty-cassandra's :execute() option.
-- @return _execute_kong_query()
function BaseDao:find_by_keys(t, page_size, paging_state)
local where, keys = {}, {}
local where_str = ""
local errors

-- if keys are passed, compute a WHERE statement
if t and utils.table_size(t) > 0 then
for k,v in pairs(t) do
if self._schema[k] and self._schema[k].queryable or k == "id" then
table.insert(where, string.format("%s = ?", k))
table.insert(keys, k)
else
errors = utils.add_error(errors, k, k.." is not queryable.")
end
end

if errors then
return nil, DaoError(errors, error_types.SCHEMA)
end

where_str = "WHERE "..table.concat(where, " AND ").." ALLOW FILTERING"
local select_where_query, args_keys, errors = self:_build_where_query(self._queries.select.query, t)
if errors then
return nil, errors
end

local select_query = string.format(self._queries.select.query, where_str)

return self:_execute_kong_query({ query = select_query, args_keys = keys }, t, {
return self:_execute_kong_query({ query = select_where_query, args_keys = args_keys }, t, {
page_size = page_size,
paging_state = paging_state
})
Expand Down
35 changes: 34 additions & 1 deletion kong/dao/cassandra/consumers.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local BaseDao = require "kong.dao.cassandra.base_dao"
local constants = require "kong.constants"
local stringy = require "stringy"
local constants = require "kong.constants"
local PluginsConfigurations = require "kong.dao.cassandra.plugins_configurations"

local function check_custom_id_and_username(value, consumer_t)
if (consumer_t.custom_id == nil or stringy.strip(consumer_t.custom_id) == "")
Expand Down Expand Up @@ -56,4 +57,36 @@ function Consumers:new(properties)
Consumers.super.new(self, properties)
end

-- @override
function Consumers:delete(consumer_id)
local ok, err = Consumers.super.delete(self, consumer_id)
if not ok then
return err
end

-- delete all related plugins configurations
local plugins_dao = PluginsConfigurations(self._properties)
local query, args_keys, errors = plugins_dao:_build_where_query(plugins_dao._queries.select.query, {
consumer_id = consumer_id
})
if errors then
return nil, errors
end

for _, rows, page, err in plugins_dao:_execute_kong_query({query=query, args_keys=args_keys}, {consumer_id=consumer_id}, {auto_paging=true}) do
if err then
return nil, err
end

for _, row in ipairs(rows) do
local ok_del_plugin, err = plugins_dao:delete(row.id)
if not ok_del_plugin then
return nil, err
end
end
end

return ok
end

return Consumers
2 changes: 1 addition & 1 deletion kong/dao/cassandra/plugins_configurations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function PluginsConfigurations:find_distinct()
end

local result = {}
for k,_ in pairs(distinct_names) do
for k, _ in pairs(distinct_names) do
table.insert(result, k)
end

Expand Down
8 changes: 4 additions & 4 deletions kong/tools/faker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Faker.FIXTURES = {
{ name = "keyauth", value = { key_names = { "apikey" }}, __api = 1 },
{ name = "tcplog", value = { host = "127.0.0.1", port = 7777 }, __api = 1 },
{ name = "udplog", value = { host = "127.0.0.1", port = 8888 }, __api = 1 },
{ name = "filelog", value = { }, __api = 1 },
{ name = "filelog", value = {}, __api = 1 },
-- API 2
{ name = "basicauth", value = {}, __api = 2 },
-- API 3
Expand All @@ -73,9 +73,9 @@ Faker.FIXTURES = {
-- API 6
{ name = "cors", value = {}, __api = 6 },
-- API 7
{ name = "cors", value = { origin = "example.com",
methods = "GET",
headers = "origin, type, accepts",
{ name = "cors", value = { origin = "example.com",
methods = "GET",
headers = "origin, type, accepts",
exposed_headers = "x-auth-token",
max_age = 23,
credentials = true }, __api = 7 }
Expand Down
Loading

0 comments on commit d6b6198

Please sign in to comment.