From: schillinge Date: Wed, 30 Jan 2019 19:17:54 +0000 (+0100) Subject: Allow filtering ports by device_id X-Git-Tag: v6.0.0~38 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F52%2F7152%2F6;p=osm%2Fvim-emu.git Allow filtering ports by device_id 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 --- diff --git a/src/emuvim/api/openstack/openstack_dummies/neutron_dummy_api.py b/src/emuvim/api/openstack/openstack_dummies/neutron_dummy_api.py index 13698bd..e017660 100755 --- a/src/emuvim/api/openstack/openstack_dummies/neutron_dummy_api.py +++ b/src/emuvim/api/openstack/openstack_dummies/neutron_dummy_api.py @@ -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') diff --git a/src/emuvim/test/unittests/test_openstack.py b/src/emuvim/test/unittests/test_openstack.py index 5540247..7530f15 100755 --- a/src/emuvim/test/unittests/test_openstack.py +++ b/src/emuvim/test/unittests/test_openstack.py @@ -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"