Commit 9f964975 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Add option to get value from pool

Extend PoolManager 'get' method to also take as argument a value to
get from the pool. If the value is already reserved, then a
ValueNotAvailable exception will be raised.
parent 134e019c
......@@ -55,15 +55,22 @@ class PoolManager(object):
def pool(self):
return (self.available & self.reserved)
def get(self):
def get(self, value=None):
"""Get a value from the pool."""
if self.empty():
raise EmptyPool
# Get the first available index
index = int(self.pool.index(AVAILABLE))
assert(index < self.pool_size)
self._reserve(index)
return self.index_to_value(index)
if value is None:
# Get the first available index
index = int(self.pool.index(AVAILABLE))
assert(index < self.pool_size)
self._reserve(index)
return self.index_to_value(index)
else:
if self.is_available(value):
self.reserve(value)
return value
else:
raise ValueNotAvailable("Value %s is not available" % value)
def put(self, value, external=False):
"""Return a value to the pool."""
......@@ -180,6 +187,10 @@ class EmptyPool(Exception):
pass
class ValueNotAvailable(Exception):
pass
def find_padding(size):
extra = size % 8
return extra and (8 - extra) or 0
......
......@@ -35,7 +35,7 @@
from django.test import TestCase
from synnefo.db.pools import (PoolManager, EmptyPool, BridgePool,
MacPrefixPool, IPPool, find_padding,
bitarray_to_map)
bitarray_to_map, ValueNotAvailable)
from bitarray import bitarray
......@@ -234,3 +234,17 @@ class IPPoolTestCase(TestCase):
self.assertEqual(pool.is_available('192.168.2.1'), False)
self.assertEqual(pool.size(), 8)
self.assertEqual(pool.empty(), True)
def test_get_with_value(self):
obj = DummyObject(0)
subnet = DummyObject(0)
obj.subnet = subnet
subnet.cidr = '192.168.2.0/28'
subnet.gateway = '192.168.2.1'
pool = IPPool(obj)
# Test if reserved
pool.reserve("192.168.2.2")
self.assertRaises(ValueNotAvailable, pool.get, "192.168.2.2")
# Test if externally reserved
pool.reserve("192.168.2.3", external=True)
self.assertRaises(ValueNotAvailable, pool.get, "192.168.2.3")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment