Update Glance image search

Change-Id: I24f6179bcf896f487516a824feb9cd699d4a5d94
This commit is contained in:
Federico Ressi 2019-07-26 14:22:09 +02:00
parent 06bc412005
commit 2cdfdffd99
3 changed files with 26 additions and 30 deletions

View File

@ -18,7 +18,6 @@ from glanceclient import exc
import tobiko import tobiko
from tobiko.openstack import _client from tobiko.openstack import _client
from tobiko.openstack import _find
class GlanceClientFixture(_client.OpenstackClientFixture): class GlanceClientFixture(_client.OpenstackClientFixture):
@ -69,29 +68,34 @@ def delete_image(image_id, client=None, **params):
return True return True
def get_image(image_id, check_found=True, client=None): _RAISE_ERROR = object()
def get_image(image_id, client=None, default=_RAISE_ERROR):
try: try:
return glance_client(client).images.get(image_id=image_id) return glance_client(client).images.get(image_id=image_id)
except exc.HTTPNotFound: except exc.HTTPNotFound:
if check_found: if default is _RAISE_ERROR:
raise raise
else: else:
return None return default
def find_image(obj=None, properties=None, client=None, check_found=True, def find_image(client=None, unique=False, default=_RAISE_ERROR, **filters):
check_unique=False, **params):
"""Look for an image matching some property values""" """Look for an image matching some property values"""
resources = list_images(client=client, **params) images = list_images(client=client, limit=2, **filters)
return _find.find_resource(obj=obj, if default is _RAISE_ERROR or images:
resources=resources, if unique:
properties=properties, return images.unique
check_found=check_found, else:
check_unique=check_unique) return images.first
else:
return default
def list_images(client=None, **params): def list_images(client=None, limit=None, **filters):
return list(glance_client(client).images.list(**params)) images = glance_client(client).images.list(limit=limit, filters=filters)
return tobiko.select(images)
def upload_image(image_id, image_data, client=None, **params): def upload_image(image_id, image_data, client=None, **params):

View File

@ -108,18 +108,10 @@ class GlanceImageFixture(tobiko.SharedFixture):
return self.wait_for_image_active() return self.wait_for_image_active()
def get_image(self): def get_image(self):
images = _client.list_images(client=self.client, self.image = image = _client.find_image(client=self.client,
filters={'name': self.image_name}, default=None,
limit=1) name=self.image_name)
if images: return image
self.image = image = images[0]
LOG.debug('Found image %r (%r): %r', self.image_name, image['id'],
image)
return image
else:
self.image = None
LOG.debug('Glance image %r not found', self.image_name)
return None
def delete_image(self, image_id=None): def delete_image(self, image_id=None):
if not image_id: if not image_id:

View File

@ -32,10 +32,10 @@ class GlanceApiTestCase(testtools.TestCase):
image = glance.get_image(self.fixture.image_id) image = glance.get_image(self.fixture.image_id)
self.assertEqual(self.fixture.image_id, image['id']) self.assertEqual(self.fixture.image_id, image['id'])
def test_find_image_with_id(self): def test_find_image_by_id(self):
image = glance.find_image(self.fixture.image_id) image = glance.find_image(id=self.fixture.image_id)
self.assertEqual(self.fixture.image_id, image['id']) self.assertEqual(self.fixture.image_id, image['id'])
def test_find_image_with_name(self): def test_find_image_by_name(self):
image = glance.find_image(self.fixture.image_name) image = glance.find_image(name=self.fixture.image_name)
self.assertEqual(self.fixture.image_name, image['name']) self.assertEqual(self.fixture.image_name, image['name'])