From: Zhiteng Huang <zhiteng.huang@intel.com>
Date: Tue, 17 Jul 2012 07:11:26 +0000 (+0800)
Subject: Convert Cinder to use openstack-common jsonutils.
X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=2f7feffc64ac435846fb9ee6c467ce128f66b20d;p=openstack-build%2Fcinder-build.git

Convert Cinder to use openstack-common jsonutils.

This patch replaces json handling code in Cinder with openstack-common
jsonutils.  Also remove some unused imports.

Implement blueprint cinder-common-jsonutils

Change-Id: I28c1a095823c2f04f30c32724144297a2d67fba4
Reviewed-on: https://review.openstack.org/9912
Reviewed-by: Mark McLoughlin <markmc@redhat.com>
Reviewed-by: Vish Ishaya <vishvananda@gmail.com>
Approved: John Griffith <john.griffith@solidfire.com>
Tested-by: Jenkins
---

diff --git a/cinder/common/policy.py b/cinder/common/policy.py
index ec944a1cc..49ecbdaa9 100644
--- a/cinder/common/policy.py
+++ b/cinder/common/policy.py
@@ -17,10 +17,11 @@
 
 """Common Policy Engine Implementation"""
 
-import json
 import urllib
 import urllib2
 
+from cinder.openstack.common import jsonutils
+
 
 class NotAuthorized(Exception):
     pass
@@ -121,7 +122,7 @@ class Brain(object):
     @classmethod
     def load_json(cls, data, default_rule=None):
         """Init a brain using json instead of a rules dictionary."""
-        rules_dict = json.loads(data)
+        rules_dict = jsonutils.loads(data)
         return cls(rules=rules_dict, default_rule=default_rule)
 
     def __init__(self, rules=None, default_rule=None):
