From fc95f88f30dc3429014335cfd3e5b07197c302fe Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Mon, 17 Sep 2007 09:10:02 +0000
Subject: [PATCH] Add rename instance support to ConfigWriter

This patch adds support for renaming instances to the ConfigWriter
class. It's needed in here in order to guarantee that we don't lose the
instance during the rename (the instance will be there, either with the
old name or with the new name).

Reviewed-by: imsnah
---
 lib/config.py | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/lib/config.py b/lib/config.py
index a80f77e9f..ef53cde55 100644
--- a/lib/config.py
+++ b/lib/config.py
@@ -329,6 +329,23 @@ class ConfigWriter:
     del self._config_data.instances[instance_name]
     self._WriteConfig()
 
+  def RenameInstance(self, old_name, new_name):
+    """Rename an instance.
+
+    This needs to be done in ConfigWriter and not by RemoveInstance
+    combined with AddInstance as only we can guarantee an atomic
+    rename.
+
+    """
+    self._OpenConfig()
+    if old_name not in self._config_data.instances:
+      raise errors.ConfigurationError("Unknown instance '%s'" % old_name)
+    inst = self._config_data.instances[old_name]
+    del self._config_data.instances[old_name]
+    inst.name = new_name
+    self._config_data.instances[inst.name] = inst
+    self._WriteConfig()
+
   def MarkInstanceDown(self, instance_name):
     """Mark the status of an instance to down in the configuration.
 
-- 
GitLab