From 5224c9bf41f72ad74eabb4edeca1e22f011bf24f Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Wed, 13 Feb 2013 13:43:33 +0100
Subject: [PATCH] Helper to retrieve access permissions for RAPI resource

Merge similar code into a helper function.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/rapi/baserlib.py                  | 11 +++++++++++
 lib/server/rapi.py                    |  5 +++--
 test/py/ganeti.rapi.rlib2_unittest.py |  5 +++--
 3 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/lib/rapi/baserlib.py b/lib/rapi/baserlib.py
index 9534e3b76..4b6cc8987 100644
--- a/lib/rapi/baserlib.py
+++ b/lib/rapi/baserlib.py
@@ -406,6 +406,17 @@ def GetResourceOpcodes(cls):
                                  for (_, op_attr, _, _) in _OPCODE_ATTRS)))
 
 
+def GetHandlerAccess(handler, method):
+  """Returns the access rights for a method on a handler.
+
+  @type handler: L{ResourceBase}
+  @type method: string
+  @rtype: string or None
+
+  """
+  return getattr(handler, "%s_ACCESS" % method, None)
+
+
 class _MetaOpcodeResource(type):
   """Meta class for RAPI resources.
 
diff --git a/lib/server/rapi.py b/lib/server/rapi.py
index 3fda622db..65162ee93 100644
--- a/lib/server/rapi.py
+++ b/lib/server/rapi.py
@@ -1,7 +1,7 @@
 #
 #
 
-# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2012 Google Inc.
+# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2012, 2013 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -49,6 +49,7 @@ from ganeti import compat
 from ganeti import utils
 from ganeti import pathutils
 from ganeti.rapi import connector
+from ganeti.rapi import baserlib
 
 import ganeti.http.auth   # pylint: disable=W0611
 import ganeti.http.server
@@ -120,7 +121,7 @@ class RemoteApiHandler(http.auth.HttpServerRequestAuthentication,
         raise http.HttpNotImplemented("Method %s is unsupported for path %s" %
                                       (method, req.request_path))
 
-      ctx.handler_access = getattr(ctx.handler, "%s_ACCESS" % method, None)
+      ctx.handler_access = baserlib.GetHandlerAccess(ctx.handler, method)
 
       # Require permissions definition (usually in the base class)
       if ctx.handler_access is None:
diff --git a/test/py/ganeti.rapi.rlib2_unittest.py b/test/py/ganeti.rapi.rlib2_unittest.py
index b66d01a4c..77c44353b 100755
--- a/test/py/ganeti.rapi.rlib2_unittest.py
+++ b/test/py/ganeti.rapi.rlib2_unittest.py
@@ -1,7 +1,7 @@
 #!/usr/bin/python
 #
 
-# Copyright (C) 2010, 2012 Google Inc.
+# Copyright (C) 2010, 2012, 2013 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -1770,7 +1770,8 @@ class TestPermissions(unittest.TestCase):
   def testMethodAccess(self):
     for handler in connector.CONNECTOR.values():
       for method in baserlib._SUPPORTED_METHODS:
-        access = getattr(handler, "%s_ACCESS" % method)
+        access = baserlib.GetHandlerAccess(handler, method)
+        self.assertFalse(access is None)
         self.assertFalse(set(access) - rapi.RAPI_ACCESS_ALL,
                          msg=("Handler '%s' uses unknown access options for"
                               " method %s" % (handler, method)))
-- 
GitLab