Replaced code in routers_info module with openstacksdk function

Replaced custom code for interface listing with call to openstacksdk.
The original idea was to reduce the number of calls to the OpenStack
API but this kind of optimization is better to be implemented in the
SDK itself [1]. Reimplementing code like this increases our
maintenance burden, does not help other SDK users and increases the
likeliness of bugs. For example, variable allowed_device_owners
introduced a bug, it is not 'network_router_interface_distributed'
but 'network:router_interface_distributed'.

[1] https://review.opendev.org/c/openstack/openstacksdk/+/849967

Change-Id: I9c52de03c53ef29d7cecdf26253c0c00a7cf3689
This commit is contained in:
Jakob Meng
2022-07-15 11:50:27 +02:00
parent 7ec8e4d087
commit 1f5a2019a0

View File

@@ -202,23 +202,16 @@ class RouterInfoModule(OpenStackModule):
)
def run(self):
routers = self.conn.search_routers(name_or_id=self.params['name'],
filters=self.params['filters'])
routers = [
router.to_dict(computed=False)
for router in self.conn.search_routers(
name_or_id=self.params['name'],
filters=self.params['filters'])]
routers = [r.to_dict(computed=False) for r in routers]
# The following code replicates self.conn.list_router_interfaces()
# but only uses a single api call per router instead of four api
# calls as the former does.
allowed_device_owners = ('network:router_interface',
'network_router_interface_distributed',
'network:ha_router_replicated_interface',
'network:router_gateway')
# append interfaces_info attribute for backward compatibility
for router in routers:
interfaces_info = []
for port in self.conn.network.ports(device_id=router['id']):
if port.device_owner not in allowed_device_owners:
continue
for port in self.conn.list_router_interfaces(router):
if port.device_owner != "network:router_gateway":
for ip_spec in port.fixed_ips:
int_info = {