Skip to content

Commit cf0f229

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "[Subnet pool] Add 'subnet pool set' command support"
2 parents f8ac17a + f2fb007 commit cf0f229

5 files changed

Lines changed: 227 additions & 6 deletions

File tree

doc/source/command-objects/subnet-pool.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,48 @@ List subnet pools
3535

3636
List additional fields in output
3737

38+
subnet pool set
39+
---------------
40+
41+
Set subnet pool properties
42+
43+
.. program:: subnet pool set
44+
.. code:: bash
45+
46+
os subnet pool set
47+
[--name <name>]
48+
[--pool-prefix <pool-prefix> [...]]
49+
[--default-prefix-length <default-prefix-length>]
50+
[--min-prefix-length <min-prefix-length>]
51+
[--max-prefix-length <max-prefix-length>]
52+
<subnet-pool>
53+
54+
.. option:: --name <name>
55+
56+
Set subnet pool name
57+
58+
.. option:: --pool-prefix <pool-prefix>
59+
60+
Set subnet pool prefixes (in CIDR notation).
61+
Repeat this option to set multiple prefixes.
62+
63+
.. option:: --default-prefix-length <default-prefix-length>
64+
65+
Set subnet pool default prefix length
66+
67+
.. option:: --min-prefix-length <min-prefix-length>
68+
69+
Set subnet pool minimum prefix length
70+
71+
.. option:: --max-prefix-length <max-prefix-length>
72+
73+
Set subnet pool maximum prefix length
74+
75+
.. _subnet_pool_set-subnet-pool:
76+
.. describe:: <subnet-pool>
77+
78+
Subnet pool to modify (name or ID)
79+
3880
subnet pool show
3981
----------------
4082

openstackclient/network/v2/subnet_pool.py

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

1616
from openstackclient.common import command
17+
from openstackclient.common import exceptions
18+
from openstackclient.common import parseractions
1719
from openstackclient.common import utils
1820

1921

@@ -30,15 +32,60 @@ def _get_columns(item):
3032
}
3133

3234

35+
def _get_attrs(parsed_args):
36+
attrs = {}
37+
if parsed_args.name is not None:
38+
attrs['name'] = str(parsed_args.name)
39+
if parsed_args.prefixes is not None:
40+
attrs['prefixes'] = parsed_args.prefixes
41+
if parsed_args.default_prefix_length is not None:
42+
attrs['default_prefix_length'] = parsed_args.default_prefix_length
43+
if parsed_args.min_prefix_length is not None:
44+
attrs['min_prefix_length'] = parsed_args.min_prefix_length
45+
if parsed_args.max_prefix_length is not None:
46+
attrs['max_prefix_length'] = parsed_args.max_prefix_length
47+
48+
return attrs
49+
50+
51+
def _add_prefix_options(parser):
52+
parser.add_argument(
53+
'--pool-prefix',
54+
metavar='<pool-prefix>',
55+
dest='prefixes',
56+
action='append',
57+
help='Set subnet pool prefixes (in CIDR notation). '
58+
'Repeat this option to set multiple prefixes.',
59+
)
60+
parser.add_argument(
61+
'--default-prefix-length',
62+
metavar='<default-prefix-length>',
63+
action=parseractions.NonNegativeAction,
64+
help='Set subnet pool default prefix length',
65+
)
66+
parser.add_argument(
67+
'--min-prefix-length',
68+
metavar='<min-prefix-length>',
69+
action=parseractions.NonNegativeAction,
70+
help='Set subnet pool minimum prefix length',
71+
)
72+
parser.add_argument(
73+
'--max-prefix-length',
74+
metavar='<max-prefix-length>',
75+
action=parseractions.NonNegativeAction,
76+
help='Set subnet pool maximum prefix length',
77+
)
78+
79+
3380
class DeleteSubnetPool(command.Command):
3481
"""Delete subnet pool"""
3582

3683
def get_parser(self, prog_name):
3784
parser = super(DeleteSubnetPool, self).get_parser(prog_name)
3885
parser.add_argument(
3986
'subnet_pool',
40-
metavar="<subnet-pool>",
41-
help=("Subnet pool to delete (name or ID)")
87+
metavar='<subnet-pool>',
88+
help='Subnet pool to delete (name or ID)'
4289
)
4390
return parser
4491

@@ -98,15 +145,51 @@ def take_action(self, parsed_args):
98145
) for s in data))
99146

100147

148+
class SetSubnetPool(command.Command):
149+
"""Set subnet pool properties"""
150+
151+
def get_parser(self, prog_name):
152+
parser = super(SetSubnetPool, self).get_parser(prog_name)
153+
parser.add_argument(
154+
'subnet_pool',
155+
metavar='<subnet-pool>',
156+
help='Subnet pool to modify (name or ID)'
157+
)
158+
parser.add_argument(
159+
'--name',
160+
metavar='<name>',
161+
help='Set subnet pool name',
162+
)
163+
_add_prefix_options(parser)
164+
165+
return parser
166+
167+
def take_action(self, parsed_args):
168+
client = self.app.client_manager.network
169+
obj = client.find_subnet_pool(parsed_args.subnet_pool,
170+
ignore_missing=False)
171+
172+
attrs = _get_attrs(parsed_args)
173+
if attrs == {}:
174+
msg = "Nothing specified to be set"
175+
raise exceptions.CommandError(msg)
176+
177+
# Existing prefixes must be a subset of the new prefixes.
178+
if 'prefixes' in attrs:
179+
attrs['prefixes'].extend(obj.prefixes)
180+
181+
client.update_subnet_pool(obj, **attrs)
182+
183+
101184
class ShowSubnetPool(command.ShowOne):
102185
"""Display subnet pool details"""
103186

