Add GetDiskInfoByName function that accepts a disk name as a parameter
and returns the associated disk object. The search algorithm follows
below:

* Iterate all disks in config:
  - Compare their name with the provided name.
  - If there is a match, increment a counter and store the disk.
* Once all disks have been checked, if the counter is 1, return the
  stored disk.
* Else, if there are more than one disks with the same name, raise a
  proper exception.

Signed-off-by: Alex Pyrgiotis <[email protected]>

diff --git a/lib/config/__init__.py b/lib/config/__init__.py
index 8ed237e..33e77d4 100644
--- a/lib/config/__init__.py
+++ b/lib/config/__init__.py
@@ -488,6 +488,40 @@ class ConfigWriter(object):
     """
     return self._UnlockedGetDiskInfo(disk_uuid)
 
+  def _UnlockedGetDiskInfoByName(self, disk_name):
+    """Return information about a named disk.
+
+    Return disk information from the configuration file, searching with the
+    name of the disk.
+
+    @param disk_name: Name of the disk
+
+    @rtype: L{objects.Disk}
+    @return: the disk object
+
+    """
+    disk = None
+    count = 0
+    for d in self._ConfigData().disks.itervalues():
+      if d.name == disk_name:
+        count += 1
+        disk = d
+
+    if count > 1:
+      raise errors.ConfigurationError("There are %s disks with this name: %s"
+                                      % (count, disk_name))
+
+    return disk
+
+  @ConfigSync(shared=1)
+  def GetDiskInfoByName(self, disk_name):
+    """Return information about a named disk.
+
+    This is a simple wrapper over L{_UnlockedGetDiskInfoByName}.
+
+    """
+    return self._UnlockedGetDiskInfoByName(disk_name)
+
   def _UnlockedGetDiskList(self):
     """Get the list of disks.
 
-- 
1.7.10.4

Reply via email to