From 62e999a5941cc95d6acdc8a9d91b9a8455b12229 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Tue, 4 Jan 2011 18:30:29 +0100
Subject: [PATCH] RAPI: Add resource to modify cluster

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 doc/rapi.rst                        | 63 +++++++++++++++++++++++++++++
 lib/rapi/client.py                  | 14 +++++++
 lib/rapi/connector.py               |  1 +
 lib/rapi/rlib2.py                   | 16 ++++++++
 test/ganeti.rapi.client_unittest.py | 12 ++++++
 5 files changed, 106 insertions(+)

diff --git a/doc/rapi.rst b/doc/rapi.rst
index 0226089ec..956ecf7b9 100644
--- a/doc/rapi.rst
+++ b/doc/rapi.rst
@@ -332,6 +332,69 @@ features:
   Instance reinstall supports body parameters.
 
 
+``/2/modify``
+++++++++++++++++++++++++++++++++++++++++
+
+Modifies cluster parameters.
+
+Supports the following commands: ``PUT``.
+
+``PUT``
+~~~~~~~
+
+Returns a job ID.
+
+Body parameters:
+
+``vg_name`` (string)
+  Volume group name.
+``enabled_hypervisors`` (list)
+  List of enabled hypervisors.
+``hvparams`` (dict)
+  Cluster-wide hypervisor parameter defaults, hypervisor-dependent.
+``beparams`` (dict)
+  Cluster-wide backend parameter defaults.
+``os_hvp`` (dict)
+  Cluster-wide per-OS hypervisor parameter defaults.
+``osparams`` (dict)
+  Dictionary with OS parameters.
+``candidate_pool_size`` (int)
+  Master candidate pool size.
+``uid_pool`` (list)
+  Set UID pool. Must be list of lists describing UID ranges (two items,
+  start and end inclusive).
+``add_uids``
+  Extend UID pool. Must be list of lists describing UID ranges (two
+  items, start and end inclusive) to be added.
+``remove_uids``
+  Shrink UID pool. Must be list of lists describing UID ranges (two
+  items, start and end inclusive) to be removed.
+``maintain_node_health`` (bool)
+  Whether to automatically maintain node health.
+``prealloc_wipe_disks`` (bool)
+  Whether to wipe disks before allocating them to instances.
+``nicparams`` (dict)
+  Cluster-wide NIC parameter defaults.
+``ndparams`` (dict)
+  Cluster-wide node parameter defaults.
+``drbd_helper`` (string)
+  DRBD helper program.
+``default_iallocator`` (string)
+  Default iallocator for cluster.
+``master_netdev`` (string)
+  Master network device.
+``reserved_lvs`` (list)
+  List of reserved LVs (strings).
+``hidden_os`` (list)
+  List of modifications as lists. Each modification must have two items,
+  the operation and the OS name. The operation can be ``add`` or
+  ``remove``.
+``blacklisted_os`` (list)
+  List of modifications as lists. Each modification must have two items,
+  the operation and the OS name. The operation can be ``add`` or
+  ``remove``.
+
+
 ``/2/groups``
 +++++++++++++
 
diff --git a/lib/rapi/client.py b/lib/rapi/client.py
index b129d65f2..f6fcb4264 100644
--- a/lib/rapi/client.py
+++ b/lib/rapi/client.py
@@ -481,6 +481,20 @@ class GanetiRapiClient(object):
     return self._SendRequest(HTTP_GET, "/%s/info" % GANETI_RAPI_VERSION,
                              None, None)
 
+  def ModifyCluster(self, **kwargs):
+    """Modifies cluster parameters.
+
+    More details for parameters can be found in the RAPI documentation.
+
+    @rtype: int
+    @return: job id
+
+    """
+    body = kwargs
+
+    return self._SendRequest(HTTP_PUT,
+                             "/%s/modify" % GANETI_RAPI_VERSION, None, body)
+
   def GetClusterTags(self):
     """Gets the cluster tags.
 
diff --git a/lib/rapi/connector.py b/lib/rapi/connector.py
index c1deeefd7..17e36622f 100644
--- a/lib/rapi/connector.py
+++ b/lib/rapi/connector.py
@@ -231,6 +231,7 @@ def GetHandlers(node_name_pattern, instance_name_pattern,
     "/2/os": rlib2.R_2_os,
     "/2/redistribute-config": rlib2.R_2_redist_config,
     "/2/features": rlib2.R_2_features,
+    "/2/modify": rlib2.R_2_cluster_modify,
     }
 
 
diff --git a/lib/rapi/rlib2.py b/lib/rapi/rlib2.py
index 73eabb699..9fe73dbbf 100644
--- a/lib/rapi/rlib2.py
+++ b/lib/rapi/rlib2.py
@@ -188,6 +188,22 @@ class R_2_redist_config(baserlib.R_Generic):
     return baserlib.SubmitJob([opcodes.OpRedistributeConfig()])
 
 
+class R_2_cluster_modify(baserlib.R_Generic):
+  """/2/modify resource.
+
+  """
+  def PUT(self):
+    """Modifies cluster parameters.
+
+    @return: a job id
+
+    """
+    op = baserlib.FillOpcode(opcodes.OpSetClusterParams, self.request_body,
+                             None)
+
+    return baserlib.SubmitJob([op])
+
+
 class R_2_jobs(baserlib.R_Generic):
   """/2/jobs resource.
 
diff --git a/test/ganeti.rapi.client_unittest.py b/test/ganeti.rapi.client_unittest.py
index 544551bf2..de4f996bc 100755
--- a/test/ganeti.rapi.client_unittest.py
+++ b/test/ganeti.rapi.client_unittest.py
@@ -1039,6 +1039,18 @@ class GanetiRapiClientTests(testutils.GanetiTestCase):
     self.assertEqual(job_id, 12347)
     self.assertHandler(rlib2.R_2_groups_name_rename)
 
+  def testModifyCluster(self):
+    for mnh in [None, False, True]:
+      self.rapi.AddResponse("14470")
+      self.assertEqual(14470,
+        self.client.ModifyCluster(maintain_node_health=mnh))
+      self.assertHandler(rlib2.R_2_cluster_modify)
+      self.assertItems([])
+      data = serializer.LoadJson(self.rapi.GetLastRequestData())
+      self.assertEqual(len(data), 1)
+      self.assertEqual(data["maintain_node_health"], mnh)
+      self.assertEqual(self.rapi.CountPending(), 0)
+
 
 if __name__ == '__main__':
   client.UsesRapiClient(testutils.GanetiTestProgram)()
-- 
GitLab