From 4fe5cf90207f5a9922fc957808bfdfadbc6d3ba7 Mon Sep 17 00:00:00 2001
From: Adeodato Simo <dato@google.com>
Date: Thu, 2 Dec 2010 18:24:51 +0000
Subject: [PATCH] Group operations: OpCode and LU for renaming a group

Signed-off-by: Adeodato Simo <dato@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 doc/hooks.rst  | 10 +++++++
 lib/cmdlib.py  | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/mcpu.py    |  1 +
 lib/opcodes.py |  7 +++++
 4 files changed, 90 insertions(+)

diff --git a/doc/hooks.rst b/doc/hooks.rst
index 9d04a839c..282ed938e 100644
--- a/doc/hooks.rst
+++ b/doc/hooks.rst
@@ -193,6 +193,16 @@ not exist, and the hook is only executed in the master node.
 :pre-execution: master node
 :post-execution: master node
 
+OP_RENAME_GROUP
++++++++++++++++
+
+Renames a node group.
+
+:directory: group-rename
+:env. vars: OLD_NAME, NEW_NAME
+:pre-execution: master node and all nodes in the group
+:post-execution: master node and all nodes in the group
+
 
 Instance operations
 ~~~~~~~~~~~~~~~~~~~
diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index e28112d3a..24fb51bab 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -10533,6 +10533,78 @@ class LURemoveGroup(LogicalUnit):
     self.remove_locks[locking.LEVEL_NODEGROUP] = self.group_uuid
 
 
+class LURenameGroup(LogicalUnit):
+  HPATH = "group-rename"
+  HTYPE = constants.HTYPE_GROUP
+
+  _OP_PARAMS = [
+    ("old_name", ht.NoDefault, ht.TNonEmptyString),
+    ("new_name", ht.NoDefault, ht.TNonEmptyString),
+    ]
+
+  REQ_BGL = False
+
+  def ExpandNames(self):
+    # This raises errors.OpPrereqError on its own:
+    self.group_uuid = self.cfg.LookupNodeGroup(self.op.old_name)
+
+    self.needed_locks = {
+      locking.LEVEL_NODEGROUP: [self.group_uuid],
+      }
+
+  def CheckPrereq(self):
+    """Check prerequisites.
+
+    This checks that the given old_name exists as a node group, and that
+    new_name doesn't.
+
+    """
+    try:
+      new_name_uuid = self.cfg.LookupNodeGroup(self.op.new_name)
+    except errors.OpPrereqError:
+      pass
+    else:
+      raise errors.OpPrereqError("Desired new name '%s' clashes with existing"
+                                 " node group (UUID: %s)" %
+                                 (self.op.new_name, new_name_uuid),
+                                 errors.ECODE_EXISTS)
+
+  def BuildHooksEnv(self):
+    """Build hooks env.
+
+    """
+    env = {
+      "OLD_NAME": self.op.old_name,
+      "NEW_NAME": self.op.new_name,
+      }
+
+    mn = self.cfg.GetMasterNode()
+    all_nodes = self.cfg.GetAllNodesInfo()
+    run_nodes = [mn]
+    all_nodes.pop(mn, None)
+
+    for node in all_nodes.values():
+      if node.group == self.group_uuid:
+        run_nodes.append(node.name)
+
+    return env, run_nodes, run_nodes
+
+  def Exec(self, feedback_fn):
+    """Rename the node group.
+
+    """
+    group = self.cfg.GetNodeGroup(self.group_uuid)
+
+    if group is None:
+      raise errors.OpExecError("Could not retrieve group '%s' (UUID: %s)" %
+                               (self.op.old_name, self.group_uuid))
+
+    group.name = self.op.new_name
+    self.cfg.Update(group, feedback_fn)
+
+    return self.op.new_name
+
+
 class TagsLU(NoHooksLU): # pylint: disable-msg=W0223
   """Generic tags LU.
 
diff --git a/lib/mcpu.py b/lib/mcpu.py
index 4dabc6c6a..036b4f7c6 100644
--- a/lib/mcpu.py
+++ b/lib/mcpu.py
@@ -192,6 +192,7 @@ class Processor(object):
     opcodes.OpAddGroup: cmdlib.LUAddGroup,
     opcodes.OpQueryGroups: cmdlib.LUQueryGroups,
     opcodes.OpRemoveGroup: cmdlib.LURemoveGroup,
+    opcodes.OpRenameGroup: cmdlib.LURenameGroup,
     # os lu
     opcodes.OpDiagnoseOS: cmdlib.LUDiagnoseOS,
     # exports lu
diff --git a/lib/opcodes.py b/lib/opcodes.py
index a546f1e53..7fdd7b5e8 100644
--- a/lib/opcodes.py
+++ b/lib/opcodes.py
@@ -740,6 +740,13 @@ class OpRemoveGroup(OpCode):
   __slots__ = ["group_name"]
 
 
+class OpRenameGroup(OpCode):
+  """Rename a node group in the cluster."""
+  OP_ID = "OP_GROUP_RENAME"
+  OP_DSC_FIELD = "old_name"
+  __slots__ = ["old_name", "new_name"]
+
+
 # OS opcodes
 class OpDiagnoseOS(OpCode):
   """Compute the list of guest operating systems."""
-- 
GitLab