addstorage: separate device building and explicit validation

Have two clear paths. This will be useful when we add direct XML
editing UI
This commit is contained in:
Cole Robinson 2019-04-14 19:30:42 -04:00
parent eb870e4658
commit 11ffe2e569
3 changed files with 18 additions and 25 deletions

View File

@ -1209,17 +1209,12 @@ class vmmAddHardware(vmmGObjectUI):
for c in self.vm.xmlobj.devices.controller])):
controller_model = "virtio-scsi"
collidelist = [d.path for d in self.vm.xmlobj.devices.disk]
try:
disk = self.addstorage.validate_storage(self.vm.get_name(),
collidelist = [d.path for d in self.vm.xmlobj.devices.disk]
disk = self.addstorage.build_device(self.vm.get_name(),
collidelist=collidelist, device=device)
except Exception as e:
return self.err.val_err(_("Storage parameter error."), e)
if disk is False:
return False
try:
used = []
disk.bus = bus
if cache:
@ -1244,12 +1239,12 @@ class vmmAddHardware(vmmGObjectUI):
disk, controller_model, disks)
disk.generate_target(used, prefer_ctrl)
if self.addstorage.validate_device(disk) is False:
return False
except Exception as e:
return self.err.val_err(_("Storage parameter error."), e)
if self.addstorage.validate_disk_object(disk) is False:
return False
return disk

View File

@ -207,7 +207,7 @@ class vmmAddStorage(vmmGObjectUI):
def is_default_storage(self):
return self.widget("storage-create").get_active()
def validate_storage(self, vmname,
def build_device(self, vmname,
path=None, device="disk", collidelist=None):
if path is None:
if self.is_default_storage():
@ -215,9 +215,6 @@ class vmmAddStorage(vmmGObjectUI):
else:
path = self.widget("storage-entry").get_text().strip()
if not path and device in ["disk", "lun"]:
return self.err.val_err(_("A storage path must be specified."))
disk = virtinst.DeviceDisk(self.conn.get_backend())
disk.path = path or None
disk.device = device
@ -241,10 +238,14 @@ class vmmAddStorage(vmmGObjectUI):
logging.debug("path=%s can not use default prefs format=%s, "
"not setting it", disk.path, fmt)
disk.validate()
return disk
def validate_disk_object(self, disk):
def validate_device(self, disk):
if not disk.path and disk.device in ["disk", "lun"]:
return self.err.val_err(_("A storage path must be specified."))
disk.validate()
isfatal, errmsg = disk.is_size_conflict()
if not isfatal and errmsg:
# Fatal errors are reported when setting 'size'

View File

@ -1757,21 +1757,18 @@ class vmmCreate(vmmGObjectUI):
storage_enabled = self.widget("enable-storage").get_active()
try:
if storage_enabled:
disk = self._addstorage.validate_storage(self._guest.name,
path=path)
disk = self._addstorage.build_device(
self._guest.name, path=path)
if disk and self._addstorage.validate_device(disk) is False:
return False
except Exception as e:
return self.err.val_err(_("Storage parameter error."), e)
if disk is False:
return False
if self._get_config_install_page() == INSTALL_PAGE_ISO:
# CD/ISO install and no disks implies LiveCD
self._guest.installer_instance.livecd = not storage_enabled
if disk and self._addstorage.validate_disk_object(disk) is False:
return False
_remove_vmm_device(self._guest, "disk")
if not storage_enabled: