spacewander commented on code in PR #9044:
URL: https://github.com/apache/apisix/pull/9044#discussion_r1131887802


##########
apisix/admin/upstreams.lua:
##########
@@ -33,17 +35,43 @@ local function check_conf(id, conf, need_id)
     return true
 end
 
+local function up_id_in_plugins(plugins, up_id)
+    if plugins and plugins["traffic-split"]
+        and plugins["traffic-split"].rules then
+
+        for _, rule in ipairs (plugins["traffic-split"].rules) do
+            local plugin_upstreams = rule.weighted_upstreams
+
+            for _, plugin_upstream in ipairs(plugin_upstreams) do
+                if plugin_upstream and plugin_upstream.upstream_id
+                    and tostring(plugin_upstream.upstream_id) == 
tostring(up_id) then
+
+                    return true
+                end
+            end
+        end
+
+        return false
+    end
+end
+
 
 local function delete_checker(id)
     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.upstream_id
-               and tostring(route.value.upstream_id) == id then
-                return 400, {error_msg = "can not delete this upstream,"
-                                         .. " route [" .. route.value.id
-                                         .. "] is still using it now"}
+            if type(route) == "table" and route.value then
+                if up_id_in_plugins(route.value.plugins, id) == true then
+                    return 400, {error_msg = "can not delete this upstream,"
+                                             .. " traffic-split plugin in 
route [" .. route.value.id

Review Comment:
   Better not to mention a particular plugin name, as we might add other plugin 
check later



##########
apisix/admin/upstreams.lua:
##########
@@ -53,11 +81,35 @@ local function delete_checker(id)
     core.log.info("services_ver: ", services_ver)
     if services_ver and services then
         for _, service in ipairs(services) do
-            if type(service) == "table" and service.value
-               and service.value.upstream_id
-               and tostring(service.value.upstream_id) == id then
+            if type(service) == "table" and service.value then
+                if up_id_in_plugins(service.value.plugins, id) then
+                    return 400, {error_msg = "can not delete this upstream,"
+                                             .. " traffic-split plugin in 
service ["
+                                             .. service.value.id
+                                             .. "] is still using it now"}
+                end
+
+                if service.value.upstream_id and 
tostring(service.value.upstream_id) == id then
+                    return 400, {error_msg = "can not delete this upstream,"
+                                             .. " service [" .. 
service.value.id
+                                             .. "] is still using it now"}
+                end
+            end
+        end
+    end

Review Comment:
   We should also check plugin_config, consumer and consumer group.



##########
t/admin/upstream5.t:
##########
@@ -111,3 +111,266 @@ passed
     }
 --- response_body
 passed
+
+
+
+=== TEST4: prepare upstream

Review Comment:
   ```suggestion
   === TEST 4: prepare upstream
   ```



##########
apisix/admin/upstreams.lua:
##########
@@ -33,17 +35,43 @@ local function check_conf(id, conf, need_id)
     return true
 end
 
+local function up_id_in_plugins(plugins, up_id)
+    if plugins and plugins["traffic-split"]
+        and plugins["traffic-split"].rules then
+
+        for _, rule in ipairs (plugins["traffic-split"].rules) do
+            local plugin_upstreams = rule.weighted_upstreams
+
+            for _, plugin_upstream in ipairs(plugin_upstreams) do
+                if plugin_upstream and plugin_upstream.upstream_id

Review Comment:
   We don't need to check plugin_upstream as there is no hole in the array



##########
apisix/admin/upstreams.lua:
##########
@@ -33,17 +35,43 @@ local function check_conf(id, conf, need_id)
     return true
 end
 
+local function up_id_in_plugins(plugins, up_id)
+    if plugins and plugins["traffic-split"]
+        and plugins["traffic-split"].rules then
+
+        for _, rule in ipairs (plugins["traffic-split"].rules) do

Review Comment:
   ```suggestion
           for _, rule in ipairs(plugins["traffic-split"].rules) do
   ```



##########
apisix/admin/upstreams.lua:
##########
@@ -33,17 +35,43 @@ local function check_conf(id, conf, need_id)
     return true
 end
 
+local function up_id_in_plugins(plugins, up_id)
+    if plugins and plugins["traffic-split"]
+        and plugins["traffic-split"].rules then
+
+        for _, rule in ipairs (plugins["traffic-split"].rules) do
+            local plugin_upstreams = rule.weighted_upstreams
+
+            for _, plugin_upstream in ipairs(plugin_upstreams) do
+                if plugin_upstream and plugin_upstream.upstream_id
+                    and tostring(plugin_upstream.upstream_id) == 
tostring(up_id) then

Review Comment:
   The up_id is already a string



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to