diff options
author | Devananda van der Veen <devananda.vdv@gmail.com> | 2014-01-30 15:57:03 -0800 |
---|---|---|
committer | Devananda van der Veen <devananda.vdv@gmail.com> | 2014-01-30 15:58:44 -0800 |
commit | 469daf557416c725c9ac32f469b11dcaf3fa6b5a (patch) | |
tree | 7f3567607303aea6e92e5b5b55fdc4737a05b893 /ironic/common/hash_ring.py | |
parent | 804819d44af45c5d65b8ef7968bc09ba7b9ad875 (diff) | |
download | ironic-469daf557416c725c9ac32f469b11dcaf3fa6b5a.tar.gz |
Improve handling of invalid input in HashRing class
HashRing __init__ and get_hosts shouldn't be passed None,
but if they are, an appropriate error should be raised.
Change-Id: I139e7410c43bf4ac0f3357797ad767fde9455e36
Closes-bug: #1274762
Diffstat (limited to 'ironic/common/hash_ring.py')
-rw-r--r-- | ironic/common/hash_ring.py | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/ironic/common/hash_ring.py b/ironic/common/hash_ring.py index 0fc932279..319c3cffd 100644 --- a/ironic/common/hash_ring.py +++ b/ironic/common/hash_ring.py @@ -21,6 +21,8 @@ import struct from oslo.config import cfg +from ironic.common import exception + hash_opts = [ cfg.IntOpt('hash_partition_exponent', default=16, @@ -53,16 +55,25 @@ class HashRing(object): Default: CONF.hash_distribution_replicas """ - self.hosts = list(hosts) - self.replicas = replicas if replicas <= len(hosts) else len(hosts) + try: + self.hosts = list(hosts) + self.replicas = replicas if replicas <= len(hosts) else len(hosts) + except TypeError: + raise exception.Invalid( + _("Invalid hosts supplied when building HashRing.")) + self.partition_shift = 32 - CONF.hash_partition_exponent self.part2host = array.array('H') for p in range(2 ** CONF.hash_partition_exponent): self.part2host.append(p % len(hosts)) def _get_partition(self, data): - return (struct.unpack_from('>I', hashlib.md5(data).digest())[0] - >> self.partition_shift) + try: + return (struct.unpack_from('>I', hashlib.md5(data).digest())[0] + >> self.partition_shift) + except TypeError: + raise exception.Invalid( + _("Invalid data supplied to HashRing.get_hosts.")) def get_hosts(self, data, ignore_hosts=None): """Get the list of hosts which the supplied data maps onto. |