Skip to content

Commit a04012c

Browse files
author
Tang Chen
committed
Subnet Pool: Add "subnet pool list" command
Change-Id: I7935be2488fb728ced9680d75880870e5d315655 Closes-Bug: 1544589 Implements: blueprint neutron-client
1 parent 444fc61 commit a04012c

6 files changed

Lines changed: 145 additions & 1 deletion

File tree

doc/source/command-objects/subnet_pool.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,18 @@ Delete subnet pool
1919
.. describe:: <subnet-pool>
2020

2121
Subnet pool to delete (name or ID)
22+
23+
subnet pool list
24+
----------------
25+
26+
List subnet pools
27+
28+
.. program:: subnet pool list
29+
.. code:: bash
30+
31+
os subnet pool list
32+
[--long]
33+
34+
.. option:: --long
35+
36+
List additional fields in output

openstackclient/network/v2/subnet_pool.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""Subnet pool action implementations"""
1515

1616
from openstackclient.common import command
17+
from openstackclient.common import utils
1718

1819

1920
class DeleteSubnetPool(command.Command):
@@ -32,3 +33,53 @@ def take_action(self, parsed_args):
3233
client = self.app.client_manager.network
3334
obj = client.find_subnet_pool(parsed_args.subnet_pool)
3435
client.delete_subnet_pool(obj)
36+
37+
38+
class ListSubnetPool(command.Lister):
39+
"""List subnet pools"""
40+
41+
def get_parser(self, prog_name):
42+
parser = super(ListSubnetPool, self).get_parser(prog_name)
43+
parser.add_argument(
44+
'--long',
45+
action='store_true',
46+
default=False,
47+
help='List additional fields in output',
48+
)
49+
return parser
50+
51+
def take_action(self, parsed_args):
52+
data = self.app.client_manager.network.subnet_pools()
53+
54+
if parsed_args.long:
55+
headers = (
56+
'ID',
57+
'Name',
58+
'Prefixes',
59+
'Default Prefix Length',
60+
'Address Scope',
61+
)
62+
columns = (
63+
'id',
64+
'name',
65+
'prefixes',
66+
'default_prefixlen',
67+
'address_scope_id',
68+
)
69+
else:
70+
headers = (
71+
'ID',
72+
'Name',
73+
'Prefixes',
74+
)
75+
columns = (
76+
'id',
77+
'name',
78+
'prefixes',
79+
)
80+
81+
return (headers,
82+
(utils.get_item_properties(
83+
s, columns,
84+
formatters={},
85+
) for s in data))

openstackclient/tests/network/v2/fakes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,14 +699,18 @@ def create_one_subnet_pool(attrs={}, methods={}):
699699
subnet_pool_attrs = {
700700
'id': 'subnet-pool-id-' + uuid.uuid4().hex,
701701
'name': 'subnet-pool-name-' + uuid.uuid4().hex,
702+
'prefixes': ['10.0.0.0/24', '10.1.0.0/24'],
703+
'default_prefixlen': 8,
704+
'address_scope_id': 'address-scope-id-' + uuid.uuid4().hex,
702705
}
703706

704707
# Overwrite default attributes.
705708
subnet_pool_attrs.update(attrs)
706709

707710
# Set default methods.
708711
subnet_pool_methods = {
709-
'keys': ['id', 'name']
712+
'keys': ['id', 'name', 'prefixes', 'default_prefixlen',
713+
'address_scope_id']
710714
}
711715

712716
# Overwrite default methods.

openstackclient/tests/network/v2/test_subnet_pool.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,72 @@ def test_delete(self):
5555

5656
self.network.delete_subnet_pool.assert_called_with(self._subnet_pool)
5757
self.assertIsNone(result)
58+
59+
60+
class TestListSubnetPool(TestSubnetPool):
61+
# The subnet pools going to be listed up.
62+
_subnet_pools = network_fakes.FakeSubnetPool.create_subnet_pools(count=3)
63+
64+
columns = (
65+
'ID',
66+
'Name',
67+
'Prefixes',
68+
)
69+
columns_long = columns + (
70+
'Default Prefix Length',
71+
'Address Scope',
72+
)
73+
74+
data = []
75+
for pool in _subnet_pools:
76+
data.append((
77+
pool.id,
78+
pool.name,
79+
pool.prefixes,
80+
))
81+
82+
data_long = []
83+
for pool in _subnet_pools:
84+
data_long.append((
85+
pool.id,
86+
pool.name,
87+
pool.prefixes,
88+
pool.default_prefixlen,
89+
pool.address_scope_id,
90+
))
91+
92+
def setUp(self):
93+
super(TestListSubnetPool, self).setUp()
94+
95+
# Get the command object to test
96+
self.cmd = subnet_pool.ListSubnetPool(self.app, self.namespace)
97+
98+
self.network.subnet_pools = mock.Mock(return_value=self._subnet_pools)
99+
100+
def test_subnet_pool_list_no_option(self):
101+
arglist = []
102+
verifylist = [
103+
('long', False),
104+
]
105+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
106+
107+
columns, data = self.cmd.take_action(parsed_args)
108+
109+
self.network.subnet_pools.assert_called_with()
110+
self.assertEqual(self.columns, columns)
111+
self.assertEqual(self.data, list(data))
112+
113+
def test_subnet_pool_list_long(self):
114+
arglist = [
115+
'--long',
116+
]
117+
verifylist = [
118+
('long', True),
119+
]
120+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
121+
122+
columns, data = self.cmd.take_action(parsed_args)
123+
124+
self.network.subnet_pools.assert_called_with()
125+
self.assertEqual(self.columns_long, columns)
126+
self.assertEqual(self.data_long, list(data))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
features:
3+
- Add support for ``subnet pool list`` command.
4+
[Bug `1544589 <https://bugs.launchpad.net/python-openstackclient/+bug/1544589>`_]

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ openstack.network.v2 =
342342
security_group_rule_delete = openstackclient.network.v2.security_group_rule:DeleteSecurityGroupRule
343343
subnet_list = openstackclient.network.v2.subnet:ListSubnet
344344
subnet_pool_delete = openstackclient.network.v2.subnet_pool:DeleteSubnetPool
345+
subnet_pool_list = openstackclient.network.v2.subnet_pool:ListSubnetPool
345346

346347
openstack.object_store.v1 =
347348
object_store_account_set = openstackclient.object.v1.account:SetAccount

0 commit comments

Comments
 (0)