Adds new method nova.utils.get_hash_str

Some modules are using their own method to generate hex from
string.
This patch adds a new method "get_hash_str" in nova.utils and
cleans every module needs to use it.

Change-Id: I2fa7dd2ea12c1d516224132d13454d109bb6c1e3
This commit is contained in:
Sahid Orentino Ferdjaoui 2014-01-08 14:00:24 +01:00
parent a3d292fd2e
commit d7f2d32874
5 changed files with 21 additions and 20 deletions

View File

@ -36,7 +36,6 @@ S3 client with this module::
import bisect import bisect
import datetime import datetime
import hashlib
import os import os
import os.path import os.path
import urllib import urllib
@ -206,7 +205,7 @@ class BaseRequestHandler(object):
if self.application.bucket_depth < 1: if self.application.bucket_depth < 1:
return os.path.abspath(os.path.join( return os.path.abspath(os.path.join(
self.application.directory, bucket, object_name)) self.application.directory, bucket, object_name))
hash = hashlib.md5(object_name).hexdigest() hash = utils.get_hash_str(object_name)
path = os.path.abspath(os.path.join( path = os.path.abspath(os.path.join(
self.application.directory, bucket)) self.application.directory, bucket))
for i in range(self.application.bucket_depth): for i in range(self.application.bucket_depth):
@ -348,7 +347,7 @@ class ObjectHandler(BaseRequestHandler):
object_file.write(self.request.body) object_file.write(self.request.body)
object_file.close() object_file.close()
self.set_header('ETag', self.set_header('ETag',
'"%s"' % hashlib.md5(self.request.body).hexdigest()) '"%s"' % utils.get_hash_str(self.request.body))
self.finish() self.finish()
def delete(self, bucket, object_name): def delete(self, bucket, object_name):

View File

@ -17,6 +17,7 @@
import __builtin__ import __builtin__
import datetime import datetime
import functools import functools
import hashlib
import importlib import importlib
import os import os
import os.path import os.path
@ -303,6 +304,12 @@ class GenericUtilsTestCase(test.NoDBTestCase):
utils.get_shortened_ipv6_cidr, utils.get_shortened_ipv6_cidr,
"failure") "failure")
def test_get_hash_str(self):
base_str = "foo"
value = hashlib.md5(base_str).hexdigest()
self.assertEqual(
value, utils.get_hash_str(base_str))
class MonkeyPatchTestCase(test.NoDBTestCase): class MonkeyPatchTestCase(test.NoDBTestCase):
"""Unit test for utils.monkey_patch().""" """Unit test for utils.monkey_patch()."""

View File

