From 063c722a110031883e9615064092644de6df8da2 Mon Sep 17 00:00:00 2001 From: reedip Date: Fri, 15 Apr 2016 15:27:18 +0900 Subject: [PATCH] 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 --- doc/source/command-objects/subnet-pool.rst | 22 ++++++++++ openstackclient/network/v2/subnet_pool.py | 41 +++++++++++++++++++ .../tests/network/v2/test_subnet_pool.py | 39 ++++++++++++++++++ .../unset-subnet-pool-333052dd85b95653.yaml | 6 +++ setup.cfg | 1 + 5 files changed, 109 insertions(+) create mode 100644 releasenotes/notes/unset-subnet-pool-333052dd85b95653.yaml diff --git a/doc/source/command-objects/subnet-pool.rst b/doc/source/command-objects/subnet-pool.rst index 516b9bf4a8..005b83579b 100644 --- a/doc/source/command-objects/subnet-pool.rst +++ b/doc/source/command-objects/subnet-pool.rst @@ -185,3 +185,25 @@ Display subnet pool details .. describe:: Subnet pool to display (name or ID) + +subnet pool unset +----------------- + +Unset subnet pool properties + +.. program:: subnet pool unset +.. code:: bash + + os subnet pool unset + [--pool-prefix [...]] + + +.. option:: --pool-prefix + + Remove subnet pool prefixes (in CIDR notation). + (repeat option to unset multiple prefixes). + +.. _subnet_pool_unset-subnet-pool: +.. describe:: + + Subnet pool to modify (name or ID) diff --git a/openstackclient/network/v2/subnet_pool.py b/openstackclient/network/v2/subnet_pool.py index 55dfed8392..ed2bb0ef05 100644 --- a/openstackclient/network/v2/subnet_pool.py +++ b/openstackclient/network/v2/subnet_pool.py @@ -12,6 +12,7 @@ # """Subnet pool action implementations""" +import copy import logging @@ -337,3 +338,43 @@ class ShowSubnetPool(command.ShowOne): columns = _get_columns(obj) data = utils.get_item_properties(obj, columns, formatters=_formatters) return (columns, data) + + +class UnsetSubnetPool(command.Command): + """Unset subnet pool properties""" + + def get_parser(self, prog_name): + parser = super(UnsetSubnetPool, self).get_parser(prog_name) + parser.add_argument( + '--pool-prefix', + metavar='', + action='append', + dest='prefixes', + help=_('Remove subnet pool prefixes (in CIDR notation). ' + '(repeat option to unset multiple prefixes).'), + ) + parser.add_argument( + 'subnet_pool', + metavar="", + help=_("Subnet pool to modify (name or ID)") + ) + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.network + obj = client.find_subnet_pool( + parsed_args.subnet_pool, ignore_missing=False) + tmp_prefixes = copy.deepcopy(obj.prefixes) + attrs = {} + if parsed_args.prefixes: + for prefix in parsed_args.prefixes: + try: + tmp_prefixes.remove(prefix) + except ValueError: + msg = _( + "Subnet pool does not " + "contain prefix %s") % prefix + raise exceptions.CommandError(msg) + attrs['prefixes'] = tmp_prefixes + if attrs: + client.update_subnet_pool(obj, **attrs) diff --git a/openstackclient/tests/network/v2/test_subnet_pool.py b/openstackclient/tests/network/v2/test_subnet_pool.py index 7a96b30f67..41b6170f42 100644 --- a/openstackclient/tests/network/v2/test_subnet_pool.py +++ b/openstackclient/tests/network/v2/test_subnet_pool.py @@ -698,3 +698,42 @@ class TestShowSubnetPool(TestSubnetPool): ) self.assertEqual(self.columns, columns) self.assertEqual(self.data, data) + + +class TestUnsetSubnetPool(TestSubnetPool): + + def setUp(self): + super(TestUnsetSubnetPool, self).setUp() + self._subnetpool = network_fakes.FakeSubnetPool.create_one_subnet_pool( + {'prefixes': ['10.0.10.0/24', '10.1.10.0/24', + '10.2.10.0/24'], }) + self.network.find_subnet_pool = mock.Mock( + return_value=self._subnetpool) + self.network.update_subnet_pool = mock.Mock(return_value=None) + # Get the command object to test + self.cmd = subnet_pool.UnsetSubnetPool(self.app, self.namespace) + + def test_unset_subnet_pool(self): + arglist = [ + '--pool-prefix', '10.0.10.0/24', + '--pool-prefix', '10.1.10.0/24', + self._subnetpool.name, + ] + verifylist = [('prefixes', ['10.0.10.0/24', '10.1.10.0/24'])] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + attrs = {'prefixes': ['10.2.10.0/24']} + self.network.update_subnet_pool.assert_called_once_with( + self._subnetpool, **attrs) + self.assertIsNone(result) + + def test_unset_subnet_pool_prefix_not_existent(self): + arglist = [ + '--pool-prefix', '10.100.1.1/25', + self._subnetpool.name, + ] + verifylist = [('prefixes', ['10.100.1.1/25'])] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.assertRaises(exceptions.CommandError, + self.cmd.take_action, + parsed_args) diff --git a/releasenotes/notes/unset-subnet-pool-333052dd85b95653.yaml b/releasenotes/notes/unset-subnet-pool-333052dd85b95653.yaml new file mode 100644 index 0000000000..7fbda240a1 --- /dev/null +++ b/releasenotes/notes/unset-subnet-pool-333052dd85b95653.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Add a new command ``subnet pool unset`` to clear the information + of pool-prefixes from the subnet pools. + [ Blueprint `network-property-unset `_] diff --git a/setup.cfg b/setup.cfg index 356400e921..d41cdc0171 100644 --- a/setup.cfg +++ b/setup.cfg @@ -390,6 +390,7 @@ openstack.network.v2 = subnet_pool_list = openstackclient.network.v2.subnet_pool:ListSubnetPool subnet_pool_set = openstackclient.network.v2.subnet_pool:SetSubnetPool subnet_pool_show = openstackclient.network.v2.subnet_pool:ShowSubnetPool + subnet_pool_unset = openstackclient.network.v2.subnet_pool:UnsetSubnetPool openstack.object_store.v1 = object_store_account_set = openstackclient.object.v1.account:SetAccount