From fdad8c4dba0e730b3d34d0b2608892919ca94e12 Mon Sep 17 00:00:00 2001
From: Balazs Lecz <leczb@google.com>
Date: Thu, 8 Apr 2010 18:00:43 +0100
Subject: [PATCH] Add --add-uids/--remove-uids to gnt-cluster modify

Signed-off-by: Balazs Lecz <leczb@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/cli.py          | 14 ++++++++++++++
 lib/cmdlib.py       | 12 ++++++++++++
 lib/opcodes.py      |  2 ++
 lib/uidpool.py      | 30 ++++++++++++++++++++++++++++++
 scripts/gnt-cluster | 18 +++++++++++++++---
 5 files changed, 73 insertions(+), 3 deletions(-)

diff --git a/lib/cli.py b/lib/cli.py
index 7044fc1c0..892a60a9b 100644
--- a/lib/cli.py
+++ b/lib/cli.py
@@ -44,6 +44,7 @@ from optparse import (OptionParser, TitledHelpFormatter,
 
 __all__ = [
   # Command line options
+  "ADD_UIDS_OPT",
   "ALLOCATABLE_OPT",
   "ALL_OPT",
   "AUTO_PROMOTE_OPT",
@@ -111,6 +112,7 @@ __all__ = [
   "RAPI_CERT_OPT",
   "READD_OPT",
   "REBOOT_TYPE_OPT",
+  "REMOVE_UIDS_OPT",
   "SECONDARY_IP_OPT",
   "SELECT_OS_OPT",
   "SEP_OPT",
@@ -935,6 +937,18 @@ UIDPOOL_OPT = cli_option("--uid-pool", default=None,
                          help=("A list of user-ids or user-id"
                                " ranges separated by commas"))
 
+ADD_UIDS_OPT = cli_option("--add-uids", default=None,
+                          action="store", dest="add_uids",
+                          help=("A list of user-ids or user-id"
+                                " ranges separated by commas, to be"
+                                " added to the user-id pool"))
+
+REMOVE_UIDS_OPT = cli_option("--remove-uids", default=None,
+                             action="store", dest="remove_uids",
+                             help=("A list of user-ids or user-id"
+                                   " ranges separated by commas, to be"
+                                   " removed from the user-id pool"))
+
 
 def _ParseArgs(argv, commands, aliases):
   """Parser for the command line arguments.
diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index cc0c2625d..054d88416 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -2272,6 +2272,12 @@ class LUSetClusterParams(LogicalUnit):
     if self.op.uid_pool:
       uidpool.CheckUidPool(self.op.uid_pool)
 
+    if self.op.add_uids:
+      uidpool.CheckUidPool(self.op.add_uids)
+
+    if self.op.remove_uids:
+      uidpool.CheckUidPool(self.op.remove_uids)
+
   def ExpandNames(self):
     # FIXME: in the future maybe other cluster params won't require checking on
     # all nodes to be modified.
@@ -2466,6 +2472,12 @@ class LUSetClusterParams(LogicalUnit):
     if self.op.maintain_node_health is not None:
       self.cluster.maintain_node_health = self.op.maintain_node_health
 
+    if self.op.add_uids is not None:
+      uidpool.AddToUidPool(self.cluster.uid_pool, self.op.add_uids)
+
+    if self.op.remove_uids is not None:
+      uidpool.RemoveFromUidPool(self.cluster.uid_pool, self.op.remove_uids)
+
     if self.op.uid_pool is not None:
       self.cluster.uid_pool = self.op.uid_pool
 
diff --git a/lib/opcodes.py b/lib/opcodes.py
index f6c7845cd..0d3b8ad12 100644
--- a/lib/opcodes.py
+++ b/lib/opcodes.py
@@ -307,6 +307,8 @@ class OpSetClusterParams(OpCode):
     "candidate_pool_size",
     "maintain_node_health",
     "uid_pool",
+    "add_uids",
+    "remove_uids",
     ]
 
 
diff --git a/lib/uidpool.py b/lib/uidpool.py
index 3a94675bc..15ab31487 100644
--- a/lib/uidpool.py
+++ b/lib/uidpool.py
@@ -78,6 +78,36 @@ def ParseUidPool(value, separator=None):
   return ranges
 
 
+def AddToUidPool(uid_pool, add_uids):
+  """Add a list of user-ids/user-id ranges to a user-id pool.
+
+  @param uid_pool: a user-id pool (list of integer tuples)
+  @param add_uids: user-id ranges to be added to the pool
+                   (list of integer tuples)
+
+  """
+  for uid_range in add_uids:
+    if uid_range not in uid_pool:
+      uid_pool.append(uid_range)
+  uid_pool.sort()
+
+
+def RemoveFromUidPool(uid_pool, remove_uids):
+  """Remove a list of user-ids/user-id ranges from a user-id pool.
+
+  @param uid_pool: a user-id pool (list of integer tuples)
+  @param remove_uids: user-id ranges to be removed from the pool
+                      (list of integer tuples)
+
+  """
+  for uid_range in remove_uids:
+    if uid_range not in uid_pool:
+      raise errors.OpPrereqError(
+          "User-id range to be removed is not found in the current"
+          " user-id pool: %s" % uid_range, errors.ECODE_INVAL)
+    uid_pool.remove(uid_range)
+
+
 def CheckUidPool(uid_pool):
   """Sanity check user-id pool range definition values.
 
diff --git a/scripts/gnt-cluster b/scripts/gnt-cluster
index 37502cf12..8a5098eef 100755
--- a/scripts/gnt-cluster
+++ b/scripts/gnt-cluster
@@ -603,7 +603,9 @@ def SetClusterParams(opts, args):
           opts.beparams or opts.nicparams or
           opts.candidate_pool_size is not None or
           opts.uid_pool is not None or
-          opts.maintain_node_health is not None):
+          opts.maintain_node_health is not None or
+          opts.add_uids is not None or
+          opts.remove_uids is not None):
     ToStderr("Please give at least one of the parameters.")
     return 1
 
@@ -637,6 +639,14 @@ def SetClusterParams(opts, args):
   if uid_pool is not None:
     uid_pool = uidpool.ParseUidPool(uid_pool)
 
+  add_uids = opts.add_uids
+  if add_uids is not None:
+    add_uids = uidpool.ParseUidPool(add_uids)
+
+  remove_uids = opts.remove_uids
+  if remove_uids is not None:
+    remove_uids = uidpool.ParseUidPool(remove_uids)
+
   op = opcodes.OpSetClusterParams(vg_name=vg_name,
                                   enabled_hypervisors=hvlist,
                                   hvparams=hvparams,
@@ -645,7 +655,9 @@ def SetClusterParams(opts, args):
                                   nicparams=nicparams,
                                   candidate_pool_size=opts.candidate_pool_size,
                                   maintain_node_health=mnh,
-                                  uid_pool=uid_pool)
+                                  uid_pool=uid_pool,
+                                  add_uids=add_uids,
+                                  remove_uids=remove_uids)
   SubmitOpCode(op, opts=opts)
   return 0
 
@@ -797,7 +809,7 @@ commands = {
     SetClusterParams, ARGS_NONE,
     [BACKEND_OPT, CP_SIZE_OPT, ENABLED_HV_OPT, HVLIST_OPT,
      NIC_PARAMS_OPT, NOLVM_STORAGE_OPT, VG_NAME_OPT, MAINTAIN_NODE_HEALTH_OPT,
-     UIDPOOL_OPT],
+     UIDPOOL_OPT, ADD_UIDS_OPT, REMOVE_UIDS_OPT],
     "[opts...]",
     "Alters the parameters of the cluster"),
   "renew-crypto": (
-- 
GitLab