Network: factor out some parts for future Container

This commit is contained in:
Théophile Bastian 2020-03-09 13:00:43 +01:00
parent 25b0b714e1
commit 5f3e504913
2 changed files with 30 additions and 17 deletions

View file

@ -4,35 +4,20 @@ from . import settings
from . import util from . import util
from .xml_template import XMLTemplate from .xml_template import XMLTemplate
import uuid
import libvirt import libvirt
class Network(util.NumberedClass): class Network(util.LibvirtObject):
class AlreadyExists(Exception): class AlreadyExists(Exception):
def __str__(self): def __str__(self):
return "This network is already instanciated" 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): def __init__(self, conn, name=None):
super().__init__() super().__init__(conn)
if self.id > 250:
raise self.TooMany(self.id)
if not name: if not name:
name = str(self.id) name = str(self.id)
self.conn = conn
self.uuid = uuid.uuid4()
self.name = settings.PREFIX + "_" + name self.name = settings.PREFIX + "_" + name
self.bridge_id = settings.NETWORK_ID * 0xFF + self.id self.bridge_id = settings.NETWORK_ID * 0xFF + self.id
self.bridge_mac = util.MACAddress(self.id, None) self.bridge_mac = util.MACAddress(self.id, None)

View file

@ -1,6 +1,7 @@
""" Various utils """ """ Various utils """
from . import settings from . import settings
import uuid
class NumberedClass: class NumberedClass:
@ -18,6 +19,33 @@ class NumberedClass:
self.id = self.get_id() 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: class MACAddress:
""" A MAC address for a NIC or bridge """ """ A MAC address for a NIC or bridge """