Skip to content

Commit

Permalink
feat(db): allow primary key passed as full entity to DAOs
Browse files Browse the repository at this point in the history
### Summary

Previously you needed to write code like this:
```lua
local route = kong.db.routes:select_by_name("my-route")
kong.db.routes:update({ id = route.id }, {
  paths = { "/test" }
})
kong.db.routes:delete({ id = route.id })
```

with this change you can write it like this:

```lua
local route = kong.db.routes:select_by_name("my-route")
kong.db.routes:update(route, {
  paths = { "/test" }
})
kong.db.routes:delete(route)
```

You can pass full entity to all the places that previously required the just the primary key.

Signed-off-by: Aapo Talvensaari <[email protected]>
  • Loading branch information
bungle committed Oct 19, 2023
1 parent 30d90f3 commit 74e421e
Show file tree
Hide file tree
Showing 33 changed files with 229 additions and 337 deletions.
2 changes: 1 addition & 1 deletion kong/clustering/control_plane.lua
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function _M:handle_cp_websocket()
local purge_delay = self.conf.cluster_data_plane_purge_delay
local update_sync_status = function()
local ok
ok, err = kong.db.clustering_data_planes:upsert({ id = dp_id, }, {
ok, err = kong.db.clustering_data_planes:upsert({ id = dp_id }, {
last_seen = last_seen,
config_hash = config_hash ~= "" and config_hash or nil,
hostname = dp_hostname,
Expand Down
4 changes: 2 additions & 2 deletions kong/db/dao/certificates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function _Certificates:insert(cert, options)
cert.snis = name_list or cjson.empty_array

if name_list then
local ok, err, err_t = self.db.snis:insert_list({ id = cert.id }, name_list, options)
local ok, err, err_t = self.db.snis:insert_list(cert, name_list, options)
if not ok then
return nil, err, err_t
end
Expand Down Expand Up @@ -196,7 +196,7 @@ function _Certificates:page(size, offset, options)

for i=1, #certs do
local cert = certs[i]
local snis, err, err_t = self.db.snis:list_for_certificate({ id = cert.id })
local snis, err, err_t = self.db.snis:list_for_certificate(cert)
if not snis then
return nil, err, err_t
end
Expand Down
16 changes: 12 additions & 4 deletions kong/db/dao/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -980,12 +980,14 @@ function DAO:select(primary_key, options)
validate_options_type(options)
end

local ok, errors = self.schema:validate_primary_key(primary_key)
local ok, errors = self.schema:validate_primary_key(primary_key, true)
if not ok then
local err_t = self.errors:invalid_primary_key(errors)
return nil, tostring(err_t), err_t
end

primary_key = self.schema:extract_pk_values(primary_key)

if options ~= nil then
ok, errors = validate_options_value(self, options)
if not ok then
Expand Down Expand Up @@ -1171,12 +1173,14 @@ function DAO:update(primary_key, entity, options)
validate_options_type(options)
end

local ok, errors = self.schema:validate_primary_key(primary_key)
local ok, errors = self.schema:validate_primary_key(primary_key, true)
if not ok then
local err_t = self.errors:invalid_primary_key(errors)
return nil, tostring(err_t), err_t
end

primary_key = self.schema:extract_pk_values(primary_key)

local entity_to_update, rbw_entity, err, err_t = check_update(self,
primary_key,
entity,
Expand Down Expand Up @@ -1223,12 +1227,14 @@ function DAO:upsert(primary_key, entity, options)
validate_options_type(options)
end

local ok, errors = self.schema:validate_primary_key(primary_key)
local ok, errors = self.schema:validate_primary_key(primary_key, true)
if not ok then
local err_t = self.errors:invalid_primary_key(errors)
return nil, tostring(err_t), err_t
end

primary_key = self.schema:extract_pk_values(primary_key)

local entity_to_upsert, rbw_entity, err, err_t = check_upsert(self,
primary_key,
entity,
Expand Down Expand Up @@ -1279,12 +1285,14 @@ function DAO:delete(primary_key, options)
validate_options_type(options)
end

local ok, errors = self.schema:validate_primary_key(primary_key)
local ok, errors = self.schema:validate_primary_key(primary_key, true)
if not ok then
local err_t = self.errors:invalid_primary_key(errors)
return nil, tostring(err_t), err_t
end

primary_key = self.schema:extract_pk_values(primary_key)

local show_ws_id = { show_ws_id = true }
local entity, err, err_t = self:select(primary_key, show_ws_id)
if err then
Expand Down
1 change: 1 addition & 0 deletions kong/db/dao/snis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ end
-- Creates one instance of SNI for each name in name_list
-- All created instances will be associated to the given certificate
function _SNIs:insert_list(cert_pk, name_list)
cert_pk = self.db.certificates.schema:extract_pk_values(cert_pk)
for _, name in ipairs(name_list) do
local _, err, err_t = self:insert({
name = name,
Expand Down
2 changes: 1 addition & 1 deletion kong/db/dao/targets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function _TARGETS:upsert(pk, entity, options)
if existent.target == entity.target then
-- if the upserting entity is newer, update
if entity.created_at > existent.created_at then
local ok, err, err_t = self.super.delete(self, { id = existent.id }, opts)
local ok, err, err_t = self.super.delete(self, existent, opts)
if ok then
return self.super.insert(self, entity, options)
end
Expand Down
2 changes: 1 addition & 1 deletion kong/pdk/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ local function new(self)
end

if utils.is_valid_uuid(consumer_id) then
local result, err = kong.db.consumers:select { id = consumer_id }
local result, err = kong.db.consumers:select({ id = consumer_id })

if result then
return result
Expand Down
14 changes: 5 additions & 9 deletions kong/plugins/acme/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ local function save_dao(host, key, cert)
})

if err then
local ok, err_2 = kong.db.certificates:delete({
id = cert_entity.id,
})
local ok, err_2 = kong.db.certificates:delete(cert_entity)
if not ok then
kong.log.warn("error cleaning up certificate entity ", cert_entity.id, ": ", err_2)
end
Expand All @@ -204,9 +202,7 @@ local function save_dao(host, key, cert)

if old_sni_entity and old_sni_entity.certificate then
local id = old_sni_entity.certificate.id
local ok, err = kong.db.certificates:delete({
id = id,
})
local ok, err = kong.db.certificates:delete({ id = id })
if not ok then
kong.log.warn("error deleting expired certificate entity ", id, ": ", err)
end
Expand All @@ -228,7 +224,7 @@ end

local function get_account_key(conf)
local kid = conf.key_id
local lookup = {kid = kid}
local lookup = { kid = kid }

if conf.key_set then
local key_set, key_set_err = kong.db.key_sets:select_by_name(conf.key_set)
Expand All @@ -243,7 +239,7 @@ local function get_account_key(conf)
return nil, error("nil returned by key_sets:select_by_name for key_set ", conf.key_set)
end

lookup.set = {id = key_set.id}
lookup.set = { id = key_set.id }
end

local cache_key = kong.db.keys:cache_key(lookup)
Expand Down Expand Up @@ -405,7 +401,7 @@ local function load_certkey(conf, host)
return nil, "DAO returns empty SNI entity or Certificte entity"
end

local cert_entity, err = kong.db.certificates:select({ id = sni_entity.certificate.id })
local cert_entity, err = kong.db.certificates:select(sni_entity.certificate)
if err then
kong.log.info("can't read certificate ", sni_entity.certificate.id, " from db",
", deleting renew config")
Expand Down
4 changes: 1 addition & 3 deletions kong/plugins/acme/storage/kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ function _M:delete(k)
return
end

local _, err = self.dao:delete({
id = v.id
})
local _, err = self.dao:delete(v)
return err
end

Expand Down
10 changes: 4 additions & 6 deletions kong/plugins/oauth2/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ local function generate_token(conf, service, credential, authenticated_userid,
local refresh_token
local token, err
if existing_token and conf.reuse_refresh_token then
token, err = kong.db.oauth2_tokens:update({
id = existing_token.id
}, {
token, err = kong.db.oauth2_tokens:update(existing_token, {
access_token = random_string(),
expires_in = token_expiration,
created_at = timestamp.get_utc() / 1000
Expand Down Expand Up @@ -676,7 +674,7 @@ local function issue_token(conf)
auth_code.scope, state)

-- Delete authorization code so it cannot be reused
kong.db.oauth2_authorization_codes:delete({ id = auth_code.id })
kong.db.oauth2_authorization_codes:delete(auth_code)
end
end

Expand Down Expand Up @@ -785,7 +783,7 @@ local function issue_token(conf)
token.scope, state, false, token)
-- Delete old token if refresh token not persisted
if not conf.reuse_refresh_token then
kong.db.oauth2_tokens:delete({ id = token.id })
kong.db.oauth2_tokens:delete(token)
end
end
end
Expand Down Expand Up @@ -894,7 +892,7 @@ end


local function load_oauth2_credential_into_memory(credential_id)
local result, err = kong.db.oauth2_credentials:select { id = credential_id }
local result, err = kong.db.oauth2_credentials:select({ id = credential_id })
if err then
return nil, err
end
Expand Down
8 changes: 2 additions & 6 deletions kong/plugins/proxy-cache/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ return {
resource = "proxy-cache",

GET = function(self)
local plugin, err = kong.db.plugins:select {
id = self.params.plugin_id,
}
local plugin, err = kong.db.plugins:select({ id = self.params.plugin_id })
if err then
return kong.response.exit(500, err)
end
Expand All @@ -156,9 +154,7 @@ return {
return kong.response.exit(200, cache_val)
end,
DELETE = function(self)
local plugin, err = kong.db.plugins:select {
id = self.params.plugin_id,
}
local plugin, err = kong.db.plugins:select({ id = self.params.plugin_id })
if err then
return kong.response.exit(500, err)
end
Expand Down
4 changes: 1 addition & 3 deletions kong/plugins/proxy-cache/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,7 @@ function ProxyCacheHandler:init_worker()
kong.log.err("handling purge of '", data, "'")

local plugin_id, cache_key = unpack(utils.split(data, ":"))
local plugin, err = kong.db.plugins:select({
id = plugin_id,
})
local plugin, err = kong.db.plugins:select({ id = plugin_id })
if err then
kong.log.err("error in retrieving plugins: ", err)
return
Expand Down
2 changes: 1 addition & 1 deletion kong/runloop/balancer/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ end
-- @param upstream_id string
-- @return the upstream table, or nil+error
local function load_upstream_into_memory(upstream_id)
local upstream, err = kong.db.upstreams:select({id = upstream_id}, GLOBAL_QUERY_OPTS)
local upstream, err = kong.db.upstreams:select({ id = upstream_id }, GLOBAL_QUERY_OPTS)
if not upstream then
return nil, err
end
Expand Down
Loading

0 comments on commit 74e421e

Please sign in to comment.