Merge "Update Glance client"

This commit is contained in:
Zuul 2019-11-12 21:38:21 +00:00 committed by Gerrit Code Review
commit 57c398ad97
3 changed files with 66 additions and 37 deletions

View File

@ -28,8 +28,9 @@ find_image = _client.find_image
list_images = _client.list_images list_images = _client.list_images
delete_image = _client.delete_image delete_image = _client.delete_image
GlanceImageFixture = _image.GlanceImageFixture
FileGlanceImageFixture = _image.FileGlanceImageFixture FileGlanceImageFixture = _image.FileGlanceImageFixture
GlanceImageFixture = _image.GlanceImageFixture
HasImageMixin = _image.HasImageMixin
URLGlanceImageFixture = _image.URLGlanceImageFixture URLGlanceImageFixture = _image.URLGlanceImageFixture
open_image_file = _io.open_image_file open_image_file = _io.open_image_file

View File

@ -15,10 +15,13 @@ from __future__ import absolute_import
from glanceclient.v2 import client as glanceclient from glanceclient.v2 import client as glanceclient
from glanceclient import exc from glanceclient import exc
from oslo_log import log
import tobiko import tobiko
from tobiko.openstack import _client from tobiko.openstack import _client
LOG = log.getLogger(__name__)
class GlanceClientFixture(_client.OpenstackClientFixture): class GlanceClientFixture(_client.OpenstackClientFixture):
@ -81,9 +84,18 @@ def get_image(image_id, client=None, default=_RAISE_ERROR):
return default return default
def find_image(client=None, unique=False, default=_RAISE_ERROR, **filters): def find_image(image=None, client=None, unique=False, default=_RAISE_ERROR,
**params):
"""Look for an image matching some property values""" """Look for an image matching some property values"""
images = list_images(client=client, limit=2, **filters) client = glance_client(client)
if image:
try:
return get_image(image_id=image, default=_RAISE_ERROR)
except exc.HTTPNotFound:
params.setdefault('name', image)
limit = unique and 2 or 1
images = list_images(client=client, limit=limit, **params)
if default is _RAISE_ERROR or images: if default is _RAISE_ERROR or images:
if unique: if unique:
return images.unique return images.unique
@ -102,3 +114,16 @@ def upload_image(image_id, image_data, client=None, **params):
"""Look for the unique network matching some property values""" """Look for the unique network matching some property values"""
return glance_client(client).images.upload( return glance_client(client).images.upload(
image_id=image_id, image_data=image_data, **params) image_id=image_id, image_data=image_data, **params)
class HasGlanceClientMixin(object):
glance_client = None
def get_image(self, image_id, **params):
return get_image(image_id=image_id,
client=self.glance_client,
**params)
def find_image(self, **params):
return find_image(client=self.glance_client, **params)

View File

@ -31,6 +31,17 @@ from tobiko.openstack.glance import _io
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
class HasImageMixin(_client.HasGlanceClientMixin):
@property
def image_id(self):
raise NotImplementedError
@property
def image(self):
return self.get_image(image_id=self.image_id)
class GlanceImageStatus(object): class GlanceImageStatus(object):
#: The Image service reserved an image ID for the image in the catalog but #: The Image service reserved an image ID for the image in the catalog but
@ -67,22 +78,17 @@ class GlanceImageStatus(object):
IMPORTING = u'importing' IMPORTING = u'importing'
class GlanceImageFixture(tobiko.SharedFixture): class GlanceImageFixture(_client.HasGlanceClientMixin, tobiko.SharedFixture):
client = None
image_name = None # type: str image_name = None # type: str
username = None # type: str username = None # type: str
password = None # type: str password = None # type: str
image = None image = None
wait_interval = 5. wait_interval = 5.
def __init__(self, image_name=None, username=None, password=None, def __init__(self, image_name=None, username=None, password=None):
client=None):
super(GlanceImageFixture, self).__init__() super(GlanceImageFixture, self).__init__()
if client:
self.client = client
if image_name: if image_name:
self.image_name = image_name self.image_name = image_name
elif not self.image_name: elif not self.image_name:
@ -96,22 +102,21 @@ class GlanceImageFixture(tobiko.SharedFixture):
self.password = password self.password = password
def setup_fixture(self): def setup_fixture(self):
self.setup_client()
self.setup_image() self.setup_image()
def cleanup_fixture(self): def cleanup_fixture(self):
self.delete_image() self.delete_image()
def setup_client(self):
self.client = _client.glance_client(self.client)
def setup_image(self): def setup_image(self):
return self.wait_for_image_active() return self.wait_for_image_active()
def get_image(self): def get_image(self, image_id=None, **params):
self.image = image = _client.find_image(client=self.client, if image_id or params:
default=None, image = super(GlanceImageFixture, self).get_image(
name=self.image_name) image_id=image_id, **params)
else:
image = self.find_image(name=self.image_name, default=None)
self.image = image
return image return image
def delete_image(self, image_id=None): def delete_image(self, image_id=None):
@ -120,17 +125,19 @@ class GlanceImageFixture(tobiko.SharedFixture):
self.image = None self.image = None
LOG.debug('Deleting Glance image %r (%r)...', self.image_name, LOG.debug('Deleting Glance image %r (%r)...', self.image_name,
image_id) image_id)
if _client.delete_image(image_id=image_id, client=self.client): if _client.delete_image(image_id=image_id, client=self.glance_client):
LOG.debug('Deleted Glance image %r (%r).', self.image_name, LOG.debug('Deleted Glance image %r (%r).', self.image_name,
image_id) image_id)
@property @property
def image_id(self): def image_id(self):
return (self.image or self.get_image()).id image = self.image or self.get_image()
return image and image.id or None
@property @property
def image_status(self): def image_status(self):
return (self.image or self.get_image()).status image = self.image or self.get_image()
return image and image.status or GlanceImageStatus.DELETED
def wait_for_image_active(self, check=True): def wait_for_image_active(self, check=True):
return self.wait_for_image_status( return self.wait_for_image_status(
@ -212,22 +219,18 @@ class UploadGranceImageFixture(GlanceImageFixture):
# Cleanup cached objects # Cleanup cached objects
self.image = image = None self.image = image = None
try: LOG.debug('Creating Glance image %r '
LOG.debug('Creating Glance image %r ' '(re-tries left %d)...',
'(re-tries left %d)...', self.image_name, retries)
self.image_name, retries) image_id = _client.create_image(
image_id = _client.create_image( client=self.glance_client,
client=self.client, name=self.image_name,
name=self.image_name, disk_format=self.disk_format,
disk_format=self.disk_format, container_format=self.container_format)['id']
container_format=self.container_format)['id']
except Exception: cleanup_image_ids.add(image_id)
LOG.exception('Image creation failed %r.', LOG.debug('Created image %r (id=%r)...',
self.image_name) self.image_name, image_id)
else:
cleanup_image_ids.add(image_id)
LOG.debug('Created image %r (id=%r)...',
self.image_name, image_id)
if image: if image:
if image.id in cleanup_image_ids: if image.id in cleanup_image_ids: