summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVishvananda Ishaya <vishvananda@yahoo.com>2010-10-15 22:16:03 +0000
committerTarmac <>2010-10-15 22:16:03 +0000
commitf36cfc4d6c0557f440572f375923c76cd7d7b1df (patch)
treec12c69784ae74d1ba57cb92e1d795f35506b7582
parentcbdba97b592536ae1fbad15d4374684b9ffaa94f (diff)
parent1156100be1d358f5ffae58c6d892e0724f4d153c (diff)
This branch converts incoming data to the api into the proper type.
-rw-r--r--nova/api/ec2/apirequest.py40
-rw-r--r--nova/tests/api_unittest.py17
2 files changed, 57 insertions, 0 deletions
diff --git a/nova/api/ec2/apirequest.py b/nova/api/ec2/apirequest.py
index a87c21fb3..3c8651582 100644
--- a/nova/api/ec2/apirequest.py
+++ b/nova/api/ec2/apirequest.py
@@ -44,6 +44,41 @@ def _underscore_to_xmlcase(str):
res = _underscore_to_camelcase(str)
return res[:1].lower() + res[1:]
+def _try_convert(value):
+ """Return a non-string if possible"""
+ if value == 'None':
+ return None
+ if value == 'True':
+ return True
+ if value == 'False':
+ return False
+ valueneg = value[1:] if value[0] == '-' else value
+ if valueneg == '0':
+ return 0
+ if valueneg == '':
+ return value
+ if valueneg[0] == '0':
+ if valueneg[1] in 'xX':
+ return int(value,16)
+ elif valueneg[1] in 'bB':
+ return int(value,2)
+ else:
+ try:
+ return int(value,8)
+ except ValueError:
+ pass
+ try:
+ return int(value)
+ except ValueError:
+ pass
+ try:
+ return float(value)
+ except ValueError:
+ pass
+ try:
+ return complex(value)
+ except ValueError:
+ return value
class APIRequest(object):
def __init__(self, controller, action):
@@ -66,6 +101,10 @@ class APIRequest(object):
for key, value in kwargs.items():
parts = key.split(".")
key = _camelcase_to_underscore(parts[0])
+ if isinstance(value, str) or isinstance(value, unicode):
+ # NOTE(vish): Automatically convert strings back
+ # into their respective values
+ value = _try_convert(value)
if len(parts) > 1:
d = args.get(key, {})
d[parts[1]] = value
@@ -73,6 +112,7 @@ class APIRequest(object):
args[key] = value
for key in args.keys():
+ # NOTE(vish): Turn numeric dict keys into lists
if isinstance(args[key], dict):
if args[key] != {} and args[key].keys()[0].isdigit():
s = args[key].items()
diff --git a/nova/tests/api_unittest.py b/nova/tests/api_unittest.py
index 414db1e11..3f60f38f2 100644
--- a/nova/tests/api_unittest.py
+++ b/nova/tests/api_unittest.py
@@ -30,6 +30,7 @@ from nova import flags
from nova import test
from nova import api
from nova.api.ec2 import cloud
+from nova.api.ec2 import apirequest
from nova.auth import manager
@@ -82,6 +83,22 @@ class FakeHttplibConnection(object):
pass
+class XmlConversionTestCase(test.BaseTestCase):
+ """Unit test api xml conversion"""
+ def test_number_conversion(self):
+ conv = apirequest._try_convert
+ self.assertEqual(conv('None'), None)
+ self.assertEqual(conv('True'), True)
+ self.assertEqual(conv('False'), False)
+ self.assertEqual(conv('0'), 0)
+ self.assertEqual(conv('42'), 42)
+ self.assertEqual(conv('3.14'), 3.14)
+ self.assertEqual(conv('-57.12'), -57.12)
+ self.assertEqual(conv('0x57'), 0x57)
+ self.assertEqual(conv('-0x57'), -0x57)
+ self.assertEqual(conv('-'), '-')
+ self.assertEqual(conv('-0'), 0)
+
class ApiEc2TestCase(test.BaseTestCase):
"""Unit test for the cloud controller on an EC2 API"""
def setUp(self): # pylint: disable-msg=C0103,C0111