Convert v2 images list method to generator
We will want this to be a generator as soon as we implement pagination. Let's establish the interface now. Related to bp glance-client-v2 Change-Id: Ib98e912a7af0bb570b4fd738733edd9b837d1a07
This commit is contained in:
@@ -20,14 +20,16 @@ class Controller(object):
|
||||
self.model = model
|
||||
|
||||
def list(self):
|
||||
"""Retrieve a listing of Image objects
|
||||
|
||||
:returns generator over list of Images
|
||||
"""
|
||||
resp, body = self.http_client.json_request('GET', '/v2/images')
|
||||
images = []
|
||||
for image in body['images']:
|
||||
#NOTE(bcwaldon): remove 'self' for now until we have an elegant
|
||||
# way to pass it into the model constructor without conflict
|
||||
image.pop('self', None)
|
||||
images.append(self.model(**image))
|
||||
return images
|
||||
yield self.model(**image)
|
||||
|
||||
def get(self, image_id):
|
||||
url = '/v2/images/%s' % image_id
|
||||
|
||||
@@ -62,7 +62,8 @@ class TestController(unittest.TestCase):
|
||||
self.controller = images.Controller(self.api, FakeModel)
|
||||
|
||||
def test_list_images(self):
|
||||
images = self.controller.list()
|
||||
#NOTE(bcwaldon): cast to list since the controller returns a generator
|
||||
images = list(self.controller.list())
|
||||
self.assertEqual(images[0].id, '3a4560a1-e585-443e-9b39-553b46ec92d1')
|
||||
self.assertEqual(images[0].name, 'image-1')
|
||||
self.assertEqual(images[1].id, '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810')
|
||||
|
||||
Reference in New Issue
Block a user