Skip to content

Commit

Permalink
Handle the pagination for image list
Browse files Browse the repository at this point in the history
Handle the paginatiion for image list.  We were sorting the
data here, so nothing lost for the generator.

Change-Id: I2d7d4b3d5c9f650953f309c971ac53b64f6f7f77
  • Loading branch information
TerryHowe committed Apr 20, 2015
1 parent e60bf28 commit 3c7b518
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
12 changes: 11 additions & 1 deletion openstackclient/image/v1/image.py
Expand Up @@ -405,7 +405,17 @@ def take_action(self, parsed_args):
columns = ("ID", "Name")
column_headers = columns

data = image_client.api.image_list(**kwargs)
# List of image data received
data = []
# No pages received yet, so start the page marker at None.
marker = None
while True:
page = image_client.api.image_list(marker=marker, **kwargs)
if not page:
break
data.extend(page)
# Set the marker to the id of the last item we received
marker = page[-1]['id']

if parsed_args.property:
# NOTE(dtroyer): coerce to a list to subscript it in py3
Expand Down
12 changes: 11 additions & 1 deletion openstackclient/image/v2/image.py
Expand Up @@ -156,7 +156,17 @@ def take_action(self, parsed_args):
columns = ("ID", "Name")
column_headers = columns

data = image_client.api.image_list(**kwargs)
# List of image data received
data = []
# No pages received yet, so start the page marker at None.
marker = None
while True:
page = image_client.api.image_list(marker=marker, **kwargs)
if not page:
break
data.extend(page)
# Set the marker to the id of the last item we received
marker = page[-1]['id']

if parsed_args.property:
# NOTE(dtroyer): coerce to a list to subscript it in py3
Expand Down
20 changes: 13 additions & 7 deletions openstackclient/tests/image/v1/test_image.py
Expand Up @@ -306,8 +306,8 @@ def setUp(self):
super(TestImageList, self).setUp()

