Change in vdsm[master]: bridge: obtain method

piotr.kliczewski at gmail.com piotr.kliczewski at gmail.com
Fri Feb 12 13:41:44 UTC 2016


Piotr Kliczewski has uploaded a new change for review.

Change subject: bridge: obtain method
......................................................................

bridge: obtain method

We used to change ClassName.MethodName to ClassName_MethodName due
restrictions of parameter when using getattr. Now we use custom method
to do it so we do not need to change ClassName.MethodName.


Change-Id: I9b6cb53d8e42335510e34dfe372c1c21b80ac219
Signed-off-by: pkliczewski <piotr.kliczewski at gmail.com>
---
M lib/yajsonrpc/__init__.py
M tests/bridgeTests.py
M tests/stompTests.py
M vdsm/rpc/Bridge.py
4 files changed, 19 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/72/53472/1

diff --git a/lib/yajsonrpc/__init__.py b/lib/yajsonrpc/__init__.py
index a5e42b8..cf8defc 100644
--- a/lib/yajsonrpc/__init__.py
+++ b/lib/yajsonrpc/__init__.py
@@ -499,16 +499,15 @@
 
     def _serveRequest(self, ctx, req):
         self._attempt_log_stats()
-        mangledMethod = req.method.replace(".", "_")
         logLevel = logging.DEBUG
-        if mangledMethod in ('Host_getVMList', 'Host_getAllVmStats',
-                             'Host_getStats', 'StorageDomain_getStats',
-                             'VM_getStats', 'Host_fenceNode'):
+        if req.method in ('Host.getVMList', 'Host.getAllVmStats',
+                          'Host.getStats', 'StorageDomain.getStats',
+                          'VM.getStats', 'Host.fenceNode'):
             logLevel = logging.TRACE
         self.log.log(logLevel, "Calling '%s' in bridge with %s",
                      req.method, req.params)
         try:
-            method = getattr(self._bridge, mangledMethod)
+            method = self._bridge.get_method(req.method)
         except AttributeError:
             if req.isNotification():
                 return
diff --git a/tests/bridgeTests.py b/tests/bridgeTests.py
index bf73a74..24267ae 100644
--- a/tests/bridgeTests.py
+++ b/tests/bridgeTests.py
@@ -111,15 +111,16 @@
                   "agent": "apc_snmp", "username": "emesika",
                   "password": "pass", "action": "off", "options": "port=15"}
 
-        self.assertEquals(bridge.Host_fenceNode(**params), {'power': 'on'})
+        self.assertEquals(bridge.get_method('Host.fenceNode')(**params),
+                          {'power': 'on'})
 
     @MonkeyPatch(DynamicBridge, '_getApiInstance', _getApiInstance)
     def testMethodWithNoParams(self):
         bridge = DynamicBridge()
 
         bridge.register_server_address('127.0.0.1')
-        self.assertEquals(bridge.Host_getCapabilities()['My caps'],
-                          'My capabilites')
+        self.assertEquals(bridge.get_method('Host.getCapabilities')()
+                          ['My caps'], 'My capabilites')
         bridge.unregister_server_address()
 
     @MonkeyPatch(DynamicBridge, '_getApiInstance', _getApiInstance)
@@ -130,13 +131,14 @@
                   "force": "True",
                   "storagedomainID": "773adfc7-10d4-4e60-b700-3272ee1871f9"}
 
-        self.assertEqual(bridge.StorageDomain_detach(**params), None)
+        self.assertEqual(bridge.get_method('StorageDomain.detach')(**params),
+                         None)
 
     @MonkeyPatch(DynamicBridge, '_getApiInstance', _getApiInstance)
     def testHookError(self):
         bridge = DynamicBridge()
 
         with self.assertRaises(JsonRpcError) as e:
-            bridge.Host_ping()
+            bridge.get_method('Host.ping')()
 
         self.assertEquals(e.exception.code, 100)
diff --git a/tests/stompTests.py b/tests/stompTests.py
index 74ee6db..318e5f8 100644
--- a/tests/stompTests.py
+++ b/tests/stompTests.py
@@ -55,6 +55,9 @@
     def unregister_server_address(self):
         self.server_address = None
 
+    def get_method(self, method):
+        return getattr(self, method)
+
 
 @expandPermutations
 class StompTests(TestCaseBase):
diff --git a/vdsm/rpc/Bridge.py b/vdsm/rpc/Bridge.py
index b967ee4..1455c2f 100644
--- a/vdsm/rpc/Bridge.py
+++ b/vdsm/rpc/Bridge.py
@@ -109,12 +109,13 @@
         except KeyError:
             raise VdsmError(5, "Response is missing '%s' member" % member)
 
-    def __getattr__(self, attr):
+    def get_method(self, method):
         try:
-            className, methodName = attr.split('_', 1)
+            (className, methodName) = method.split('.')
             self.api['commands'][className][methodName]
-        except (KeyError, ValueError):
-            raise AttributeError("Attribute not found '%s'" % attr)
+        except (KeyError):
+            raise ValueError("Method %s.%s not found"
+                             % (className, methodName))
         return partial(self._dynamicMethod, className, methodName)
 
     def _convertClassName(self, name):


-- 
To view, visit https://gerrit.ovirt.org/53472
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9b6cb53d8e42335510e34dfe372c1c21b80ac219
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Piotr Kliczewski <piotr.kliczewski at gmail.com>


More information about the vdsm-patches mailing list