Skip to content

Commit 5d62981

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Introduce overwrite functionality in osc router set"
2 parents 6d63085 + 25104c7 commit 5d62981

4 files changed

Lines changed: 60 additions & 28 deletions

File tree

doc/source/command-objects/router.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ Set router properties
259259
260260
.. option:: --no-route
261261
262-
Clear routes associated with the router
262+
Clear routes associated with the router.
263+
Specify both --route and --no-route to overwrite
264+
current value of route.
263265
264266
.. option:: --ha
265267

openstackclient/network/v2/router.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ def _get_attrs(client_manager, parsed_args):
9898
).id
9999
attrs['tenant_id'] = project_id
100100

101-
# TODO(tangchen): Support getting 'external_gateway_info' property.
102-
103101
return attrs
104102

105103

@@ -464,7 +462,8 @@ def get_parser(self, prog_name):
464462
help=_("Set router to centralized mode (disabled router only)")
465463
)
466464
routes_group = parser.add_mutually_exclusive_group()
467-
routes_group.add_argument(
465+
# ToDo(Reedip):Remove mutual exclusiveness once clear-routes is removed
466+
parser.add_argument(
468467
'--route',
469468
metavar='destination=<subnet>,gateway=<ip-address>',
470469
action=parseractions.MultiKeyValueAction,
@@ -479,7 +478,9 @@ def get_parser(self, prog_name):
479478
routes_group.add_argument(
480479
'--no-route',
481480
action='store_true',
482-
help=_("Clear routes associated with the router")
481+
help=_("Clear routes associated with the router. "
482+
"Specify both --route and --no-route to overwrite "
483+
"current value of route.")
483484
)
484485
routes_group.add_argument(
485486
'--clear-routes',
@@ -539,20 +540,22 @@ def take_action(self, parsed_args):
539540
attrs['ha'] = True
540541
elif parsed_args.no_ha:
541542
attrs['ha'] = False
542-
if parsed_args.no_route:
543-
attrs['routes'] = []
544-
elif parsed_args.clear_routes:
545-
attrs['routes'] = []
543+
if parsed_args.clear_routes:
546544
LOG.warning(_(
547545
'The --clear-routes option is deprecated, '
548546
'please use --no-route instead.'
549547
))
550-
elif parsed_args.routes is not None:
551-
# Map the route keys and append to the current routes.
552-
# The REST API will handle route validation and duplicates.
548+
549+
if parsed_args.routes is not None:
553550
for route in parsed_args.routes:
554551
route['nexthop'] = route.pop('gateway')
555-
attrs['routes'] = obj.routes + parsed_args.routes
552+
attrs['routes'] = parsed_args.routes
553+
if not (parsed_args.no_route or parsed_args.clear_routes):
554+
# Map the route keys and append to the current routes.
555+
# The REST API will handle route validation and duplicates.
556+
attrs['routes'] += obj.routes
557+
elif parsed_args.no_route or parsed_args.clear_routes:
558+
attrs['routes'] = []
556559
if (parsed_args.disable_snat or parsed_args.enable_snat or
557560
parsed_args.fixed_ip) and not parsed_args.external_gateway:
558561
msg = (_("You must specify '--external-gateway' in order"

openstackclient/tests/unit/network/v2/test_router.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -704,10 +704,10 @@ def test_set_route(self):
704704
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
705705

706706
result = self.cmd.take_action(parsed_args)
707-
707+
routes = [{'destination': '10.20.30.0/24',
708+
'nexthop': '10.20.30.1'}]
708709
attrs = {
709-
'routes': self._router.routes + [{'destination': '10.20.30.0/24',
710-
'nexthop': '10.20.30.1'}],
710+
'routes': routes + self._router.routes
711711
}
712712
self.network.update_router.assert_called_once_with(
713713
self._router, **attrs)
@@ -733,21 +733,31 @@ def test_set_no_route(self):
733733
self._router, **attrs)
734734
self.assertIsNone(result)
735735

736-
def test_set_route_no_route(self):
736+
def test_set_route_overwrite_route(self):
737+
_testrouter = network_fakes.FakeRouter.create_one_router(
738+
{'routes': [{"destination": "10.0.0.2",
739+
"nexthop": "1.1.1.1"}]})
740+
self.network.find_router = mock.Mock(return_value=_testrouter)
737741
arglist = [
738-
self._router.name,
742+
_testrouter.name,
739743
'--route', 'destination=10.20.30.0/24,gateway=10.20.30.1',
740744
'--no-route',
741745
]
742746
verifylist = [
743-
('router', self._router.name),
747+
('router', _testrouter.name),
744748
('routes', [{'destination': '10.20.30.0/24',
745749
'gateway': '10.20.30.1'}]),
746750
('no_route', True),
747751
]
748-
749-
self.assertRaises(tests_utils.ParserException, self.check_parser,
750-
self.cmd, arglist, verifylist)
752+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
753+
result = self.cmd.take_action(parsed_args)
754+
attrs = {
755+
'routes': [{'destination': '10.20.30.0/24',
756+
'nexthop': '10.20.30.1'}]
757+
}
758+
self.network.update_router.assert_called_once_with(
759+
_testrouter, **attrs)
760+
self.assertIsNone(result)
751761

752762
def test_set_clear_routes(self):
753763
arglist = [
@@ -769,21 +779,31 @@ def test_set_clear_routes(self):
769779
self._router, **attrs)
770780
self.assertIsNone(result)
771781

772-
def test_set_route_clear_routes(self):
782+
def test_overwrite_route_clear_routes(self):
783+
_testrouter = network_fakes.FakeRouter.create_one_router(
784+
{'routes': [{"destination": "10.0.0.2",
785+
"nexthop": "1.1.1.1"}]})
786+
self.network.find_router = mock.Mock(return_value=_testrouter)
773787
arglist = [
774-
self._router.name,
788+
_testrouter.name,
775789
'--route', 'destination=10.20.30.0/24,gateway=10.20.30.1',
776790
'--clear-routes',
777791
]
778792
verifylist = [
779-
('router', self._router.name),
793+
('router', _testrouter.name),
780794
('routes', [{'destination': '10.20.30.0/24',
781795
'gateway': '10.20.30.1'}]),
782796
('clear_routes', True),
783797
]
784-
785-
self.assertRaises(tests_utils.ParserException, self.check_parser,
786-
self.cmd, arglist, verifylist)
798+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
799+
result = self.cmd.take_action(parsed_args)
800+
attrs = {
801+
'routes': [{'destination': '10.20.30.0/24',
802+
'nexthop': '10.20.30.1'}]
803+
}
804+
self.network.update_router.assert_called_once_with(
805+
_testrouter, **attrs)
806+
self.assertIsNone(result)
787807

788808
def test_set_nothing(self):
789809
arglist = [
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
features:
3+
- |
4+
Add support to overwrite routes in a router instance, using ``--router``
5+
and ``--no-router`` option in ``osc router set``.
6+
[ Blueprint `allow-overwrite-set-options <https://blueprints.launchpad.net/python-openstackclient/+spec/allow-overwrite-set-options>`_]
7+

0 commit comments

Comments
 (0)