self.api_mock = mock.Mock()
self.api_mock.image_list.return_value = [
copy.deepcopy(image_fakes.IMAGE),
self.api_mock.image_list.side_effect = [
[copy.deepcopy(image_fakes.IMAGE)], [],
]
self.app.client_manager.image.api = self.api_mock

Expand All @@ -327,6 +327,7 @@ def test_image_list_no_options(self):
columns, data = self.cmd.take_action(parsed_args)
self.api_mock.image_list.assert_called_with(
detailed=False,
marker=image_fakes.image_id,
)

collist = ('ID', 'Name')
Expand Down Expand Up @@ -354,6 +355,7 @@ def test_image_list_public_option(self):
self.api_mock.image_list.assert_called_with(
detailed=False,
public=True,
marker=image_fakes.image_id,
)

collist = ('ID', 'Name')
Expand Down Expand Up @@ -381,6 +383,7 @@ def test_image_list_private_option(self):
self.api_mock.image_list.assert_called_with(
detailed=False,
private=True,
marker=image_fakes.image_id,
)

collist = ('ID', 'Name')
Expand All @@ -405,6 +408,7 @@ def test_image_list_long_option(self):
columns, data = self.cmd.take_action(parsed_args)
self.api_mock.image_list.assert_called_with(
detailed=True,
marker=image_fakes.image_id,
)

collist = (
Expand Down Expand Up @@ -437,8 +441,8 @@ def test_image_list_long_option(self):

@mock.patch('openstackclient.api.utils.simple_filter')
def test_image_list_property_option(self, sf_mock):
sf_mock.return_value = [
copy.deepcopy(image_fakes.IMAGE),
sf_mock.side_effect = [
[copy.deepcopy(image_fakes.IMAGE)], [],
]

arglist = [
Expand All @@ -453,6 +457,7 @@ def test_image_list_property_option(self, sf_mock):
columns, data = self.cmd.take_action(parsed_args)
self.api_mock.image_list.assert_called_with(
detailed=True,
marker=image_fakes.image_id,
)
sf_mock.assert_called_with(
[image_fakes.IMAGE],
Expand All @@ -472,8 +477,8 @@ def test_image_list_property_option(self, sf_mock):

@mock.patch('openstackclient.common.utils.sort_items')
def test_image_list_sort_option(self, si_mock):
si_mock.return_value = [
copy.deepcopy(image_fakes.IMAGE)
si_mock.side_effect = [
[copy.deepcopy(image_fakes.IMAGE)], [],
]

arglist = ['--sort', 'name:asc']
Expand All @@ -483,7 +488,8 @@ def test_image_list_sort_option(self, si_mock):
# DisplayCommandBase.take_action() returns two tuples
columns, data = self.cmd.take_action(parsed_args)
self.api_mock.image_list.assert_called_with(
detailed=False
detailed=False,
marker=image_fakes.image_id,
)
si_mock.assert_called_with(
[image_fakes.IMAGE],
Expand Down
23 changes: 17 additions & 6 deletions openstackclient/tests/image/v2/test_image.py
Expand Up @@ -70,8 +70,8 @@ def setUp(self):
super(TestImageList, self).setUp()

self.api_mock = mock.Mock()
self.api_mock.image_list.return_value = [
copy.deepcopy(image_fakes.IMAGE),
self.api_mock.image_list.side_effect = [
[copy.deepcopy(image_fakes.IMAGE)], [],
]
self.app.client_manager.image.api = self.api_mock

Expand All @@ -90,7 +90,9 @@ def test_image_list_no_options(self):

# DisplayCommandBase.take_action() returns two tuples
columns, data = self.cmd.take_action(parsed_args)
self.api_mock.image_list.assert_called_with()
self.api_mock.image_list.assert_called_with(
marker=image_fakes.image_id,
)

collist = ('ID', 'Name')

Expand All @@ -117,6 +119,7 @@ def test_image_list_public_option(self):
columns, data = self.cmd.take_action(parsed_args)
self.api_mock.image_list.assert_called_with(
public=True,
marker=image_fakes.image_id,
)

collist = ('ID', 'Name')
Expand Down Expand Up @@ -144,6 +147,7 @@ def test_image_list_private_option(self):
columns, data = self.cmd.take_action(parsed_args)
self.api_mock.image_list.assert_called_with(
private=True,
marker=image_fakes.image_id,
)

collist = ('ID', 'Name')
Expand Down Expand Up @@ -171,6 +175,7 @@ def test_image_list_shared_option(self):
columns, data = self.cmd.take_action(parsed_args)
self.api_mock.image_list.assert_called_with(
shared=True,
marker=image_fakes.image_id,
)

collist = ('ID', 'Name')
Expand All @@ -193,7 +198,9 @@ def test_image_list_long_option(self):

# DisplayCommandBase.take_action() returns two tuples
columns, data = self.cmd.take_action(parsed_args)
self.api_mock.image_list.assert_called_with()
self.api_mock.image_list.assert_called_with(
marker=image_fakes.image_id,
)

collist = (
'ID',
Expand Down Expand Up @@ -239,7 +246,9 @@ def test_image_list_property_option(self, sf_mock):

# DisplayCommandBase.take_action() returns two tuples
columns, data = self.cmd.take_action(parsed_args)
self.api_mock.image_list.assert_called_with()
self.api_mock.image_list.assert_called_with(
marker=image_fakes.image_id,
)
sf_mock.assert_called_with(
[image_fakes.IMAGE],
attr='a',
Expand Down Expand Up @@ -268,7 +277,9 @@ def test_image_list_sort_option(self, si_mock):

# DisplayCommandBase.take_action() returns two tuples
columns, data = self.cmd.take_action(parsed_args)
self.api_mock.image_list.assert_called_with()
self.api_mock.image_list.assert_called_with(
marker=image_fakes.image_id,
)
si_mock.assert_called_with(
[image_fakes.IMAGE],
'name:asc'
Expand Down

0 comments on commit 3c7b518

Please sign in to comment.