From: Helga Velroyen Date: Wed, 7 Aug 2013 11:39:51 +0000 (+0200) Subject: bootstrap: restrict ipolicy to enabled disk templates X-Git-Tag: v2.9.0beta1~23 X-Git-Url: https://code.grnet.gr/git/ganeti-local/commitdiff_plain/d514e18b9dbf26058c9b506099e80b6ddde93e26 bootstrap: restrict ipolicy to enabled disk templates 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 Reviewed-by: Thomas Thrainer --- diff --git a/lib/bootstrap.py b/lib/bootstrap.py index e8250cc..1b862c5 100644 --- a/lib/bootstrap.py +++ b/lib/bootstrap.py @@ -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) diff --git a/test/py/ganeti.bootstrap_unittest.py b/test/py/ganeti.bootstrap_unittest.py index 7b38213..82c4a16 100755 --- a/test/py/ganeti.bootstrap_unittest.py +++ b/test/py/ganeti.bootstrap_unittest.py @@ -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()