Skip to content

Commit

Permalink
Merge pull request Kong#302 from Mashape/feature/dao-loading
Browse files Browse the repository at this point in the history
[feature] Plugin DAOs

Former-commit-id: 5a2fbe518dd9cd489770b8320733d050ca61c113
  • Loading branch information
thibaultcha committed Jun 9, 2015
2 parents f6bc5b5 + fa745a2 commit 1d62e58
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 241 deletions.
10 changes: 5 additions & 5 deletions kong-0.3.2-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,24 @@ build = {
["kong.dao.schemas.plugins_configurations"] = "kong/dao/schemas/plugins_configurations.lua",
["kong.dao.cassandra.factory"] = "kong/dao/cassandra/factory.lua",
["kong.dao.cassandra.base_dao"] = "kong/dao/cassandra/base_dao.lua",
["kong.dao.cassandra.migrations"] = "kong/dao/cassandra/migrations.lua",
["kong.dao.cassandra.apis"] = "kong/dao/cassandra/apis.lua",
["kong.dao.cassandra.consumers"] = "kong/dao/cassandra/consumers.lua",
["kong.dao.cassandra.plugins_configurations"] = "kong/dao/cassandra/plugins_configurations.lua",
["kong.dao.cassandra.migrations"] = "kong/dao/cassandra/migrations.lua",
["kong.dao.cassandra.ratelimiting_metrics"] = "kong/dao/cassandra/ratelimiting_metrics.lua",
["kong.dao.cassandra.basicauth_credentials"] = "kong/dao/cassandra/basicauth_credentials.lua",
["kong.dao.cassandra.keyauth_credentials"] = "kong/dao/cassandra/keyauth_credentials.lua",

["kong.plugins.base_plugin"] = "kong/plugins/base_plugin.lua",

["kong.plugins.basicauth.handler"] = "kong/plugins/basicauth/handler.lua",
["kong.plugins.basicauth.access"] = "kong/plugins/basicauth/access.lua",
["kong.plugins.basicauth.schema"] = "kong/plugins/basicauth/schema.lua",
["kong.plugins.basicauth.api"] = "kong/plugins/basicauth/api.lua",
["kong.plugins.basicauth.daos"] = "kong/plugins/basicauth/daos.lua",

["kong.plugins.keyauth.handler"] = "kong/plugins/keyauth/handler.lua",
["kong.plugins.keyauth.access"] = "kong/plugins/keyauth/access.lua",
["kong.plugins.keyauth.schema"] = "kong/plugins/keyauth/schema.lua",
["kong.plugins.keyauth.api"] = "kong/plugins/keyauth/api.lua",
["kong.plugins.keyauth.daos"] = "kong/plugins/keyauth/daos.lua",

["kong.plugins.tcplog.handler"] = "kong/plugins/tcplog/handler.lua",
["kong.plugins.tcplog.log"] = "kong/plugins/tcplog/log.lua",
Expand All @@ -122,7 +121,8 @@ build = {
["kong.plugins.ratelimiting.handler"] = "kong/plugins/ratelimiting/handler.lua",
["kong.plugins.ratelimiting.access"] = "kong/plugins/ratelimiting/access.lua",
["kong.plugins.ratelimiting.schema"] = "kong/plugins/ratelimiting/schema.lua",

["kong.plugins.ratelimiting.daos"] = "kong/plugins/ratelimiting/daos.lua",

["kong.plugins.requestsizelimiting.handler"] = "kong/plugins/requestsizelimiting/handler.lua",
["kong.plugins.requestsizelimiting.access"] = "kong/plugins/requestsizelimiting/access.lua",
["kong.plugins.requestsizelimiting.schema"] = "kong/plugins/requestsizelimiting/schema.lua",
Expand Down
2 changes: 1 addition & 1 deletion kong/api/crud_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function _M.post(params, dao_collection, success)
if err then
return app_helpers.yield_error(err)
else
if success then success(utils.table_copy(data)) end
if success then success(utils.deep_copy(data)) end
return responses.send_HTTP_CREATED(data)
end
end
Expand Down
4 changes: 2 additions & 2 deletions kong/cli/utils/signal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,16 @@ end
-- @param args_config Path to the desired configuration (usually from the --config CLI argument)
local function prepare_database(args_config)
local _, _, dao_factory = get_kong_config(args_config)
local migrations = require("kong.tools.migrations")(dao_factory, cutils.get_luarocks_install_dir())

-- Migrate the DB if needed and possible
local keyspace, err = dao_factory.migrations:get_migrations()
local keyspace, err = migrations:get_migrations()
if err then
cutils.logger:error_exit(err)
elseif keyspace == nil then
cutils.logger:info("Database not initialized. Running migrations...")
end

local migrations = require("kong.tools.migrations")(dao_factory, cutils.get_luarocks_install_dir())
migrations:migrate(function(migration, err)
if err then
cutils.logger:error_exit(err)
Expand Down
8 changes: 4 additions & 4 deletions kong/dao/cassandra/apis.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local BaseDao = require "kong.dao.cassandra.base_dao"
local apis_schema = require "kong.dao.schemas.apis"
local PluginsConfigurations = require "kong.dao.cassandra.plugins_configurations"

local Apis = BaseDao:extend()

Expand Down Expand Up @@ -41,7 +40,8 @@ function Apis:new(properties)
args_keys = { "public_dns" },
query = [[ SELECT id FROM apis WHERE public_dns = ?; ]]
}
}
},
drop = "TRUNCATE apis;"
}

