Skip to content

Commit 3278b3a

Browse files
tang-chenTerryHowe
authored andcommitted
Router: Add "router list" command using SDK
Add "router list" command. It takes one "--long" option. By default, the command will print router id, name, status, admin state up, distributed, ha and project id. With "--long" option, it will also print routes and external gateway info. Change-Id: I9d21904c41c11ee1fa107f985744878a1dc2f970 Implements: blueprint neutron-client Partial-bug: #1519503
1 parent 185412f commit 3278b3a

5 files changed

Lines changed: 220 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
======
2+
router
3+
======
4+
5+
Network v2
6+
7+
router list
8+
-----------
9+
10+
List routers
11+
12+
.. program:: router list
13+
.. code:: bash
14+
15+
os router list
16+
[--long]
17+
18+
.. option:: --long
19+
20+
List additional fields in output

doc/source/commands.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ referring to both Compute and Volume quotas.
107107
* ``request token``: (**Identity**) temporary OAuth-based token
108108
* ``role``: (**Identity**) a policy object used to determine authorization
109109
* ``role assignment``: (**Identity**) a relationship between roles, users or groups, and domains or projects
110+
* ``router``: (**Network**) - a virtual router
110111
* ``security group``: (**Compute**, **Network**) - groups of network access rules
111112
* ``security group rule``: (**Compute**, **Network**) - the individual rules that define protocol/IP/port access
112113
* ``server``: (**Compute**) virtual machine instance
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
#
13+
14+
"""Router action implementations"""
15+
16+
import json
17+
import logging
18+
19+
from cliff import lister
20+
21+
from openstackclient.common import utils
22+
23+
24+
def _format_admin_state(state):
25+
return 'UP' if state else 'DOWN'
26+
27+
28+
def _format_external_gateway_info(info):
29+
try:
30+
return json.dumps(info)
31+
except (TypeError, KeyError):
32+
return ''
33+
34+
35+
_formatters = {
36+
'admin_state_up': _format_admin_state,
37+
'external_gateway_info': _format_external_gateway_info,
38+
}
39+
40+
41+
class ListRouter(lister.Lister):
42+
"""List routers"""
43+
44+
log = logging.getLogger(__name__ + '.ListRouter')
45+
46+
def get_parser(self, prog_name):
47+
parser = super(ListRouter, self).get_parser(prog_name)
48+
parser.add_argument(
49+
'--long',
50+
action='store_true',
51+
default=False,
52+
help='List additional fields in output',
53+
)
54+
return parser
55+
56+
def take_action(self, parsed_args):
57+
self.log.debug('take_action(%s)' % parsed_args)
58+
client = self.app.client_manager.network
59+
60+
columns = (
61+
'id',
62+
'name',
63+
'status',
64+
'admin_state_up',
65+
'distributed',
66+
'ha',
67+
'tenant_id',
68+
)
69+
column_headers = (
70+
'ID',
71+
'Name',
72+
'Status',
73+
'State',
74+
'Distributed',
75+
'HA',
76+
'Project',
77+
)
78+
if parsed_args.long:
79+
columns = columns + (
80+
'routes',
81+
'external_gateway_info',
82+
)
83+
column_headers = column_headers + (
84+
'Routes',
85+
'External gateway info',
86+
)
87+
88+
data = client.routers()
89+
return (column_headers,
90+
(utils.get_item_properties(
91+
s, columns,
92+
formatters=_formatters,
93+
) for s in data))
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
#
13+
14+
import mock
15+
16+
from openstackclient.network.v2 import router
17+
from openstackclient.tests.network.v2 import fakes as network_fakes
18+
19+
20+
class TestRouter(network_fakes.TestNetworkV2):
21+
22+
def setUp(self):
23+
super(TestRouter, self).setUp()
24+
25+
# Get a shortcut to the network client
26+
self.network = self.app.client_manager.network
27+
28+
29+
class TestListRouter(TestRouter):
30+
31+
# The routers going to be listed up.
32+
routers = network_fakes.FakeRouter.create_routers(count=3)
33+
34+
columns = (
35+
'ID',
36+
'Name',
37+
'Status',
38+
'State',
39+
'Distributed',
40+
'HA',
41+
'Project',
42+
)
43+
columns_long = columns + (
44+
'Routes',
45+
'External gateway info',
46+
)
47+
48+
data = []
49+
for r in routers:
50+
data.append((
51+
r.id,
52+
r.name,
53+
r.status,
54+
router._format_admin_state(r.admin_state_up),
55+
r.distributed,
56+
r.ha,
57+
r.tenant_id,
58+
))
59+
data_long = []
60+
for i in range(0, len(routers)):
61+
r = routers[i]
62+
data_long.append(
63+
data[i] + (
64+
r.routes,
65+
router._format_external_gateway_info(r.external_gateway_info),
66+
)
67+
)
68+
69+
def setUp(self):
70+
super(TestListRouter, self).setUp()
71+
72+
# Get the command object to test
73+
self.cmd = router.ListRouter(self.app, self.namespace)
74+
75+
self.network.routers = mock.Mock(return_value=self.routers)
76+
77+
def test_router_list_no_options(self):
78+
arglist = []
79+
verifylist = [
80+
('long', False),
81+
]
82+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
83+
84+
# DisplayCommandBase.take_action() returns two tuples
85+
columns, data = self.cmd.take_action(parsed_args)
86+
87+
self.network.routers.assert_called_with()
88+
self.assertEqual(self.columns, columns)
89+
self.assertEqual(self.data, list(data))
90+
91+
def test_router_list_long(self):
92+
arglist = [
93+
'--long',
94+
]
95+
verifylist = [
96+
('long', True),
97+
]
98+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
99+
100+
# DisplayCommandBase.take_action() returns two tuples
101+
columns, data = self.cmd.take_action(parsed_args)
102+
103+
self.network.routers.assert_called_with()
104+
self.assertEqual(self.columns_long, columns)
105+
self.assertEqual(self.data_long, list(data))

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ openstack.network.v2 =
332332
network_list = openstackclient.network.v2.network:ListNetwork
333333
network_set = openstackclient.network.v2.network:SetNetwork
334334
network_show = openstackclient.network.v2.network:ShowNetwork
335+
router_list = openstackclient.network.v2.router:ListRouter
335336

336337
openstack.object_store.v1 =
337338
object_store_account_set = openstackclient.object.v1.account:SetAccount

0 commit comments

Comments
 (0)