Skip to content

Commit 36b2146

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Support "network list" command in nova network"
2 parents bb153b7 + ddc97c6 commit 36b2146

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
@@ -587,3 +587,61 @@ def get_floating_ips(floating_ips=None, count=2):
587587
if floating_ips is None:
588588
floating_ips = FakeFloatingIP.create_floating_ips(count)
589589
return mock.MagicMock(side_effect=floating_ips)
590+
591+
592+
class FakeNetwork(object):
593+
"""Fake one or more networks."""
594+
595+
@staticmethod
596+
def create_one_network(attrs={}, methods={}):
597+
"""Create a fake network.
598+
599+
:param Dictionary attrs:
600+
A dictionary with all attributes
601+
:param Dictionary methods:
602+
A dictionary with all methods
603+
:return:
604+
A FakeResource object, with id, label, cidr
605+
"""
606+
# Set default attributes.
607+
network_attrs = {
608+
'id': 'network-id-' + uuid.uuid4().hex,
609+
'label': 'network-label-' + uuid.uuid4().hex,
610+
'cidr': '10.0.0.0/24',
611+
}
612+
613+
# Overwrite default attributes.
614+
network_attrs.update(attrs)
615+
616+
# Set default methods.
617+
network_methods = {
618+
'keys': ['id', 'label', 'cidr'],
619+
}
620+
621+
# Overwrite default methods.
622+
network_methods.update(methods)
623+
624+
network = fakes.FakeResource(info=copy.deepcopy(network_attrs),
625+
methods=copy.deepcopy(network_methods),
626+
loaded=True)
627+
628+
return network
629+
630+
@staticmethod
631+
def create_networks(attrs={}, methods={}, count=2):
632+
"""Create multiple fake networks.
633+
634+
:param Dictionary attrs:
635+
A dictionary with all attributes
636+
:param Dictionary methods:
637+
A dictionary with all methods
638+
:param int count:
639+
The number of networks to fake
640+
:return:
641+
A list of FakeResource objects faking the networks
642+
"""
643+
networks = []
644+
for i in range(0, count):
645+
networks.append(FakeNetwork.create_one_network(attrs, methods))
646+
647+
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)