Skip to content

Commit 0bae345

Browse files
committed
feat(standalone): add duplicate resource check in update function
1 parent 994682a commit 0bae345

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

apisix/admin/standalone.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ local EVENT_UPDATE = "standalone-api-configuration-update"
3434

3535
local _M = {}
3636

37+
local function check_duplicate(item, key, id_set)
38+
local identifier, identifier_type
39+
if key == "consumer" then
40+
identifier = item.username
41+
identifier_type = "username"
42+
else
43+
identifier = item.id
44+
identifier_type = "id"
45+
end
46+
47+
if not identifier then
48+
return
49+
end
50+
51+
if id_set[identifier] then
52+
return "duplicate " .. identifier_type .. " found " .. identifier
53+
end
54+
id_set[identifier] = true
55+
end
3756

3857
local function get_config()
3958
local config = shared_dict:get("config")
@@ -142,6 +161,7 @@ local function update(ctx)
142161
apisix_yaml[key] = table_new(#items, 0)
143162
local item_schema = obj.item_schema
144163
local item_checker = obj.checker
164+
local id_set = {}
145165

146166
for index, item in ipairs(items) do
147167
local item_temp = tbl_deepcopy(item)
@@ -167,6 +187,13 @@ local function update(ctx)
167187
core.response.exit(400, {error_msg = err_prefix .. err})
168188
end
169189
end
190+
-- check duplicate resource
191+
local err = check_duplicate(item, key, id_set)
192+
if err then
193+
core.log.error(err_prefix, err)
194+
core.response.exit(400, { error_msg = err_prefix .. err })
195+
end
196+
170197
table_insert(apisix_yaml[key], item)
171198
end
172199
end

0 commit comments

Comments
 (0)