diff --git a/lxc_net/network.py b/lxc_net/network.py index da196a2..26b59b7 100644 --- a/lxc_net/network.py +++ b/lxc_net/network.py @@ -4,35 +4,20 @@ from . import settings from . import util from .xml_template import XMLTemplate -import uuid import libvirt -class Network(util.NumberedClass): +class Network(util.LibvirtObject): class AlreadyExists(Exception): def __str__(self): return "This network is already instanciated" - class TooMany(Exception): - def __init__(self, count): - self.count = count - - def __str__(self): - return ( - "Limit number reached. The current instance #{} does not fit in IPv4" - ).format(self.count) - def __init__(self, conn, name=None): - super().__init__() - - if self.id > 250: - raise self.TooMany(self.id) + super().__init__(conn) if not name: name = str(self.id) - self.conn = conn - self.uuid = uuid.uuid4() self.name = settings.PREFIX + "_" + name self.bridge_id = settings.NETWORK_ID * 0xFF + self.id self.bridge_mac = util.MACAddress(self.id, None) diff --git a/lxc_net/util.py b/lxc_net/util.py index 93d03cd..dea3b7c 100644 --- a/lxc_net/util.py +++ b/lxc_net/util.py @@ -1,6 +1,7 @@ """ Various utils """ from . import settings +import uuid class NumberedClass: @@ -18,6 +19,33 @@ class NumberedClass: self.id = self.get_id() +class LibvirtObject(NumberedClass): + """ A class that has the basic attributes of a libvirt objects: + * id + * uuid + and the applicable restrictions + """ + + class TooMany(Exception): + def __init__(self, count): + self.count = count + + def __str__(self): + return ( + "Limit number reached. The current instance #{} does not fit in IPv4" + ).format(self.count) + + def __init__(self, conn): + super().__init__() + + self.conn = conn + + if self.id > 250: + raise self.TooMany(self.id) + + self.uuid = uuid.uuid4() + + class MACAddress: """ A MAC address for a NIC or bridge """