On 12/02/2014 06:21 PM, Klaus Aehlig wrote:
On Tue, Dec 02, 2014 at 05:56:51PM +0100, Niklas Hambuechen wrote:
On 12/02/2014 03:32 PM, 'Klaus Aehlig' via ganeti-devel wrote:
In general, it makes sense to insist that a newly added instance has a
UUID that is not in use. Upon committing forthcoming instances,
however, the expectation is that the instance is already present in a
forthcoming variant. Add a flag to AddInstance to support this use
case.

Signed-off-by: Klaus Aehlig <[email protected]>
---
  lib/config/__init__.py           | 17 +++++++++++++++--
  test/py/testutils/config_mock.py |  2 +-
  2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/lib/config/__init__.py b/lib/config/__init__.py
index bb57ef8..9d0f89b 100644
--- a/lib/config/__init__.py
+++ b/lib/config/__init__.py
@@ -1731,7 +1731,7 @@ class ConfigWriter(object):
      """
      return [(uuid, self._UnlockedGetNodeGroup(uuid)) for uuid in group_uuids]

-  def AddInstance(self, instance, _ec_id):
+  def AddInstance(self, instance, _ec_id, replaces=False):

Needs `@param/type replaces`.

      """Add an instance to the config.

      This should be used after creating a new instance.
@@ -1750,7 +1750,10 @@ class ConfigWriter(object):
                                          " MAC address '%s' already in use." %
                                          (instance.name, nic.mac))

-    self._CheckUniqueUUID(instance, include_temporary=False)
+    if replaces:
+      self._CheckUUIDpresent(instance)
+    else:
+      self._CheckUniqueUUID(instance, include_temporary=False)

      instance.serial_no = 1
      instance.ctime = instance.mtime = time.time()
@@ -1786,6 +1789,16 @@ class ConfigWriter(object):
        raise errors.ConfigurationError("Cannot add '%s': UUID %s already"
                                        " in use" % (item.name, item.uuid))

+  def _CheckUUIDpresent(self, item):
+    """Checks that an object with the given UUID exists.
+

Needs `@param/type item`.

+    """
+    if not item.uuid:
+      raise errors.ConfigurationError("'%s' must have an UUID" % (item.name,))
+    if not item.uuid in self._AllIDs(include_temporary=False):

Consider `not in`.

+      raise errors.ConfigurationError("Cannot replace '%s': UUID %s not 
present"
+                                      % (item.name, item.uuid))
+
    def _SetInstanceStatus(self, inst_uuid, status, disks_active,
                           admin_state_source):

I'd like to add the following interdiff; I'll send a separate mail with the 
changes
this implies on patch 6.

commit 3ec286aed5d24c5711e5990dd5153fcef0781fcd
Author: Klaus Aehlig <[email protected]>
Date:   Tue Dec 2 18:16:54 2014 +0100

     Interdiff [PATCH master 03/10] Support replacing an instance on creation

diff --git a/lib/config/__init__.py b/lib/config/__init__.py
index c8b56c2..0a7e084 100644
--- a/lib/config/__init__.py
+++ b/lib/config/__init__.py
@@ -1734,13 +1734,16 @@ class ConfigWriter(object):
      """
      return [(uuid, self._UnlockedGetNodeGroup(uuid)) for uuid in group_uuids]

-  def AddInstance(self, instance, _ec_id, replaces=False):
+  def AddInstance(self, instance, _ec_id, replace=False):
      """Add an instance to the config.

      This should be used after creating a new instance.

      @type instance: L{objects.Instance}
      @param instance: the instance object
+    @type replace: bool
+    @param replace: if true, expect the instance to be present and
+        replace rather than add.

      """
      if not isinstance(instance, objects.Instance):
@@ -1753,7 +1756,7 @@ class ConfigWriter(object):
                                          " MAC address '%s' already in use." %
                                          (instance.name, nic.mac))

-    if replaces:
+    if replace:
        self._CheckUUIDpresent(instance)
      else:
        self._CheckUniqueUUID(instance, include_temporary=False)
@@ -1795,10 +1798,13 @@ class ConfigWriter(object):
    def _CheckUUIDpresent(self, item):
      """Checks that an object with the given UUID exists.

+    @param: the instance or other UUID possessing object to verify that
+        its UUID is present
+
      """
      if not item.uuid:
        raise errors.ConfigurationError("'%s' must have an UUID" % (item.name,))
-    if not item.uuid in self._AllIDs(include_temporary=False):
+    if item.uuid not in self._AllIDs(include_temporary=False):
        raise errors.ConfigurationError("Cannot replace '%s': UUID %s not 
present"
                                        % (item.name, item.uuid))





LGTM

--
Niklas Hambüchen
Google Germany GmbH, Dienerstr. 12, 80331 Muenchen
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschaeftsfuehrer: Graham Law, Christine Elizabeth Flores

Reply via email to