Revision da5f09ef lib/objects.py

b/lib/objects.py
82 82
  return ret_dict
83 83

  
84 84

  
85
def FillIPolicy(default_ipolicy, custom_ipolicy, skip_keys=None):
85
def _FillMinMaxISpecs(default_specs, custom_specs):
86
  assert frozenset(default_specs.keys()) == constants.ISPECS_MINMAX_KEYS
87
  ret_specs = {}
88
  for key in constants.ISPECS_MINMAX_KEYS:
89
    ret_specs[key] = FillDict(default_specs[key],
90
                              custom_specs.get(key, {}))
91
  return ret_specs
92

  
93

  
94
def FillIPolicy(default_ipolicy, custom_ipolicy):
86 95
  """Fills an instance policy with defaults.
87 96

  
88 97
  """
89 98
  assert frozenset(default_ipolicy.keys()) == constants.IPOLICY_ALL_KEYS
90 99
  ret_dict = {}
91
  for key in constants.IPOLICY_ISPECS:
92
    ret_dict[key] = FillDict(default_ipolicy[key],
93
                             custom_ipolicy.get(key, {}),
94
                             skip_keys=skip_keys)
100
  # Instance specs
101
  new_mm = _FillMinMaxISpecs(default_ipolicy[constants.ISPECS_MINMAX],
102
                             custom_ipolicy.get(constants.ISPECS_MINMAX, {}))
103
  ret_dict[constants.ISPECS_MINMAX] = new_mm
104
  new_std = FillDict(default_ipolicy[constants.ISPECS_STD],
105
                     custom_ipolicy.get(constants.ISPECS_STD, {}))
106
  ret_dict[constants.ISPECS_STD] = new_std
95 107
  # list items
96 108
  for key in [constants.IPOLICY_DTS]:
97 109
    ret_dict[key] = list(custom_ipolicy.get(key, default_ipolicy[key]))
......
186 198
  """Create empty IPolicy dictionary.
187 199

  
188 200
  """
189
  return dict([
190
    (constants.ISPECS_MIN, {}),
191
    (constants.ISPECS_MAX, {}),
192
    (constants.ISPECS_STD, {}),
193
    ])
201
  return {
202
    constants.ISPECS_MINMAX: {
203
      constants.ISPECS_MIN: {},
204
      constants.ISPECS_MAX: {},
205
      },
206
    constants.ISPECS_STD: {},
207
    }
194 208

  
195 209

  
196 210
class ConfigObject(outils.ValidatedSlots):
......
912 926
class InstancePolicy(ConfigObject):
913 927
  """Config object representing instance policy limits dictionary.
914 928

  
915

  
916 929
  Note that this object is not actually used in the config, it's just
917 930
  used as a placeholder for a few functions.
918 931

  
......
921 934
  def CheckParameterSyntax(cls, ipolicy, check_std):
922 935
    """ Check the instance policy for validity.
923 936

  
937
    @type ipolicy: dict
938
    @param ipolicy: dictionary with min/max/std specs and policies
939
    @type check_std: bool
940
    @param check_std: Whether to check std value or just assume compliance
941
    @raise errors.ConfigurationError: when the policy is not legal
942

  
924 943
    """
925
    for param in constants.ISPECS_PARAMETERS:
926
      InstancePolicy.CheckISpecSyntax(ipolicy, param, check_std)
944
    if constants.ISPECS_MINMAX in ipolicy:
945
      if check_std and constants.ISPECS_STD not in ipolicy:
946
        msg = "Missing key in ipolicy: %s" % constants.ISPECS_STD
947
        raise errors.ConfigurationError(msg)
948
      minmaxspecs = ipolicy[constants.ISPECS_MINMAX]
949
      stdspec = ipolicy.get(constants.ISPECS_STD)
950
      for param in constants.ISPECS_PARAMETERS:
951
        InstancePolicy.CheckISpecSyntax(minmaxspecs, stdspec, param, check_std)
927 952
    if constants.IPOLICY_DTS in ipolicy:
928 953
      InstancePolicy.CheckDiskTemplates(ipolicy[constants.IPOLICY_DTS])
929 954
    for key in constants.IPOLICY_PARAMETERS:
......
935 960
                                      utils.CommaJoin(wrong_keys))
936 961

  
937 962
  @classmethod
938
  def CheckISpecSyntax(cls, ipolicy, name, check_std):
939
    """Check the instance policy for validity on a given key.
963
  def CheckISpecSyntax(cls, minmaxspecs, stdspec, name, check_std):
964
    """Check the instance policy specs for validity on a given key.
940 965

  
941
    We check if the instance policy makes sense for a given key, that is
942
    if ipolicy[min][name] <= ipolicy[std][name] <= ipolicy[max][name].
966
    We check if the instance specs makes sense for a given key, that is
967
    if minmaxspecs[min][name] <= stdspec[name] <= minmaxspec[max][name].
943 968

  
944
    @type ipolicy: dict
945
    @param ipolicy: dictionary with min, max, std specs
969
    @type minmaxspecs: dict
970
    @param minmaxspecs: dictionary with min and max instance spec
971
    @type stdspec: dict
972
    @param stdspec: dictionary with standard instance spec
946 973
    @type name: string
947 974
    @param name: what are the limits for
948 975
    @type check_std: bool
949 976
    @param check_std: Whether to check std value or just assume compliance
950
    @raise errors.ConfigureError: when specs for given name are not valid
977
    @raise errors.ConfigurationError: when specs for the given name are not
978
        valid
951 979

  
952 980
    """
953
    min_v = ipolicy[constants.ISPECS_MIN].get(name, 0)
981
    missing = constants.ISPECS_MINMAX_KEYS - frozenset(minmaxspecs.keys())
982
    if missing:
983
      msg = "Missing instance specification: %s" % utils.CommaJoin(missing)
984
      raise errors.ConfigurationError(msg)
985

  
986
    minspec = minmaxspecs[constants.ISPECS_MIN]
987
    maxspec = minmaxspecs[constants.ISPECS_MAX]
988
    min_v = minspec.get(name, 0)
954 989

  
955 990
    if check_std:
956
      std_v = ipolicy[constants.ISPECS_STD].get(name, min_v)
991
      std_v = stdspec.get(name, min_v)
957 992
      std_msg = std_v
958 993
    else:
959 994
      std_v = min_v
960 995
      std_msg = "-"
961 996

  
962
    max_v = ipolicy[constants.ISPECS_MAX].get(name, std_v)
963
    err = ("Invalid specification of min/max/std values for %s: %s/%s/%s" %
964
           (name,
965
            ipolicy[constants.ISPECS_MIN].get(name, "-"),
966
            ipolicy[constants.ISPECS_MAX].get(name, "-"),
967
            std_msg))
997
    max_v = maxspec.get(name, std_v)
968 998
    if min_v > std_v or std_v > max_v:
999
      err = ("Invalid specification of min/max/std values for %s: %s/%s/%s" %
1000
             (name,
1001
              minspec.get(name, "-"),
1002
              maxspec.get(name, "-"),
1003
              std_msg))
969 1004
      raise errors.ConfigurationError(err)
970 1005

  
971 1006
  @classmethod

Also available in: Unified diff