@@ -215,8 +216,8 @@ class HttpBrain(Brain):
 
         """
         url = match % target_dict
-        data = {'target': json.dumps(target_dict),
-                'credentials': json.dumps(cred_dict)}
+        data = {'target': jsonutils.dumps(target_dict),
+                'credentials': jsonutils.dumps(cred_dict)}
         post_data = urllib.urlencode(data)
         f = urllib2.urlopen(url, post_data)
         return f.read() == "True"
diff --git a/cinder/log.py b/cinder/log.py
index 2e458659e..8fec8ebce 100644
--- a/cinder/log.py
+++ b/cinder/log.py
@@ -32,7 +32,6 @@ It also allows setting of formatting information through flags.
 import cStringIO
 import inspect
 import itertools
-import json
 import logging
 import logging.config
 import logging.handlers
@@ -44,6 +43,7 @@ import traceback
 import cinder
 from cinder import flags
 from cinder.openstack.common import cfg
+from cinder.openstack.common import jsonutils
 from cinder.openstack.common import local
 from cinder import version
 
@@ -221,7 +221,7 @@ class JSONFormatter(logging.Formatter):
         if record.exc_info:
             message['traceback'] = self.formatException(record.exc_info)
 
-        return json.dumps(message)
+        return jsonutils.dumps(message)
 
 
 class LegacyCinderFormatter(logging.Formatter):
diff --git a/cinder/notifier/log_notifier.py b/cinder/notifier/log_notifier.py
index 8bd78c016..e6aceaaf3 100644
--- a/cinder/notifier/log_notifier.py
+++ b/cinder/notifier/log_notifier.py
@@ -13,10 +13,10 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-import json
 
 from cinder import flags
 from cinder import log as logging
+from cinder.openstack.common import jsonutils
 
 
 FLAGS = flags.FLAGS
@@ -31,4 +31,4 @@ def notify(message):
     priority = priority.lower()
     logger = logging.getLogger(
             'cinder.notification.%s' % message['event_type'])
-    getattr(logger, priority)(json.dumps(message))
+    getattr(logger, priority)(jsonutils.dumps(message))
diff --git a/cinder/tests/api/openstack/common.py b/cinder/tests/api/openstack/common.py
index 19515ca67..2d2d737e1 100644
--- a/cinder/tests/api/openstack/common.py
+++ b/cinder/tests/api/openstack/common.py
@@ -15,27 +15,9 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-import json
-
 import webob
 
 
-def webob_factory(url):
-    """Factory for removing duplicate webob code from tests"""
-
-    base_url = url
-
-    def web_request(url, method=None, body=None):
-        req = webob.Request.blank("%s%s" % (base_url, url))
-        if method:
-            req.content_type = "application/json"
-            req.method = method
-        if body:
-            req.body = json.dumps(body)
-        return req
-    return web_request
-
-
 def compare_links(actual, expected):
     """Compare xml atom links."""
 
diff --git a/cinder/tests/api/openstack/test_faults.py b/cinder/tests/api/openstack/test_faults.py
index 9d85a14f4..33f830005 100644
--- a/cinder/tests/api/openstack/test_faults.py
+++ b/cinder/tests/api/openstack/test_faults.py
@@ -15,7 +15,6 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-import json
 from xml.dom import minidom
 
 import webob
@@ -25,6 +24,7 @@ import webob.exc
 from cinder import test
 from cinder.api.openstack import common
 from cinder.api.openstack import wsgi
+from cinder.openstack.common import jsonutils
 
 
 class TestFaults(test.TestCase):
@@ -54,7 +54,7 @@ class TestFaults(test.TestCase):
                     "code": 400,
                 },
             }
-            actual = json.loads(response.body)
+            actual = jsonutils.loads(response.body)
 
             self.assertEqual(response.content_type, "application/json")
             self.assertEqual(expected, actual)
@@ -79,7 +79,7 @@ class TestFaults(test.TestCase):
                     "retryAfter": 4,
                 },
             }
-            actual = json.loads(response.body)
+            actual = jsonutils.loads(response.body)
 
             self.assertEqual(response.content_type, "application/json")
             self.assertEqual(expected, actual)
diff --git a/cinder/tests/api/openstack/volume/contrib/test_extended_snapshot_attributes.py b/cinder/tests/api/openstack/volume/contrib/test_extended_snapshot_attributes.py
index 953965a61..56e95e6fa 100644
--- a/cinder/tests/api/openstack/volume/contrib/test_extended_snapshot_attributes.py
+++ b/cinder/tests/api/openstack/volume/contrib/test_extended_snapshot_attributes.py
@@ -15,14 +15,14 @@
 
 from lxml import etree
 import webob
-import json
 
 from cinder.api.openstack.volume.contrib import extended_snapshot_attributes
-from cinder import volume
 from cinder import exception
 from cinder import flags
+from cinder.openstack.common import jsonutils
 from cinder import test
 from cinder.tests.api.openstack import fakes
+from cinder import volume
 
 
 FLAGS = flags.FLAGS
@@ -73,10 +73,10 @@ class ExtendedSnapshotAttributesTest(test.TestCase):
         return res
 
     def _get_snapshot(self, body):
-        return json.loads(body).get('snapshot')
+        return jsonutils.loads(body).get('snapshot')
 
     def _get_snapshots(self, body):
-        return json.loads(body).get('snapshots')
+        return jsonutils.loads(body).get('snapshots')
 
     def assertSnapshotAttributes(self, snapshot, project_id, progress):
         self.assertEqual(snapshot.get('%sproject_id' % self.prefix),
diff --git a/cinder/tests/api/openstack/volume/contrib/test_volume_actions.py b/cinder/tests/api/openstack/volume/contrib/test_volume_actions.py
index 579022013..8edf37d8e 100644
--- a/cinder/tests/api/openstack/volume/contrib/test_volume_actions.py
+++ b/cinder/tests/api/openstack/volume/contrib/test_volume_actions.py
@@ -12,17 +12,12 @@
 #   License for the specific language governing permissions and limitations
 #   under the License.
 
-import datetime
-import json
-
 import webob
 
-from cinder.api.openstack import volume as volume_api
 from cinder import volume
-from cinder import context
-from cinder import exception
 from cinder import flags
 from cinder import test
+from cinder.openstack.common import jsonutils
 from cinder.tests.api.openstack import fakes
 from cinder import utils
 
@@ -59,7 +54,7 @@ class VolumeActionsTest(test.TestCase):
             req = webob.Request.blank('/v1/fake/volumes/%s/action' %
                     self.UUID)
             req.method = 'POST'
-            req.body = json.dumps({_action: None})
+            req.body = jsonutils.dumps({_action: None})
             req.content_type = 'application/json'
             res = req.get_response(app)
             self.assertEqual(res.status_int, 202)
@@ -73,11 +68,11 @@ class VolumeActionsTest(test.TestCase):
         body = {'os-initialize_connection': {'connector': 'fake'}}
         req = webob.Request.blank('/v1/fake/volumes/1/action')
         req.method = "POST"
-        req.body = json.dumps(body)
+        req.body = jsonutils.dumps(body)
         req.headers["content-type"] = "application/json"
 
         res = req.get_response(fakes.wsgi_app())
-        output = json.loads(res.body)
+        output = jsonutils.loads(res.body)
         self.assertEqual(res.status_int, 200)
 
     def test_terminate_connection(self):
@@ -89,7 +84,7 @@ class VolumeActionsTest(test.TestCase):
         body = {'os-terminate_connection': {'connector': 'fake'}}
         req = webob.Request.blank('/v1/fake/volumes/1/action')
         req.method = "POST"
-        req.body = json.dumps(body)
+        req.body = jsonutils.dumps(body)
         req.headers["content-type"] = "application/json"
 
         res = req.get_response(fakes.wsgi_app())
@@ -100,7 +95,7 @@ class VolumeActionsTest(test.TestCase):
                               'mountpoint': '/dev/vdc'}}
         req = webob.Request.blank('/v1/fake/volumes/1/action')
         req.method = "POST"
-        req.body = json.dumps(body)
+        req.body = jsonutils.dumps(body)
         req.headers["content-type"] = "application/json"
 
         res = req.get_response(fakes.wsgi_app())
diff --git a/cinder/tests/api/openstack/volume/test_extensions.py b/cinder/tests/api/openstack/volume/test_extensions.py
index 62b4beba4..72749ce4c 100644
--- a/cinder/tests/api/openstack/volume/test_extensions.py
+++ b/cinder/tests/api/openstack/volume/test_extensions.py
@@ -16,7 +16,6 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-import json
 
 import webob
 from lxml import etree
@@ -25,6 +24,7 @@ import iso8601
 from cinder.api.openstack import volume
 from cinder.api.openstack import xmlutil
 from cinder import flags
+from cinder.openstack.common import jsonutils
 from cinder import test
 
 FLAGS = flags.FLAGS
@@ -59,7 +59,7 @@ class ExtensionControllerTest(ExtensionTestCase):
         self.assertEqual(200, response.status_int)
 
         # Make sure we have all the extensions, extra extensions being OK.
-        data = json.loads(response.body)
+        data = jsonutils.loads(response.body)
         names = [str(x['name']) for x in data['extensions']
                  if str(x['name']) in self.ext_list]
         names.sort()
@@ -86,7 +86,7 @@ class ExtensionControllerTest(ExtensionTestCase):
             url = '/fake/extensions/%s' % ext['alias']
             request = webob.Request.blank(url)
             response = request.get_response(app)
-            output = json.loads(response.body)
+            output = jsonutils.loads(response.body)
             self.assertEqual(output['extension']['alias'], ext['alias'])
 
     def test_get_extension_json(self):
@@ -95,7 +95,7 @@ class ExtensionControllerTest(ExtensionTestCase):
         response = request.get_response(app)
         self.assertEqual(200, response.status_int)
 
-        data = json.loads(response.body)
+        data = jsonutils.loads(response.body)
         self.assertEqual(data['extension'], {
                 "namespace": "http://www.fox.in.socks/api/ext/pie/v1.0",
                 "name": "Fox In Socks",
diff --git a/cinder/tests/integrated/api/client.py b/cinder/tests/integrated/api/client.py
index 51e247d4f..fbd899012 100644
--- a/cinder/tests/integrated/api/client.py
+++ b/cinder/tests/integrated/api/client.py
@@ -14,12 +14,12 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-import json
 import httplib
 import urllib
 import urlparse
 
 from cinder import log as logging
+from cinder.openstack.common import jsonutils
 
 
 LOG = logging.getLogger(__name__)
@@ -167,7 +167,7 @@ class TestOpenStackClient(object):
         body = response.read()
         LOG.debug(_("Decoding JSON: %s") % (body))
         if body:
-            return json.loads(body)
+            return jsonutils.loads(body)
         else:
             return ""
 
@@ -181,7 +181,7 @@ class TestOpenStackClient(object):
         if body:
             headers = kwargs.setdefault('headers', {})
             headers['Content-Type'] = 'application/json'
-            kwargs['body'] = json.dumps(body)
+            kwargs['body'] = jsonutils.dumps(body)
 
         kwargs.setdefault('check_response_status', [200, 202])
         response = self.api_request(relative_uri, **kwargs)
@@ -192,7 +192,7 @@ class TestOpenStackClient(object):
         if body:
             headers = kwargs.setdefault('headers', {})
             headers['Content-Type'] = 'application/json'
-            kwargs['body'] = json.dumps(body)
+            kwargs['body'] = jsonutils.dumps(body)
 
         kwargs.setdefault('check_response_status', [200, 202, 204])
         response = self.api_request(relative_uri, **kwargs)
diff --git a/cinder/tests/test_log.py b/cinder/tests/test_log.py
index 3d07df447..f6e34eabe 100644
--- a/cinder/tests/test_log.py
+++ b/cinder/tests/test_log.py
@@ -1,11 +1,10 @@
 import cStringIO
-import json
 import logging
-import sys
 
 from cinder import context
 from cinder import flags
 from cinder import log
+from cinder.openstack.common import jsonutils
 from cinder.notifier import api as notifier
 from cinder import test
 
@@ -180,7 +179,7 @@ class JSONFormatterTestCase(test.TestCase):
         test_data = {'test': 'log'}
         self.log.debug(test_msg, test_data)
 
-        data = json.loads(self.stream.getvalue())
+        data = jsonutils.loads(self.stream.getvalue())
         self.assertTrue(data)
         self.assertTrue('extra' in data)
         self.assertEqual('test-json', data['name'])
@@ -204,7 +203,7 @@ class JSONFormatterTestCase(test.TestCase):
         except Exception:
             self.log.exception(test_msg, test_data)
 
-        data = json.loads(self.stream.getvalue())
+        data = jsonutils.loads(self.stream.getvalue())
         self.assertTrue(data)
         self.assertTrue('extra' in data)
         self.assertEqual('test-json', data['name'])
diff --git a/cinder/volume/nexenta/jsonrpc.py b/cinder/volume/nexenta/jsonrpc.py
index 7a696f840..7e30d94eb 100644
--- a/cinder/volume/nexenta/jsonrpc.py
+++ b/cinder/volume/nexenta/jsonrpc.py
@@ -22,9 +22,9 @@
 .. moduleauthor:: Yuriy Taraday <yorik.sar@gmail.com>
 """
 
