From f236869b77cb61e09b4be11aff4d88c83927e38e Mon Sep 17 00:00:00 2001 From: David Shrewsbury Date: Tue, 1 Mar 2016 14:18:04 -0500 Subject: [PATCH] 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 --- ...e_server_network_fix-c4a56b31d2850a4b.yaml | 6 +++++ shade/openstackcloud.py | 2 +- shade/tests/unit/test_create_server.py | 25 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/create_server_network_fix-c4a56b31d2850a4b.yaml 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')