Apis.super.new(self, properties)
Expand Down Expand Up @@ -70,7 +70,7 @@ function Apis:delete(api_id)
end

-- delete all related plugins configurations
local plugins_dao = PluginsConfigurations(self._properties)
local plugins_dao = self._factory.plugins_configurations
local query, args_keys, errors = plugins_dao:_build_where_query(plugins_dao._queries.select.query, {
api_id = api_id
})
Expand All @@ -94,4 +94,4 @@ function Apis:delete(api_id)
return ok
end

return Apis
return { apis = Apis }
6 changes: 6 additions & 0 deletions kong/dao/cassandra/base_dao.lua
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,10 @@ function BaseDao:delete(id)
return self:_execute_kong_query(self._queries.delete, { id = id })
end

function BaseDao:drop()
if self._queries.drop then
return self:_execute_kong_query(self._queries.drop)
end
end

return BaseDao
8 changes: 4 additions & 4 deletions kong/dao/cassandra/consumers.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local BaseDao = require "kong.dao.cassandra.base_dao"
local consumers_schema = require "kong.dao.schemas.consumers"
local PluginsConfigurations = require "kong.dao.cassandra.plugins_configurations"

local Consumers = BaseDao:extend()

Expand Down Expand Up @@ -36,7 +35,8 @@ function Consumers:new(properties)
args_keys = { "username" },
query = [[ SELECT id FROM consumers WHERE username = ?; ]]
}
}
},
drop = "TRUNCATE consumers;"
}

Consumers.super.new(self, properties)
Expand All @@ -50,7 +50,7 @@ function Consumers:delete(consumer_id)
end

-- delete all related plugins configurations
local plugins_dao = PluginsConfigurations(self._properties)
local plugins_dao = self._factory.plugins_configurations
local query, args_keys, errors = plugins_dao:_build_where_query(plugins_dao._queries.select.query, {
consumer_id = consumer_id
})
Expand All @@ -74,4 +74,4 @@ function Consumers:delete(consumer_id)
return ok
end

return Consumers
return { consumers = Consumers }
81 changes: 48 additions & 33 deletions kong/dao/cassandra/factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,62 @@ local cassandra = require "cassandra"
local DaoError = require "kong.dao.error"
local stringy = require "stringy"
local Object = require "classic"

