Skip to content

Commit ed64788

Browse files
author
reedip
committed
Add command to unset information from Routers
This patch introduces the ``router unset`` command to clear the routing information from the routers. Implements: blueprint network-property-unset Change-Id: Iac8d32ca42fb28878805b4b58ab411b67fa6555b
1 parent 7cda2b2 commit ed64788

5 files changed

Lines changed: 126 additions & 0 deletions

File tree

doc/source/command-objects/router.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,27 @@ Display router details
240240
.. describe:: <router>
241241
242242
Router to display (name or ID)
243+
244+
router unset
245+
------------
246+
247+
Unset router properties
248+
249+
.. program:: router unset
250+
.. code:: bash
251+
252+
os router unset
253+
[--route destination=<subnet>,gateway=<ip-address>]
254+
<router>
255+
256+
.. option:: --route destination=<subnet>,gateway=<ip-address>
257+
258+
Routes to be removed from the router
259+
destination: destination subnet (in CIDR notation)
260+
gateway: nexthop IP address
261+
(repeat option to unset multiple routes)
262+
263+
.. _router_unset-router:
264+
.. describe:: <router>
265+
266+
Router to modify (name or ID)

openstackclient/network/v2/router.py

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

1616
import argparse
17+
import copy
1718
import json
1819
import logging
1920

@@ -462,3 +463,45 @@ def take_action(self, parsed_args):
462463
columns = _get_columns(obj)
463464
data = utils.get_item_properties(obj, columns, formatters=_formatters)
464465
return (columns, data)
466+
467+
468+
class UnsetRouter(command.Command):
469+
"""Unset router properties"""
470+
471+
def get_parser(self, prog_name):
472+
parser = super(UnsetRouter, self).get_parser(prog_name)
473+
parser.add_argument(
474+
'--route',
475+
metavar='destination=<subnet>,gateway=<ip-address>',
476+
action=parseractions.MultiKeyValueAction,
477+
dest='routes',
478+
default=None,
479+
required_keys=['destination', 'gateway'],
480+
help=_("Routes to be removed from the router "
481+
"destination: destination subnet (in CIDR notation) "
482+
"gateway: nexthop IP address "
483+
"(repeat option to unset multiple routes)"))
484+
parser.add_argument(
485+
'router',
486+
metavar="<router>",
487+
help=_("Router to modify (name or ID)")
488+
)
489+
return parser
490+
491+
def take_action(self, parsed_args):
492+
client = self.app.client_manager.network
493+
obj = client.find_router(parsed_args.router, ignore_missing=False)
494+
tmp_routes = copy.deepcopy(obj.routes)
495+
attrs = {}
496+
if parsed_args.routes:
497+
try:
498+
for route in parsed_args.routes:
499+
tmp_routes.remove(route)
500+
except ValueError:
501+
msg = (_("Router does not contain route %s") % route)
502+
raise exceptions.CommandError(msg)
503+
for route in tmp_routes:
504+
route['nexthop'] = route.pop('gateway')
505+
attrs['routes'] = tmp_routes
506+
if attrs:
507+
client.update_router(obj, **attrs)

openstackclient/tests/network/v2/test_router.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,3 +698,54 @@ def test_show_all_options(self):
698698
self._router.name, ignore_missing=False)
699699
self.assertEqual(self.columns, columns)
700700
self.assertEqual(self.data, data)
701+
702+
703+
class TestUnsetRouter(TestRouter):
704+
705+
def setUp(self):
706+
super(TestUnsetRouter, self).setUp()
707+
self._testrouter = network_fakes.FakeRouter.create_one_router(
708+
{'routes': [{"destination": "192.168.101.1/24",
709+
"gateway": "172.24.4.3"},
710+
{"destination": "192.168.101.2/24",
711+
"gateway": "172.24.4.3"}], })
712+
self.fake_subnet = network_fakes.FakeSubnet.create_one_subnet()
713+
self.network.find_router = mock.Mock(return_value=self._testrouter)
714+
self.network.update_router = mock.Mock(return_value=None)
715+
# Get the command object to test
716+
self.cmd = router.UnsetRouter(self.app, self.namespace)
717+
718+
def test_unset_router_params(self):
719+
arglist = [
720+
'--route', 'destination=192.168.101.1/24,gateway=172.24.4.3',
721+
self._testrouter.name,
722+
]
723+
verifylist = [
724+
('routes', [
725+
{"destination": "192.168.101.1/24", "gateway": "172.24.4.3"}]),
726+
]
727+
728+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
729+
result = self.cmd.take_action(parsed_args)
730+
731+
attrs = {
732+
'routes': [{"destination": "192.168.101.2/24",
733+
"nexthop": "172.24.4.3"}],
734+
}
735+
self.network.update_router.assert_called_once_with(
736+
self._testrouter, **attrs)
737+
self.assertIsNone(result)
738+
739+
def test_unset_router_wrong_routes(self):
740+
arglist = [
741+
'--route', 'destination=192.168.101.1/24,gateway=172.24.4.2',
742+
self._testrouter.name,
743+
]
744+
verifylist = [
745+
('routes', [
746+
{"destination": "192.168.101.1/24", "gateway": "172.24.4.2"}]),
747+
]
748+
749+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
750+
self.assertRaises(exceptions.CommandError,
751+
self.cmd.take_action, parsed_args)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
features:
3+
- |
4+
Add a new command ``router unset`` to clear the information
5+
of routes from the router.
6+
[ Blueprint `network-property-unset <https://blueprints.launchpad.net/python-openstackclient/+spec/network-property-unset>`_]
7+

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ openstack.network.v2 =
366366
router_remove_subnet = openstackclient.network.v2.router:RemoveSubnetFromRouter
367367
router_set = openstackclient.network.v2.router:SetRouter
368368
router_show = openstackclient.network.v2.router:ShowRouter
369+
router_unset = openstackclient.network.v2.router:UnsetRouter
369370

370371
security_group_create = openstackclient.network.v2.security_group:CreateSecurityGroup
371372
security_group_delete = openstackclient.network.v2.security_group:DeleteSecurityGroup

0 commit comments

Comments
 (0)