Allow filtering ports by device_id 52/7152/6
authorschillinge <ablu@mail.uni-paderborn.de>
Wed, 30 Jan 2019 19:17:54 +0000 (20:17 +0100)
committerschillinge <ablu@mail.uni-paderborn.de>
Tue, 5 Feb 2019 15:07:51 +0000 (16:07 +0100)
This otherwise leads to errors in RO, which checks whether the ports of a
specific instance are created.

Change-Id: I563dc84db299e6148b7970e8c9716460653c6b48
Signed-off-by: schillinge <ablu@mail.uni-paderborn.de>
src/emuvim/api/openstack/openstack_dummies/neutron_dummy_api.py
src/emuvim/test/unittests/test_openstack.py

index 13698bd..e017660 100755 (executable)
@@ -717,20 +717,29 @@ class NeutronListPorts(Resource):
                 show_port = NeutronShowPort(self.api)
                 return show_port.get_port(request.args.get('id'), True)
 
-            port_list = list()
-            port_dict = dict()
+            ports = self.api.compute.ports.values()
+            if len(id_list) != 0:
+                ports = filter(lambda port: port.id in id_list, ports)
+
+            device_id = request.args.get('device_id')
+            if device_id:
+                server = self.api.compute.find_server_by_name_or_id(device_id)
+                if not server:
+                    raise RuntimeError("Unable to find server '%s' in order to return it's ports" % server)
+
+                ports = filter(lambda port: (
+                    any(
+                        filter(
+                            lambda server_port_name_or_id: (
+                                port.id == server_port_name_or_id or port.name == server_port_name_or_id
+                            ),
+                            server.port_names
+                        )
+                    )
+                ), ports)
 
-            if len(id_list) == 0:
-                for port in self.api.compute.ports.values():
-                    tmp_port_dict = port.create_port_dict(self.api.compute)
-                    port_list.append(tmp_port_dict)
-            else:
-                for port in self.api.compute.ports.values():
-                    if port.id in id_list:
-                        tmp_port_dict = port.create_port_dict(self.api.compute)
-                        port_list.append(tmp_port_dict)
-
-            port_dict["ports"] = port_list
+            port_dict = dict()
+            port_dict["ports"] = map(lambda x: x.create_port_dict(self.api.compute), ports)
 
             return Response(json.dumps(port_dict), status=200,
                             mimetype='application/json')
index 5540247..7530f15 100755 (executable)
@@ -568,6 +568,22 @@ class testRestApi(ApiBaseOpenStack):
                          "ports"][0]["id"], listPortsId1)
         print(" ")
 
+        print('->>>>>>> test Neutron List Ports By Device ->>>>>>>>>>>>>>>')
+        print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
+        server_url = "http://0.0.0.0:18774/v2.1/id_bla/servers/firewall1:9df6a98f-9e11-4cb7-b3c0-InAdUnitTest"
+        server_response = requests.get(server_url, headers=headers)
+        firewall1_server = json.loads(server_response.content)["server"]
+        device_id = firewall1_server["id"]
+        url = "http://0.0.0.0:19696/v2.0/ports?device_id=%s" % device_id
+        list_ports_by_device_id_response = requests.get(url, headers=headers)
+        self.assertEqual(list_ports_by_device_id_response.status_code, 200)
+        list_ports_by_device_id_ports = json.loads(list_ports_by_device_id_response.content)["ports"]
+
+        self.assertTrue(any(list_ports_by_device_id_ports), "Expected at least one port for device")
+        for port in list_ports_by_device_id_ports:
+            self.assertTrue(port["name"].startswith("firewall1:"), "Expected all ports to belong to firewall1")
+        print(" ")
+
         print('->>>>>>> test Neutron List Non-Existing Ports ->>>>>>>>>>>>>>>')
         print('->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
         url = "http://0.0.0.0:19696/v2.0/ports?id=non-existing-port-id"