From 1f5a2019a0a9167b78c4026a6afdd40a3d41874b Mon Sep 17 00:00:00 2001
From: Jakob Meng <code@jakobmeng.de>
Date: Fri, 15 Jul 2022 11:50:27 +0200
Subject: [PATCH] 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
---
 plugins/modules/routers_info.py | 23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)

diff --git a/plugins/modules/routers_info.py b/plugins/modules/routers_info.py
index a2b4f8d..37bbecb 100644
--- a/plugins/modules/routers_info.py
+++ b/plugins/modules/routers_info.py
@@ -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 = [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')
+        routers = [
+            router.to_dict(computed=False)
+            for router in self.conn.search_routers(
+                name_or_id=self.params['name'],
+                filters=self.params['filters'])]
+
+        # 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 = {
-- 
GitLab