Change in vdsm[master]: vm: Add _findConfDisk() helper
nsoffer at redhat.com
nsoffer at redhat.com
Fri Apr 24 14:26:23 UTC 2015
Nir Soffer has uploaded a new change for review.
Change subject: vm: Add _findConfDisk() helper
......................................................................
vm: Add _findConfDisk() helper
The new helper eliminate duplicate code looking up a disk in Vm.conf
dict in 3 methods.
Change-Id: Ib3a59a2b798650035870c335ed7c949e2bd431d0
Signed-off-by: Nir Soffer <nsoffer at redhat.com>
---
M vdsm/virt/vm.py
1 file changed, 21 insertions(+), 25 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/17/40217/1
diff --git a/vdsm/virt/vm.py b/vdsm/virt/vm.py
index ec7ce9c..9e61cc4 100644
--- a/vdsm/virt/vm.py
+++ b/vdsm/virt/vm.py
@@ -2710,6 +2710,13 @@
raise LookupError("No such drive: '%s'" % drive)
+ def _findConfDisk(self, name):
+ devices = self.conf["devices"][:]
+ for device in devices:
+ if device['type'] == hwclass.DISK and device.get("name") == name:
+ return device
+ raise LookupError("No such disk %r" % name)
+
def updateDriveVolume(self, vmDrive):
if not vmDrive.device == 'disk' or not isVdsmImage(vmDrive):
return
@@ -2741,14 +2748,13 @@
driveParams["name"])
# Updating the VM configuration
- for vmDriveConfig in self.conf["devices"][:]:
- if (vmDriveConfig['type'] == hwclass.DISK and
- vmDriveConfig.get("name") == driveParams["name"]):
- vmDriveConfig.update(driveParams)
- break
- else:
+ try:
+ disk = self._findConfDisk(driveParams["name"])
+ except LookupError:
self.log.error("Unable to update the device configuration ",
- "for: %s", driveParams["name"])
+ "for disk %s", driveParams["name"])
+ else:
+ disk.update(driveParams)
self.saveState()
@@ -3001,15 +3007,10 @@
raise RuntimeError("Disk '%s' already has an ongoing "
"replication" % drive.name)
- for device in self.conf["devices"]:
- if (device['type'] == hwclass.DISK
- and device.get("name") == drive.name):
- with self._confLock:
- device['diskReplicate'] = replica
- self.saveState()
- break
- else:
- raise LookupError("No such drive: '%s'" % drive.name)
+ disk = self._findConfDisk(drive.name)
+ with self._confLock:
+ disk['diskReplicate'] = replica
+ self.saveState()
drive.diskReplicate = replica
@@ -3018,15 +3019,10 @@
This utility method is the inverse of _setDiskReplica, look at the
_setDiskReplica description for more information.
"""
- for device in self.conf["devices"]:
- if (device['type'] == hwclass.DISK
- and device.get("name") == drive.name):
- with self._confLock:
- del device['diskReplicate']
- self.saveState()
- break
- else:
- raise LookupError("No such drive: '%s'" % drive.name)
+ disk = self._findConfDisk(drive.name)
+ with self._confLock:
+ del disk['diskReplicate']
+ self.saveState()
del drive.diskReplicate
--
To view, visit https://gerrit.ovirt.org/40217
To unsubscribe, visit https://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib3a59a2b798650035870c335ed7c949e2bd431d0
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <nsoffer at redhat.com>
More information about the vdsm-patches
mailing list