104187
def get_parser(self, prog_name):
105188
parser = super(ShowSubnetPool, self).get_parser(prog_name)
106189
parser.add_argument(
107190
'subnet_pool',
108-
metavar="<subnet-pool>",
109-
help=("Subnet pool to display (name or ID)")
191+
metavar='<subnet-pool>',
192+
help='Subnet pool to display (name or ID)'
110193
)
111194
return parser
112195

openstackclient/tests/network/v2/test_subnet_pool.py

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
# under the License.
1212
#
1313

14+
import argparse
1415
import mock
1516

17+
from openstackclient.common import exceptions
1618
from openstackclient.common import utils
1719
from openstackclient.network.v2 import subnet_pool
1820
from openstackclient.tests.network.v2 import fakes as network_fakes
@@ -129,6 +131,96 @@ def test_subnet_pool_list_long(self):
129131
self.assertEqual(self.data_long, list(data))
130132

131133

134+
class TestSetSubnetPool(TestSubnetPool):
135+
136+
# The subnet_pool to set.
137+
_subnet_pool = network_fakes.FakeSubnetPool.create_one_subnet_pool()
138+
139+
def setUp(self):
140+
super(TestSetSubnetPool, self).setUp()
141+
142+
self.network.update_subnet_pool = mock.Mock(return_value=None)
143+
144+
self.network.find_subnet_pool = mock.Mock(
145+
return_value=self._subnet_pool)
146+
147+
# Get the command object to test
148+
self.cmd = subnet_pool.SetSubnetPool(self.app, self.namespace)
149+
150+
def test_set_this(self):
151+
arglist = [
152+
self._subnet_pool.name,
153+
'--name', 'noob',
154+
'--default-prefix-length', '8',
155+
'--min-prefix-length', '8',
156+
]
157+
verifylist = [
158+
('subnet_pool', self._subnet_pool.name),
159+
('name', 'noob'),
160+
('default_prefix_length', '8'),
161+
('min_prefix_length', '8'),
162+
]
163+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
164+
165+
result = self.cmd.take_action(parsed_args)
166+
167+
attrs = {
168+
'name': 'noob',
169+
'default_prefix_length': '8',
170+
'min_prefix_length': '8',
171+
}
172+
self.network.update_subnet_pool.assert_called_with(
173+
self._subnet_pool, **attrs)
174+
self.assertIsNone(result)
175+
176+
def test_set_that(self):
177+
arglist = [
178+
self._subnet_pool.name,
179+
'--pool-prefix', '10.0.1.0/24',
180+
'--pool-prefix', '10.0.2.0/24',
181+
'--max-prefix-length', '16',
182+
]
183+
verifylist = [
184+
('subnet_pool', self._subnet_pool.name),
185+
('prefixes', ['10.0.1.0/24', '10.0.2.0/24']),
186+
('max_prefix_length', '16'),
187+
]
188+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
189+
190+
result = self.cmd.take_action(parsed_args)
191+
192+
prefixes = ['10.0.1.0/24', '10.0.2.0/24']
193+
prefixes.extend(self._subnet_pool.prefixes)
194+
attrs = {
195+
'prefixes': prefixes,
196+
'max_prefix_length': '16',
197+
}
198+
self.network.update_subnet_pool.assert_called_with(
199+
self._subnet_pool, **attrs)
200+
self.assertIsNone(result)
201+
202+
def test_set_nothing(self):
203+
arglist = [self._subnet_pool.name, ]
204+
verifylist = [('subnet_pool', self._subnet_pool.name), ]
205+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
206+
207+
self.assertRaises(exceptions.CommandError, self.cmd.take_action,
208+
parsed_args)
209+
210+
def test_set_len_negative(self):
211+
arglist = [
212+
self._subnet_pool.name,
213+
'--max-prefix-length', '-16',
214+
]
215+
verifylist = [
216+
('subnet_pool', self._subnet_pool.name),
217+
('max_prefix_length', '-16'),
218+
]
219+
220+
self.assertRaises(argparse.ArgumentTypeError, self.check_parser,
221+
self.cmd, arglist, verifylist)
222+
223+
132224
class TestShowSubnetPool(TestSubnetPool):
133225

134226
# The subnet_pool to set.
@@ -189,14 +281,13 @@ def test_show_all_options(self):
189281
verifylist = [
190282
('subnet_pool', self._subnet_pool.name),
191283
]
192-
193284
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
285+
194286
columns, data = self.cmd.take_action(parsed_args)
195287

196288
self.network.find_subnet_pool.assert_called_with(
197289
self._subnet_pool.name,
198290
ignore_missing=False
199291
)
200-
201292
self.assertEqual(self.columns, columns)
202293
self.assertEqual(self.data, data)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
features:
3+
- Add ``subnet pool set`` command.
4+
[Bug `1544591 <https://bugs.launchpad.net/python-openstackclient/+bug/1544591>`_]

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ openstack.network.v2 =
357357

358358
subnet_pool_delete = openstackclient.network.v2.subnet_pool:DeleteSubnetPool
359359
subnet_pool_list = openstackclient.network.v2.subnet_pool:ListSubnetPool
360+
subnet_pool_set = openstackclient.network.v2.subnet_pool:SetSubnetPool
360361
subnet_pool_show = openstackclient.network.v2.subnet_pool:ShowSubnetPool
361362

362363
openstack.object_store.v1 =

0 commit comments

Comments
 (0)