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
from tobiko.openstack import _client
from tobiko.openstack import _find
class GlanceClientFixture(_client.OpenstackClientFixture):
@ -69,29 +68,34 @@ def delete_image(image_id, client=None, **params):
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:
return glance_client(client).images.get(image_id=image_id)
except exc.HTTPNotFound:
if check_found:
if default is _RAISE_ERROR:
raise
else:
return None
return default
def find_image(obj=None, properties=None, client=None, check_found=True,
check_unique=False, **params):
def find_image(client=None, unique=False, default=_RAISE_ERROR, **filters):
"""Look for an image matching some property values"""
resources = list_images(client=client, **params)
return _find.find_resource(obj=obj,
resources=resources,
properties=properties,
check_found=check_found,
check_unique=check_unique)
images = list_images(client=client, limit=2, **filters)
if default is _RAISE_ERROR or images:
if unique:
return images.unique
else:
return images.first
else:
return default
def list_images(client=None, **params):
return list(glance_client(client).images.list(**params))
def list_images(client=None, limit=None, **filters):
images = glance_client(client).images.list(limit=limit, filters=filters)
return tobiko.select(images)
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()
def get_image(self):
images = _client.list_images(client=self.client,
filters={'name': self.image_name},
limit=1)
if images:
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
self.image = image = _client.find_image(client=self.client,
default=None,
name=self.image_name)
return image
def delete_image(self, image_id=None):
if not image_id:

View File

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