From e5ed68d8d4b9c7bbba6d9a38f4ad341fafc39ef4 Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Wed, 29 Oct 2014 12:57:32 -0500
Subject: [PATCH 2/3] Remove DeviceTree.getDevicesByType.

It does very little to simplify the caller's life other than add the
overhead of logging the return value.
---
 blivet/__init__.py   | 66 ++++++++++++++++++++--------------------------------
 blivet/devicetree.py | 15 ------------
 blivet/iscsi.py      |  3 +--
 tests/action_test.py |  6 ++---
 4 files changed, 29 insertions(+), 61 deletions(-)

diff --git a/blivet/__init__.py b/blivet/__init__.py
index 50f39cc..333a9cb 100644
--- a/blivet/__init__.py
+++ b/blivet/__init__.py
@@ -587,9 +587,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        vgs = self.devicetree.getDevicesByType("lvmvg")
-        vgs.sort(key=lambda d: d.name)
-        return vgs
+        vgs = (d for d in self.devices if d.type == "lvmvg")
+        return sorted(vgs, key=lambda d: d.name)
 
     @property
     def lvs(self):
@@ -599,9 +598,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        lvs = self.devicetree.getDevicesByType("lvmlv")
-        lvs.sort(key=lambda d: d.name)
-        return lvs
+        lvs = (d for d in self.devices if d.type == "lvmlv")
+        return sorted(lvs, key=lambda d: d.name)
 
     @property
     def thinlvs(self):
@@ -611,9 +609,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        thin = self.devicetree.getDevicesByType("lvmthinlv")
-        thin.sort(key=lambda d: d.name)
-        return thin
+        thin = (d for d in self.devices if d.type == "lvmthinlv")
+        return sorted(thin, key=lambda d: d.name)
 
     @property
     def thinpools(self):
@@ -623,9 +620,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        pools = self.devicetree.getDevicesByType("lvmthinpool")
-        pools.sort(key=lambda d: d.name)
-        return pools
+        pools = (d for d in self.devices if d.type == "lvmthinpool")
+        return sorted(pools, key=lambda d: d.name)
 
     @property
     def pvs(self):
@@ -635,10 +631,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        devices = self.devicetree.devices
-        pvs = [d for d in devices if d.format.type == "lvmpv"]
-        pvs.sort(key=lambda d: d.name)
-        return pvs
+        pvs = (d for d in self.devices if d.format.type == "lvmpv")
+        return sorted(pvs, key=lambda d: d.name)
 
     @property
     def mdarrays(self):
@@ -648,16 +642,14 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        arrays = self.devicetree.getDevicesByType("mdarray")
-        arrays.sort(key=lambda d: d.name)
-        return arrays
+        arrays = (d for d in self.devices if d.type == "mdarray")
+        return sorted(arrays, key=lambda d: d.name)
 
     @property
     def mdcontainers(self):
         """ A list of the MD containers in the device tree. """
-        arrays = self.devicetree.getDevicesByType("mdcontainer")
-        arrays.sort(key=lambda d: d.name)
-        return arrays
+        arrays = (d for d in self.devices if d.type == "mdcontainer")
+        return sorted(arrays, key=lambda d: d.name)
 
     @property
     def mdmembers(self):
@@ -667,10 +659,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        devices = self.devicetree.devices
-        members = [d for d in devices if d.format.type == "mdmember"]
-        members.sort(key=lambda d: d.name)
-        return members
+        members = (d for d in self.devices if d.format.type == "mdmember")
+        return sorted(members, key=lambda d: d.name)
 
     @property
     def btrfsVolumes(self):
@@ -680,7 +670,7 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        return sorted(self.devicetree.getDevicesByType("btrfs volume"),
+        return sorted(d for d in self.devices if d.type == "btrfs volume"),
                       key=lambda d: d.name)
 
     @property
