Add improved error handling
This commit is contained in:
@@ -27,6 +27,8 @@ def GetInterfaces(library):
|
||||
elif library == 'IPR':
|
||||
for itf in iproute.get_links():
|
||||
interfaces.append(Interface(itf))
|
||||
else:
|
||||
raise NetDhcpError(f'No Interface Library selected')
|
||||
return interfaces
|
||||
|
||||
def GetInterface(library, ifname):
|
||||
@@ -34,6 +36,8 @@ def GetInterface(library, ifname):
|
||||
return Interface(ndb.interfaces[ifname])
|
||||
elif library == 'IPR':
|
||||
return Interface(iproute.get_links(ifname=ifname)[0])
|
||||
else:
|
||||
raise NetDhcpError(f'No Interface Library selected')
|
||||
return None
|
||||
|
||||
class Interface:
|
||||
@@ -52,6 +56,8 @@ class Interface:
|
||||
self.ipr = interface
|
||||
elif isinstance(interface, pyroute2.ndb.objects.interface.Interface):
|
||||
self.ndb = interface
|
||||
else:
|
||||
raise NetDhcpError(f'Unknown Interface type')
|
||||
|
||||
def _initialize_attributes_(self):
|
||||
if self.ndb:
|
||||
@@ -103,6 +109,8 @@ class Interface:
|
||||
route['gateway'] = attrs['RTA_GATEWAY'] if 'RTA_GATEWAY' in attrs else None
|
||||
route['dst_len'] = r['dst_len']
|
||||
self.routes.append(route)
|
||||
else:
|
||||
raise NetDhcpError(f'No Interface Library selected')
|
||||
|
||||
def SetAddress(self, address):
|
||||
self.address = address
|
||||
@@ -110,18 +118,24 @@ class Interface:
|
||||
self.ndb.set('address', self.address).commit()
|
||||
elif self.ipr:
|
||||
iproute.link('set', index=self.index, address=self.address)
|
||||
else:
|
||||
raise NetDhcpError(f'No Interface Library selected')
|
||||
|
||||
def Up(self):
|
||||
if self.ndb:
|
||||
self.ndb.set('state', 'up').commit()
|
||||
elif self.ipr:
|
||||
iproute.link("set", index=self.index, state="up")
|
||||
else:
|
||||
raise NetDhcpError(f'No Interface Library selected')
|
||||
|
||||
def Down(self):
|
||||
if self.ndb:
|
||||
self.ndb.set('state', 'down').commit()
|
||||
elif self.ipr:
|
||||
iproute.link("set", index=self.index, state="down")
|
||||
else:
|
||||
raise NetDhcpError(f'No Interface Library selected')
|
||||
|
||||
def CreateInterface(library, ifname, kind, peer):
|
||||
try:
|
||||
@@ -131,6 +145,8 @@ def CreateInterface(library, ifname, kind, peer):
|
||||
elif library == 'IPR':
|
||||
iproute.link("add", ifname=ifname, kind=kind, peer=peer)
|
||||
return Interface(iproute.get_links(ifname=ifname)[0])
|
||||
else:
|
||||
raise NetDhcpError(f'No Interface Library selected')
|
||||
return None
|
||||
except Exception as e:
|
||||
return None
|
||||
@@ -141,6 +157,8 @@ def RemoveInterface(library, ifname):
|
||||
ndb.interfaces[ifname].remove().commit()
|
||||
elif library == 'IPR':
|
||||
iproute.link("del", index=iproute.link_lookup(ifname=ifname)[0])
|
||||
else:
|
||||
raise NetDhcpError(f'No Interface Library selected')
|
||||
except Exception as e:
|
||||
return
|
||||
|
||||
@@ -150,6 +168,8 @@ def AddPort(library, bridge, ifname):
|
||||
ndb.interfaces[bridge].add_port(ifname).commit()
|
||||
elif library == 'IPR':
|
||||
iproute.link("set", index=iproute.link_lookup(ifname=ifname)[0], master=iproute.link_lookup(ifname=bridge)[0])
|
||||
else:
|
||||
raise NetDhcpError(f'No Interface Library selected')
|
||||
except Exception as e:
|
||||
return
|
||||
|
||||
@@ -159,6 +179,8 @@ def DelPort(library, bridge, ifname):
|
||||
ndb.interfaces[bridge].del_port(ifname).commit()
|
||||
elif library == 'IPR':
|
||||
iproute.link("set", index=iproute.link_lookup(ifname=ifname)[0], master=0)
|
||||
else:
|
||||
raise NetDhcpError(f'No Interface Library selected')
|
||||
except Exception as e:
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user