+class InstancePolicy(ConfigObject):
+ """Config object representing instance policy limits dictionary.
+
+
+ Note that this object is not actually used in the config, it's just
+ used as a placeholder for a few functions.
+
+ """
+ @classmethod
+ def CheckParameterSyntax(cls, ipolicy):
+ """ Check the instance policy for validity.
+
+ """
+ for param in constants.ISPECS_PARAMETERS:
+ InstancePolicy.CheckISpecSyntax(ipolicy, param)
+ if constants.IPOLICY_DTS in ipolicy:
+ InstancePolicy.CheckDiskTemplates(ipolicy[constants.IPOLICY_DTS])
+ for key in constants.IPOLICY_PARAMETERS:
+ if key in ipolicy:
+ InstancePolicy.CheckParameter(key, ipolicy[key])
+ wrong_keys = frozenset(ipolicy.keys()) - constants.IPOLICY_ALL_KEYS
+ if wrong_keys:
+ raise errors.ConfigurationError("Invalid keys in ipolicy: %s" %
+ utils.CommaJoin(wrong_keys))
+
+ @classmethod
+ def CheckISpecSyntax(cls, ipolicy, name):
+ """Check the instance policy for validity on a given key.
+
+ We check if the instance policy makes sense for a given key, that is
+ if ipolicy[min][name] <= ipolicy[std][name] <= ipolicy[max][name].
+
+ @type ipolicy: dict
+ @param ipolicy: dictionary with min, max, std specs
+ @type name: string
+ @param name: what are the limits for
+ @raise errors.ConfigureError: when specs for given name are not valid
+
+ """
+ min_v = ipolicy[constants.ISPECS_MIN].get(name, 0)
+ std_v = ipolicy[constants.ISPECS_STD].get(name, min_v)
+ max_v = ipolicy[constants.ISPECS_MAX].get(name, std_v)
+ err = ("Invalid specification of min/max/std values for %s: %s/%s/%s" %
+ (name,
+ ipolicy[constants.ISPECS_MIN].get(name, "-"),
+ ipolicy[constants.ISPECS_MAX].get(name, "-"),
+ ipolicy[constants.ISPECS_STD].get(name, "-")))
+ if min_v > std_v or std_v > max_v:
+ raise errors.ConfigurationError(err)
+
+ @classmethod
+ def CheckDiskTemplates(cls, disk_templates):
+ """Checks the disk templates for validity.
+
+ """
+ wrong = frozenset(disk_templates).difference(constants.DISK_TEMPLATES)
+ if wrong:
+ raise errors.ConfigurationError("Invalid disk template(s) %s" %
+ utils.CommaJoin(wrong))
+
+ @classmethod
+ def CheckParameter(cls, key, value):
+ """Checks a parameter.
+
+ Currently we expect all parameters to be float values.
+
+ """
+ try:
+ float(value)
+ except (TypeError, ValueError), err:
+ raise errors.ConfigurationError("Invalid value for key" " '%s':"
+ " '%s', error: %s" % (key, value, err))
+
+