@ -569,7 +569,7 @@ class LibvirtVolumeTestCase(test.NoDBTestCase):
libvirt_driver = volume.LibvirtNFSVolumeDriver(self.fake_conn) libvirt_driver = volume.LibvirtNFSVolumeDriver(self.fake_conn)
export_string = '192.168.1.1:/nfs/share1' export_string = '192.168.1.1:/nfs/share1'
export_mnt_base = os.path.join(mnt_base, export_mnt_base = os.path.join(mnt_base,
libvirt_driver.get_hash_str(export_string)) utils.get_hash_str(export_string))
file_path = os.path.join(export_mnt_base, self.name) file_path = os.path.join(export_mnt_base, self.name)
connection_info = {'data': {'export': export_string, connection_info = {'data': {'export': export_string,
@ -592,7 +592,7 @@ class LibvirtVolumeTestCase(test.NoDBTestCase):
export_string = '192.168.1.1:/nfs/share1' export_string = '192.168.1.1:/nfs/share1'
options = '-o intr,nfsvers=3' options = '-o intr,nfsvers=3'
export_mnt_base = os.path.join(mnt_base, export_mnt_base = os.path.join(mnt_base,
libvirt_driver.get_hash_str(export_string)) utils.get_hash_str(export_string))
file_path = os.path.join(export_mnt_base, self.name) file_path = os.path.join(export_mnt_base, self.name)
connection_info = {'data': {'export': export_string, connection_info = {'data': {'export': export_string,
@ -640,7 +640,7 @@ class LibvirtVolumeTestCase(test.NoDBTestCase):
libvirt_driver = volume.LibvirtGlusterfsVolumeDriver(self.fake_conn) libvirt_driver = volume.LibvirtGlusterfsVolumeDriver(self.fake_conn)
export_string = '192.168.1.1:/volume-00001' export_string = '192.168.1.1:/volume-00001'
export_mnt_base = os.path.join(mnt_base, export_mnt_base = os.path.join(mnt_base,
libvirt_driver.get_hash_str(export_string)) utils.get_hash_str(export_string))
file_path = os.path.join(export_mnt_base, self.name) file_path = os.path.join(export_mnt_base, self.name)
connection_info = {'data': {'export': export_string, connection_info = {'data': {'export': export_string,
@ -683,7 +683,7 @@ class LibvirtVolumeTestCase(test.NoDBTestCase):
export_string = '192.168.1.1:/volume-00001' export_string = '192.168.1.1:/volume-00001'
options = '-o backupvolfile-server=192.168.1.2' options = '-o backupvolfile-server=192.168.1.2'
export_mnt_base = os.path.join(mnt_base, export_mnt_base = os.path.join(mnt_base,
libvirt_driver.get_hash_str(export_string)) utils.get_hash_str(export_string))
file_path = os.path.join(export_mnt_base, self.name) file_path = os.path.join(export_mnt_base, self.name)
connection_info = {'data': {'export': export_string, connection_info = {'data': {'export': export_string,

View File

@ -22,6 +22,7 @@
import contextlib import contextlib
import datetime import datetime
import functools import functools
import hashlib
import inspect import inspect
import os import os
import pyclbr import pyclbr
@ -1137,3 +1138,8 @@ def get_image_from_system_metadata(system_meta):
image_meta['properties'] = properties image_meta['properties'] = properties
return image_meta 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()

View File

@ -19,7 +19,6 @@
"""Volume drivers for libvirt.""" """Volume drivers for libvirt."""
import glob import glob
import hashlib
import os import os
import time import time
import urllib2 import urllib2
@ -616,7 +615,7 @@ class LibvirtNFSVolumeDriver(LibvirtBaseVolumeDriver):
@type options: string @type options: string
""" """
mount_path = os.path.join(CONF.libvirt.nfs_mount_point_base, mount_path = os.path.join(CONF.libvirt.nfs_mount_point_base,
self.get_hash_str(nfs_export)) utils.get_hash_str(nfs_export))
self._mount_nfs(mount_path, nfs_export, options, ensure=True) self._mount_nfs(mount_path, nfs_export, options, ensure=True)
return mount_path return mount_path
@ -640,11 +639,6 @@ class LibvirtNFSVolumeDriver(LibvirtBaseVolumeDriver):
else: else:
raise raise
@staticmethod
def get_hash_str(base_str):
"""returns string that represents hash of base_str (in hex format)."""
return hashlib.md5(base_str).hexdigest()
class LibvirtAOEVolumeDriver(LibvirtBaseVolumeDriver): class LibvirtAOEVolumeDriver(LibvirtBaseVolumeDriver):
"""Driver to attach AoE volumes to libvirt.""" """Driver to attach AoE volumes to libvirt."""
@ -753,7 +747,7 @@ class LibvirtGlusterfsVolumeDriver(LibvirtBaseVolumeDriver):
@type options: string @type options: string
""" """
mount_path = os.path.join(CONF.libvirt.glusterfs_mount_point_base, mount_path = os.path.join(CONF.libvirt.glusterfs_mount_point_base,
self.get_hash_str(glusterfs_export)) utils.get_hash_str(glusterfs_export))
self._mount_glusterfs(mount_path, glusterfs_export, self._mount_glusterfs(mount_path, glusterfs_export,
options, ensure=True) options, ensure=True)
return mount_path return mount_path
@ -776,11 +770,6 @@ class LibvirtGlusterfsVolumeDriver(LibvirtBaseVolumeDriver):
else: else:
raise raise
@staticmethod
def get_hash_str(base_str):
"""returns string that represents hash of base_str (in hex format)."""
return hashlib.md5(base_str).hexdigest()
class LibvirtFibreChannelVolumeDriver(LibvirtBaseVolumeDriver): class LibvirtFibreChannelVolumeDriver(LibvirtBaseVolumeDriver):
"""Driver to attach Fibre Channel Network volumes to libvirt.""" """Driver to attach Fibre Channel Network volumes to libvirt."""