From 8a1537349aec97fb74776639d5302296432a86b2 Mon Sep 17 00:00:00 2001 From: "Jingwei.Zhang" Date: Wed, 10 Aug 2022 17:45:18 +0800 Subject: [PATCH] refactor: Optimize the method of obtaining port based on device owners Optimize the method of obtaining port based on device owners: call from multiple APIs, adjust to one API call, because the device_owner parameter supports array format Change-Id: Ia14c187f793e5c3528cdd1a603f2e52510d90f03 --- src/resources/neutron/floatingip.js | 40 ++++++++++++----------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/src/resources/neutron/floatingip.js b/src/resources/neutron/floatingip.js index 957c3f0f..33135153 100644 --- a/src/resources/neutron/floatingip.js +++ b/src/resources/neutron/floatingip.js @@ -52,23 +52,21 @@ export function getSubnetToRouter( ) { const canReachSubnetIdsWithRouterId = []; // Save the information of all subnets and create a mapping of router_id => subnet_id - portsWithFixedIPs.forEach((type) => { - type.forEach((port) => { - const router = routerIdWithExternalNetworkInfo.find((r) => { - if (shouldHaveExternalGateway && !r.external_gateway_info) { - return false; - } - return r.id === port.device_id; - }); - if (router && router.id === port.device_id) { - port.fixed_ips.forEach((item) => { - canReachSubnetIdsWithRouterId.push({ - subnet_id: item.subnet_id, - router_id: port.device_id, - }); - }); + portsWithFixedIPs.forEach((port) => { + const router = routerIdWithExternalNetworkInfo.find((r) => { + if (shouldHaveExternalGateway && !r.external_gateway_info) { + return false; } + return r.id === port.device_id; }); + if (router) { + port.fixed_ips.forEach((item) => { + canReachSubnetIdsWithRouterId.push({ + subnet_id: item.subnet_id, + router_id: port.device_id, + }); + }); + } }); return canReachSubnetIdsWithRouterId; } @@ -79,14 +77,10 @@ export async function getPortsWithFixedIPs() { 'network:router_interface', 'network:ha_router_replicated_interface', ]; - const portsWithFixedIPs = await Promise.all( - deviceOwnerList.map((item) => - globalPortStore.pureFetchList({ - device_owner: item, - fields: ['fixed_ips', 'device_id'], - }) - ) - ); + const portsWithFixedIPs = await globalPortStore.pureFetchList({ + device_owner: deviceOwnerList, + fields: ['fixed_ips', 'device_id', 'device_owner'], + }); return portsWithFixedIPs; }