Use version convert methods from oslo.utils

oslo.utils provides version convert methods in versionutils[1], so it is
redundant to have them in Cinder.

[1]https://github.com/openstack/oslo.utils/blob/master/oslo_utils/versionutils.py

Closes-Bug: #1487273

Change-Id: Ic8ba775bc067759319829a1bcbde2660ddeb921e
This commit is contained in:
ChangBo Guo(gcb) 2015-08-19 16:52:33 +08:00
parent 43e9cd4e09
commit 6831f27976
5 changed files with 6 additions and 48 deletions

View File

@ -18,6 +18,7 @@ import binascii
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import versionutils
from oslo_versionedobjects import fields
import six
@ -26,7 +27,6 @@ from cinder import exception
from cinder.i18n import _
from cinder import objects
from cinder.objects import base
from cinder import utils
CONF = cfg.CONF
LOG = logging.getLogger(__name__)
@ -76,7 +76,7 @@ class Backup(base.CinderPersistentObject, base.CinderObject,
def obj_make_compatible(self, primitive, target_version):
"""Make an object representation compatible with a target version."""
super(Backup, self).obj_make_compatible(primitive, target_version)
target_version = utils.convert_version_to_tuple(target_version)
target_version = versionutils.convert_version_to_tuple(target_version)
@staticmethod
def _from_db_object(context, backup, db_backup):

View File

@ -14,6 +14,7 @@
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import versionutils
from oslo_versionedobjects import fields
from cinder import db
@ -21,7 +22,6 @@ from cinder import exception
from cinder.i18n import _
from cinder import objects
from cinder.objects import base
from cinder import utils
CONF = cfg.CONF
# NOTE(thangp): OPTIONAL_FIELDS are fields that would be lazy-loaded. They are
@ -99,7 +99,7 @@ class Snapshot(base.CinderPersistentObject, base.CinderObject,
def obj_make_compatible(self, primitive, target_version):
"""Make an object representation compatible with a target version."""
super(Snapshot, self).obj_make_compatible(primitive, target_version)
target_version = utils.convert_version_to_tuple(target_version)
target_version = versionutils.convert_version_to_tuple(target_version)
@staticmethod
def _from_db_object(context, snapshot, db_snapshot, expected_attrs=None):

View File

@ -14,6 +14,7 @@
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import versionutils
from oslo_versionedobjects import fields
from cinder import db
@ -21,7 +22,6 @@ from cinder import exception
from cinder.i18n import _
from cinder import objects
from cinder.objects import base
from cinder import utils
CONF = cfg.CONF
OPTIONAL_FIELDS = []
@ -97,7 +97,7 @@ class Volume(base.CinderPersistentObject, base.CinderObject,
def obj_make_compatible(self, primitive, target_version):
"""Make an object representation compatible with a target version."""
super(Volume, self).obj_make_compatible(primitive, target_version)
target_version = utils.convert_version_to_tuple(target_version)
target_version = versionutils.convert_version_to_tuple(target_version)
@staticmethod
def _from_db_object(context, volume, db_volume):

View File

@ -1506,22 +1506,6 @@ class TestRetryDecorator(test.TestCase):
self.assertFalse(mock_sleep.called)
class VersionTestCase(test.TestCase):
def test_convert_version_to_int(self):
self.assertEqual(6002000, utils.convert_version_to_int('6.2.0'))
self.assertEqual(6004003, utils.convert_version_to_int((6, 4, 3)))
self.assertEqual(5, utils.convert_version_to_int((5, )))
self.assertRaises(exception.CinderException,
utils.convert_version_to_int, '5a.6b')
def test_convert_version_to_string(self):
self.assertEqual('6.7.0', utils.convert_version_to_str(6007000))
self.assertEqual('4', utils.convert_version_to_str(4))
def test_convert_version_to_tuple(self):
self.assertEqual((6, 7, 0), utils.convert_version_to_tuple('6.7.0'))
class LogTracingTestCase(test.TestCase):
def test_utils_setup_tracing(self):

View File

@ -833,32 +833,6 @@ def retry(exceptions, interval=1, retries=3, backoff_rate=2,
return _decorator
def convert_version_to_int(version):
try:
if isinstance(version, six.string_types):
version = convert_version_to_tuple(version)
if isinstance(version, tuple):
return six.moves.reduce(lambda x, y: (x * 1000) + y, version)
except Exception:
msg = _("Version %s is invalid.") % version
raise exception.CinderException(msg)
def convert_version_to_str(version_int):
version_numbers = []
factor = 1000
while version_int != 0:
version_number = version_int - (version_int // factor * factor)
version_numbers.insert(0, six.text_type(version_number))
version_int = version_int // factor
return '.'.join(map(str, version_numbers))
def convert_version_to_tuple(version_str):
return tuple(int(part) for part in version_str.split('.'))
def convert_str(text):
"""Convert to native string.