summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/source/command-objects/router.rst24
-rw-r--r--openstackclient/network/v2/router.py43
-rw-r--r--openstackclient/tests/network/v2/test_router.py51
-rw-r--r--releasenotes/notes/unset-router-7b0cbd9518bb1de6.yaml7
-rw-r--r--setup.cfg1
5 files changed, 126 insertions, 0 deletions
diff --git a/doc/source/command-objects/router.rst b/doc/source/command-objects/router.rst
index 9ca7661e..9d25dc6f 100644
--- a/doc/source/command-objects/router.rst
+++ b/doc/source/command-objects/router.rst
@@ -240,3 +240,27 @@ Display router details
.. describe:: <router>
Router to display (name or ID)
+
+router unset
+------------
+
+Unset router properties
+
+.. program:: router unset
+.. code:: bash
+
+ os router unset
+ [--route destination=<subnet>,gateway=<ip-address>]
+ <router>
+
+.. option:: --route destination=<subnet>,gateway=<ip-address>
+
+ Routes to be removed from the router
+ destination: destination subnet (in CIDR notation)
+ gateway: nexthop IP address
+ (repeat option to unset multiple routes)
+
+.. _router_unset-router:
+.. describe:: <router>
+
+ Router to modify (name or ID)
diff --git a/openstackclient/network/v2/router.py b/openstackclient/network/v2/router.py
index 6271a878..8a09543f 100644
--- a/openstackclient/network/v2/router.py
+++ b/openstackclient/network/v2/router.py
@@ -14,6 +14,7 @@
"""Router action implementations"""
import argparse
+import copy
import json
import logging
@@ -462,3 +463,45 @@ class ShowRouter(command.ShowOne):
columns = _get_columns(obj)
data = utils.get_item_properties(obj, columns, formatters=_formatters)
return (columns, data)
+
+
+class UnsetRouter(command.Command):
+ """Unset router properties"""
+
+ def get_parser(self, prog_name):
+ parser = super(UnsetRouter, self).get_parser(prog_name)
+ parser.add_argument(
+ '--route',
+ metavar='destination=<subnet>,gateway=<ip-address>',
+ action=parseractions.MultiKeyValueAction,
+ dest='routes',
+ default=None,
+ required_keys=['destination', 'gateway'],
+ help=_("Routes to be removed from the router "
+ "destination: destination subnet (in CIDR notation) "
+ "gateway: nexthop IP address "
+ "(repeat option to unset multiple routes)"))
+ parser.add_argument(
+ 'router',
+ metavar="<router>",
+ help=_("Router to modify (name or ID)")
+ )
+ return parser
+
+ def take_action(self, parsed_args):
+ client = self.app.client_manager.network
+ obj = client.find_router(parsed_args.router, ignore_missing=False)
+ tmp_routes = copy.deepcopy(obj.routes)
+ attrs = {}
+ if parsed_args.routes:
+ try:
+ for route in parsed_args.routes:
+ tmp_routes.remove(route)
+ except ValueError:
+ msg = (_("Router does not contain route %s") % route)
+ raise exceptions.CommandError(msg)
+ for route in tmp_routes:
+ route['nexthop'] = route.pop('gateway')
+ attrs['routes'] = tmp_routes
+ if attrs:
+ client.update_router(obj, **attrs)
diff --git a/openstackclient/tests/network/v2/test_router.py b/openstackclient/tests/network/v2/test_router.py
index e3da253a..1ef4707b 100644
--- a/openstackclient/tests/network/v2/test_router.py
+++ b/openstackclient/tests/network/v2/test_router.py
@@ -698,3 +698,54 @@ class TestShowRouter(TestRouter):
self._router.name, ignore_missing=False)
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data)
+
+
+class TestUnsetRouter(TestRouter):
+
+ def setUp(self):
+ super(TestUnsetRouter, self).setUp()
+ self._testrouter = network_fakes.FakeRouter.create_one_router(
+ {'routes': [{"destination": "192.168.101.1/24",
+ "gateway": "172.24.4.3"},
+ {"destination": "192.168.101.2/24",
+ "gateway": "172.24.4.3"}], })
+ self.fake_subnet = network_fakes.FakeSubnet.create_one_subnet()
+ self.network.find_router = mock.Mock(return_value=self._testrouter)
+ self.network.update_router = mock.Mock(return_value=None)
+ # Get the command object to test
+ self.cmd = router.UnsetRouter(self.app, self.namespace)
+
+ def test_unset_router_params(self):
+ arglist = [
+ '--route', 'destination=192.168.101.1/24,gateway=172.24.4.3',
+ self._testrouter.name,
+ ]
+ verifylist = [
+ ('routes', [
+ {"destination": "192.168.101.1/24", "gateway": "172.24.4.3"}]),
+ ]
+
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+ result = self.cmd.take_action(parsed_args)
+
+ attrs = {
+ 'routes': [{"destination": "192.168.101.2/24",
+ "nexthop": "172.24.4.3"}],
+ }
+ self.network.update_router.assert_called_once_with(
+ self._testrouter, **attrs)
+ self.assertIsNone(result)
+
+ def test_unset_router_wrong_routes(self):
+ arglist = [
+ '--route', 'destination=192.168.101.1/24,gateway=172.24.4.2',
+ self._testrouter.name,
+ ]
+ verifylist = [
+ ('routes', [
+ {"destination": "192.168.101.1/24", "gateway": "172.24.4.2"}]),
+ ]
+
+ 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-router-7b0cbd9518bb1de6.yaml b/releasenotes/notes/unset-router-7b0cbd9518bb1de6.yaml
new file mode 100644
index 00000000..6811c84e
--- /dev/null
+++ b/releasenotes/notes/unset-router-7b0cbd9518bb1de6.yaml
@@ -0,0 +1,7 @@
+---
+features:
+ - |
+ Add a new command ``router unset`` to clear the information
+ of routes from the router.
+ [ Blueprint `network-property-unset <https://blueprints.launchpad.net/python-openstackclient/+spec/network-property-unset>`_]
+
diff --git a/setup.cfg b/setup.cfg
index 5e26d38d..d7e63b95 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -366,6 +366,7 @@ openstack.network.v2 =
router_remove_subnet = openstackclient.network.v2.router:RemoveSubnetFromRouter
router_set = openstackclient.network.v2.router:SetRouter
router_show = openstackclient.network.v2.router:ShowRouter
+ router_unset = openstackclient.network.v2.router:UnsetRouter
security_group_create = openstackclient.network.v2.security_group:CreateSecurityGroup
security_group_delete = openstackclient.network.v2.security_group:DeleteSecurityGroup