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

[feature] cascade delete of plugins_configurations. fix #107 #198

Merged
merged 2 commits into from
May 6, 2015
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
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