Revision 918eb80b lib/objects.py

b/lib/objects.py
79 79
  return ret_dict
80 80

  
81 81

  
82
def FillDictOfDicts(defaults_dict, custom_dict, skip_keys=None):
83
  """Run FillDict for each key in dictionary.
84

  
85
  """
86
  ret_dict = {}
87
  for key in defaults_dict.keys():
88
    ret_dict[key] = FillDict(defaults_dict[key],
89
                             custom_dict.get(key, {}),
90
                             skip_keys=skip_keys)
91
  return ret_dict
92

  
93

  
82 94
def UpgradeGroupedParams(target, defaults):
83 95
  """Update all groups for the target parameter.
84 96

  
......
136 148
  return result
137 149

  
138 150

  
151
def MakeEmptyIPolicy():
152
  """Create empty IPolicy dictionary.
153

  
154
  """
155
  return dict([
156
    (constants.MIN_ISPECS, dict()),
157
    (constants.MAX_ISPECS, dict()),
158
    (constants.STD_ISPECS, dict()),
159
    ])
160

  
161

  
139 162
class ConfigObject(object):
140 163
  """A generic config object.
141 164

  
......
780 803
    # add here config upgrade for this disk
781 804

  
782 805

  
806
class InstancePolicy(ConfigObject):
807
  """Config object representing instance policy limits dictionary."""
808
  __slots__ = ["min", "max", "std"]
809

  
810
  @classmethod
811
  def CheckParameterSyntax(cls, ipolicy):
812
    """ Check the instance policy for validity.
813

  
814
    """
815
    for param in constants.ISPECS_PARAMETERS:
816
      InstancePolicy.CheckISpecSyntax(ipolicy, param)
817

  
818
  @classmethod
819
  def CheckISpecSyntax(cls, ipolicy, name):
820
    """Check the instance policy for validity on a given key.
821

  
822
    We check if the instance policy makes sense for a given key, that is
823
    if ipolicy[min][name] <= ipolicy[std][name] <= ipolicy[max][name].
824

  
825
    @type ipolicy: dict
826
    @param ipolicy: dictionary with min, max, std specs
827
    @type name: string
828
    @param name: what are the limits for
829
    @raise errors.ConfigureError: when specs for given name are not valid
830

  
831
    """
832
    min_v = ipolicy[constants.MIN_ISPECS].get(name, 0)
833
    std_v = ipolicy[constants.STD_ISPECS].get(name, min_v)
834
    max_v = ipolicy[constants.MAX_ISPECS].get(name, std_v)
835
    err = ("Invalid specification of min/max/std values for %s: %s/%s/%s" %
836
           (name,
837
            ipolicy[constants.MIN_ISPECS].get(name, "-"),
838
            ipolicy[constants.MAX_ISPECS].get(name, "-"),
839
            ipolicy[constants.STD_ISPECS].get(name, "-")))
840
    if min_v > std_v or std_v > max_v:
841
      raise errors.ConfigurationError(err)
842

  
843

  
783 844
class Instance(TaggableObject):
784 845
  """Config object representing an instance."""
785 846
  __slots__ = [
......
1238 1299
    "shared_file_storage_dir",
1239 1300
    "enabled_hypervisors",
1240 1301
    "hvparams",
1302
    "ipolicy",
1241 1303
    "os_hvp",
1242 1304
    "beparams",
1243 1305
    "osparams",
......
1354 1416

  
1355 1417
    self.diskparams = UpgradeDiskParams(self.diskparams)
1356 1418

  
1419
    # instance policy added before 2.6
1420
    if self.ipolicy is None:
1421
      self.ipolicy = MakeEmptyIPolicy()
1422

  
1357 1423
  @property
1358 1424
  def primary_hypervisor(self):
1359 1425
    """The first hypervisor is the primary.
......
1537 1603
    """
1538 1604
    return FillDict(self.ndparams, ndparams)
1539 1605

  
1606
  def SimpleFillIPolicy(self, ipolicy):
1607
    """ Fill instance policy dict with defaults.
1608

  
1609
    @type ipolicy: dict
1610
    @param ipolicy: the dict to fill
1611
    @rtype: dict
1612
    @return: a copy of passed ipolicy with missing keys filled from
1613
      the cluster defaults
1614

  
1615
    """
1616
    return FillDictOfDicts(self.ipolicy, ipolicy)
1617

  
1540 1618

  
1541 1619
class BlockDevStatus(ConfigObject):
1542 1620
  """Config object representing the status of a block device."""

Also available in: Unified diff