From f18fab7d6f8b06b6a4d35a352a219e52130c19db Mon Sep 17 00:00:00 2001
From: Adeodato Simo <dato@google.com>
Date: Thu, 9 Dec 2010 15:43:54 +0000
Subject: [PATCH] Expose OpSetGroupParams in RAPI and RAPI client

This creates the /2/groups/<name>/modify resource; at the moment, only the
"alloc_policy" attribute can be modified.

Signed-off-by: Adeodato Simo <dato@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 doc/rapi.rst          | 18 ++++++++++++++++++
 lib/rapi/client.py    | 15 +++++++++++++++
 lib/rapi/connector.py |  2 ++
 lib/rapi/rlib2.py     | 28 ++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+)

diff --git a/doc/rapi.rst b/doc/rapi.rst
index 67e1e6f1a..0226089ec 100644
--- a/doc/rapi.rst
+++ b/doc/rapi.rst
@@ -420,6 +420,24 @@ Deletes a node group.
 It supports the ``dry-run`` argument.
 
 
+``/2/groups/[group_name]/modify``
++++++++++++++++++++++++++++++++++
+
+Modifies the parameters of a node group.
+
+Supports the following commands: ``PUT``.
+
+``PUT``
+~~~~~~~
+
+Returns a job ID.
+
+Body parameters:
+
+``alloc_policy`` (string)
+  If present, the new allocation policy for the node group.
+
+
 ``/2/groups/[group_name]/rename``
 +++++++++++++++++++++++++++++++++
 
diff --git a/lib/rapi/client.py b/lib/rapi/client.py
index 096463ee7..b129d65f2 100644
--- a/lib/rapi/client.py
+++ b/lib/rapi/client.py
@@ -1434,6 +1434,21 @@ class GanetiRapiClient(object):
     return self._SendRequest(HTTP_POST, "/%s/groups" % GANETI_RAPI_VERSION,
                              query, body)
 
+  def ModifyGroup(self, group, **kwargs):
+    """Modifies a node group.
+
+    More details for parameters can be found in the RAPI documentation.
+
+    @type group: string
+    @param group: Node group name
+    @rtype: int
+    @return: job id
+
+    """
+    return self._SendRequest(HTTP_PUT,
+                             ("/%s/groups/%s/modify" %
+                              (GANETI_RAPI_VERSION, group)), None, kwargs)
+
   def DeleteGroup(self, group, dry_run=False):
     """Deletes a node group.
 
diff --git a/lib/rapi/connector.py b/lib/rapi/connector.py
index 9f3a314e3..c1deeefd7 100644
--- a/lib/rapi/connector.py
+++ b/lib/rapi/connector.py
@@ -215,6 +215,8 @@ def GetHandlers(node_name_pattern, instance_name_pattern,
     "/2/groups": rlib2.R_2_groups,
     re.compile(r'^/2/groups/(%s)$' % group_name_pattern):
       rlib2.R_2_groups_name,
+    re.compile(r'^/2/groups/(%s)/modify$' % group_name_pattern):
+      rlib2.R_2_groups_name_modify,
     re.compile(r'^/2/groups/(%s)/rename$' % group_name_pattern):
       rlib2.R_2_groups_name_rename,
 
diff --git a/lib/rapi/rlib2.py b/lib/rapi/rlib2.py
index 418c0034d..73eabb699 100644
--- a/lib/rapi/rlib2.py
+++ b/lib/rapi/rlib2.py
@@ -604,6 +604,34 @@ class R_2_groups_name(baserlib.R_Generic):
     return baserlib.SubmitJob([op])
 
 
+def _ParseModifyGroupRequest(name, data):
+  """Parses a request for modifying a node group.
+
+  @rtype: L{opcodes.OpSetGroupParams}
+  @return: Group modify opcode
+
+  """
+  alloc_policy = baserlib.CheckParameter(data, "alloc_policy", default=None)
+  return opcodes.OpSetGroupParams(group_name=name, alloc_policy=alloc_policy)
+
+
+class R_2_groups_name_modify(baserlib.R_Generic):
+  """/2/groups/[group_name]/modify resource.
+
+  """
+  def PUT(self):
+    """Changes some parameters of node group.
+
+    @return: a job id
+
+    """
+    baserlib.CheckType(self.request_body, dict, "Body contents")
+
+    op = _ParseModifyGroupRequest(self.items[0], self.request_body)
+
+    return baserlib.SubmitJob([op])
+
+
 def _ParseRenameGroupRequest(name, data, dry_run):
   """Parses a request for renaming a node group.
 
-- 
GitLab