@@ -691,10 +681,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        devices = self.devicetree.devices
-        swaps = [d for d in devices if d.format.type == "swap"]
-        swaps.sort(key=lambda d: d.name)
-        return swaps
+        swaps = (d for d in self.devices if d.format.type == "swap")
+        return sorted(swaps, key=lambda d: d.name)
 
     def shouldClear(self, device, **kwargs):
         """ Return True if a clearpart settings say a device should be cleared.
@@ -2669,7 +2657,7 @@ class FSSet(object):
             open(mdadm_path, "w").write(mdadm_conf)
 
         # /etc/multipath.conf
-        if self.devicetree.getDevicesByType("dm-multipath"):
+        if any(d.type == "dm-multipath" for d in self.devices):
             util.copy_to_system("/etc/multipath.conf")
             util.copy_to_system("/etc/multipath/wwids")
             util.copy_to_system("/etc/multipath/bindings")
@@ -2703,15 +2691,11 @@ class FSSet(object):
 
     def mdadmConf(self):
         """ Return the contents of mdadm.conf. """
-        arrays = self.devicetree.getDevicesByType("mdarray")
-        arrays.extend(self.devicetree.getDevicesByType("mdbiosraidarray"))
-        arrays.extend(self.devicetree.getDevicesByType("mdcontainer"))
-        # Sort it, this not only looks nicer, but this will also put
-        # containers (which get md0, md1, etc.) before their members
-        # (which get md127, md126, etc.). and lame as it is mdadm will not
-        # assemble the whole stack in one go unless listed in the proper order
-        # in mdadm.conf
-        arrays.sort(key=lambda d: d.path)
+        # mdadm will not assemble the whole stack in one go unless listed in the
+        # proper order in mdadm.conf
+        arrays = [d for d in self.devices if d.type == "mdcontainer"]
+        arrays.extend([d for d in self.devices if d.type == "mdbiosraidarray"])
+        arrays.extend([d for d in self.devices if d.type == "mdarray"])
         if not arrays:
             return ""
 
diff --git a/blivet/devicetree.py b/blivet/devicetree.py
index d13e534..1dd5234 100644
--- a/blivet/devicetree.py
+++ b/blivet/devicetree.py
@@ -2330,21 +2330,6 @@ class DeviceTree(object):
         log_method_return(self, result)
         return result
 
-    def getDevicesByType(self, device_type, incomplete=False, hidden=False):
-        """ Return a list of devices with a matching device type.
-
-            :param str device_type: the type to match
-            :param bool incomplete: include incomplete devices in search
-            :param bool hidden: include hidden devices in search
-            :returns: all matching device found
-            :rtype: list of :class:`~.devices.Device`
-        """
-        log_method_call(self, device_type=device_type, incomplete=incomplete, hidden=hidden)
-        devices = self._filterDevices(incomplete=incomplete, hidden=hidden)
-        result = [d for d in devices if d.type == device_type]
-        log_method_return(self, result)
-        return result
-
     def getDeviceByID(self, id_num, incomplete=False, hidden=False):
         """ Return a device with specified device id.
 
diff --git a/blivet/iscsi.py b/blivet/iscsi.py
index cc557cb..42fca12 100644
--- a/blivet/iscsi.py
+++ b/blivet/iscsi.py
@@ -430,8 +430,7 @@ class iscsi(object):
 
     def getNodeDisks(self, node, storage):
         nodeDisks = []
-        iscsiDisks = storage.devicetree.getDevicesByType("iscsi")
-        for disk in iscsiDisks:
+        for disk in (d for d in storage.disks if d.type == "iscsi"):
             if disk.node == node:
                 nodeDisks.append(disk)
 
diff --git a/tests/action_test.py b/tests/action_test.py
index 6e99a9f..ad7b40d 100644
--- a/tests/action_test.py
+++ b/tests/action_test.py
@@ -118,9 +118,9 @@ class DeviceActionTestCase(StorageTestCase):
 
         # clear the disks
         self.destroyAllDevices()
-        self.assertEqual(devicetree.getDevicesByType("lvmlv"), [])
-        self.assertEqual(devicetree.getDevicesByType("lvmvg"), [])
-        self.assertEqual(devicetree.getDevicesByType("partition"), [])
+        self.assertEqual(self.storage.lvs, [])
+        self.assertEqual(self.storage.vgs, [])
+        self.assertEqual(self.storage.partitions, [])
 
         sda = devicetree.getDeviceByName("sda")
         self.assertNotEqual(sda, None, "failed to find disk 'sda'")
-- 
1.9.3

