Migrate "network set" command to use SDK.

This patch makes "network set" command use sdk.

Since we have to keep the other commands runnable with
the old network client, we use a temporary method to
create sdk network client. And as a result, the tests
need to patch a method to fake the temporary method,
which will be removed at last.

Change-Id: I794ac4b82d9200747298f1a9ee44611140e9b6d0
Implements: blueprint neutron-client
Co-Authored-By: Terry Howe <terrylhowe@gmail.com>
Co-Authored-By: Tang Chen <tangchen@cn.fujitsu.com>
This commit is contained in:
Tang Chen 2015-12-05 11:16:44 +08:00
parent 511e862294
commit 45c644d428
2 changed files with 40 additions and 31 deletions

View File

@ -25,7 +25,6 @@ from openstack import connection
from openstackclient.common import exceptions
from openstackclient.common import utils
from openstackclient.identity import common as identity_common
from openstackclient.network import common
def _format_admin_state(item):
@ -288,21 +287,23 @@ class SetNetwork(command.Command):
def take_action(self, parsed_args):
self.log.debug('take_action(%s)' % parsed_args)
self.app.client_manager.network = \
_make_client_sdk(self.app.client_manager)
client = self.app.client_manager.network
_id = common.find(client, 'network', 'networks',
parsed_args.identifier)
body = {}
obj = client.find_network(parsed_args.identifier, ignore_missing=False)
if parsed_args.name is not None:
body['name'] = str(parsed_args.name)
obj.name = str(parsed_args.name)
if parsed_args.admin_state is not None:
body['admin_state_up'] = parsed_args.admin_state
obj.admin_state_up = parsed_args.admin_state
if parsed_args.shared is not None:
body['shared'] = parsed_args.shared
if body == {}:
obj.shared = parsed_args.shared
if not obj.is_dirty:
msg = "Nothing specified to be set"
raise exceptions.CommandError(msg)
update_method = getattr(client, "update_network")
update_method(_id, {'network': body})
client.update_network(obj)
return

View File

@ -458,31 +458,35 @@ class TestListNetwork(TestNetwork):
self.assertEqual(self.data_long, list(data))
@mock.patch('openstackclient.network.v2.network._make_client_sdk')
class TestSetNetwork(TestNetwork):
# The network to set.
_network = network_fakes.FakeNetwork.create_one_network()
def setUp(self):
super(TestSetNetwork, self).setUp()
self.network.update_network = mock.Mock(
return_value=None
)
self.network.update_network = mock.Mock(return_value=None)
self.network.list_networks = mock.Mock(
return_value={RESOURCES: [copy.deepcopy(RECORD)]}
)
self.network.find_network = mock.Mock(return_value=self._network)
# Get the command object to test
self.cmd = network.SetNetwork(self.app, self.namespace)
def test_set_this(self):
def test_set_this(self, _make_client_sdk):
_make_client_sdk.return_value = self.app.client_manager.network
self._network.is_dirty = True
arglist = [
FAKE_NAME,
self._network.name,
'--enable',
'--name', 'noob',
'--share',
]
verifylist = [
('identifier', FAKE_NAME),
('identifier', self._network.name),
('admin_state', True),
('name', 'noob'),
('shared', True),
@ -491,19 +495,21 @@ class TestSetNetwork(TestNetwork):
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
exp = {'admin_state_up': True, 'name': 'noob', 'shared': True}
exp_record = {RESOURCE: exp}
self.network.update_network.assert_called_with(FAKE_ID, exp_record)
self.network.update_network.assert_called_with(self._network)
self.assertEqual(None, result)
def test_set_that(self):
def test_set_that(self, _make_client_sdk):
_make_client_sdk.return_value = self.app.client_manager.network
self._network.is_dirty = True
arglist = [
FAKE_NAME,
self._network.name,
'--disable',
'--no-share',
]
verifylist = [
('identifier', FAKE_NAME),
('identifier', self._network.name),
('admin_state', False),
('shared', False),
]
@ -511,14 +517,16 @@ class TestSetNetwork(TestNetwork):
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
exp = {'admin_state_up': False, 'shared': False}
exp_record = {RESOURCE: exp}
self.network.update_network.assert_called_with(FAKE_ID, exp_record)
self.network.update_network.assert_called_with(self._network)
self.assertEqual(None, result)
def test_set_nothing(self):
arglist = [FAKE_NAME, ]
verifylist = [('identifier', FAKE_NAME), ]
def test_set_nothing(self, _make_client_sdk):
_make_client_sdk.return_value = self.app.client_manager.network
self._network.is_dirty = False
arglist = [self._network.name, ]
verifylist = [('identifier', self._network.name), ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.assertRaises(exceptions.CommandError, self.cmd.take_action,