diff options
author | zhaolihui <zhaolh@awcloud.com> | 2018-07-12 11:12:54 +0000 |
---|---|---|
committer | Takashi NATSUME <natsume.takashi@lab.ntt.co.jp> | 2018-12-13 07:50:37 +0000 |
commit | b13ba0138f6ee2e10ce3b4b95aee37c6cebd1f20 (patch) | |
tree | 9f6345192bcf459868e01c754bdea75aca011df7 | |
parent | a789bd30ae07eff636e6ea3da4d5d8f9d46e8ff1 (diff) | |
download | python-novaclient-b13ba0138f6ee2e10ce3b4b95aee37c6cebd1f20.tar.gz |
Fix flavor keyerror when nova boot vm
When creating a server (the 'nova boot' command),
it calls 'POST /servers' API.
The response does not have 'addresses' and 'flavor' attributes.
When accessing 'networks' attribute in the '_print_server' function,
it calls 'GET /servers/{server_id}' and get the 'addresses' and
'flavor' attributes.
If 'GET /servers/{server_id}' fails, the server object does not have
the 'flavor' attribute, then KeyError is raised when accessing it.
Fix the issue by making it raise a CommandError
when the 'GET /servers/{server_id}' fails.
Co-Authored-By: Takashi Natsume <natsume.takashi@lab.ntt.co.jp>
Change-Id: I3ef096c61b0e05a637ab0c4a1027338fa87e4f09
Closes-Bug: #1781368
-rw-r--r-- | novaclient/tests/unit/v2/test_shell.py | 12 | ||||
-rw-r--r-- | novaclient/v2/servers.py | 2 | ||||
-rw-r--r-- | novaclient/v2/shell.py | 6 |
3 files changed, 18 insertions, 2 deletions
diff --git a/novaclient/tests/unit/v2/test_shell.py b/novaclient/tests/unit/v2/test_shell.py index 5e9b9951..ae11d89a 100644 --- a/novaclient/tests/unit/v2/test_shell.py +++ b/novaclient/tests/unit/v2/test_shell.py @@ -1334,6 +1334,18 @@ class ShellTest(utils.TestCase): }}, ) + @mock.patch.object(servers.Server, 'networks', + new_callable=mock.PropertyMock) + def test_boot_with_not_found_when_accessing_addresses_attribute( + self, mock_networks): + mock_networks.side_effect = exceptions.NotFound( + 404, 'Instance %s could not be found.' % FAKE_UUID_1) + ex = self.assertRaises( + exceptions.CommandError, self.run_command, + 'boot --flavor 1 --image %s some-server' % FAKE_UUID_2) + self.assertIn('Instance %s could not be found.' % FAKE_UUID_1, + six.text_type(ex)) + def test_flavor_list(self): out, _ = self.run_command('flavor-list') self.assert_called_anytime('GET', '/flavors/detail') diff --git a/novaclient/v2/servers.py b/novaclient/v2/servers.py index 98f22ba2..0843e4e3 100644 --- a/novaclient/v2/servers.py +++ b/novaclient/v2/servers.py @@ -405,7 +405,7 @@ class Server(base.Resource): for network_label, address_list in self.addresses.items(): networks[network_label] = [a['addr'] for a in address_list] return networks - except Exception: + except AttributeError: return {} @api_versions.wraps("2.0", "2.24") diff --git a/novaclient/v2/shell.py b/novaclient/v2/shell.py index ef025a27..c98724c9 100644 --- a/novaclient/v2/shell.py +++ b/novaclient/v2/shell.py @@ -2368,7 +2368,11 @@ def _print_server(cs, args, server=None, wrap=0): minimal = getattr(args, "minimal", False) - networks = server.networks + try: + networks = server.networks + except Exception as e: + raise exceptions.CommandError(six.text_type(e)) + info = server.to_dict() for network_label, address_list in networks.items(): info['%s network' % network_label] = ', '.join(address_list) |