+class IpBlock(object):
+ def __init__(self, base_ip, step_ip, count_ip):
+ self.base_ip_int = Device.ip_to_int(base_ip)
+ self.step = Device.ip_to_int(step_ip)
+ self.max_available = count_ip
+ self.next_free = 0
+
+ def get_ip(self, index=0):
+ '''Return the IP address at given index
+ '''
+ if index < 0 or index >= self.max_available:
+ raise IndexError('Index out of bounds')
+ return Device.int_to_ip(self.base_ip_int + index * self.step)
+
+ def reserve_ip_range(self, count):
+ '''Reserve a range of count consecutive IP addresses spaced by step
+ '''
+ if self.next_free + count > self.max_available:
+ raise IndexError('No more IP addresses next free=%d max_available=%d requested=%d',
+ self.next_free,
+ self.max_available,
+ count)
+ first_ip = self.get_ip(self.next_free)
+ last_ip = self.get_ip(self.next_free + count - 1)
+ self.next_free += count
+ return (first_ip, last_ip)
+
+ def reset_reservation(self):
+ self.next_free = 0