diff --git a/releasenotes/notes/create_server_network_fix-c4a56b31d2850a4b.yaml b/releasenotes/notes/create_server_network_fix-c4a56b31d2850a4b.yaml new file mode 100644 index 000000000..9f9bd5474 --- /dev/null +++ b/releasenotes/notes/create_server_network_fix-c4a56b31d2850a4b.yaml @@ -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. diff --git a/shade/openstackcloud.py b/shade/openstackcloud.py index 80998df46..6caeffacc 100644 --- a/shade/openstackcloud.py +++ b/shade/openstackcloud.py @@ -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( diff --git a/shade/tests/unit/test_create_server.py b/shade/tests/unit/test_create_server.py index 211ac977b..5ff0b1d7e 100644 --- a/shade/tests/unit/test_create_server.py +++ b/shade/tests/unit/test_create_server.py @@ -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')