Extract helper method to get image metadata from volume

This refactoring needs for futher review(-s).
First is here - https://review.openstack.org/#/c/170243
But it will be divided to separate reviews and some refactoring
should be done first.

Change-Id: Iac713b832b4cbc62ae69bfa1c4680c6932a735c3
This commit is contained in:
Andrey Pavlov 2015-06-02 08:54:26 +03:00
parent 7badd522dc
commit 5a44a6b3af
3 changed files with 51 additions and 21 deletions

View File

@ -32,7 +32,6 @@ from oslo_serialization import jsonutils
from oslo_utils import excutils
from oslo_utils import strutils
from oslo_utils import timeutils
from oslo_utils import units
from oslo_utils import uuidutils
import six
from six.moves import range
@ -1003,26 +1002,7 @@ class API(base.Base):
if not volume.get('bootable', True):
raise exception.InvalidBDMVolumeNotBootable(id=volume_id)
properties = volume.get('volume_image_metadata', {})
image_meta = {'properties': properties}
# NOTE(yjiang5): restore the basic attributes
# NOTE(mdbooth): These values come from volume_glance_metadata
# in cinder. This is a simple key/value table, and all values
# are strings. We need to convert them to ints to avoid
# unexpected type errors.
image_meta['min_ram'] = int(properties.get('min_ram', 0))
image_meta['min_disk'] = int(properties.get('min_disk', 0))
# Volume size is no longer related to the original image size,
# so we take it from the volume directly. Cinder creates
# volumes in Gb increments, and stores size in Gb, whereas
# glance reports size in bytes. As we're returning glance
# metadata here, we need to convert it.
image_meta['size'] = volume.get('size', 0) * units.Gi
# NOTE(yjiang5): Always set the image status as 'active'
# and depends on followed volume_api.check_attach() to
# verify it. This hack should be harmless with that check.
image_meta['status'] = 'active'
return image_meta
return utils.get_image_metadata_from_volume(volume)
return {}
@staticmethod

View File

@ -33,6 +33,7 @@ from oslo_context import context as common_context
from oslo_context import fixture as context_fixture
from oslo_utils import encodeutils
from oslo_utils import timeutils
from oslo_utils import units
import six
@ -912,6 +913,31 @@ class GetImageFromSystemMetadataTestCase(test.NoDBTestCase):
self.assertNotIn("foo1", image)
class GetImageMetadataFromVolumeTestCase(test.NoDBTestCase):
def test_inherit_image_properties(self):
properties = {"fake_prop": "fake_value"}
volume = {"volume_image_metadata": properties}
image_meta = utils.get_image_metadata_from_volume(volume)
self.assertEqual(properties, image_meta["properties"])
def test_image_size(self):
volume = {"size": 10}
image_meta = utils.get_image_metadata_from_volume(volume)
self.assertEqual(10 * units.Gi, image_meta["size"])
def test_image_status(self):
volume = {}
image_meta = utils.get_image_metadata_from_volume(volume)
self.assertEqual("active", image_meta["status"])
def test_values_conversion(self):
properties = {"min_ram": "5", "min_disk": "7"}
volume = {"volume_image_metadata": properties}
image_meta = utils.get_image_metadata_from_volume(volume)
self.assertEqual(5, image_meta["min_ram"])
self.assertEqual(7, image_meta["min_disk"])
class VersionTestCase(test.NoDBTestCase):
def test_convert_version_to_int(self):
self.assertEqual(utils.convert_version_to_int('6.2.0'), 6002000)

View File

@ -46,6 +46,7 @@ from oslo_utils import encodeutils
from oslo_utils import excutils
from oslo_utils import importutils
from oslo_utils import timeutils
from oslo_utils import units
import six
from six.moves import range
@ -1126,6 +1127,29 @@ def get_image_from_system_metadata(system_meta):
return image_meta
def get_image_metadata_from_volume(volume):
properties = volume.get('volume_image_metadata', {})
image_meta = {'properties': properties}
# NOTE(yjiang5): restore the basic attributes
# NOTE(mdbooth): These values come from volume_glance_metadata
# in cinder. This is a simple key/value table, and all values
# are strings. We need to convert them to ints to avoid
# unexpected type errors.
image_meta['min_ram'] = int(properties.get('min_ram', 0))
image_meta['min_disk'] = int(properties.get('min_disk', 0))
# Volume size is no longer related to the original image size,
# so we take it from the volume directly. Cinder creates
# volumes in Gb increments, and stores size in Gb, whereas
# glance reports size in bytes. As we're returning glance
# metadata here, we need to convert it.
image_meta['size'] = volume.get('size', 0) * units.Gi
# NOTE(yjiang5): Always set the image status as 'active'
# and depends on followed volume_api.check_attach() to
# verify it. This hack should be harmless with that check.
image_meta['status'] = 'active'
return image_meta
def get_hash_str(base_str):
"""returns string that represents hash of base_str (in hex format)."""
return hashlib.md5(base_str).hexdigest()