+def UpgradeBeParams(target):
+ """Update the be parameters dict to the new format.
+
+ @type target: dict
+ @param target: "be" parameters dict
+
+ """
+ if constants.BE_MEMORY in target:
+ memory = target[constants.BE_MEMORY]
+ target[constants.BE_MAXMEM] = memory
+ target[constants.BE_MINMEM] = memory
+ del target[constants.BE_MEMORY]
+
+
+def UpgradeDiskParams(diskparams):
+ """Upgrade the disk parameters.
+
+ @type diskparams: dict
+ @param diskparams: disk parameters to upgrade
+ @rtype: dict
+ @return: the upgraded disk parameters dit
+
+ """
+ result = dict()
+ if diskparams is None:
+ result = constants.DISK_DT_DEFAULTS.copy()
+ else:
+ # Update the disk parameter values for each disk template.
+ # The code iterates over constants.DISK_TEMPLATES because new templates
+ # might have been added.
+ for template in constants.DISK_TEMPLATES:
+ if template not in diskparams:
+ result[template] = constants.DISK_DT_DEFAULTS[template].copy()
+ else:
+ result[template] = FillDict(constants.DISK_DT_DEFAULTS[template],
+ diskparams[template])
+
+ return result
+
+
+def MakeEmptyIPolicy():
+ """Create empty IPolicy dictionary.
+
+ """
+ return dict([
+ (constants.ISPECS_MIN, {}),
+ (constants.ISPECS_MAX, {}),
+ (constants.ISPECS_STD, {}),
+ ])
+
+
+def CreateIPolicyFromOpts(ispecs_mem_size=None,
+ ispecs_cpu_count=None,
+ ispecs_disk_count=None,
+ ispecs_disk_size=None,
+ ispecs_nic_count=None,
+ ipolicy_disk_templates=None,
+ group_ipolicy=False,
+ allowed_values=None,
+ fill_all=False):
+ """Creation of instance policy based on command line options.
+
+ @param fill_all: whether for cluster policies we should ensure that
+ all values are filled
+
+
+ """
+ # prepare ipolicy dict
+ ipolicy_transposed = {
+ constants.ISPEC_MEM_SIZE: ispecs_mem_size,
+ constants.ISPEC_CPU_COUNT: ispecs_cpu_count,
+ constants.ISPEC_DISK_COUNT: ispecs_disk_count,
+ constants.ISPEC_DISK_SIZE: ispecs_disk_size,
+ constants.ISPEC_NIC_COUNT: ispecs_nic_count,
+ }
+
+ # first, check that the values given are correct
+ if group_ipolicy:
+ forced_type = TISPECS_GROUP_TYPES
+ else:
+ forced_type = TISPECS_CLUSTER_TYPES
+
+ for specs in ipolicy_transposed.values():
+ utils.ForceDictType(specs, forced_type, allowed_values=allowed_values)
+
+ # then transpose
+ ipolicy_out = MakeEmptyIPolicy()
+ for name, specs in ipolicy_transposed.iteritems():
+ assert name in constants.ISPECS_PARAMETERS
+ for key, val in specs.items(): # {min: .. ,max: .., std: ..}
+ ipolicy_out[key][name] = val
+
+ # no filldict for lists
+ if not group_ipolicy and fill_all and ipolicy_disk_templates is None:
+ ipolicy_disk_templates = constants.DISK_TEMPLATES
+ if ipolicy_disk_templates is not None:
+ ipolicy_out[constants.IPOLICY_DTS] = list(ipolicy_disk_templates)
+
+ assert not (frozenset(ipolicy_out.keys()) - constants.IPOLICY_ALL_KEYS)
+
+ return ipolicy_out
+
+