devices: interface: Simplify is_conflict_net calls

We don't return a non-fatal error these days, so adjust all callers
to only handle failure
This commit is contained in:
Cole Robinson 2018-09-03 16:54:23 -04:00
parent c27cdc67de
commit ebd6091cc8
4 changed files with 11 additions and 40 deletions

View File

@ -692,10 +692,7 @@ class vmmCloneVM(vmmGObjectUI):
row = self.net_list[orig] row = self.net_list[orig]
try: try:
ignore, msg = DeviceInterface.is_conflict_net( DeviceInterface.is_conflict_net(self.conn.get_backend(), new)
self.conn.get_backend(), new)
if msg:
raise RuntimeError(msg)
row[NETWORK_INFO_NEW_MAC] = new row[NETWORK_INFO_NEW_MAC] = new
except Exception as e: except Exception as e:
self.err.show_err(_("Error changing MAC address: %s") % str(e)) self.err.show_err(_("Error changing MAC address: %s") % str(e))

View File

@ -373,17 +373,6 @@ class vmmNetworkList(vmmGObjectUI):
net.validate() net.validate()
except Exception as e: except Exception as e:
return self.err.val_err(_("Error with network parameters."), e) return self.err.val_err(_("Error with network parameters."), e)
# Make sure there is no mac address collision
isfatal, errmsg = net.is_conflict_net(net.conn, net.macaddr)
if isfatal:
return self.err.val_err(_("Mac address collision."), errmsg)
elif errmsg is not None:
retv = self.err.yes_no(_("Mac address collision."),
_("%s Are you sure you want to use this address?") % errmsg)
if not retv:
return False
return net return net
def reset_state(self): def reset_state(self):

View File

@ -149,10 +149,7 @@ class Cloner(object):
def set_clone_macs(self, mac): def set_clone_macs(self, mac):
maclist = util.listify(mac) maclist = util.listify(mac)
for m in maclist: for m in maclist:
msg = DeviceInterface.is_conflict_net(self.conn, m)[1] DeviceInterface.is_conflict_net(self.conn, m)
if msg:
raise RuntimeError(msg)
self._clone_macs = maclist self._clone_macs = maclist
def get_clone_macs(self): def get_clone_macs(self):
return self._clone_macs return self._clone_macs

View File

@ -170,9 +170,11 @@ class DeviceInterface(Device):
for ignore in range(256): for ignore in range(256):
mac = _random_mac(conn) mac = _random_mac(conn)
ret = DeviceInterface.is_conflict_net(conn, mac) try:
if ret[1] is None: DeviceInterface.is_conflict_net(conn, mac)
return mac return mac
except RuntimeError:
continue
logging.debug("Failed to generate non-conflicting MAC") logging.debug("Failed to generate non-conflicting MAC")
return None return None
@ -180,24 +182,16 @@ class DeviceInterface(Device):
@staticmethod @staticmethod
def is_conflict_net(conn, searchmac): def is_conflict_net(conn, searchmac):
""" """
:returns: a two element tuple: Raise RuntimeError if the passed mac conflicts with a defined VM
first element is True if fatal collision occurred
second element is a string description of the collision.
Non fatal collisions (mac addr collides with inactive guest) will
return (False, "description of collision")
""" """
if searchmac is None:
return (False, None)
vms = conn.fetch_all_domains() vms = conn.fetch_all_domains()
for vm in vms: for vm in vms:
for nic in vm.devices.interface: for nic in vm.devices.interface:
nicmac = nic.macaddr or "" nicmac = nic.macaddr or ""
if nicmac.lower() == searchmac.lower(): if nicmac.lower() == searchmac.lower():
return (True, _("The MAC address '%s' is in use " raise RuntimeError(
"by another virtual machine.") % searchmac) _("The MAC address '%s' is in use "
return (False, None) "by another virtual machine.") % searchmac)
############### ###############
@ -282,13 +276,7 @@ class DeviceInterface(Device):
return return
util.validate_macaddr(self.macaddr) util.validate_macaddr(self.macaddr)
ret, msg = self.is_conflict_net(self.conn, self.macaddr) self.is_conflict_net(self.conn, self.macaddr)
if msg is None:
return
if ret is False:
logging.warning(msg)
else:
raise RuntimeError(msg)
def set_default_source(self): def set_default_source(self):
if (self.conn.is_qemu_session() or self.conn.is_test()): if (self.conn.is_qemu_session() or self.conn.is_test()):