From ebd6091cc8e4ed06c702fb804aee6ae898a7f4fa Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Mon, 3 Sep 2018 16:54:23 -0400 Subject: [PATCH] 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 --- virtManager/clone.py | 5 +---- virtManager/netlist.py | 11 ----------- virtinst/cloner.py | 5 +---- virtinst/devices/interface.py | 30 +++++++++--------------------- 4 files changed, 11 insertions(+), 40 deletions(-) diff --git a/virtManager/clone.py b/virtManager/clone.py index 965fe566b..dd0af618e 100644 --- a/virtManager/clone.py +++ b/virtManager/clone.py @@ -692,10 +692,7 @@ class vmmCloneVM(vmmGObjectUI): row = self.net_list[orig] try: - ignore, msg = DeviceInterface.is_conflict_net( - self.conn.get_backend(), new) - if msg: - raise RuntimeError(msg) + DeviceInterface.is_conflict_net(self.conn.get_backend(), new) row[NETWORK_INFO_NEW_MAC] = new except Exception as e: self.err.show_err(_("Error changing MAC address: %s") % str(e)) diff --git a/virtManager/netlist.py b/virtManager/netlist.py index 1fdd81904..cb043b7e8 100644 --- a/virtManager/netlist.py +++ b/virtManager/netlist.py @@ -373,17 +373,6 @@ class vmmNetworkList(vmmGObjectUI): net.validate() except Exception as 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 def reset_state(self): diff --git a/virtinst/cloner.py b/virtinst/cloner.py index 3e8f58243..c8c85db69 100644 --- a/virtinst/cloner.py +++ b/virtinst/cloner.py @@ -149,10 +149,7 @@ class Cloner(object): def set_clone_macs(self, mac): maclist = util.listify(mac) for m in maclist: - msg = DeviceInterface.is_conflict_net(self.conn, m)[1] - if msg: - raise RuntimeError(msg) - + DeviceInterface.is_conflict_net(self.conn, m) self._clone_macs = maclist def get_clone_macs(self): return self._clone_macs diff --git a/virtinst/devices/interface.py b/virtinst/devices/interface.py index 45cde9650..61072bde3 100644 --- a/virtinst/devices/interface.py +++ b/virtinst/devices/interface.py @@ -170,9 +170,11 @@ class DeviceInterface(Device): for ignore in range(256): mac = _random_mac(conn) - ret = DeviceInterface.is_conflict_net(conn, mac) - if ret[1] is None: + try: + DeviceInterface.is_conflict_net(conn, mac) return mac + except RuntimeError: + continue logging.debug("Failed to generate non-conflicting MAC") return None @@ -180,24 +182,16 @@ class DeviceInterface(Device): @staticmethod def is_conflict_net(conn, searchmac): """ - :returns: a two element tuple: - 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") + Raise RuntimeError if the passed mac conflicts with a defined VM """ - if searchmac is None: - return (False, None) - vms = conn.fetch_all_domains() for vm in vms: for nic in vm.devices.interface: nicmac = nic.macaddr or "" if nicmac.lower() == searchmac.lower(): - return (True, _("The MAC address '%s' is in use " - "by another virtual machine.") % searchmac) - return (False, None) + raise RuntimeError( + _("The MAC address '%s' is in use " + "by another virtual machine.") % searchmac) ############### @@ -282,13 +276,7 @@ class DeviceInterface(Device): return util.validate_macaddr(self.macaddr) - ret, msg = self.is_conflict_net(self.conn, self.macaddr) - if msg is None: - return - if ret is False: - logging.warning(msg) - else: - raise RuntimeError(msg) + self.is_conflict_net(self.conn, self.macaddr) def set_default_source(self): if (self.conn.is_qemu_session() or self.conn.is_test()):