local Apis = require "kong.dao.cassandra.apis"
local Consumers = require "kong.dao.cassandra.consumers"
local PluginsConfigurations = require "kong.dao.cassandra.plugins_configurations"
local Migrations = require "kong.dao.cassandra.migrations"
local BasicAuthCredentials = require "kong.dao.cassandra.basicauth_credentials"
local RateLimitingMetrics = require "kong.dao.cassandra.ratelimiting_metrics"
local KeyAuthCredentials = require "kong.dao.cassandra.keyauth_credentials"
local utils = require "kong.tools.utils"

local CassandraFactory = Object:extend()

-- Instanciate a Cassandra DAO.
-- @param properties Cassandra properties
function CassandraFactory:new(properties)
self.type = "cassandra"
-- Shorthand for accessing one of the underlying DAOs
function CassandraFactory:__index(key)
if key ~= "daos" and self.daos and self.daos[key] then
return self.daos[key]
else
return CassandraFactory[key]
end
end

-- Instanciate a Cassandra Factory and all its DAOs for various entities
-- @param `properties` Cassandra properties
function CassandraFactory:new(properties, plugins)
self._properties = properties
self.type = "cassandra"
self.daos = {}

self.apis = Apis(properties)
self.consumers = Consumers(properties)
self.plugins_configurations = PluginsConfigurations(properties)
self.basicauth_credentials = BasicAuthCredentials(properties)
self.ratelimiting_metrics = RateLimitingMetrics(properties)
self.keyauth_credentials = KeyAuthCredentials(properties)
-- Load core entities DAOs
for _, entity in ipairs({"apis", "consumers", "plugins_configurations"}) do
self:load_daos(require("kong.dao.cassandra."..entity))
end

self.migrations = Migrations(properties)
-- Load plugins DAOs
if plugins then
for _, v in ipairs(plugins) do
local loaded, plugin_daos_mod = utils.load_module_if_exists("kong.plugins."..v..".daos")
if loaded then
if ngx then
ngx.log(ngx.DEBUG, "Loading DAO for plugin: "..v)
end
self:load_daos(plugin_daos_mod)
elseif ngx then
ngx.log(ngx.DEBUG, "No DAO loaded for plugin: "..v)
end
end
end
end

function CassandraFactory:load_daos(plugin_daos)
for name, plugin_dao in pairs(plugin_daos) do
self.daos[name] = plugin_dao(self._properties)
self.daos[name]._factory = self
end
end

function CassandraFactory:drop()
return self:execute_queries [[
TRUNCATE apis;
TRUNCATE consumers;
TRUNCATE plugins_configurations;
TRUNCATE basicauth_credentials;
TRUNCATE keyauth_credentials;
TRUNCATE ratelimiting_metrics;
]]
local err
for _, dao in pairs(self.daos) do
err = select(2, dao:drop())
if err then
return err
end
end
end

-- Prepare all statements of collections `._queries` property and put them
Expand Down Expand Up @@ -72,12 +92,7 @@ function CassandraFactory:prepare()
end
end

for _, collection in ipairs({ self.apis,
self.consumers,
self.plugins_configurations,
self.ratelimiting_metrics,
self.basicauth_credentials,
self.keyauth_credentials }) do
for _, collection in pairs(self.daos) do
local status, err = pcall(function() prepare_collection(collection) end)
if not status then
return err
Expand Down
2 changes: 1 addition & 1 deletion kong/dao/cassandra/migrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ function Migrations:delete_migration(migration_name)
{ cassandra.list({ migration_name }) })
end

return Migrations
return { migrations = Migrations }
5 changes: 3 additions & 2 deletions kong/dao/cassandra/plugins_configurations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ function PluginsConfigurations:new(properties)
args_keys = { "consumer_id" },
query = [[ SELECT id FROM consumers WHERE id = ?; ]]
}
}
},
drop = "TRUNCATE plugins_configurations;"
}

PluginsConfigurations.super.new(self, properties)
Expand Down Expand Up @@ -108,4 +109,4 @@ function PluginsConfigurations:find_distinct()
return result, nil
end

return PluginsConfigurations
return { plugins_configurations = PluginsConfigurations }
Loading

0 comments on commit 1d62e58

Please sign in to comment.