X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/08db7c5cd2748c55676018f2aad9499b0f5530dc..c8fcde472922e4ee664d904e0bf1a583f1d5040d:/lib/objects.py diff --git a/lib/objects.py b/lib/objects.py index 26cdc3d..64b4418 100644 --- a/lib/objects.py +++ b/lib/objects.py @@ -37,7 +37,39 @@ from ganeti import constants __all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance", - "OS", "Node", "Cluster"] + "OS", "Node", "Cluster", "FillDict"] + +def FillDict(defaults_dict, custom_dict): + """Basic function to apply settings on top a default dict. + + @type defaults_dict: dict + @param defaults_dict: dictionary holding the default values + @type custom_dict: dict + @param custom_dict: dictionary holding customized value + @rtype: dict + @return: dict with the 'full' values + + """ + ret_dict = copy.deepcopy(defaults_dict) + ret_dict.update(custom_dict) + return ret_dict + + +def UpgradeGroupedParams(target, defaults): + """Update all groups for the target parameter. + + @type target: dict of dicts + @param target: {group: {parameter: value}} + @type defaults: dict + @param defaults: default parameter values + + """ + if target is None: + target = {constants.PP_DEFAULT: defaults} + else: + for group in target: + target[group] = FillDict(defaults, target[group]) + return target class ConfigObject(object): @@ -50,7 +82,7 @@ class ConfigObject(object): as None instead of raising an error Classes derived from this must always declare __slots__ (we use many - config objects and the memory reduction is useful. + config objects and the memory reduction is useful) """ __slots__ = [] @@ -58,6 +90,7 @@ class ConfigObject(object): def __init__(self, **kwargs): for k, v in kwargs.iteritems(): setattr(self, k, v) + self.UpgradeConfig() def __getattr__(self, name): if name not in self.__slots__: @@ -157,6 +190,15 @@ class ConfigObject(object): """Implement __repr__ for ConfigObjects.""" return repr(self.ToDict()) + def UpgradeConfig(self): + """Fill defaults for missing configuration values. + + This method will be called at object init time, and its implementation will + be object dependent. + + """ + pass + class TaggableObject(ConfigObject): """An generic class supporting tags. @@ -179,7 +221,7 @@ class TaggableObject(ConfigObject): constants.MAX_TAG_LEN) if not tag: raise errors.TagError("Tags cannot be empty") - if not re.match("^[ \w.+*/:-]+$", tag): + if not re.match("^[\w.+*/:-]+$", tag): raise errors.TagError("Tag contains invalid characters") def GetTags(self): @@ -270,6 +312,24 @@ class NIC(ConfigObject): """Config object representing a network card.""" __slots__ = ["mac", "ip", "bridge"] + @classmethod + def CheckParameterSyntax(cls, nicparams): + """Check the given parameters for validity. + + @type nicparams: dict + @param nicparams: dictionary with parameter names/value + @raise errors.ConfigurationError: when a parameter is not valid + + """ + if nicparams[constants.NIC_MODE] not in constants.NIC_VALID_MODES: + err = "Invalid nic mode: %s" % nicparams[constants.NIC_MODE] + raise errors.ConfigurationError(err) + + if (nicparams[constants.NIC_MODE] is constants.NIC_MODE_BRIDGED and + not nicparams[constants.NIC_LINK]): + err = "Missing bridged nic link" + raise errors.ConfigurationError(err) + class Disk(ConfigObject): """Config object representing a block device.""" @@ -472,6 +532,7 @@ class Disk(ConfigObject): if self.dev_type == constants.LD_LV: val = "