-import json
 import urllib2
 
+from cinder.openstack.common import jsonutils
 from cinder.volume import nexenta
 from cinder import log as logging
 
@@ -55,7 +55,7 @@ class NexentaJSONProxy(object):
                                 obj, method)
 
     def __call__(self, *args):
-        data = json.dumps({'object': self.obj,
+        data = jsonutils.dumps({'object': self.obj,
                            'method': self.method,
                            'params': args})
         auth = ('%s:%s' % (self.user, self.password)).encode('base64')[:-1]
@@ -77,7 +77,7 @@ class NexentaJSONProxy(object):
 
         response_data = response_obj.read()
         LOG.debug(_('Got response: %s'), response_data)
-        response = json.loads(response_data)
+        response = jsonutils.loads(response_data)
         if response.get('error') is not None:
             raise NexentaJSONException(response['error'].get('message', ''))
         else:
diff --git a/cinder/volume/san.py b/cinder/volume/san.py
index af2161259..d57e8675e 100644
--- a/cinder/volume/san.py
+++ b/cinder/volume/san.py
@@ -23,7 +23,6 @@ controller on the SAN hardware.  We expect to access it over SSH or some API.
 
 import base64
 import httplib
-import json
 import os
 import paramiko
 import random
@@ -37,6 +36,7 @@ from cinder import exception
 from cinder import flags
 from cinder import log as logging
 from cinder.openstack.common import cfg
+from cinder.openstack.common import jsonutils
 from cinder import utils
 import cinder.volume.driver
 
@@ -671,7 +671,7 @@ class SolidFireSanISCSIDriver(SanISCSIDriver):
         if params is not None:
             command['params'] = params
 
-        payload = json.dumps(command, ensure_ascii=False)
+        payload = jsonutils.dumps(command, ensure_ascii=False)
         payload.encode('utf-8')
         # we use json-rpc, webserver needs to see json-rpc in header
         header = {'Content-Type': 'application/json-rpc; charset=utf-8'}
@@ -696,7 +696,7 @@ class SolidFireSanISCSIDriver(SanISCSIDriver):
         else:
             data = response.read()
             try:
-                data = json.loads(data)
+                data = jsonutils.loads(data)
 
             except (TypeError, ValueError), exc:
                 connection.close()