Skip to content

Commit c161a76

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Network: Add interfaces info in router show"
2 parents 67c8da4 + e7ef9e8 commit c161a76

3 files changed

Lines changed: 44 additions & 6 deletions

File tree

openstackclient/network/v2/router.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _format_admin_state(state):
3636
return 'UP' if state else 'DOWN'
3737

3838

39-
def _format_external_gateway_info(info):
39+
def _format_router_info(info):
4040
try:
4141
return json.dumps(info)
4242
except (TypeError, KeyError):
@@ -54,7 +54,7 @@ def _format_routes(routes):
5454
_formatters = {
5555
'admin_state_up': _format_admin_state,
5656
'is_admin_state_up': _format_admin_state,
57-
'external_gateway_info': _format_external_gateway_info,
57+
'external_gateway_info': _format_router_info,
5858
'availability_zones': utils.format_list,
5959
'availability_zone_hints': utils.format_list,
6060
'routes': _format_routes,
@@ -69,6 +69,8 @@ def _get_columns(item):
6969
'is_distributed': 'distributed',
7070
'is_admin_state_up': 'admin_state_up',
7171
}
72+
if hasattr(item, 'interfaces_info'):
73+
column_map['interfaces_info'] = 'interfaces_info'
7274
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
7375

7476

@@ -657,7 +659,22 @@ def get_parser(self, prog_name):
657659
def take_action(self, parsed_args):
658660
client = self.app.client_manager.network
659661
obj = client.find_router(parsed_args.router, ignore_missing=False)
662+
interfaces_info = []
663+
filters = {}
664+
filters['device_id'] = obj.id
665+
for port in client.ports(**filters):
666+
if port.device_owner != "network:router_gateway":
667+
for ip_spec in port.fixed_ips:
668+
int_info = {
669+
'port_id': port.id,
670+
'ip_address': ip_spec.get('ip_address'),
671+
'subnet_id': ip_spec.get('subnet_id')
672+
}
673+
interfaces_info.append(int_info)
674+
675+
setattr(obj, 'interfaces_info', interfaces_info)
660676
display_columns, columns = _get_columns(obj)
677+
_formatters['interfaces_info'] = _format_router_info
661678
data = utils.get_item_properties(obj, columns, formatters=_formatters)
662679

663680
return (display_columns, data)

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class TestCreateRouter(TestRouter):
137137
osc_utils.format_list(new_router.availability_zones),
138138
new_router.description,
139139
new_router.distributed,
140-
router._format_external_gateway_info(new_router.external_gateway_info),
140+
router._format_router_info(new_router.external_gateway_info),
141141
new_router.ha,
142142
new_router.id,
143143
new_router.name,
@@ -448,7 +448,7 @@ class TestListRouter(TestRouter):
448448
data_long.append(
449449
data[i] + (
450450
router._format_routes(r.routes),
451-
router._format_external_gateway_info(r.external_gateway_info),
451+
router._format_router_info(r.external_gateway_info),
452452
osc_utils.format_list(r.availability_zones),
453453
osc_utils.format_list(r.tags),
454454
)
@@ -459,7 +459,7 @@ class TestListRouter(TestRouter):
459459
data_long_no_az.append(
460460
data[i] + (
461461
router._format_routes(r.routes),
462-
router._format_external_gateway_info(r.external_gateway_info),
462+
router._format_router_info(r.external_gateway_info),
463463
osc_utils.format_list(r.tags),
464464
)
465465
)
@@ -1118,6 +1118,15 @@ class TestShowRouter(TestRouter):
11181118

11191119
# The router to set.
11201120
_router = network_fakes.FakeRouter.create_one_router()
1121+
_port = network_fakes.FakePort.create_one_port({
1122+
'device_owner': 'network:router_interface',
1123+
'device_id': _router.id
1124+
})
1125+
setattr(_router,
1126+
'interfaces_info',
1127+
[{'port_id': _port.id,
1128+
'ip_address': _port.fixed_ips[0]['ip_address'],
1129+
'subnet_id': _port.fixed_ips[0]['subnet_id']}])
11211130

11221131
columns = (
11231132
'admin_state_up',
@@ -1128,6 +1137,7 @@ class TestShowRouter(TestRouter):
11281137
'external_gateway_info',
11291138
'ha',
11301139
'id',
1140+
'interfaces_info',
11311141
'name',
11321142
'project_id',
11331143
'routes',
@@ -1140,9 +1150,10 @@ class TestShowRouter(TestRouter):
11401150
osc_utils.format_list(_router.availability_zones),
11411151
_router.description,
11421152
_router.distributed,
1143-
router._format_external_gateway_info(_router.external_gateway_info),
1153+
router._format_router_info(_router.external_gateway_info),
11441154
_router.ha,
11451155
_router.id,
1156+
router._format_router_info(_router.interfaces_info),
11461157
_router.name,
11471158
_router.tenant_id,
11481159
router._format_routes(_router.routes),
@@ -1154,6 +1165,7 @@ def setUp(self):
11541165
super(TestShowRouter, self).setUp()
11551166

11561167
self.network.find_router = mock.Mock(return_value=self._router)
1168+
self.network.ports = mock.Mock(return_value=[self._port])
11571169

11581170
# Get the command object to test
11591171
self.cmd = router.ShowRouter(self.app, self.namespace)
@@ -1178,6 +1190,9 @@ def test_show_all_options(self):
11781190

11791191
self.network.find_router.assert_called_once_with(
11801192
self._router.name, ignore_missing=False)
1193+
self.network.ports.assert_called_with(**{
1194+
'device_id': self._router.id
1195+
})
11811196
self.assertEqual(self.columns, columns)
11821197
self.assertEqual(self.data, data)
11831198

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- Add router interfaces info (as field ``interfaces_info``) to ``router show`` command.
4+
The information of router interface include port's ID, IP address,
5+
the subnet ID it belongs.
6+
[Bug `1675489 <https://bugs.launchpad.net/python-openstackclient/+bug/1675489>`_]

0 commit comments

Comments
 (0)