Skip to content

Commit b4dd909

Browse files
authored
fix(api) upsert of targets now require the PUT HTTP method
1 parent 6983215 commit b4dd909

File tree

9 files changed

+73
-68
lines changed

9 files changed

+73
-68
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
execution, avoiding unnecessary concurrent executions.
8686
[#8567](https://github.com/Kong/kong/pull/8567)
8787

88-
8988
#### Plugins
9089

9190
- **ACME**: `auth_method` default value is set to `token`
@@ -106,6 +105,11 @@
106105
- The cluster listener now uses the value of `admin_error_log` for its log file
107106
instead of `proxy_error_log` [8583](https://github.com/Kong/kong/pull/8583)
108107

108+
#### Admin API
109+
110+
- Insert and update operations on target entities require using the `PUT` HTTP
111+
method now. [#8596](https://github.com/Kong/kong/pull/8596)
112+
109113
### Additions
110114

111115
#### Performance

kong/api/routes/upstreams.lua

+14-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ local tostring = tostring
1010
local fmt = string.format
1111

1212

13-
local function post_health(self, db, is_healthy)
13+
local function set_target_health(self, db, is_healthy)
1414
local upstream, _, err_t = endpoints.select_entity(self, db, db.upstreams.schema)
1515
if err_t then
1616
return endpoints.handle_error(err_t)
@@ -180,9 +180,7 @@ return {
180180
kong.db.upstreams.schema,
181181
"upstream",
182182
"page_for_upstream"),
183-
POST = function(self, db)
184-
-- updating a target using POST is a compatibility with existent API and
185-
-- should be deprecated in next major version
183+
PUT = function(self, db)
186184
local entity, _, err_t = update_existent_target(self, db)
187185
if err_t then
188186
return endpoints.handle_error(err_t)
@@ -194,7 +192,10 @@ return {
194192
local create = endpoints.post_collection_endpoint(kong.db.targets.schema,
195193
kong.db.upstreams.schema, "upstream")
196194
return create(self, db)
197-
end
195+
end,
196+
POST = function(self, db)
197+
return kong.response.exit(405)
198+
end,
198199
},
199200

200201
["/upstreams/:upstreams/targets/all"] = {
@@ -232,26 +233,26 @@ return {
232233
},
233234

234235
["/upstreams/:upstreams/targets/:targets/healthy"] = {
235-
POST = function(self, db)
236-
return post_health(self, db, true)
236+
PUT = function(self, db)
237+
return set_target_health(self, db, true)
237238
end,
238239
},
239240

240241
["/upstreams/:upstreams/targets/:targets/unhealthy"] = {
241-
POST = function(self, db)
242-
return post_health(self, db, false)
242+
PUT = function(self, db)
243+
return set_target_health(self, db, false)
243244
end,
244245
},
245246

246247
["/upstreams/:upstreams/targets/:targets/:address/healthy"] = {
247-
POST = function(self, db)
248-
return post_health(self, db, true)
248+
PUT = function(self, db)
249+
return set_target_health(self, db, true)
249250
end,
250251
},
251252

252253
["/upstreams/:upstreams/targets/:targets/:address/unhealthy"] = {
253-
POST = function(self, db)
254-
return post_health(self, db, false)
254+
PUT = function(self, db)
255+
return set_target_health(self, db, false)
255256
end,
256257
},
257258

spec/02-integration/04-admin_api/07-upstreams_routes_spec.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ describe("Admin API: #" .. strategy, function()
723723

724724
-- create the target
725725
local res = assert(client:send {
726-
method = "POST",
726+
method = "PUT",
727727
path = "/upstreams/my-upstream/targets",
728728
body = {
729729
target = "127.0.0.1:8000",
@@ -791,7 +791,7 @@ describe("Admin API: #" .. strategy, function()
791791

792792
-- create the target
793793
local res = assert(client:send {
794-
method = "POST",
794+
method = "PUT",
795795
path = "/upstreams/my-upstream/targets",
796796
body = {
797797
target = "127.0.0.1:8000",

spec/02-integration/04-admin_api/08-targets_routes_spec.lua

+18-18
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ describe("Admin API #" .. strategy, function()
7474
end)
7575

7676
describe("/upstreams/{upstream}/targets/", function()
77-
describe("POST", function()
77+
describe("PUT", function()
7878
it_content_types("creates a target with defaults", function(content_type)
7979
return function()
8080
local upstream = bp.upstreams:insert { slots = 10 }
8181
local res = assert(client:send {
82-
method = "POST",
82+
method = "PUT",
8383
path = "/upstreams/" .. upstream.name .. "/targets/",
8484
body = {
8585
target = "mashape.com",
@@ -98,7 +98,7 @@ describe("Admin API #" .. strategy, function()
9898
return function()
9999
local upstream = bp.upstreams:insert { slots = 10 }
100100
local res = assert(client:send {
101-
method = "POST",
101+
method = "PUT",
102102
path = "/upstreams/" .. upstream.name .. "/targets/",
103103
body = {
104104
target = "mashape.com:123",
@@ -119,7 +119,7 @@ describe("Admin API #" .. strategy, function()
119119
return function()
120120
local upstream = bp.upstreams:insert { slots = 10 }
121121
local res = assert(client:send {
122-
method = "POST",
122+
method = "PUT",
123123
path = "/upstreams/" .. upstream.name .. "/targets/",
124124
body = {
125125
target = "zero.weight.test:8080",
@@ -146,7 +146,7 @@ describe("Admin API #" .. strategy, function()
146146
return function()
147147
local upstream = bp.upstreams:insert { slots = 10 }
148148
local res = assert(client:send {
149-
method = "POST",
149+
method = "PUT",
150150
path = "/upstreams/" .. upstream.name .. "/targets/",
151151
body = {
152152
target = "single-target.test:8080",
@@ -163,7 +163,7 @@ describe("Admin API #" .. strategy, function()
163163
assert.are.equal(1, json.weight)
164164

165165
local res = assert(client:send {
166-
method = "POST",
166+
method = "PUT",
167167
path = "/upstreams/" .. upstream.name .. "/targets/",
168168
body = {
169169
target = "single-target.test:8080",
@@ -183,7 +183,7 @@ describe("Admin API #" .. strategy, function()
183183
it("handles malformed JSON body", function()
184184
local upstream = bp.upstreams:insert { slots = 10 }
185185
local res = assert(client:request {
186-
method = "POST",
186+
method = "PUT",
187187
path = "/upstreams/" .. upstream.name .. "/targets/",
188188
body = '{"hello": "world"',
189189
headers = {["Content-Type"] = "application/json"}
@@ -197,7 +197,7 @@ describe("Admin API #" .. strategy, function()
197197
local upstream = bp.upstreams:insert { slots = 10 }
198198
-- Missing parameter
199199
local res = assert(client:send {
200-
method = "POST",
200+
method = "PUT",
201201
path = "/upstreams/" .. upstream.name .. "/targets/",
202202
body = {
203203
weight = weight_min,
@@ -211,7 +211,7 @@ describe("Admin API #" .. strategy, function()
211211

212212
-- Invalid target parameter
213213
res = assert(client:send {
214-
method = "POST",
214+
method = "PUT",
215215
path = "/upstreams/" .. upstream.name .. "/targets/",
216216
body = {
217217
target = "some invalid host name",
@@ -225,7 +225,7 @@ describe("Admin API #" .. strategy, function()
225225

226226
-- Invalid weight parameter
227227
res = assert(client:send {
228-
method = "POST",
228+
method = "PUT",
229229
path = "/upstreams/" .. upstream.name .. "/targets/",
230230
body = {
231231
target = "mashape.com",
@@ -240,7 +240,7 @@ describe("Admin API #" .. strategy, function()
240240
end
241241
end)
242242

243-
for _, method in ipairs({"PUT", "PATCH", "DELETE"}) do
243+
for _, method in ipairs({"POST", "PATCH", "DELETE"}) do
244244
it_content_types("returns 405 on " .. method, function(content_type)
245245
return function()
246246
local upstream = bp.upstreams:insert { slots = 10 }
@@ -337,7 +337,7 @@ describe("Admin API #" .. strategy, function()
337337

338338
for i = 1, #weights do
339339
local status, body = client_send({
340-
method = "POST",
340+
method = "PUT",
341341
path = "/upstreams/" .. upstream.name .. "/targets",
342342
headers = {
343343
["Content-Type"] = "application/json",
@@ -668,7 +668,7 @@ describe("Admin API #" .. strategy, function()
668668
local json = assert(cjson.decode(body))
669669

670670
status, body = assert(client_send({
671-
method = "POST",
671+
method = "PUT",
672672
path = "/upstreams/" .. upstream.id .. "/targets",
673673
headers = {["Content-Type"] = "application/json"},
674674
body = {
@@ -691,7 +691,7 @@ describe("Admin API #" .. strategy, function()
691691
local expected = (i >= 3 and j >= 4) and 204 or 404
692692
local path = "/upstreams/" .. u .. "/targets/" .. t .. "/" .. e
693693
local status = assert(client_send {
694-
method = "POST",
694+
method = "PUT",
695695
path = "/upstreams/" .. u .. "/targets/" .. t .. "/" .. e
696696
})
697697
assert.same(expected, status, "bad status for path " .. path)
@@ -703,7 +703,7 @@ describe("Admin API #" .. strategy, function()
703703
it("flips the target status from UNHEALTHY to HEALTHY", function()
704704
local status, body, json
705705
status, body = assert(client_send {
706-
method = "POST",
706+
method = "PUT",
707707
path = target_path .. "/unhealthy"
708708
})
709709
assert.same(204, status, body)
@@ -716,7 +716,7 @@ describe("Admin API #" .. strategy, function()
716716
assert.same(target.target, json.data[1].target)
717717
assert.same("UNHEALTHY", json.data[1].health)
718718
status = assert(client_send {
719-
method = "POST",
719+
method = "PUT",
720720
path = target_path .. "/healthy"
721721
})
722722
assert.same(204, status)
@@ -733,7 +733,7 @@ describe("Admin API #" .. strategy, function()
733733
it("flips the target status from HEALTHY to UNHEALTHY", function()
734734
local status, body, json
735735
status = assert(client_send {
736-
method = "POST",
736+
method = "PUT",
737737
path = target_path .. "/healthy"
738738
})
739739
assert.same(204, status)
@@ -746,7 +746,7 @@ describe("Admin API #" .. strategy, function()
746746
assert.same(target.target, json.data[1].target)
747747
assert.same("HEALTHY", json.data[1].health)
748748
status = assert(client_send {
749-
method = "POST",
749+
method = "PUT",
750750
path = target_path .. "/unhealthy"
751751
})
752752
assert.same(204, status)

spec/02-integration/04-admin_api/15-off_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ describe("Admin API #off", function()
799799
assert.response(res).has.status(201)
800800

801801
local res = assert(client:send {
802-
method = "POST",
802+
method = "PUT",
803803
path = "/upstreams/foo/targets/c830b59e-59cc-5392-adfd-b414d13adfc4/10.20.30.40/unhealthy",
804804
})
805805

0 commit comments

Comments
 (0)