Skip to content

Commit 5a38769

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Router: Add "router set" command using SDK"
2 parents 1c5528d + 8a805da commit 5a38769

4 files changed

Lines changed: 208 additions & 0 deletions

File tree

doc/source/command-objects/router.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,42 @@ List routers
7474
.. option:: --long
7575
7676
List additional fields in output
77+
78+
router set
79+
----------
80+
81+
Set router properties
82+
83+
.. program:: router set
84+
.. code:: bash
85+
86+
os router set
87+
[--name <name>]
88+
[--enable | --disable]
89+
[--distributed | --centralized]
90+
<router>
91+
92+
.. option:: --name <name>
93+
94+
Set router name
95+
96+
.. option:: --enable
97+
98+
Enable router
99+
100+
.. option:: --disable
101+
102+
Disable router
103+
104+
.. option:: --distributed
105+
106+
Set router to distributed mode (disabled router only)
107+
108+
.. option:: --centralized
109+
110+
Set router to centralized mode (disabled router only)
111+
112+
.. _router_set-router:
113+
.. describe:: <router>
114+
115+
Router to modify (name or ID)

openstackclient/network/v2/router.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from cliff import lister
2121
from cliff import show
2222

23+
from openstackclient.common import exceptions
2324
from openstackclient.common import utils
2425
from openstackclient.identity import common as identity_common
2526

@@ -49,6 +50,7 @@ def _get_attrs(client_manager, parsed_args):
4950
attrs['admin_state_up'] = parsed_args.admin_state_up
5051
if parsed_args.distributed is not None:
5152
attrs['distributed'] = parsed_args.distributed
53+
# "router set" command doesn't support setting project.
5254
if 'project' in parsed_args and parsed_args.project is not None:
5355
identity_client = client_manager.identity
5456
project_id = identity_common.find_project(
@@ -57,6 +59,11 @@ def _get_attrs(client_manager, parsed_args):
5759
parsed_args.project_domain,
5860
).id
5961
attrs['tenant_id'] = project_id
62+
63+
# TODO(tangchen): Support getting 'ha' property.
64+
# TODO(tangchen): Support getting 'external_gateway_info' property.
65+
# TODO(tangchen): Support getting 'routes' property.
66+
6067
return attrs
6168

6269

@@ -195,3 +202,74 @@ def take_action(self, parsed_args):
195202
s, columns,
196203
formatters=_formatters,
197204
) for s in data))
205+
206+
207+
class SetRouter(command.Command):
208+
"""Set router properties"""
209+
210+
log = logging.getLogger(__name__ + '.SetRouter')
211+
212+
def get_parser(self, prog_name):
213+
parser = super(SetRouter, self).get_parser(prog_name)
214+
parser.add_argument(
215+
'router',
216+
metavar="<router>",
217+
help=("Router to modify (name or ID)")
218+
)
219+
parser.add_argument(
220+
'--name',
221+
metavar='<name>',
222+
help='Set router name',
223+
)
224+
admin_group = parser.add_mutually_exclusive_group()
225+
admin_group.add_argument(
226+
'--enable',
227+
dest='admin_state_up',
228+
action='store_true',
229+
default=None,
230+
help='Enable router',
231+
)
232+
admin_group.add_argument(
233+
'--disable',
234+
dest='admin_state_up',
235+
action='store_false',
236+
help='Disable router',
237+
)
238+
distribute_group = parser.add_mutually_exclusive_group()
239+
distribute_group.add_argument(
240+
'--distributed',
241+
dest='distributed',
242+
action='store_true',
243+
default=None,
244+
help="Set router to distributed mode (disabled router only)",
245+
)
246+
distribute_group.add_argument(
247+
'--centralized',
248+
dest='distributed',
249+
action='store_false',
250+
help="Set router to centralized mode (disabled router only)",
251+
)
252+
253+
# TODO(tangchen): Support setting 'ha' property in 'router set'
254+
# command. It appears that changing the ha state is supported by
255+
# neutron under certain conditions.
256+
257+
# TODO(tangchen): Support setting 'external_gateway_info' property in
258+
# 'router set' command.
259+
260+
# TODO(tangchen): Support setting 'routes' property in 'router set'
261+
# command.
262+
263+
return parser
264+
265+
def take_action(self, parsed_args):
266+
self.log.debug('take_action(%s)' % parsed_args)
267+
client = self.app.client_manager.network
268+
obj = client.find_router(parsed_args.router, ignore_missing=False)
269+
270+
attrs = _get_attrs(self.app.client_manager, parsed_args)
271+
if attrs == {}:
272+
msg = "Nothing specified to be set"
273+
raise exceptions.CommandError(msg)
274+
275+
client.update_router(obj, **attrs)

