Helper to retrieve access permissions for RAPI resource
authorMichael Hanselmann <hansmi@google.com>
Wed, 13 Feb 2013 12:43:33 +0000 (13:43 +0100)
committerMichael Hanselmann <hansmi@google.com>
Tue, 19 Feb 2013 14:29:58 +0000 (15:29 +0100)
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
lib/server/rapi.py
test/py/ganeti.rapi.rlib2_unittest.py

index 9534e3b..4b6cc89 100644 (file)
@@ -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.
 
index 3fda622..65162ee 100644 (file)
@@ -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:
index b66d01a..77c4435 100755 (executable)
@@ -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)))