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

feat(plugin config): add dependency for delete plugin config #6092

Merged
merged 8 commits into from
Jan 18, 2022
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
20 changes: 20 additions & 0 deletions apisix/admin/plugin_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
-- limitations under the License.
--
local core = require("apisix.core")
local get_routes = require("apisix.router").http_routes
local utils = require("apisix.admin.utils")
local schema_plugin = require("apisix.admin.plugins").check_schema
local type = type
local tostring = tostring
local ipairs = ipairs


local _M = {
Expand Down Expand Up @@ -100,13 +102,31 @@ end


function _M.delete(id)
if not id then
return 400, {error_msg = "missing plugin config id"}
end

local routes, routes_ver = get_routes()
if routes_ver and routes then
for _, route in ipairs(routes) do
if type(route) == "table" and route.value
and route.value.plugin_config_id
and tostring(route.value.plugin_config_id) == id then
return 400, {error_msg = "can not delete this plugin config,"
.. " route [" .. route.value.id
.. "] is still using it now"}
end
end
end

local key = "/plugin_configs/" .. id
local res, err = core.etcd.delete(key)
if not res then
core.log.error("failed to delete plugin config[", key, "]: ", err)
return 503, {error_msg = err}
end


return res.status, res.body
end

Expand Down
121 changes: 121 additions & 0 deletions t/admin/plugin-configs.t
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,124 @@ passed
--- response_body
{"error_msg":"invalid configuration: property \"labels\" validation failed: wrong type: expected object, got string"}
--- error_code: 400



=== TEST 10: set plugin-configs(id: 1)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local etcd = require("apisix.core.etcd")
local code, body = t('/apisix/admin/plugin_configs/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr"
}
}
}]]
)

ngx.status = code
ngx.say(body)

local res = assert(etcd.get('/plugin_configs/1'))
local create_time = res.body.node.value.create_time
assert(create_time ~= nil, "create_time is nil")
local update_time = res.body.node.value.update_time
assert(update_time ~= nil, "update_time is nil")
}
}
--- response_body
passed



=== TEST 11: set route(id: 1)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugin_config_id": 1,
"uri": "/index.html",
"upstream": {
"nodes": {
"127.0.0.1:8080": 1
},
"type": "roundrobin"
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed



=== TEST 12: delete-plugin configs failed(id: 1)
--- config
location /t {
content_by_lua_block {
ngx.sleep(0.3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, is there any reason for which we need to add this 0.3-sec sleep?
Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current step depends on the previous steps, so set a short time for etcd to sync data.

local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/plugin_configs/1',
ngx.HTTP_DELETE,
nil,
[[{"action": "delete"}]]
)
ngx.print(body)
}
}
--- response_body
{"error_msg":"can not delete this plugin config, route [1] is still using it now"}



=== TEST 13: delete route(id: 1)
--- config
location /t {
content_by_lua_block {
ngx.sleep(0.3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_DELETE,
nil,
[[{"action": "delete"}]]
)
ngx.say(body)
}
}
--- response_body
passed



=== TEST 14: delete plugin-configs(id: 1)
--- config
location /t {
content_by_lua_block {
ngx.sleep(0.3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/plugin_configs/1',
ngx.HTTP_DELETE,
nil,
[[{"action": "delete"}]]
)
ngx.say(body)
}
}
--- response_body
passed