bootstrap: restrict ipolicy to enabled disk templates
authorHelga Velroyen <helgav@google.com>
Wed, 7 Aug 2013 11:39:51 +0000 (13:39 +0200)
committerHelga Velroyen <helgav@google.com>
Thu, 8 Aug 2013 15:48:36 +0000 (17:48 +0200)
With this patch, on cluster creation, the initial instance
policy's list of allowed disk templates will be modified
in a way that it does not contain any disk templates which
are not enabled cluster-wise.

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

lib/bootstrap.py
test/py/ganeti.bootstrap_unittest.py

index e8250cc..1b862c5 100644 (file)
@@ -452,6 +452,26 @@ def _InitCheckEnabledDiskTemplates(enabled_disk_templates):
                                errors.ECODE_INVAL)
 
 
+def _RestrictIpolicyToEnabledDiskTemplates(ipolicy, enabled_disk_templates):
+  """Restricts the ipolicy's disk templates to the enabled ones.
+
+  This function clears the ipolicy's list of allowed disk templates from the
+  ones that are not enabled by the cluster.
+
+  @type ipolicy: dict
+  @param ipolicy: the instance policy
+  @type enabled_disk_templates: list of string
+  @param enabled_disk_templates: the list of cluster-wide enabled disk
+    templates
+
+  """
+  assert constants.IPOLICY_DTS in ipolicy
+  allowed_disk_templates = ipolicy[constants.IPOLICY_DTS]
+  restricted_disk_templates = list(set(allowed_disk_templates)
+                                   .intersection(set(enabled_disk_templates)))
+  ipolicy[constants.IPOLICY_DTS] = restricted_disk_templates
+
+
 def InitCluster(cluster_name, mac_prefix, # pylint: disable=R0913, R0914
                 master_netmask, master_netdev, file_storage_dir,
                 shared_file_storage_dir, candidate_pool_size, secondary_ip=None,
@@ -595,6 +615,7 @@ def InitCluster(cluster_name, mac_prefix, # pylint: disable=R0913, R0914
   objects.NIC.CheckParameterSyntax(nicparams)
 
   full_ipolicy = objects.FillIPolicy(constants.IPOLICY_DEFAULTS, ipolicy)
+  _RestrictIpolicyToEnabledDiskTemplates(full_ipolicy, enabled_disk_templates)
 
   if ndparams is not None:
     utils.ForceDictType(ndparams, constants.NDS_PARAMETER_TYPES)
index 7b38213..82c4a16 100755 (executable)
@@ -111,5 +111,24 @@ class TestInitCheckEnabledDiskTemplates(unittest.TestCase):
         bootstrap._InitCheckEnabledDiskTemplates, enabled_disk_templates)
 
 
+class TestRestrictIpolicyToEnabledDiskTemplates(unittest.TestCase):
+
+  def testNoRestriction(self):
+    allowed_disk_templates = list(constants.DISK_TEMPLATES)
+    ipolicy = {constants.IPOLICY_DTS: allowed_disk_templates}
+    enabled_disk_templates = list(constants.DISK_TEMPLATES)
+    bootstrap._RestrictIpolicyToEnabledDiskTemplates(
+        ipolicy, enabled_disk_templates)
+    self.assertEqual(ipolicy[constants.IPOLICY_DTS], allowed_disk_templates)
+
+  def testRestriction(self):
+    allowed_disk_templates = [constants.DT_DRBD8, constants.DT_PLAIN]
+    ipolicy = {constants.IPOLICY_DTS: allowed_disk_templates}
+    enabled_disk_templates = [constants.DT_PLAIN, constants.DT_FILE]
+    bootstrap._RestrictIpolicyToEnabledDiskTemplates(
+        ipolicy, enabled_disk_templates)
+    self.assertEqual(ipolicy[constants.IPOLICY_DTS], [constants.DT_PLAIN])
+
+
 if __name__ == "__main__":
   testutils.GanetiTestProgram()