openstackclient/tests/network/v2/test_router.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import mock
1515

16+
from openstackclient.common import exceptions
1617
from openstackclient.network.v2 import router
1718
from openstackclient.tests.network.v2 import fakes as network_fakes
1819
from openstackclient.tests import utils as tests_utils
@@ -194,3 +195,92 @@ def test_router_list_long(self):
194195
self.network.routers.assert_called_with()
195196
self.assertEqual(self.columns_long, columns)
196197
self.assertEqual(self.data_long, list(data))
198+
199+
200+
class TestSetRouter(TestRouter):
201+
202+
# The router to set.
203+
_router = network_fakes.FakeRouter.create_one_router()
204+
205+
def setUp(self):
206+
super(TestSetRouter, self).setUp()
207+
208+
self.network.update_router = mock.Mock(return_value=None)
209+
210+
self.network.find_router = mock.Mock(return_value=self._router)
211+
212+
# Get the command object to test
213+
self.cmd = router.SetRouter(self.app, self.namespace)
214+
215+
def test_set_this(self):
216+
arglist = [
217+
self._router.name,
218+
'--enable',
219+
'--distributed',
220+
'--name', 'noob',
221+
]
222+
verifylist = [
223+
('router', self._router.name),
224+
('admin_state_up', True),
225+
('distributed', True),
226+
('name', 'noob'),
227+
]
228+
229+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
230+
result = self.cmd.take_action(parsed_args)
231+
232+
attrs = {
233+
'admin_state_up': True,
234+
'distributed': True,
235+
'name': 'noob',
236+
}
237+
self.network.update_router.assert_called_with(self._router, **attrs)
238+
self.assertEqual(None, result)
239+
240+
def test_set_that(self):
241+
arglist = [
242+
self._router.name,
243+
'--disable',
244+
'--centralized',
245+
]
246+
verifylist = [
247+
('router', self._router.name),
248+
('admin_state_up', False),
249+
('distributed', False),
250+
]
251+
252+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
253+
result = self.cmd.take_action(parsed_args)
254+
255+
attrs = {
256+
'admin_state_up': False,
257+
'distributed': False,
258+
}
259+
self.network.update_router.assert_called_with(self._router, **attrs)
260+
self.assertEqual(None, result)
261+
262+
def test_set_distributed_centralized(self):
263+
arglist = [
264+
self._router.name,
265+
'--distributed',
266+
'--centralized',
267+
]
268+
verifylist = [
269+
('router', self._router.name),
270+
('distributed', True),
271+
('distributed', False),
272+
]
273+
274+
try:
275+
# Argument parse failing should bail here
276+
self.check_parser(self.cmd, arglist, verifylist)
277+
except tests_utils.ParserException:
278+
pass
279+
280+
def test_set_nothing(self):
281+
arglist = [self._router.name, ]
282+
verifylist = [('router', self._router.name), ]
283+
284+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
285+
self.assertRaises(exceptions.CommandError, self.cmd.take_action,
286+
parsed_args)

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ openstack.network.v2 =
335335
router_create = openstackclient.network.v2.router:CreateRouter
336336
router_delete = openstackclient.network.v2.router:DeleteRouter
337337
router_list = openstackclient.network.v2.router:ListRouter
338+
router_set = openstackclient.network.v2.router:SetRouter
338339

339340
openstack.object_store.v1 =
340341
object_store_account_set = openstackclient.object.v1.account:SetAccount

0 commit comments

Comments
 (0)