Skip to content

Commit 063c722

Browse files
author
reedip
committed
Add command to unset information from Subnet-pools
This patch introduces the ``subnet pool unset`` command to clear the pool prefix information from the subnet-pools. Change-Id: I84b7259d6e26e695343d41cea6d807396faaf69a Implements: blueprint network-property-unset
1 parent 4b61efe commit 063c722

5 files changed

Lines changed: 109 additions & 0 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,25 @@ Display subnet pool details
185185
.. describe:: <subnet-pool>
186186
187187
Subnet pool to display (name or ID)
188+
189+
subnet pool unset
190+
-----------------
191+
192+
Unset subnet pool properties
193+
194+
.. program:: subnet pool unset
195+
.. code:: bash
196+
197+
os subnet pool unset
198+
[--pool-prefix <pool-prefix> [...]]
199+
<subnet-pool>
200+
201+
.. option:: --pool-prefix <pool-prefix>
202+
203+
Remove subnet pool prefixes (in CIDR notation).
204+
(repeat option to unset multiple prefixes).
205+
206+
.. _subnet_pool_unset-subnet-pool:
207+
.. describe:: <subnet-pool>
208+
209+
Subnet pool to modify (name or ID)

openstackclient/network/v2/subnet_pool.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#
1313

1414
"""Subnet pool action implementations"""
15+
import copy
1516

1617
import logging
1718

@@ -337,3 +338,43 @@ def take_action(self, parsed_args):
337338
columns = _get_columns(obj)
338339
data = utils.get_item_properties(obj, columns, formatters=_formatters)
339340
return (columns, data)
341+
342+
343+
class UnsetSubnetPool(command.Command):
344+
"""Unset subnet pool properties"""
345+
346+
def get_parser(self, prog_name):
347+
parser = super(UnsetSubnetPool, self).get_parser(prog_name)
348+
parser.add_argument(
349+
'--pool-prefix',
350+
metavar='<pool-prefix>',
351+
action='append',
352+
dest='prefixes',
353+
help=_('Remove subnet pool prefixes (in CIDR notation). '
354+
'(repeat option to unset multiple prefixes).'),
355+
)
356+
parser.add_argument(
357+
'subnet_pool',
358+
metavar="<subnet-pool>",
359+
help=_("Subnet pool to modify (name or ID)")
360+
)
361+
return parser
362+
363+
def take_action(self, parsed_args):
364+
client = self.app.client_manager.network
365+
obj = client.find_subnet_pool(
366+
parsed_args.subnet_pool, ignore_missing=False)
367+
tmp_prefixes = copy.deepcopy(obj.prefixes)
368+
attrs = {}
369+
if parsed_args.prefixes:
370+
for prefix in parsed_args.prefixes:
371+
try:
372+
tmp_prefixes.remove(prefix)
373+
except ValueError:
374+
msg = _(
375+
"Subnet pool does not "
376+
"contain prefix %s") % prefix
377+
raise exceptions.CommandError(msg)
378+
attrs['prefixes'] = tmp_prefixes
379+
if attrs:
380+
client.update_subnet_pool(obj, **attrs)

openstackclient/tests/network/v2/test_subnet_pool.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,3 +698,42 @@ def test_show_all_options(self):
698698
)
699699
self.assertEqual(self.columns, columns)
700700
self.assertEqual(self.data, data)
701+
702+
703+
class TestUnsetSubnetPool(TestSubnetPool):
704+
705+
def setUp(self):
706+
super(TestUnsetSubnetPool, self).setUp()
707+
self._subnetpool = network_fakes.FakeSubnetPool.create_one_subnet_pool(
708+
{'prefixes': ['10.0.10.0/24', '10.1.10.0/24',
709+
'10.2.10.0/24'], })
710+
self.network.find_subnet_pool = mock.Mock(
711+
return_value=self._subnetpool)
712+
self.network.update_subnet_pool = mock.Mock(return_value=None)
713+
# Get the command object to test
714+
self.cmd = subnet_pool.UnsetSubnetPool(self.app, self.namespace)
715+
716+
def test_unset_subnet_pool(self):
717+
arglist = [
718+
'--pool-prefix', '10.0.10.0/24',
719+
'--pool-prefix', '10.1.10.0/24',
720+
self._subnetpool.name,
721+
]
722+
verifylist = [('prefixes', ['10.0.10.0/24', '10.1.10.0/24'])]
723+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
724+
result = self.cmd.take_action(parsed_args)
725+
attrs = {'prefixes': ['10.2.10.0/24']}
726+
self.network.update_subnet_pool.assert_called_once_with(
727+
self._subnetpool, **attrs)
728+
self.assertIsNone(result)
729+
730+
def test_unset_subnet_pool_prefix_not_existent(self):
731+
arglist = [
732+
'--pool-prefix', '10.100.1.1/25',
733+
self._subnetpool.name,
734+
]
735+
verifylist = [('prefixes', ['10.100.1.1/25'])]
736+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
737+
self.assertRaises(exceptions.CommandError,
738+
self.cmd.take_action,
739+
parsed_args)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- |
4+
Add a new command ``subnet pool unset`` to clear the information
5+
of pool-prefixes from the subnet pools.
6+
[ Blueprint `network-property-unset <https://blueprints.launchpad.net/python-openstackclient/+spec/network-property-unset>`_]

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ openstack.network.v2 =
390390
subnet_pool_list = openstackclient.network.v2.subnet_pool:ListSubnetPool
391391
subnet_pool_set = openstackclient.network.v2.subnet_pool:SetSubnetPool
392392
subnet_pool_show = openstackclient.network.v2.subnet_pool:ShowSubnetPool
393+
subnet_pool_unset = openstackclient.network.v2.subnet_pool:UnsetSubnetPool
393394

394395
openstack.object_store.v1 =
395396
object_store_account_set = openstackclient.object.v1.account:SetAccount

0 commit comments

Comments
 (0)