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):
     """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.
+
+    """
+    if not item.uuid:
+      raise errors.ConfigurationError("'%s' must have an UUID" % (item.name,))
+    if not item.uuid in self._AllIDs(include_temporary=False):
+      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):
     """Set the instance's status to a given value.
diff --git a/test/py/testutils/config_mock.py b/test/py/testutils/config_mock.py
index 73174ca..761f0ad 100644
--- a/test/py/testutils/config_mock.py
+++ b/test/py/testutils/config_mock.py
@@ -814,7 +814,7 @@ class ConfigMock(config.ConfigWriter):
     if net_uuid:
       return self._UnlockedReserveIp(net_uuid, address, ec_id, check)
 
-  def AddInstance(self, instance, ec_id):
+  def AddInstance(self, instance, ec_id, replaces=False):
     """Add an instance to the config.
 
     """
-- 
2.2.0.rc0.207.ga3a616c

Reply via email to