Skip to content

Commit

Permalink
Revert "feat(dao) use cache_key for target uniqueness detection" (#…
Browse files Browse the repository at this point in the history
…8705)

This reverts commit 9eba2a1.
  • Loading branch information
hutchic committed Apr 20, 2022
1 parent a05cc4c commit 579537b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 130 deletions.
5 changes: 0 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@

### Additions

#### Core

- Added `cache_key` on target entity for uniqueness detection.
[#8179](https://github.com/Kong/kong/pull/8179)

#### Plugins

- **Zipkin**: add support for including HTTP path in span name
Expand Down
23 changes: 23 additions & 0 deletions kong/api/routes/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ local function target_endpoint(self, db, callback)
end


local function update_existent_target(self, db)
local upstream = endpoints.select_entity(self, db, db.upstreams.schema)
local filter = { target = unescape_uri(self.params.target) }
local opts = endpoints.extract_options(self.args.uri, db.targets.schema, "select")
local target = db.targets:select_by_upstream_filter(upstream, filter, opts)

if target then
self.params.targets = db.targets.schema:extract_pk_values(target)
return endpoints.update_entity(self, db, db.targets.schema)
end

return nil
end


return {
["/upstreams/:upstreams/health"] = {
GET = function(self, db)
Expand Down Expand Up @@ -166,6 +181,14 @@ return {
"upstream",
"page_for_upstream"),
PUT = function(self, db)
local entity, _, err_t = update_existent_target(self, db)
if err_t then
return endpoints.handle_error(err_t)
end
if entity then
return kong.response.exit(200, entity, { ["Deprecation"] = "true" })
end

local create = endpoints.post_collection_endpoint(kong.db.targets.schema,
kong.db.upstreams.schema, "upstream")
return create(self, db)
Expand Down
9 changes: 9 additions & 0 deletions kong/db/dao/targets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ function _TARGETS:insert(entity, options)
entity.target = formatted_target
end

local workspace = workspaces.get_workspace_id()
local opts = { nulls = true, workspace = workspace }
for existent in self:each_for_upstream(entity.upstream, nil, opts) do
if existent.target == entity.target then
local err_t = self.errors:unique_violation({ target = existent.target })
return nil, tostring(err_t), err_t
end
end

return self.super.insert(self, entity, options)
end

Expand Down
120 changes: 0 additions & 120 deletions kong/db/migrations/core/016_280_to_300.lua

This file was deleted.

1 change: 0 additions & 1 deletion kong/db/migrations/core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ return {
"013_220_to_230",
"014_230_to_270",
"015_270_to_280",
"016_280_to_300"
}
1 change: 0 additions & 1 deletion kong/db/schema/entities/targets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ return {
name = "targets",
dao = "kong.db.dao.targets",
primary_key = { "id" },
cache_key = { "upstream", "target" },
endpoint_key = "target",
workspaceable = true,
fields = {
Expand Down
11 changes: 8 additions & 3 deletions spec/02-integration/04-admin_api/08-targets_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe("Admin API #" .. strategy, function()
end
end)

it_content_types("refuses to create duplicated targets", function(content_type)
it_content_types("updates and does not create duplicated targets (#deprecated)", function(content_type)
return function()
local upstream = bp.upstreams:insert { slots = 10 }
local res = assert(client:send {
Expand All @@ -159,9 +159,10 @@ describe("Admin API #" .. strategy, function()
assert.equal("single-target.test:8080", json.target)
assert.is_number(json.created_at)
assert.is_string(json.id)
local id = json.id
assert.are.equal(1, json.weight)

local res2 = assert(client:send {
local res = assert(client:send {
method = "PUT",
path = "/upstreams/" .. upstream.name .. "/targets/",
body = {
Expand All @@ -170,7 +171,11 @@ describe("Admin API #" .. strategy, function()
},
headers = {["Content-Type"] = content_type}
})
assert.response(res2).has.status(409)
local body = assert.response(res).has.status(200)
local json = cjson.decode(body)
assert.are.equal(100, json.weight)
assert.are.equal(id, json.id)
assert.equal("true", res.headers["Deprecation"])
end
end)

Expand Down

0 comments on commit 579537b

Please sign in to comment.