backend.py: GetInstanceListForHypervisor
authorHelga Velroyen <helgav@google.com>
Wed, 5 Jun 2013 09:20:02 +0000 (11:20 +0200)
committerHelga Velroyen <helgav@google.com>
Wed, 12 Jun 2013 07:17:25 +0000 (09:17 +0200)
In same cases, the call 'GetInstanceList' is only done for
one hypervisor. In those cases, it is more convenient to
not provide the full collection of hypervisor parameters
for all hypervisors, but only the ones for this hypervisor.
This patch factors out this particular case.

Signed-off-by: Helga Velroyen <helgav@google.com>
Reviewed-by: Thomas Thrainer <thomasth@google.com>

lib/backend.py

index 01701d5..664602c 100644 (file)
@@ -1112,6 +1112,35 @@ def BridgesExist(bridges_list):
     _Fail("Missing bridges %s", utils.CommaJoin(missing))
 
 
+def GetInstanceListForHypervisor(hname, hvparams=None,
+                                 get_hv_fn=hypervisor.GetHypervisor):
+  """Provides a list of instances of the given hypervisor.
+
+  @type hname: string
+  @param hname: name of the hypervisor
+  @type hvparams: dict of strings
+  @param hvparams: hypervisor parameters for the given hypervisor
+  @type get_hv_fn: function
+  @param get_hv_fn: function that returns a hypervisor for the given hypervisor
+    name; optional parameter to increase testability
+
+  @rtype: list
+  @return: a list of all running instances on the current node
+    - instance1.example.com
+    - instance2.example.com
+
+  """
+  results = []
+  try:
+    hv = get_hv_fn(hname)
+    names = hv.ListInstances(hvparams)
+    results.extend(names)
+  except errors.HypervisorError, err:
+    _Fail("Error enumerating instances (hypervisor %s): %s",
+          hname, err, exc=True)
+  return results
+
+
 def GetInstanceList(hypervisor_list, all_hvparams=None,
                     get_hv_fn=hypervisor.GetHypervisor):
   """Provides a list of instances.
@@ -1133,17 +1162,11 @@ def GetInstanceList(hypervisor_list, all_hvparams=None,
   """
   results = []
   for hname in hypervisor_list:
-    try:
-      hvparams = None
-      if all_hvparams is not None:
-        hvparams = all_hvparams[hname]
-      hv = get_hv_fn(hname)
-      names = hv.ListInstances(hvparams)
-      results.extend(names)
-    except errors.HypervisorError, err:
-      _Fail("Error enumerating instances (hypervisor %s): %s",
-            hname, err, exc=True)
-
+    hvparams = None
+    if all_hvparams is not None:
+      hvparams = all_hvparams[hname]
+    results.extend(GetInstanceListForHypervisor(hname, hvparams,
+                                                get_hv_fn=get_hv_fn))
   return results