diff --git a/doc/source/command-objects/port.rst b/doc/source/command-objects/port.rst index 0c91f3ac00..3083b07518 100644 --- a/doc/source/command-objects/port.rst +++ b/doc/source/command-objects/port.rst @@ -98,6 +98,16 @@ Delete port(s) Port(s) to delete (name or ID) +port list +--------- + +List ports + +.. program:: port list +.. code:: bash + + os port list + port show --------- diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py index f9d0fc957f..449dcfd498 100644 --- a/openstackclient/network/v2/port.py +++ b/openstackclient/network/v2/port.py @@ -238,6 +238,33 @@ class DeletePort(command.Command): client.delete_port(res) +class ListPort(command.Lister): + """List ports""" + + def take_action(self, parsed_args): + client = self.app.client_manager.network + + columns = ( + 'id', + 'name', + 'mac_address', + 'fixed_ips', + ) + column_headers = ( + 'ID', + 'Name', + 'MAC Address', + 'Fixed IP Addresses', + ) + + data = client.ports() + return (column_headers, + (utils.get_item_properties( + s, columns, + formatters=_formatters, + ) for s in data)) + + class ShowPort(command.ShowOne): """Display port details""" diff --git a/openstackclient/tests/network/v2/test_port.py b/openstackclient/tests/network/v2/test_port.py index 907d8a7d10..30e290c6ad 100644 --- a/openstackclient/tests/network/v2/test_port.py +++ b/openstackclient/tests/network/v2/test_port.py @@ -199,6 +199,47 @@ class TestDeletePort(TestPort): self.assertIsNone(result) +class TestListPort(TestPort): + + _ports = network_fakes.FakePort.create_ports(count=3) + + columns = ( + 'ID', + 'Name', + 'MAC Address', + 'Fixed IP Addresses', + ) + + data = [] + for prt in _ports: + data.append(( + prt.id, + prt.name, + prt.mac_address, + utils.format_list_of_dicts(prt.fixed_ips), + )) + + def setUp(self): + super(TestListPort, self).setUp() + + # Get the command object to test + self.cmd = port.ListPort(self.app, self.namespace) + + self.network.ports = mock.Mock(return_value=self._ports) + + def test_port_list_no_options(self): + arglist = [] + verifylist = [] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.ports.assert_called_with() + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + class TestShowPort(TestPort): # The port to show. diff --git a/releasenotes/notes/add-port-list-command-29b4452003bc6bab.yaml b/releasenotes/notes/add-port-list-command-29b4452003bc6bab.yaml new file mode 100644 index 0000000000..6c62539714 --- /dev/null +++ b/releasenotes/notes/add-port-list-command-29b4452003bc6bab.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add support for the ``port list`` command. + [Bug `1519909 `_] \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index b49eeba9eb..728b5d646b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -326,27 +326,34 @@ openstack.network.v2 = ip_floating_delete = openstackclient.network.v2.floating_ip:DeleteFloatingIP ip_floating_list = openstackclient.network.v2.floating_ip:ListFloatingIP ip_floating_show = openstackclient.network.v2.floating_ip:ShowFloatingIP + network_create = openstackclient.network.v2.network:CreateNetwork network_delete = openstackclient.network.v2.network:DeleteNetwork network_list = openstackclient.network.v2.network:ListNetwork network_set = openstackclient.network.v2.network:SetNetwork network_show = openstackclient.network.v2.network:ShowNetwork + port_create = openstackclient.network.v2.port:CreatePort port_delete = openstackclient.network.v2.port:DeletePort + port_list = openstackclient.network.v2.port:ListPort port_show = openstackclient.network.v2.port:ShowPort + router_create = openstackclient.network.v2.router:CreateRouter router_delete = openstackclient.network.v2.router:DeleteRouter router_list = openstackclient.network.v2.router:ListRouter router_set = openstackclient.network.v2.router:SetRouter router_show = openstackclient.network.v2.router:ShowRouter + security_group_delete = openstackclient.network.v2.security_group:DeleteSecurityGroup security_group_list = openstackclient.network.v2.security_group:ListSecurityGroup security_group_set = openstackclient.network.v2.security_group:SetSecurityGroup security_group_rule_delete = openstackclient.network.v2.security_group_rule:DeleteSecurityGroupRule security_group_rule_show = openstackclient.network.v2.security_group_rule:ShowSecurityGroupRule + subnet_delete = openstackclient.network.v2.subnet:DeleteSubnet subnet_list = openstackclient.network.v2.subnet:ListSubnet subnet_show = openstackclient.network.v2.subnet:ShowSubnet + subnet_pool_delete = openstackclient.network.v2.subnet_pool:DeleteSubnetPool subnet_pool_list = openstackclient.network.v2.subnet_pool:ListSubnetPool subnet_pool_show = openstackclient.network.v2.subnet_pool:ShowSubnetPool