Revision 0f511c8a lib/cmdlib.py

b/lib/cmdlib.py
813 813
  return params_copy
814 814

  
815 815

  
816
def _UpdateMinMaxISpecs(ipolicy, new_minmax, group_policy):
817
  use_none = use_default = group_policy
818
  minmax = ipolicy.setdefault(constants.ISPECS_MINMAX, {})
819
  for (key, value) in new_minmax.items():
820
    if key not in constants.ISPECS_MINMAX_KEYS:
821
      raise errors.OpPrereqError("Invalid key in new ipolicy/%s: %s" %
822
                                 (constants.ISPECS_MINMAX, key),
823
                                 errors.ECODE_INVAL)
824
    old_spec = minmax.get(key, {})
825
    minmax[key] = _GetUpdatedParams(old_spec, value, use_none=use_none,
826
                                    use_default=use_default)
827
    utils.ForceDictType(minmax[key], constants.ISPECS_PARAMETER_TYPES)
828

  
829

  
830 816
def _GetUpdatedIPolicy(old_ipolicy, new_ipolicy, group_policy=False):
831 817
  """Return the new version of an instance policy.
832 818

  
......
834 820
    we should support removal of policy entries
835 821

  
836 822
  """
837
  use_none = use_default = group_policy
838 823
  ipolicy = copy.deepcopy(old_ipolicy)
839 824
  for key, value in new_ipolicy.items():
840 825
    if key not in constants.IPOLICY_ALL_KEYS:
841 826
      raise errors.OpPrereqError("Invalid key in new ipolicy: %s" % key,
842 827
                                 errors.ECODE_INVAL)
843
    if key == constants.ISPECS_MINMAX:
844
      _UpdateMinMaxISpecs(ipolicy, value, group_policy)
845
    elif key == constants.ISPECS_STD:
846
      ipolicy[key] = _GetUpdatedParams(old_ipolicy.get(key, {}), value,
847
                                       use_none=use_none,
848
                                       use_default=use_default)
849
      utils.ForceDictType(ipolicy[key], constants.ISPECS_PARAMETER_TYPES)
828
    if (not value or value == [constants.VALUE_DEFAULT] or
829
        value == constants.VALUE_DEFAULT):
830
      if group_policy:
831
        del ipolicy[key]
832
      else:
833
        raise errors.OpPrereqError("Can't unset ipolicy attribute '%s'"
834
                                   " on the cluster'" % key,
835
                                   errors.ECODE_INVAL)
850 836
    else:
851
      if (not value or value == [constants.VALUE_DEFAULT] or
852
          value == constants.VALUE_DEFAULT):
837
      if key in constants.IPOLICY_PARAMETERS:
838
        # FIXME: we assume all such values are float
839
        try:
840
          ipolicy[key] = float(value)
841
        except (TypeError, ValueError), err:
842
          raise errors.OpPrereqError("Invalid value for attribute"
843
                                     " '%s': '%s', error: %s" %
844
                                     (key, value, err), errors.ECODE_INVAL)
845
      elif key == constants.ISPECS_MINMAX:
846
        for k in value.keys():
847
          utils.ForceDictType(value[k], constants.ISPECS_PARAMETER_TYPES)
848
        ipolicy[key] = value
849
      elif key == constants.ISPECS_STD:
853 850
        if group_policy:
854
          del ipolicy[key]
855
        else:
856
          raise errors.OpPrereqError("Can't unset ipolicy attribute '%s'"
857
                                     " on the cluster'" % key,
858
                                     errors.ECODE_INVAL)
851
          msg = "%s cannot appear in group instance specs" % key
852
          raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
853
        ipolicy[key] = _GetUpdatedParams(old_ipolicy.get(key, {}), value,
854
                                         use_none=False, use_default=False)
855
        utils.ForceDictType(ipolicy[key], constants.ISPECS_PARAMETER_TYPES)
859 856
      else:
860
        if key in constants.IPOLICY_PARAMETERS:
861
          # FIXME: we assume all such values are float
862
          try:
863
            ipolicy[key] = float(value)
864
          except (TypeError, ValueError), err:
865
            raise errors.OpPrereqError("Invalid value for attribute"
866
                                       " '%s': '%s', error: %s" %
867
                                       (key, value, err), errors.ECODE_INVAL)
868
        else:
869
          # FIXME: we assume all others are lists; this should be redone
870
          # in a nicer way
871
          ipolicy[key] = list(value)
857
        # FIXME: we assume all others are lists; this should be redone
858
        # in a nicer way
859
        ipolicy[key] = list(value)
872 860
  try:
873 861
    objects.InstancePolicy.CheckParameterSyntax(ipolicy, not group_policy)
874 862
  except errors.ConfigurationError, err:

Also available in: Unified diff