diff --git a/lib/cmdlib.py b/lib/cmdlib.py index 30c431e0e845ff65720ed3d9718e23778f577bd4..f1f38db1d97f800563cbf271c0d6ef9d8400233a 100644 --- a/lib/cmdlib.py +++ b/lib/cmdlib.py @@ -751,11 +751,11 @@ def _NICListToTuple(lu, nics): """ hooks_nics = [] - c_nicparams = lu.cfg.GetClusterInfo().nicparams[constants.PP_DEFAULT] + cluster = lu.cfg.GetClusterInfo() for nic in nics: ip = nic.ip mac = nic.mac - filled_params = objects.FillDict(c_nicparams, nic.nicparams) + filled_params = cluster.SimpleFillNIC(nic.nicparams) mode = filled_params[constants.NIC_MODE] link = filled_params[constants.NIC_LINK] hooks_nics.append((ip, mac, mode, link)) @@ -827,14 +827,12 @@ def _DecideSelfPromotion(lu, exceptions=None): return mc_now < mc_should -def _CheckNicsBridgesExist(lu, target_nics, target_node, - profile=constants.PP_DEFAULT): +def _CheckNicsBridgesExist(lu, target_nics, target_node): """Check that the brigdes needed by a list of nics exist. """ - c_nicparams = lu.cfg.GetClusterInfo().nicparams[profile] - paramslist = [objects.FillDict(c_nicparams, nic.nicparams) - for nic in target_nics] + cluster = lu.cfg.GetClusterInfo() + paramslist = [cluster.SimpleFillNIC(nic.nicparams) for nic in target_nics] brlist = [params[constants.NIC_LINK] for params in paramslist if params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED] if brlist: @@ -2332,13 +2330,11 @@ class LUSetClusterParams(LogicalUnit): # validate params changes if self.op.beparams: utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES) - self.new_beparams = objects.FillDict( - cluster.beparams[constants.PP_DEFAULT], self.op.beparams) + self.new_beparams = cluster.SimpleFillBE(self.op.beparams) if self.op.nicparams: utils.ForceDictType(self.op.nicparams, constants.NICS_PARAMETER_TYPES) - self.new_nicparams = objects.FillDict( - cluster.nicparams[constants.PP_DEFAULT], self.op.nicparams) + self.new_nicparams = cluster.SimpleFillNIC(self.op.nicparams) objects.NIC.CheckParameterSyntax(self.new_nicparams) nic_errors = [] @@ -4816,8 +4812,7 @@ class LUQueryInstances(NoHooksLU): iout = [] i_hv = cluster.FillHV(instance, skip_globals=True) i_be = cluster.FillBE(instance) - i_nicp = [objects.FillDict(cluster.nicparams[constants.PP_DEFAULT], - nic.nicparams) for nic in instance.nics] + i_nicp = [cluster.SimpleFillNIC(nic.nicparams) for nic in instance.nics] for field in self.op.output_fields: st_match = self._FIELDS_STATIC.Matches(field) if field in self._SIMPLE_FIELDS: @@ -6470,17 +6465,17 @@ class LUCreateInstance(LogicalUnit): """ # hvparams - hv_defs = cluster.GetHVDefaults(self.op.hypervisor, self.op.os_type) + hv_defs = cluster.SimpleFillHV(self.op.hypervisor, self.op.os_type, {}) for name in self.op.hvparams.keys(): if name in hv_defs and hv_defs[name] == self.op.hvparams[name]: del self.op.hvparams[name] # beparams - be_defs = cluster.beparams.get(constants.PP_DEFAULT, {}) + be_defs = cluster.SimpleFillBE({}) for name in self.op.beparams.keys(): if name in be_defs and be_defs[name] == self.op.beparams[name]: del self.op.beparams[name] # nic params - nic_defs = cluster.nicparams.get(constants.PP_DEFAULT, {}) + nic_defs = cluster.SimpleFillNIC({}) for nic in self.op.nics: for name in constants.NICS_PARAMETERS: if name in nic and name in nic_defs and nic[name] == nic_defs[name]: @@ -6514,9 +6509,8 @@ class LUCreateInstance(LogicalUnit): # check hypervisor parameter syntax (locally) utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES) - filled_hvp = objects.FillDict(cluster.GetHVDefaults(self.op.hypervisor, - self.op.os_type), - self.op.hvparams) + filled_hvp = cluster.SimpleFillHV(self.op.hypervisor, self.op.os_type, + self.op.hvparams) hv_type = hypervisor.GetHypervisor(self.op.hypervisor) hv_type.CheckParameterSyntax(filled_hvp) self.hv_full = filled_hvp @@ -6525,8 +6519,7 @@ class LUCreateInstance(LogicalUnit): # fill and remember the beparams dict utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES) - self.be_full = objects.FillDict(cluster.beparams[constants.PP_DEFAULT], - self.op.beparams) + self.be_full = cluster.SimpleFillBE(self.op.beparams) # now that hvp/bep are in final format, let's reset to defaults, # if told to do so @@ -6599,8 +6592,7 @@ class LUCreateInstance(LogicalUnit): if link: nicparams[constants.NIC_LINK] = link - check_params = objects.FillDict(cluster.nicparams[constants.PP_DEFAULT], - nicparams) + check_params = cluster.SimpleFillNIC(nicparams) objects.NIC.CheckParameterSyntax(check_params) self.nics.append(objects.NIC(mac=mac, ip=nic_ip, nicparams=nicparams)) @@ -8382,7 +8374,6 @@ class LUSetInstanceParams(LogicalUnit): if self.op.nics: args['nics'] = [] nic_override = dict(self.op.nics) - c_nicparams = self.cluster.nicparams[constants.PP_DEFAULT] for idx, nic in enumerate(self.instance.nics): if idx in nic_override: this_nic_override = nic_override[idx] @@ -8399,7 +8390,7 @@ class LUSetInstanceParams(LogicalUnit): if idx in self.nic_pnew: nicparams = self.nic_pnew[idx] else: - nicparams = objects.FillDict(c_nicparams, nic.nicparams) + nicparams = self.cluster.SimpleFillNIC(nic.nicparams) mode = nicparams[constants.NIC_MODE] link = nicparams[constants.NIC_LINK] args['nics'].append((ip, mac, mode, link)) @@ -9699,9 +9690,7 @@ class IAllocator(object): for iinfo, beinfo in i_list: nic_data = [] for nic in iinfo.nics: - filled_params = objects.FillDict( - cluster_info.nicparams[constants.PP_DEFAULT], - nic.nicparams) + filled_params = cluster_info.SimpleFillNIC(nic.nicparams) nic_dict = {"mac": nic.mac, "ip": nic.ip, "mode": filled_params[constants.NIC_MODE], diff --git a/lib/objects.py b/lib/objects.py index 19c6d8f8d0a88b9f7b99d83cf76435731c3ac0c7..17a8190c3506aa0ccc139c0a4565956fe67f1b82 100644 --- a/lib/objects.py +++ b/lib/objects.py @@ -961,9 +961,31 @@ class Cluster(TaggableObject): return ret_dict + def SimpleFillHV(self, hv_name, os_name, hvparams, skip_globals=False): + """Fill a given hvparams dict with cluster defaults. + + @type hv_name: string + @param hv_name: the hypervisor to use + @type os_name: string + @param os_name: the OS to use for overriding the hypervisor defaults + @type skip_globals: boolean + @param skip_globals: if True, the global hypervisor parameters will + not be filled + @rtype: dict + @return: a copy of the given hvparams with missing keys filled from + the cluster defaults + + """ + if skip_globals: + skip_keys = constants.HVC_GLOBALS + else: + skip_keys = [] + + def_dict = self.GetHVDefaults(hv_name, os_name, skip_keys=skip_keys) + return FillDict(def_dict, hvparams, skip_keys=skip_keys) def FillHV(self, instance, skip_globals=False): - """Fill an instance's hvparams dict. + """Fill an instance's hvparams dict with cluster defaults. @type instance: L{objects.Instance} @param instance: the instance parameter to fill @@ -975,17 +997,23 @@ class Cluster(TaggableObject): the cluster defaults """ - if skip_globals: - skip_keys = constants.HVC_GLOBALS - else: - skip_keys = [] + return self.SimpleFillHV(instance.hypervisor, instance.os, + instance.hvparams, skip_globals) - def_dict = self.GetHVDefaults(instance.hypervisor, instance.os, - skip_keys=skip_keys) - return FillDict(def_dict, instance.hvparams, skip_keys=skip_keys) + def SimpleFillBE(self, beparams): + """Fill a given beparams dict with cluster defaults. + + @type beparam: dict + @param beparam: the dict to fill + @rtype: dict + @return: a copy of the passed in beparams with missing keys filled + from the cluster defaults + + """ + return FillDict(self.beparams.get(constants.PP_DEFAULT, {}), beparams) def FillBE(self, instance): - """Fill an instance's beparams dict. + """Fill an instance's beparams dict with cluster defaults. @type instance: L{objects.Instance} @param instance: the instance parameter to fill @@ -994,8 +1022,19 @@ class Cluster(TaggableObject): the cluster defaults """ - return FillDict(self.beparams.get(constants.PP_DEFAULT, {}), - instance.beparams) + return self.SimpleFillBE(instance.beparams) + + def SimpleFillNIC(self, nicparams): + """Fill a given nicparams dict with cluster defaults. + + @type nicparam: dict + @param nicparam: the dict to fill + @rtype: dict + @return: a copy of the passed in nicparams with missing keys filled + from the cluster defaults + + """ + return FillDict(self.nicparams.get(constants.PP_DEFAULT, {}), nicparams) class BlockDevStatus(ConfigObject):