Unify the “--backend-parameters” option
[ganeti-local] / lib / objects.py
index 6f99a81..e2154db 100644 (file)
@@ -39,6 +39,7 @@ from ganeti import constants
 __all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance",
            "OS", "Node", "Cluster", "FillDict"]
 
+_TIMESTAMPS = ["ctime", "mtime"]
 
 def FillDict(defaults_dict, custom_dict):
   """Basic function to apply settings on top a default dict.
@@ -99,11 +100,6 @@ class ConfigObject(object):
                            (type(self).__name__, name))
     return None
 
-  def __setitem__(self, key, value):
-    if key not in self.__slots__:
-      raise KeyError(key)
-    setattr(self, key, value)
-
   def __setstate__(self, state):
     for name in state:
       if name in self.__slots__:
@@ -187,6 +183,14 @@ class ConfigObject(object):
                       " _ContainerFromDicts" % c_type)
     return ret
 
+  def Copy(self):
+    """Makes a deep copy of the current object and its children.
+
+    """
+    dict_form = self.ToDict()
+    clone_obj = self.__class__.FromDict(dict_form)
+    return clone_obj
+
   def __repr__(self):
     """Implement __repr__ for ConfigObjects."""
     return repr(self.ToDict())
@@ -281,7 +285,8 @@ class TaggableObject(ConfigObject):
 
 class ConfigData(ConfigObject):
   """Top-level config object."""
-  __slots__ = ["version", "cluster", "nodes", "instances", "serial_no"]
+  __slots__ = (["version", "cluster", "nodes", "instances", "serial_no"] +
+               _TIMESTAMPS)
 
   def ToDict(self):
     """Custom function for top-level config data.
@@ -463,6 +468,15 @@ class Disk(ConfigObject):
       raise errors.ProgrammerError("Disk.RecordGrow called for unsupported"
                                    " disk type %s" % self.dev_type)
 
+  def UnsetSize(self):
+    """Sets recursively the size to zero for the disk and its children.
+
+    """
+    if self.children:
+      for child in self.children:
+        child.UnsetSize()
+    self.size = 0
+
   def SetPhysicalID(self, target_node, nodes_ip):
     """Convert the logical ID to the physical ID.
 
@@ -600,7 +614,7 @@ class Instance(TaggableObject):
     "disk_template",
     "network_port",
     "serial_no",
-    ]
+    ] + _TIMESTAMPS
 
   def _ComputeSecondaryNodes(self):
     """Compute the list of secondary nodes.
@@ -757,7 +771,7 @@ class Node(TaggableObject):
     "master_candidate",
     "offline",
     "drained",
-    ]
+    ] + _TIMESTAMPS
 
 
 class Cluster(TaggableObject):
@@ -782,7 +796,7 @@ class Cluster(TaggableObject):
     "nicparams",
     "candidate_pool_size",
     "modify_etc_hosts",
-    ]
+    ] + _TIMESTAMPS
 
   def UpgradeConfig(self):
     """Fill defaults for missing configuration values.
@@ -814,12 +828,11 @@ class Cluster(TaggableObject):
 
     # default_hypervisor is just the first enabled one in 2.1
     if self.default_hypervisor is not None:
-      self.enabled_hypervisors = [self.default_hypervisor] + \
+      self.enabled_hypervisors = ([self.default_hypervisor] +
         [hvname for hvname in self.enabled_hypervisors
-         if hvname != self.default_hypervisor]
+         if hvname != self.default_hypervisor])
       self.default_hypervisor = None
 
-
   def ToDict(self):
     """Custom function for cluster.