def _validateNetworkSetup(networks, bondings): for network, networkAttrs in networks.iteritems(): if networkAttrs.get('remove', False): if set(networkAttrs) - set(['remove']): raise ConfigNetworkError(ne.ERR_BAD_PARAMS, 'Cannot specify ' 'any attribute when removing') currentBondings = netinfo.bondings() currentNicsSet = set(netinfo.nics()) for bonding, bondingAttrs in bondings.iteritems(): Bond.validateName(bonding) if 'options' in bondingAttrs: Bond.validateOptions(bonding, bondingAttrs['options']) if bondingAttrs.get('remove', False): if bonding not in currentBondings: raise ConfigNetworkError(ne.ERR_BAD_BONDING, "Cannot remove " "bonding %s: Doesn't exist" % bonding) continue nics = bondingAttrs.get('nics', None) if not nics: raise ConfigNetworkError(ne.ERR_BAD_PARAMS, "Must specify nics for bonding") if not set(nics).issubset(currentNicsSet): raise ConfigNetworkError(ne.ERR_BAD_NIC, "Unknown nics in: %r" % list(nics))
def _validateNetworkSetup(networks, bondings): _netinfo = netinfo.NetInfo() for network, networkAttrs in networks.iteritems(): if networkAttrs.get('remove', False): if set(networkAttrs) - set(['remove']): raise ConfigNetworkError( ne.ERR_BAD_PARAMS, 'Cannot specify ' 'any attribute when removing') for bonding, bondingAttrs in bondings.iteritems(): Bond.validateName(bonding) if 'options' in bondingAttrs: Bond.validateOptions(bonding, bondingAttrs['options']) if bondingAttrs.get('remove', False): if bonding not in _netinfo.bondings: raise ConfigNetworkError( ne.ERR_BAD_BONDING, "Cannot remove " "bonding %s: Doesn't exist" % bonding) continue nics = bondingAttrs.get('nics', None) if not nics: raise ConfigNetworkError(ne.ERR_BAD_PARAMS, "Must specify nics for bonding") if not set(nics).issubset(set(_netinfo.nics)): raise ConfigNetworkError(ne.ERR_BAD_NIC, "Unknown nics in: %r" % list(nics))
def testIsBondingNameValid(self): bondNames = ('badValue', ' bond14', 'bond14 ', 'bond14a', 'bond0 0') for bondName in bondNames: with self.assertRaises(neterrors.ConfigNetworkError) \ as cneContext: Bond.validateName(bondName) self.assertEqual(cneContext.exception.errCode, neterrors.ERR_BAD_BONDING) self.assertEqual(Bond.validateName('bond11'), None) self.assertEqual(Bond.validateName('bond11128421982'), None)
def delNetwork(network, vlan=None, bonding=None, nics=None, force=False, configWriter=None, implicitBonding=True, _netinfo=None, **options): if _netinfo is None: _netinfo = netinfo.NetInfo() if configWriter is None: configWriter = ConfigWriter() if network not in _netinfo.networks: logging.info("Network %r: doesn't exist in libvirt database", network) if network in netinfo.bridges(): configWriter.removeBridge(network) else: raise ConfigNetworkError(ne.ERR_BAD_BRIDGE, "Cannot delete network" " %r: It doesn't exist in the system" % network) if vlan: configWriter.removeVlan(vlan, bonding or nics[0]) return nics, vlan, bonding = _netinfo.getNicsVlanAndBondingForNetwork(network) bridged = _netinfo.networks[network]['bridged'] logging.info("Removing network %s with vlan=%s, bonding=%s, nics=%s," "options=%s" % (network, vlan, bonding, nics, options)) if not utils.tobool(force): if bonding: Bond.validateName(bonding) if set(nics) != set(_netinfo.bondings[bonding]["slaves"]): raise ConfigNetworkError(ne.ERR_BAD_NIC, 'delNetwork: %s are ' 'not all nics enslaved to %s' % (nics, bonding)) if vlan: Vlan.validateTag(vlan) if bridged: assertBridgeClean(network, vlan, bonding, nics) configWriter.setNewMtu(network=network, bridged=bridged, _netinfo=_netinfo) configWriter.removeLibvirtNetwork(network) # We need to gather NetInfo again to refresh networks info from libvirt. # The deleted bridge should never be up at this stage. _netinfo = netinfo.NetInfo() if network in _netinfo.networks: raise ConfigNetworkError(ne.ERR_USED_BRIDGE, 'delNetwork: bridge %s ' 'still exists' % network) if network and bridged: configWriter.removeBridge(network) nic = nics[0] if nics else None iface = bonding if bonding else nic if iface: ifdown(iface) if vlan: configWriter.removeVlan(vlan, iface) else: cf = netinfo.NET_CONF_PREF + iface if not bridged: # When removing bridgeless non-VLANed network # we need to remove IP/NETMASK from the cfg file for key in ('IPADDR', 'NETMASK', 'GATEWAY', 'BOOTPROTO'): configWriter._updateConfigValue(cf, key, '', True) else: # When removing bridged non-VLANed network # we need to remove BRIDGE from the cfg file configWriter._updateConfigValue(cf, 'BRIDGE', '', True) # Now we can restart changed interface ifup(iface) # The (relatively) new setupNetwork verb allows to remove a network # defined on top of an bonding device without break the bond itself. _netinfo = netinfo.NetInfo() if implicitBonding: if bonding and not _netinfo.ifaceUsers(bonding): ifdown(bonding) configWriter.removeBonding(bonding) _removeUnusedNics(network, vlan, bonding, nics, configWriter) elif not bonding: _removeUnusedNics(network, vlan, bonding, nics, configWriter) elif not _netinfo.ifaceUsers(bonding): ifdown(bonding) configWriter.setBondingMtu(bonding, DEFAULT_MTU) ifup(bonding)