Skip to content

Commit ddc97c6

Browse files
author
Tang Chen
committed
Support "network list" command in nova network
"network list" command is not implemented in nova network. This patch implements it. The Network object in novaclient is quite different from the one in sdk. And the output of "network list" using Nova network is also quite different from using Neutron. It is like this: # openstack network list +--------------------------------------+---------+-------------+ | ID | Name | Subnet | +--------------------------------------+---------+-------------+ | 96a98ec4-31f6-45f6-99e6-9384569b3bb5 | private | 10.0.0.0/24 | +--------------------------------------+---------+-------------+ --long and --external options have not been implemented because the attrs in Network object in novaclient is too much different. This patch also introduces a new FakeNetwork class in compute/v2/fake.py to fake nova network. Change-Id: Id1fdf81fb2fa8b39f2c76b7bae37ac4fecafd0f7 Depends-On: I1b59264cd40aaf1062f4e8db233ccb7fd0e95f0e partial-Bug: 1543672
1 parent 0a3ba91 commit ddc97c6

5 files changed

Lines changed: 136 additions & 10 deletions

File tree

doc/source/command-objects/network.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
network
33
=======
44

5-
Network v2
5+
Compute v2, Network v2
66

77
network create
88
--------------

openstackclient/network/v2/network.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,10 @@ def take_action_compute(self, client, parsed_args):
168168
client.networks.delete(network.id)
169169

170170

171-
class ListNetwork(command.Lister):
171+
class ListNetwork(common.NetworkAndComputeLister):
172172
"""List networks"""
173173

174-
def get_parser(self, prog_name):
175-
parser = super(ListNetwork, self).get_parser(prog_name)
174+
def update_parser_common(self, parser):
176175
parser.add_argument(
177176
'--external',
178177
action='store_true',
@@ -187,9 +186,7 @@ def get_parser(self, prog_name):
187186
)
188187
return parser
189188

190-
def take_action(self, parsed_args):
191-
client = self.app.client_manager.network
192-
189+
def take_action_network(self, client, parsed_args):
193190
if parsed_args.long:
194191
columns = (
195192
'id',
@@ -231,7 +228,29 @@ def take_action(self, parsed_args):
231228
args = {'router:external': True}
232229
else:
233230
args = {}
231+
234232
data = client.networks(**args)
233+
234+
return (column_headers,
235+
(utils.get_item_properties(
236+
s, columns,
237+
formatters=_formatters,
238+
) for s in data))
239+
240+
def take_action_compute(self, client, parsed_args):
241+
columns = (
242+
'id',
243+
'label',
244+
'cidr',
245+
)
246+
column_headers = (
247+
'ID',
248+
'Name',
249+
'Subnet',
250+
)
251+
252+
data = client.networks.list()
253+
235254
return (column_headers,
236255
(utils.get_item_properties(
237256
s, columns,

openstackclient/tests/compute/v2/fakes.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,3 +525,61 @@ def get_floating_ips(floating_ips=None, count=2):
525525
if floating_ips is None:
526526
floating_ips = FakeFloatingIP.create_floating_ips(count)
527527
return mock.MagicMock(side_effect=floating_ips)
528+
529+
530+
class FakeNetwork(object):
531+
"""Fake one or more networks."""
532+
533+
@staticmethod
534+
def create_one_network(attrs={}, methods={}):
535+
"""Create a fake network.
536+
537+
:param Dictionary attrs:
538+
A dictionary with all attributes
539+
:param Dictionary methods:
540+
A dictionary with all methods
541+
:return:
542+
A FakeResource object, with id, label, cidr
543+
"""
544+
# Set default attributes.
545+
network_attrs = {
546+
'id': 'network-id-' + uuid.uuid4().hex,
547+
'label': 'network-label-' + uuid.uuid4().hex,
548+
'cidr': '10.0.0.0/24',
549+
}
550+
551+
# Overwrite default attributes.
552+
network_attrs.update(attrs)
553+
554+
# Set default methods.
555+
network_methods = {
556+
'keys': ['id', 'label', 'cidr'],
557+
}
558+
559+
# Overwrite default methods.
560+
network_methods.update(methods)
561+
562+
network = fakes.FakeResource(info=copy.deepcopy(network_attrs),
563+
methods=copy.deepcopy(network_methods),
564+
loaded=True)
565+
566+
return network
567+
568+
@staticmethod
569+
def create_networks(attrs={}, methods={}, count=2):
570+
"""Create multiple fake networks.
571+
572+
:param Dictionary attrs:
573+
A dictionary with all attributes
574+
:param Dictionary methods:
575+
A dictionary with all methods
576+
:param int count:
577+
The number of networks to fake
578+
:return:
579+
A list of FakeResource objects faking the networks
580+
"""
581+
networks = []
582+
for i in range(0, count):
583+
networks.append(FakeNetwork.create_one_network(attrs, methods))
584+
585+
return networks

openstackclient/tests/network/v2/test_network.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ def setUp(self):
579579
class TestDeleteNetworkCompute(TestNetworkCompute):
580580

581581
# The network to delete.
582-
_network = network_fakes.FakeNetwork.create_one_network()
582+
_network = compute_fakes.FakeNetwork.create_one_network()
583583

584584
def setUp(self):
585585
super(TestDeleteNetworkCompute, self).setUp()
@@ -596,14 +596,61 @@ def setUp(self):
596596

597597
def test_network_delete(self):
598598
arglist = [
599-
self._network.name,
599+
self._network.label,
600600
]
601601
verifylist = [
602-
('network', [self._network.name]),
602+
('network', [self._network.label]),
603603
]
604604

605605
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
606606
result = self.cmd.take_action(parsed_args)
607607

608608
self.compute.networks.delete.assert_called_with(self._network.id)
609609
self.assertIsNone(result)
610+
611+
612+
class TestListNetworkCompute(TestNetworkCompute):
613+
614+
# The networks going to be listed up.
615+
_networks = compute_fakes.FakeNetwork.create_networks(count=3)
616+
617+
columns = (
618+
'ID',
619+
'Name',
620+
'Subnet',
621+
)
622+
623+
data = []
624+
for net in _networks:
625+
data.append((
626+
net.id,
627+
net.label,
628+
net.cidr,
629+
))
630+
631+
def setUp(self):
632+
super(TestListNetworkCompute, self).setUp()
633+
634+
self.app.client_manager.network_endpoint_enabled = False
635+
636+
self.compute.networks.list.return_value = self._networks
637+
638+
# Get the command object to test
639+
self.cmd = network.ListNetwork(self.app, None)
640+
641+
def test_network_list_no_options(self):
642+
arglist = []
643+
verifylist = [
644+
('external', False),
645+
('long', False),
646+
]
647+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
648+
649+
# In base command class Lister in cliff, abstract method take_action()
650+
# returns a tuple containing the column names and an iterable
651+
# containing the data to be listed.
652+
columns, data = self.cmd.take_action(parsed_args)
653+
654+
self.compute.networks.list.assert_called_with()
655+
self.assertEqual(self.columns, columns)
656+
self.assertEqual(self.data, list(data))

releasenotes/notes/bug-1543672-bad2fc4c6c8f3125.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
features:
33
- Command ``network delete`` is now available for nova network.
44
[Bug `1543672 <https://bugs.launchpad.net/python-openstackclient/+bug/1543672>`_]
5+
- Command ``network list`` is now available for nova network.
6+
[Bug `1543672 <https://bugs.launchpad.net/python-openstackclient/+bug/1543672>`_]

0 commit comments

Comments
 (0)