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')
"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"