+def _FormatListInfoDefault(data, def_data):
+ if data is not None:
+ ret = utils.CommaJoin(data)
+ else:
+ ret = "default (%s)" % utils.CommaJoin(def_data)
+ return ret
+
+
+def FormatPolicyInfo(custom_ipolicy, eff_ipolicy, iscluster):
+ """Formats an instance policy.
+
+ @type custom_ipolicy: dict
+ @param custom_ipolicy: own policy
+ @type eff_ipolicy: dict
+ @param eff_ipolicy: effective policy (including defaults); ignored for
+ cluster
+ @type iscluster: bool
+ @param iscluster: the policy is at cluster level
+ @rtype: list of pairs
+ @return: formatted data, suitable for L{PrintGenericInfo}
+
+ """
+ if iscluster:
+ eff_ipolicy = custom_ipolicy
+
+ minmax_out = []
+ custom_minmax = custom_ipolicy.get(constants.ISPECS_MINMAX)
+ if custom_minmax:
+ for (k, minmax) in enumerate(custom_minmax):
+ minmax_out.append([
+ ("%s/%s" % (key, k),
+ FormatParamsDictInfo(minmax[key], minmax[key]))
+ for key in constants.ISPECS_MINMAX_KEYS
+ ])
+ else:
+ for (k, minmax) in enumerate(eff_ipolicy[constants.ISPECS_MINMAX]):
+ minmax_out.append([
+ ("%s/%s" % (key, k),
+ FormatParamsDictInfo({}, minmax[key]))
+ for key in constants.ISPECS_MINMAX_KEYS
+ ])
+ ret = [("bounds specs", minmax_out)]
+
+ if iscluster:
+ stdspecs = custom_ipolicy[constants.ISPECS_STD]
+ ret.append(
+ (constants.ISPECS_STD,
+ FormatParamsDictInfo(stdspecs, stdspecs))
+ )
+
+ ret.append(
+ ("allowed disk templates",
+ _FormatListInfoDefault(custom_ipolicy.get(constants.IPOLICY_DTS),
+ eff_ipolicy[constants.IPOLICY_DTS]))
+ )
+ ret.extend([
+ (key, str(custom_ipolicy.get(key, "default (%s)" % eff_ipolicy[key])))
+ for key in constants.IPOLICY_PARAMETERS
+ ])
+ return ret
+
+
+def _PrintSpecsParameters(buf, specs):
+ values = ("%s=%s" % (par, val) for (par, val) in sorted(specs.items()))
+ buf.write(",".join(values))
+
+
+def PrintIPolicyCommand(buf, ipolicy, isgroup):
+ """Print the command option used to generate the given instance policy.
+
+ Currently only the parts dealing with specs are supported.
+
+ @type buf: StringIO
+ @param buf: stream to write into
+ @type ipolicy: dict
+ @param ipolicy: instance policy
+ @type isgroup: bool
+ @param isgroup: whether the policy is at group level
+
+ """
+ if not isgroup:
+ stdspecs = ipolicy.get("std")
+ if stdspecs:
+ buf.write(" %s " % IPOLICY_STD_SPECS_STR)
+ _PrintSpecsParameters(buf, stdspecs)
+ minmaxes = ipolicy.get("minmax", [])
+ first = True
+ for minmax in minmaxes:
+ minspecs = minmax.get("min")
+ maxspecs = minmax.get("max")
+ if minspecs and maxspecs:
+ if first:
+ buf.write(" %s " % IPOLICY_BOUNDS_SPECS_STR)
+ first = False
+ else:
+ buf.write("//")
+ buf.write("min:")
+ _PrintSpecsParameters(buf, minspecs)
+ buf.write("/max:")
+ _PrintSpecsParameters(buf, maxspecs)
+
+