Fix create_server() with a named network

If 'network' was supplied to create_server() along with an empty
'nics' list, we would never attempt to find the network. The os_server
Ansible module uses the API this way. This treats an empty list the
same as if the 'nics' parameter were never supplied.

Change-Id: Idc844fab2c4c08f158c892104d065e5554ed90f3
This commit is contained in:
David Shrewsbury 2016-03-01 14:18:04 -05:00
parent a7fe2520ae
commit f236869b77
3 changed files with 32 additions and 1 deletions

View File

@ -0,0 +1,6 @@
---
fixes:
- The create_server() API call would not use the supplied 'network'
parameter if the 'nics' parameter was also supplied, even though it would
be an empty list. It now uses 'network' if 'nics' is not supplied or if
it is an empty list.

View File

@ -3350,7 +3350,7 @@ class OpenStackCloud(object):
raise OpenStackCloudException(
'nics parameter to create_server takes a list of dicts.'
' Got: {nics}'.format(nics=kwargs['nics']))
if network and 'nics' not in kwargs:
if network and ('nics' not in kwargs or not kwargs['nics']):
network_obj = self.get_network(name_or_id=network)
if not network_obj:
raise OpenStackCloudException(

View File

@ -250,3 +250,28 @@ class TestCreateServer(base.TestCase):
OpenStackCloudException, self.client.create_server,
'server-name', 'image-id', 'flavor-id',
wait=True)
@patch('shade.OpenStackCloud.nova_client')
@patch('shade.OpenStackCloud.get_network')
def test_create_server_network_with_no_nics(self, mock_get_network,
mock_nova):
"""
Verify that if 'network' is supplied, and 'nics' is not, that we
attempt to get the network for the server.
"""
self.client.create_server('server-name', 'image-id', 'flavor-id',
network='network-name')
mock_get_network.assert_called_once_with(name_or_id='network-name')
@patch('shade.OpenStackCloud.nova_client')
@patch('shade.OpenStackCloud.get_network')
def test_create_server_network_with_empty_nics(self,
mock_get_network,
mock_nova):
"""
Verify that if 'network' is supplied, along with an empty 'nics' list,
it's treated the same as if 'nics' were not included.
"""
self.client.create_server('server-name', 'image-id', 'flavor-id',
network='network-name', nics=[])
mock_get_network.assert_called_once_with(name_or_id='network-name')