virtinst: Remove name field validate_cb usage

Either openly do this at callers, or wrap it in a validate() call.
Doing this at set time has always had issues, like the self.replace hack
This commit is contained in:
Cole Robinson 2018-09-03 15:45:26 -04:00
parent 2ac54ac001
commit 852d7b564d
8 changed files with 43 additions and 52 deletions

View File

@ -1818,6 +1818,7 @@ class vmmCreate(vmmGObjectUI):
try:
name = self._generate_default_name(self._os)
self.widget("create-vm-name").set_text(name)
self._guest.validate_name(self._guest.conn, name)
self._guest.name = name
except Exception as e:
return self.err.val_err(_("Error setting default name."), e)
@ -1956,6 +1957,7 @@ class vmmCreate(vmmGObjectUI):
name = self._get_config_name()
if name != self._guest.name:
try:
self._guest.validate_name(self._guest.conn, name)
self._guest.name = name
except Exception as e:
return self.err.val_err(_("Invalid guest name"), str(e))

View File

@ -284,8 +284,8 @@ class vmmCreateNetwork(vmmGObjectUI):
def validate_name(self):
try:
net = self._build_xmlstub()
net.name = self.widget("net-name").get_text()
name = self.widget("net-name").get_text()
Network.validate_name(self.conn.get_backend(), name)
except Exception as e:
return self.err.val_err(_("Invalid network name"), str(e))
@ -720,11 +720,9 @@ class vmmCreateNetwork(vmmGObjectUI):
# XML build and install #
#########################
def _build_xmlstub(self):
return Network(self.conn.get_backend())
def _build_xmlobj(self):
net = self._build_xmlstub()
net = Network(self.conn.get_backend())
net.name = self.widget("net-name").get_text()
net.domain_name = self.widget("net-domain-name").get_text() or None

View File

@ -480,7 +480,10 @@ class vmmCreatePool(vmmGObjectUI):
self._pool = usepool
else:
self._pool = self._make_stub_pool()
self._pool.name = self.get_config_name()
name = self.get_config_name()
self._pool.validate_name(self._pool.conn, name)
self._pool.name = name
except ValueError as e:
return self.err.val_err(_("Pool Parameter Error"), e)

View File

@ -95,7 +95,7 @@ class Guest(XMLBuilder):
(str(e)))
@staticmethod
def validate_name(conn, name, check_collision, validate=True):
def validate_name(conn, name, check_collision=True, validate=True):
if validate:
util.validate_name(_("Guest"), name)
if not check_collision:
@ -142,11 +142,7 @@ class Guest(XMLBuilder):
# Property accessors #
######################
def _validate_name(self, val):
if val == self.name:
return
self.validate_name(self.conn, val, check_collision=False)
name = XMLProperty("./name", validate_cb=_validate_name)
name = XMLProperty("./name")
def _set_memory(self, val):
if val is None:

View File

@ -386,6 +386,7 @@ class Installer(object):
:param return_xml: Don't create the guest, just return generated XML
:param autostart: If True, mark the VM to autostart on host boot
"""
guest.validate_name(guest.conn, guest.name)
self.set_install_defaults(guest)
try:
@ -403,7 +404,6 @@ class Installer(object):
if dry:
return
guest.check_vm_collision(self.conn, guest.name, False)
domain = self._create_guest(
guest, meter, install_xml, final_xml,
doboot, transient)

View File

@ -157,11 +157,10 @@ class Interface(XMLBuilder):
# Validation helpers #
######################
def _validate_name(self, name):
if name == self.name:
return
@staticmethod
def validate_name(conn, name):
try:
self.conn.interfaceLookupByName(name)
conn.interfaceLookupByName(name)
except libvirt.libvirtError:
return
@ -181,8 +180,7 @@ class Interface(XMLBuilder):
mtu = XMLProperty("./mtu/@size", is_int=True)
start_mode = XMLProperty("./start/@mode")
name = XMLProperty("./@name", validate_cb=_validate_name)
name = XMLProperty("./@name")
macaddr = XMLProperty("./mac/@address", validate_cb=_validate_mac)
def add_protocol(self, obj):
@ -229,6 +227,8 @@ class Interface(XMLBuilder):
##################
def validate(self):
self.validate_name(self.conn, self.name)
if (self.type == self.INTERFACE_TYPE_VLAN and
(self.tag is None or self.parent_interface is None)):
raise ValueError(_("VLAN Tag and parent interface are required."))

View File

@ -187,11 +187,12 @@ class Network(XMLBuilder):
# Validation helpers #
######################
def _validate_name(self, name):
@staticmethod
def validate_name(conn, name):
util.validate_name(_("Network"), name)
try:
self.conn.networkLookupByName(name)
conn.networkLookupByName(name)
except libvirt.libvirtError:
return
raise ValueError(_("Name '%s' already in use by another network." %
@ -208,7 +209,7 @@ class Network(XMLBuilder):
"macaddr", "ips", "routes", "bandwidth"]
ipv6 = XMLProperty("./@ipv6", is_yesno=True)
name = XMLProperty("./name", validate_cb=_validate_name)
name = XMLProperty("./name")
uuid = XMLProperty("./uuid")
virtualport_type = XMLProperty("./virtualport/@type")

View File

@ -38,26 +38,11 @@ class _StorageObject(XMLBuilder):
Meaningless to directly instantiate.
"""
######################
# Validation helpers #
######################
def _check_name_collision(self, name):
raise NotImplementedError()
def _validate_name(self, name):
if name == self.name:
return
util.validate_name(_("Storage object"), name)
self._check_name_collision(name)
return name
##############
# Properties #
##############
name = XMLProperty("./name", validate_cb=_validate_name)
name = XMLProperty("./name")
permissions = XMLChildProperty(_StoragePermissions,
relative_xpath="./target",
is_single=True)
@ -293,15 +278,16 @@ class StoragePool(_StorageObject):
# Validation helpers #
######################
def _check_name_collision(self, name):
pool = None
@staticmethod
def validate_name(conn, name):
util.validate_name(_("Storage object"), name)
try:
pool = self.conn.storagePoolLookupByName(name)
conn.storagePoolLookupByName(name)
except libvirt.libvirtError:
pass
if pool:
raise ValueError(_("Name '%s' already in use by another pool." %
name))
return
raise ValueError(_("Name '%s' already in use by another pool." %
name))
def default_target_path(self):
if not self.supports_property("target_path"):
@ -460,6 +446,8 @@ class StoragePool(_StorageObject):
##################
def validate(self):
self.validate_name(self.conn, self.name)
if not self.target_path:
self.target_path = self.default_target_path()
if not self.source_name:
@ -631,15 +619,16 @@ class StorageVolume(_StorageObject):
# XML validation helpers #
##########################
def _check_name_collision(self, name):
vol = None
@staticmethod
def validate_name(pool, name):
util.validate_name(_("Storage object"), name)
try:
vol = self.pool.storageVolLookupByName(name)
pool.storageVolLookupByName(name)
except libvirt.libvirtError:
pass
if vol:
raise ValueError(_("Name '%s' already in use by another volume." %
name))
return
raise ValueError(_("Name '%s' already in use by another volume." %
name))
def _get_vol_type(self):
if self.type:
@ -732,6 +721,8 @@ class StorageVolume(_StorageObject):
##################
def validate(self):
self.validate_name(self.pool, self.name)
if not self.format and self.file_type == self.TYPE_FILE:
self.format = "raw"
if self._prop_is_unset("lazy_refcounts") and self.format == "qcow2":