Add specific list method for NovaServer res

Nova API returns limited number of resources by one call. To clean all nova
servers, we should:
- list all pages(via marker option) [old novaclient].
- use limit=-1 and novaclient will list all pages for us.

Closes-Bug: #1490940
Change-Id: Ic4d2852d9fd7602ac99b08654d2f99c52adf1cfe
This commit is contained in:
Andrey Kurilin 2015-08-26 15:59:47 +03:00 committed by Andrey Kurilin
parent 513668403a
commit 720eff5472
2 changed files with 48 additions and 0 deletions

View File

@ -61,6 +61,31 @@ _nova_order = get_order(200)
@base.resource("nova", "servers", order=next(_nova_order))
class NovaServer(base.ResourceManager):
def list(self):
"""List all servers."""
if hasattr(self._manager().api, "api_version"):
# NOTE(andreykurilin): novaclient v2.27.0 includes ability to
# return all servers(see https://review.openstack.org/#/c/217101
# for more details). This release can be identified by presence
# of "api_version" property of ``novaclient.client.Client`` cls.
return self._manager().list(limit=-1)
else:
# FIXME(andreykurilin): Remove code below, when minimum version of
# novaclient in requirements will allow it.
# NOTE(andreykurilin): Nova API returns only limited number(
# 'osapi_max_limit' option in nova.conf) of servers, so we need
# to use 'marker' option to list all pages of servers.
result = []
marker = None
while True:
servers = self._manager().list(marker=marker)
if not servers:
break
result.extend(servers)
marker = servers[-1].id
return result
def delete(self):
if getattr(self.raw_resource, "OS-EXT-STS:locked", False):
self.raw_resource.unlock()

View File

@ -84,6 +84,29 @@ class QuotaMixinTestCase(test.TestCase):
class NovaServerTestCase(test.TestCase):
def test_list(self):
server = resources.NovaServer()
server._manager = mock.MagicMock()
server.list()
server._manager.return_value.list.assert_called_once_with(limit=-1)
def test_list_old_novaclient(self):
servers = [mock.MagicMock(), mock.MagicMock(), mock.MagicMock(),
mock.MagicMock()]
server = resources.NovaServer()
server._manager = mock.MagicMock()
server._manager.return_value.api = None
server._manager.return_value.list.side_effect = (
servers[:2], servers[2:4], [])
self.assertEqual(servers, server.list())
self.assertEqual(
[mock.call(marker=None), mock.call(marker=servers[1].id),
mock.call(marker=servers[3].id)],
server._manager.return_value.list.call_args_list)
def test_delete(self):
server = resources.NovaServer()
server.raw_resource = mock.Mock()