From e0ac6ce67da018c7ad6f14117323f74d535f5018 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Fri, 16 Jul 2010 19:43:50 +0200
Subject: [PATCH] RAPI client: Support migrating instances

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/rapi/client.py                  | 23 +++++++++++++++++++++++
 test/ganeti.rapi.client_unittest.py | 25 +++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/lib/rapi/client.py b/lib/rapi/client.py
index 94000967a..b7f8dda1d 100644
--- a/lib/rapi/client.py
+++ b/lib/rapi/client.py
@@ -931,6 +931,29 @@ class GanetiRapiClient(object):
                              ("/%s/instances/%s/export" %
                               (GANETI_RAPI_VERSION, instance)), None, body)
 
+  def MigrateInstance(self, instance, mode=None, cleanup=None):
+    """Starts up an instance.
+
+    @type instance: string
+    @param instance: Instance name
+    @type mode: string
+    @param mode: Migration mode
+    @type cleanup: bool
+    @param cleanup: Whether to clean up a previously failed migration
+
+    """
+    body = {}
+
+    if mode is not None:
+      body["mode"] = mode
+
+    if cleanup is not None:
+      body["cleanup"] = cleanup
+
+    return self._SendRequest(HTTP_PUT,
+                             ("/%s/instances/%s/migrate" %
+                              (GANETI_RAPI_VERSION, instance)), None, body)
+
   def GetJobs(self):
     """Gets all jobs for the cluster.
 
diff --git a/test/ganeti.rapi.client_unittest.py b/test/ganeti.rapi.client_unittest.py
index 6069bcf99..b835ccec0 100755
--- a/test/ganeti.rapi.client_unittest.py
+++ b/test/ganeti.rapi.client_unittest.py
@@ -698,6 +698,31 @@ class GanetiRapiClientTests(testutils.GanetiTestCase):
     self.assertEqual(data["destination"], "nodeX")
     self.assertEqual(data["shutdown"], True)
 
+  def testMigrateInstanceDefaults(self):
+    self.rapi.AddResponse("24873")
+    job_id = self.client.MigrateInstance("inst91")
+    self.assertEqual(job_id, 24873)
+    self.assertHandler(rlib2.R_2_instances_name_migrate)
+    self.assertItems(["inst91"])
+
+    data = serializer.LoadJson(self.rapi.GetLastRequestData())
+    self.assertFalse(data)
+
+  def testMigrateInstance(self):
+    for mode in constants.HT_MIGRATION_MODES:
+      for cleanup in [False, True]:
+        self.rapi.AddResponse("31910")
+        job_id = self.client.MigrateInstance("inst289", mode=mode,
+                                             cleanup=cleanup)
+        self.assertEqual(job_id, 31910)
+        self.assertHandler(rlib2.R_2_instances_name_migrate)
+        self.assertItems(["inst289"])
+
+        data = serializer.LoadJson(self.rapi.GetLastRequestData())
+        self.assertEqual(len(data), 2)
+        self.assertEqual(data["mode"], mode)
+        self.assertEqual(data["cleanup"], cleanup)
+
   def testGetJobs(self):
     self.rapi.AddResponse('[ { "id": "123", "uri": "\\/2\\/jobs\\/123" },'
                           '  { "id": "124", "uri": "\\/2\\/jobs\\/124" } ]')
-- 
GitLab