diff --git a/doc/source/command-objects/network.rst b/doc/source/command-objects/network.rst index bb36667253..0ef8f56d28 100644 --- a/doc/source/command-objects/network.rst +++ b/doc/source/command-objects/network.rst @@ -2,7 +2,7 @@ network ======= -Network v2 +Compute v2, Network v2 network create -------------- diff --git a/openstackclient/network/v2/network.py b/openstackclient/network/v2/network.py index 636c333e7a..ed25117317 100644 --- a/openstackclient/network/v2/network.py +++ b/openstackclient/network/v2/network.py @@ -168,11 +168,10 @@ class DeleteNetwork(common.NetworkAndComputeCommand): client.networks.delete(network.id) -class ListNetwork(command.Lister): +class ListNetwork(common.NetworkAndComputeLister): """List networks""" - def get_parser(self, prog_name): - parser = super(ListNetwork, self).get_parser(prog_name) + def update_parser_common(self, parser): parser.add_argument( '--external', action='store_true', @@ -187,9 +186,7 @@ class ListNetwork(command.Lister): ) return parser - def take_action(self, parsed_args): - client = self.app.client_manager.network - + def take_action_network(self, client, parsed_args): if parsed_args.long: columns = ( 'id', @@ -231,7 +228,29 @@ class ListNetwork(command.Lister): args = {'router:external': True} else: args = {} + data = client.networks(**args) + + return (column_headers, + (utils.get_item_properties( + s, columns, + formatters=_formatters, + ) for s in data)) + + def take_action_compute(self, client, parsed_args): + columns = ( + 'id', + 'label', + 'cidr', + ) + column_headers = ( + 'ID', + 'Name', + 'Subnet', + ) + + data = client.networks.list() + return (column_headers, (utils.get_item_properties( s, columns, diff --git a/openstackclient/tests/compute/v2/fakes.py b/openstackclient/tests/compute/v2/fakes.py index 00f7374807..66e488b79b 100644 --- a/openstackclient/tests/compute/v2/fakes.py +++ b/openstackclient/tests/compute/v2/fakes.py @@ -525,3 +525,61 @@ class FakeFloatingIP(object): if floating_ips is None: floating_ips = FakeFloatingIP.create_floating_ips(count) return mock.MagicMock(side_effect=floating_ips) + + +class FakeNetwork(object): + """Fake one or more networks.""" + + @staticmethod + def create_one_network(attrs={}, methods={}): + """Create a fake network. + + :param Dictionary attrs: + A dictionary with all attributes + :param Dictionary methods: + A dictionary with all methods + :return: + A FakeResource object, with id, label, cidr + """ + # Set default attributes. + network_attrs = { + 'id': 'network-id-' + uuid.uuid4().hex, + 'label': 'network-label-' + uuid.uuid4().hex, + 'cidr': '10.0.0.0/24', + } + + # Overwrite default attributes. + network_attrs.update(attrs) + + # Set default methods. + network_methods = { + 'keys': ['id', 'label', 'cidr'], + } + + # Overwrite default methods. + network_methods.update(methods) + + network = fakes.FakeResource(info=copy.deepcopy(network_attrs), + methods=copy.deepcopy(network_methods), + loaded=True) + + return network + + @staticmethod + def create_networks(attrs={}, methods={}, count=2): + """Create multiple fake networks. + + :param Dictionary attrs: + A dictionary with all attributes + :param Dictionary methods: + A dictionary with all methods + :param int count: + The number of networks to fake + :return: + A list of FakeResource objects faking the networks + """ + networks = [] + for i in range(0, count): + networks.append(FakeNetwork.create_one_network(attrs, methods)) + + return networks diff --git a/openstackclient/tests/network/v2/test_network.py b/openstackclient/tests/network/v2/test_network.py index f7721951e7..c83e7e8447 100644 --- a/openstackclient/tests/network/v2/test_network.py +++ b/openstackclient/tests/network/v2/test_network.py @@ -579,7 +579,7 @@ class TestNetworkCompute(compute_fakes.TestComputev2): class TestDeleteNetworkCompute(TestNetworkCompute): # The network to delete. - _network = network_fakes.FakeNetwork.create_one_network() + _network = compute_fakes.FakeNetwork.create_one_network() def setUp(self): super(TestDeleteNetworkCompute, self).setUp() @@ -596,10 +596,10 @@ class TestDeleteNetworkCompute(TestNetworkCompute): def test_network_delete(self): arglist = [ - self._network.name, + self._network.label, ] verifylist = [ - ('network', [self._network.name]), + ('network', [self._network.label]), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -607,3 +607,50 @@ class TestDeleteNetworkCompute(TestNetworkCompute): self.compute.networks.delete.assert_called_with(self._network.id) self.assertIsNone(result) + + +class TestListNetworkCompute(TestNetworkCompute): + + # The networks going to be listed up. + _networks = compute_fakes.FakeNetwork.create_networks(count=3) + + columns = ( + 'ID', + 'Name', + 'Subnet', + ) + + data = [] + for net in _networks: + data.append(( + net.id, + net.label, + net.cidr, + )) + + def setUp(self): + super(TestListNetworkCompute, self).setUp() + + self.app.client_manager.network_endpoint_enabled = False + + self.compute.networks.list.return_value = self._networks + + # Get the command object to test + self.cmd = network.ListNetwork(self.app, None) + + def test_network_list_no_options(self): + arglist = [] + verifylist = [ + ('external', False), + ('long', False), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # In base command class Lister in cliff, abstract method take_action() + # returns a tuple containing the column names and an iterable + # containing the data to be listed. + columns, data = self.cmd.take_action(parsed_args) + + self.compute.networks.list.assert_called_with() + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) diff --git a/releasenotes/notes/bug-1543672-bad2fc4c6c8f3125.yaml b/releasenotes/notes/bug-1543672-bad2fc4c6c8f3125.yaml index e8f75dd860..9cecc06039 100644 --- a/releasenotes/notes/bug-1543672-bad2fc4c6c8f3125.yaml +++ b/releasenotes/notes/bug-1543672-bad2fc4c6c8f3125.yaml @@ -2,3 +2,5 @@ features: - Command ``network delete`` is now available for nova network. [Bug `1543672 `_] + - Command ``network list`` is now available for nova network. + [Bug `1543672 `_]