Skip to content

Commit 01c19ef

Browse files
author
Tang Chen
committed
Router: Add --route and --clear-routes options to "router set" command
--route option is used to set routes to the router. It is used like this: --route destination=subnet,gateway=ip-address destination: destination subnet CIDR gateway: nexthop IP address --clear-routes is used to clear all routes on the router. Change-Id: I97ce4871113c684b29c98cdad4dec9cc80ed20f7 Implements: blueprint neutron-client Partial-bug: #1519503
1 parent ada06f4 commit 01c19ef

3 files changed

Lines changed: 95 additions & 4 deletions

File tree

doc/source/command-objects/router.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Set router properties
9393
[--name <name>]
9494
[--enable | --disable]
9595
[--distributed | --centralized]
96+
[--route destination=<subnet>,gateway=<ip-address> | --clear-routes]
9697
<router>
9798
9899
.. option:: --name <name>
@@ -115,6 +116,17 @@ Set router properties
115116
116117
Set router to centralized mode (disabled router only)
117118
119+
.. option:: --route destination=<subnet>,gateway=<ip-address>
120+
121+
Routes associated with the router.
122+
Repeat this option to set multiple routes.
123+
destination: destination subnet (in CIDR notation).
124+
gateway: nexthop IP address.
125+
126+
.. option:: --clear-routes
127+
128+
Clear routes associated with the router
129+
118130
.. _router_set-router:
119131
.. describe:: <router>
120132

openstackclient/network/v2/router.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from openstackclient.common import command
1919
from openstackclient.common import exceptions
20+
from openstackclient.common import parseractions
2021
from openstackclient.common import utils
2122
from openstackclient.identity import common as identity_common
2223

@@ -51,6 +52,12 @@ def _get_attrs(client_manager, parsed_args):
5152
if ('availability_zone_hints' in parsed_args
5253
and parsed_args.availability_zone_hints is not None):
5354
attrs['availability_zone_hints'] = parsed_args.availability_zone_hints
55+
56+
if 'clear_routes' in parsed_args and parsed_args.clear_routes:
57+
attrs['routes'] = []
58+
elif 'routes' in parsed_args and parsed_args.routes is not None:
59+
attrs['routes'] = parsed_args.routes
60+
5461
# "router set" command doesn't support setting project.
5562
if 'project' in parsed_args and parsed_args.project is not None:
5663
identity_client = client_manager.identity
@@ -63,7 +70,6 @@ def _get_attrs(client_manager, parsed_args):
6370

6471
# TODO(tangchen): Support getting 'ha' property.
6572
# TODO(tangchen): Support getting 'external_gateway_info' property.
66-
# TODO(tangchen): Support getting 'routes' property.
6773

6874
return attrs
6975

@@ -250,6 +256,25 @@ def get_parser(self, prog_name):
250256
action='store_false',
251257
help="Set router to centralized mode (disabled router only)",
252258
)
259+
routes_group = parser.add_mutually_exclusive_group()
260+
routes_group.add_argument(
261+
'--route',
262+
metavar='destination=<subnet>,gateway=<ip-address>',
263+
action=parseractions.MultiKeyValueAction,
264+
dest='routes',
265+
default=None,
266+
required_keys=['destination', 'gateway'],
267+
help="Routes associated with the router. "
268+
"Repeat this option to set multiple routes. "
269+
"destination: destination subnet (in CIDR notation). "
270+
"gateway: nexthop IP address.",
271+
)
272+
routes_group.add_argument(
273+
'--clear-routes',
274+
dest='clear_routes',
275+
action='store_true',
276+
help="Clear routes associated with the router",
277+
)
253278

254279
# TODO(tangchen): Support setting 'ha' property in 'router set'
255280
# command. It appears that changing the ha state is supported by
@@ -258,9 +283,6 @@ def get_parser(self, prog_name):
258283
# TODO(tangchen): Support setting 'external_gateway_info' property in
259284
# 'router set' command.
260285

261-
# TODO(tangchen): Support setting 'routes' property in 'router set'
262-
# command.
263-
264286
return parser
265287

266288
def take_action(self, parsed_args):

openstackclient/tests/network/v2/test_router.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,63 @@ def test_set_distributed_centralized(self):
306306
self.assertRaises(tests_utils.ParserException, self.check_parser,
307307
self.cmd, arglist, verifylist)
308308

309+
def test_set_route(self):
310+
arglist = [
311+
self._router.name,
312+
'--route', 'destination=10.20.30.0/24,gateway=10.20.30.1',
313+
]
314+
verifylist = [
315+
('router', self._router.name),
316+
('routes', [{'destination': '10.20.30.0/24',
317+
'gateway': '10.20.30.1'}]),
318+
]
319+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
320+
321+
result = self.cmd.take_action(parsed_args)
322+
323+
attrs = {
324+
'routes': [{'destination': '10.20.30.0/24',
325+
'gateway': '10.20.30.1'}],
326+
}
327+
self.network.update_router.assert_called_with(self._router, **attrs)
328+
self.assertIsNone(result)
329+
330+
def test_set_clear_routes(self):
331+
arglist = [
332+
self._router.name,
333+
'--clear-routes',
334+
]
335+
verifylist = [
336+
('router', self._router.name),
337+
('clear_routes', True),
338+
]
339+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
340+
341+
result = self.cmd.take_action(parsed_args)
342+
343+
attrs = {
344+
'routes': [],
345+
}
346+
self.network.update_router.assert_called_with(self._router, **attrs)
347+
self.assertIsNone(result)
348+
349+
def test_set_route_clear_routes(self):
350+
arglist = [
351+
self._router.name,
352+
'--route', 'destination=10.20.30.0/24,gateway=10.20.30.1',
353+
'--clear-routes',
354+
]
355+
verifylist = [
356+
('router', self._router.name),
357+
('routes', [{'destination': '10.20.30.0/24',
358+
'gateway': '10.20.30.1'}]),
359+
('clear_routes', True),
360+
]
361+
362+
# Argument parse failing should bail here
363+
self.assertRaises(tests_utils.ParserException, self.check_parser,
364+
self.cmd, arglist, verifylist)
365+
309366
def test_set_nothing(self):
310367
arglist = [self._router.name, ]
311368
verifylist = [('router', self._router.name), ]

0 commit comments

Comments
 (0)