Skip to content

Commit bf9b0e5

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Fix router set --route option"
2 parents b33ee3d + a90c824 commit bf9b0e5

3 files changed

Lines changed: 39 additions & 11 deletions

File tree

openstackclient/network/v2/router.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,20 @@ def _format_external_gateway_info(info):
3434
return ''
3535

3636

37+
def _format_routes(routes):
38+
# Map the route keys to match --route option.
39+
for route in routes:
40+
if 'nexthop' in route:
41+
route['gateway'] = route.pop('nexthop')
42+
return utils.format_list_of_dicts(routes)
43+
44+
3745
_formatters = {
3846
'admin_state_up': _format_admin_state,
3947
'external_gateway_info': _format_external_gateway_info,
4048
'availability_zones': utils.format_list,
4149
'availability_zone_hints': utils.format_list,
50+
'routes': _format_routes,
4251
}
4352

4453

@@ -67,11 +76,6 @@ def _get_attrs(client_manager, parsed_args):
6776
and parsed_args.availability_zone_hints is not None):
6877
attrs['availability_zone_hints'] = parsed_args.availability_zone_hints
6978

70-
if 'clear_routes' in parsed_args and parsed_args.clear_routes:
71-
attrs['routes'] = []
72-
elif 'routes' in parsed_args and parsed_args.routes is not None:
73-
attrs['routes'] = parsed_args.routes
74-
7579
# "router set" command doesn't support setting project.
7680
if 'project' in parsed_args and parsed_args.project is not None:
7781
identity_client = client_manager.identity
@@ -393,7 +397,19 @@ def take_action(self, parsed_args):
393397
client = self.app.client_manager.network
394398
obj = client.find_router(parsed_args.router, ignore_missing=False)
395399

400+
# Get the common attributes.
396401
attrs = _get_attrs(self.app.client_manager, parsed_args)
402+
403+
# Get the route attributes.
404+
if parsed_args.clear_routes:
405+
attrs['routes'] = []
406+
elif parsed_args.routes is not None:
407+
# Map the route keys and append to the current routes.
408+
# The REST API will handle route validation and duplicates.
409+
for route in parsed_args.routes:
410+
route['nexthop'] = route.pop('gateway')
411+
attrs['routes'] = obj.routes + parsed_args.routes
412+
397413
if attrs == {}:
398414
msg = "Nothing specified to be set"
399415
raise exceptions.CommandError(msg)

openstackclient/tests/network/v2/test_router.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class TestCreateRouter(TestRouter):
138138
new_router.id,
139139
new_router.name,
140140
new_router.tenant_id,
141-
new_router.routes,
141+
router._format_routes(new_router.routes),
142142
new_router.status,
143143
)
144144

@@ -268,7 +268,7 @@ class TestListRouter(TestRouter):
268268
r = routers[i]
269269
data_long.append(
270270
data[i] + (
271-
r.routes,
271+
router._format_routes(r.routes),
272272
router._format_external_gateway_info(r.external_gateway_info),
273273
osc_utils.format_list(r.availability_zones),
274274
)
@@ -399,7 +399,10 @@ def test_remove_subnet_required_options(self):
399399
class TestSetRouter(TestRouter):
400400

401401
# The router to set.
402-
_router = network_fakes.FakeRouter.create_one_router()
402+
_default_route = {'destination': '10.20.20.0/24', 'nexthop': '10.20.30.1'}
403+
_router = network_fakes.FakeRouter.create_one_router(
404+
attrs={'routes': [_default_route]}
405+
)
403406

404407
def setUp(self):
405408
super(TestSetRouter, self).setUp()
@@ -491,8 +494,8 @@ def test_set_route(self):
491494
result = self.cmd.take_action(parsed_args)
492495

493496
attrs = {
494-
'routes': [{'destination': '10.20.30.0/24',
495-
'gateway': '10.20.30.1'}],
497+
'routes': self._router.routes + [{'destination': '10.20.30.0/24',
498+
'nexthop': '10.20.30.1'}],
496499
}
497500
self.network.update_router.assert_called_once_with(
498501
self._router, **attrs)
@@ -572,7 +575,7 @@ class TestShowRouter(TestRouter):
572575
_router.id,
573576
_router.name,
574577
_router.tenant_id,
575-
_router.routes,
578+
router._format_routes(_router.routes),
576579
_router.status,
577580
)
578581

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
fixes:
3+
- Fixed the ``--route`` option on the ``router set`` command
4+
which did not properly format the new routes to set resulting
5+
in a ``Bad Request`` error. In addition, the ``router create``,
6+
``router list`` and ``router show`` command output for routes
7+
was fixed to improve readability and to align with the
8+
``--route`` option on the ``router set`` command.
9+
[Bug `1564460 <https://bugs.launchpad.net/bugs/1564460>`_]

0 commit comments

Comments
 (0)