From bc82629248edd8900275d94de6002d1ecc6ac01b Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Tue, 4 Dec 2012 17:57:13 +0100
Subject: [PATCH] Export error codes from RAPI client module
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Until now the error codes were not available from the RAPI client
module. A newly added unit test ensures all error codes are contained in
β€œECODE_ALL”, as well as ensuring consistency between the RAPI client and
the authoritative β€œerrors” module.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/rapi/client.py                  | 44 +++++++++++++++++++++++++++++
 test/ganeti.rapi.client_unittest.py | 12 ++++++++
 2 files changed, 56 insertions(+)

diff --git a/lib/rapi/client.py b/lib/rapi/client.py
index 04d4160f5..a19a09e7c 100644
--- a/lib/rapi/client.py
+++ b/lib/rapi/client.py
@@ -114,6 +114,50 @@ _INST_REINSTALL_REQV1 = INST_REINSTALL_REQV1
 _NODE_MIGRATE_REQV1 = NODE_MIGRATE_REQV1
 _NODE_EVAC_RES1 = NODE_EVAC_RES1
 
+#: Resolver errors
+ECODE_RESOLVER = "resolver_error"
+
+#: Not enough resources (iallocator failure, disk space, memory, etc.)
+ECODE_NORES = "insufficient_resources"
+
+#: Temporarily out of resources; operation can be tried again
+ECODE_TEMP_NORES = "insufficient_resources"
+
+#: Wrong arguments (at syntax level)
+ECODE_INVAL = "wrong_input"
+
+#: Wrong entity state
+ECODE_STATE = "wrong_state"
+
+#: Entity not found
+ECODE_NOENT = "unknown_entity"
+
+#: Entity already exists
+ECODE_EXISTS = "already_exists"
+
+#: Resource not unique (e.g. MAC or IP duplication)
+ECODE_NOTUNIQUE = "resource_not_unique"
+
+#: Internal cluster error
+ECODE_FAULT = "internal_error"
+
+#: Environment error (e.g. node disk error)
+ECODE_ENVIRON = "environment_error"
+
+#: List of all failure types
+ECODE_ALL = frozenset([
+  ECODE_RESOLVER,
+  ECODE_NORES,
+  ECODE_TEMP_NORES,
+  ECODE_INVAL,
+  ECODE_STATE,
+  ECODE_NOENT,
+  ECODE_EXISTS,
+  ECODE_NOTUNIQUE,
+  ECODE_FAULT,
+  ECODE_ENVIRON,
+  ])
+
 # Older pycURL versions don't have all error constants
 try:
   _CURLE_SSL_CACERT = pycurl.E_SSL_CACERT
diff --git a/test/ganeti.rapi.client_unittest.py b/test/ganeti.rapi.client_unittest.py
index e5b243682..d9820c25f 100755
--- a/test/ganeti.rapi.client_unittest.py
+++ b/test/ganeti.rapi.client_unittest.py
@@ -34,6 +34,7 @@ from ganeti import utils
 from ganeti import query
 from ganeti import objects
 from ganeti import rapi
+from ganeti import errors
 
 import ganeti.rapi.testutils
 from ganeti.rapi import connector
@@ -136,6 +137,17 @@ class TestConstants(unittest.TestCase):
     self.assertEqual(client._NODE_EVAC_RES1, rlib2._NODE_EVAC_RES1)
     self.assertEqual(client.NODE_EVAC_RES1, rlib2._NODE_EVAC_RES1)
 
+  def testErrors(self):
+    self.assertEqual(client.ECODE_ALL, errors.ECODE_ALL)
+
+    # Make sure all error codes are in both RAPI client and errors module
+    for name in filter(lambda s: (s.startswith("ECODE_") and s != "ECODE_ALL"),
+                       dir(client)):
+      value = getattr(client, name)
+      self.assertEqual(value, getattr(errors, name))
+      self.assertTrue(value in client.ECODE_ALL)
+      self.assertTrue(value in errors.ECODE_ALL)
+
 
 class RapiMockTest(unittest.TestCase):
   def test404(self):
-- 
GitLab