From d63479b51077ec93bc9433d2cda494006bae2b58 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Mon, 12 Apr 2010 11:48:36 +0200
Subject: [PATCH] objects.Cluster: add method to get hv defaults

Currently the FillHV method is the one that does the cluster hvparams +
os hvparams merger. However, in some cases we need to do just this,
without adding the instance hvparams on top.

This patch adds a function to compute just this (hv + os hvp
combination) default dict, and modifies FillHV to use it to build the
final dict.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/objects.py                  | 39 +++++++++++++++++++++++----------
 test/ganeti.objects_unittest.py | 10 +++++++++
 2 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/lib/objects.py b/lib/objects.py
index b952520ff..4e6874b6e 100644
--- a/lib/objects.py
+++ b/lib/objects.py
@@ -933,6 +933,30 @@ class Cluster(TaggableObject):
       obj.tcpudp_port_pool = set(obj.tcpudp_port_pool)
     return obj
 
+  def GetHVDefaults(self, hypervisor, os_name=None, skip_keys=None):
+    """Get the default hypervisor parameters for the cluster.
+
+    @param hypervisor: the hypervisor name
+    @param os_name: if specified, we'll also update the defaults for this OS
+    @param skip_keys: if passed, list of keys not to use
+    @return: the defaults dict
+
+    """
+    if skip_keys is None:
+      skip_keys = []
+
+    fill_stack = [self.hvparams.get(hypervisor, {})]
+    if os_name is not None:
+      os_hvp = self.os_hvp.get(os_name, {}).get(hypervisor, {})
+      fill_stack.append(os_hvp)
+
+    ret_dict = {}
+    for o_dict in fill_stack:
+      ret_dict = FillDict(ret_dict, o_dict, skip_keys=skip_keys)
+
+    return ret_dict
+
+
   def FillHV(self, instance, skip_globals=False):
     """Fill an instance's hvparams dict.
 
@@ -951,18 +975,9 @@ class Cluster(TaggableObject):
     else:
       skip_keys = []
 
-    # We fill the list from least to most important override
-    fill_stack = [
-      self.hvparams.get(instance.hypervisor, {}),
-      self.os_hvp.get(instance.os, {}).get(instance.hypervisor, {}),
-      instance.hvparams,
-      ]
-
-    ret_dict = {}
-    for o_dict in fill_stack:
-      ret_dict = FillDict(ret_dict, o_dict, skip_keys=skip_keys)
-
-    return ret_dict
+    def_dict = self.GetHVDefaults(instance.hypervisor, instance.os,
+                                  skip_keys=skip_keys)
+    return FillDict(def_dict, instance.hvparams, skip_keys=skip_keys)
 
   def FillBE(self, instance):
     """Fill an instance's beparams dict.
diff --git a/test/ganeti.objects_unittest.py b/test/ganeti.objects_unittest.py
index 5acddf2bd..6b5cfe716 100755
--- a/test/ganeti.objects_unittest.py
+++ b/test/ganeti.objects_unittest.py
@@ -79,6 +79,16 @@ class TestClusterObject(unittest.TestCase):
     self.fake_cl = objects.Cluster(hvparams=hvparams, os_hvp=os_hvp)
     self.fake_cl.UpgradeConfig()
 
+  def testGetHVDefaults(self):
+    cl = self.fake_cl
+    self.failUnlessEqual(cl.GetHVDefaults(constants.HT_FAKE),
+                         cl.hvparams[constants.HT_FAKE])
+    self.failUnlessEqual(cl.GetHVDefaults(None), {})
+    self.failUnlessEqual(cl.GetHVDefaults(constants.HT_XEN_PVM,
+                                          os_name="lenny-image"),
+                         cl.os_hvp["lenny-image"][constants.HT_XEN_PVM])
+
+
   def testFillHvFullMerge(self):
     inst_hvparams = {
       "blah": "blubb",
-- 
GitLab