From 9f2658d8cf0b805560169d457ac83d10325f24f2 Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Mon, 16 Jun 2014 09:26:39 +0200 Subject: [PATCH] Consistently use jsonutils instead of specific implementation jsonutils have several benefits in comparison to pure json implementation, like enabling C boosted encoders and decoders for Python2.6 by using simplejson when available. Change-Id: I24d0cd442e8d9d89fac50e43fc97f7bb4a293c3d Closes-Bug: 1329496 --- nova/api/metadata/base.py | 8 ++++---- .../openstack/compute/contrib/security_groups.py | 4 ++-- .../compute/plugins/v3/security_groups.py | 5 ++--- nova/image/glance.py | 3 +-- nova/scheduler/scheduler_options.py | 4 ++-- .../contrib/test_server_external_events.py | 5 ++--- .../plugins/v3/test_server_external_events.py | 5 ++--- nova/tests/api/test_auth.py | 7 +++---- nova/tests/integrated/test_api_samples.py | 5 ++--- .../integrated/v3/test_console_auth_tokens.py | 4 ++-- nova/tests/policy_fixture.py | 6 +++--- nova/tests/test_metadata.py | 15 ++++++++------- nova/tests/virt/libvirt/test_imagecache.py | 4 ++-- nova/virt/libvirt/imagecache.py | 3 +-- nova/virt/storage_users.py | 8 ++++---- 15 files changed, 40 insertions(+), 46 deletions(-) diff --git a/nova/api/metadata/base.py b/nova/api/metadata/base.py index 7d77807dba40..86318ceb37bc 100644 --- a/nova/api/metadata/base.py +++ b/nova/api/metadata/base.py @@ -17,7 +17,6 @@ """Instance Metadata information.""" import base64 -import json import os import posixpath @@ -33,6 +32,7 @@ from nova import network from nova import objects from nova.objects import base as obj_base from nova.openstack.common import importutils +from nova.openstack.common import jsonutils from nova.openstack.common import log as logging from nova.openstack.common import timeutils from nova import utils @@ -327,7 +327,7 @@ class InstanceMetadata(): metadata['random_seed'] = base64.b64encode(os.urandom(512)) self.set_mimetype(MIME_TYPE_APPLICATION_JSON) - return json.dumps(metadata) + return jsonutils.dumps(metadata) def _handle_content(self, path_tokens): if len(path_tokens) == 1: @@ -361,7 +361,7 @@ class InstanceMetadata(): def _vendor_data(self, version, path): if self._check_os_version(HAVANA, version): self.set_mimetype(MIME_TYPE_APPLICATION_JSON) - return json.dumps(self.vddriver.get()) + return jsonutils.dumps(self.vddriver.get()) raise KeyError(path) def _check_version(self, required, requested, versions=VERSIONS): @@ -440,7 +440,7 @@ class InstanceMetadata(): pass filepath = os.path.join('ec2', version, 'meta-data.json') - yield (filepath, json.dumps(data['meta-data'])) + yield (filepath, jsonutils.dumps(data['meta-data'])) ALL_OPENSTACK_VERSIONS = OPENSTACK_VERSIONS + ["latest"] for version in ALL_OPENSTACK_VERSIONS: diff --git a/nova/api/openstack/compute/contrib/security_groups.py b/nova/api/openstack/compute/contrib/security_groups.py index 06e27580b36a..415fcfed9e8f 100644 --- a/nova/api/openstack/compute/contrib/security_groups.py +++ b/nova/api/openstack/compute/contrib/security_groups.py @@ -17,7 +17,6 @@ """The security groups extension.""" import contextlib -import json import webob from webob import exc @@ -32,6 +31,7 @@ from nova import exception from nova.i18n import _ from nova.network.security_group import neutron_driver from nova.network.security_group import openstack_driver +from nova.openstack.common import jsonutils from nova.openstack.common import log as logging from nova.openstack.common import xmlutils from nova.virt import netutils @@ -565,7 +565,7 @@ class SecurityGroupsOutputController(wsgi.Controller): else: try: # try converting to json - req_obj = json.loads(req.body) + req_obj = jsonutils.loads(req.body) # Add security group to server, if no security group was in # request add default since that is the group it is part of servers[0][key] = req_obj['server'].get( diff --git a/nova/api/openstack/compute/plugins/v3/security_groups.py b/nova/api/openstack/compute/plugins/v3/security_groups.py index 08aa949d04ae..19bc5fa385d1 100644 --- a/nova/api/openstack/compute/plugins/v3/security_groups.py +++ b/nova/api/openstack/compute/plugins/v3/security_groups.py @@ -16,8 +16,6 @@ """The security groups extension.""" -import json - from nova.api.openstack.compute.schemas.v3 import security_groups as \ schema_security_groups from nova.api.openstack import extensions @@ -27,6 +25,7 @@ from nova.compute import api as compute_api from nova import exception from nova.network.security_group import neutron_driver from nova.network.security_group import openstack_driver +from nova.openstack.common import jsonutils ALIAS = 'os-security-groups' @@ -81,7 +80,7 @@ class SecurityGroupsOutputController(wsgi.Controller): # one server in an API request. else: # try converting to json - req_obj = json.loads(req.body) + req_obj = jsonutils.loads(req.body) # Add security group to server, if no security group was in # request add default since that is the group it is part of servers[0][ATTRIBUTE_NAME] = req_obj['server'].get( diff --git a/nova/image/glance.py b/nova/image/glance.py index 1f2a0664e4b9..fccbafa55bff 100644 --- a/nova/image/glance.py +++ b/nova/image/glance.py @@ -19,7 +19,6 @@ from __future__ import absolute_import import copy import itertools -import json import random import sys import time @@ -126,7 +125,7 @@ def generate_identity_headers(context, status='Confirmed'): 'X-Tenant-Id': getattr(context, 'tenant', None), 'X-Roles': ','.join(context.roles), 'X-Identity-Status': status, - 'X-Service-Catalog': json.dumps(context.service_catalog), + 'X-Service-Catalog': jsonutils.dumps(context.service_catalog), } diff --git a/nova/scheduler/scheduler_options.py b/nova/scheduler/scheduler_options.py index 48019d5187ac..c0cf848d1961 100644 --- a/nova/scheduler/scheduler_options.py +++ b/nova/scheduler/scheduler_options.py @@ -21,13 +21,13 @@ dynamic configuration. """ import datetime -import json import os from oslo.config import cfg from nova.i18n import _ from nova.openstack.common import excutils +from nova.openstack.common import jsonutils from nova.openstack.common import log as logging from nova.openstack.common import timeutils @@ -73,7 +73,7 @@ class SchedulerOptions(object): def _load_file(self, handle): """Decode the JSON file. Broken out for testing.""" try: - return json.load(handle) + return jsonutils.load(handle) except ValueError as e: LOG.exception(_("Could not decode scheduler options: '%s'"), e) return {} diff --git a/nova/tests/api/openstack/compute/contrib/test_server_external_events.py b/nova/tests/api/openstack/compute/contrib/test_server_external_events.py index c2852fdde416..5439ce79db1b 100644 --- a/nova/tests/api/openstack/compute/contrib/test_server_external_events.py +++ b/nova/tests/api/openstack/compute/contrib/test_server_external_events.py @@ -12,8 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import json - import mock import webob @@ -21,6 +19,7 @@ from nova.api.openstack.compute.contrib import server_external_events from nova import context from nova import exception from nova import objects +from nova.openstack.common import jsonutils from nova import test fake_instances = { @@ -71,7 +70,7 @@ class ServerExternalEventsTest(test.NoDBTestCase): req.method = 'POST' req.headers['content-type'] = 'application/json' req.environ['nova.context'] = self.context - req.body = json.dumps(body) + req.body = jsonutils.dumps(body) return req def _assert_call(self, req, body, expected_uuids, expected_events): diff --git a/nova/tests/api/openstack/compute/plugins/v3/test_server_external_events.py b/nova/tests/api/openstack/compute/plugins/v3/test_server_external_events.py index 882a4859841e..52ce2941a00b 100644 --- a/nova/tests/api/openstack/compute/plugins/v3/test_server_external_events.py +++ b/nova/tests/api/openstack/compute/plugins/v3/test_server_external_events.py @@ -12,8 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import json - import mock import webob @@ -21,6 +19,7 @@ from nova.api.openstack.compute.plugins.v3 import server_external_events from nova import context from nova import exception from nova import objects +from nova.openstack.common import jsonutils from nova import test fake_instances = { @@ -66,7 +65,7 @@ class ServerExternalEventsTest(test.NoDBTestCase): req.method = 'POST' req.headers['content-type'] = 'application/json' req.environ['nova.context'] = self.context - req.body = json.dumps(body) + req.body = jsonutils.dumps(body) return req def _assert_call(self, req, body, expected_uuids, expected_events): diff --git a/nova/tests/api/test_auth.py b/nova/tests/api/test_auth.py index 992ba48942a6..8505e381e60e 100644 --- a/nova/tests/api/test_auth.py +++ b/nova/tests/api/test_auth.py @@ -12,14 +12,13 @@ # License for the specific language governing permissions and limitations # under the License. -import json - from oslo.config import cfg import webob import webob.exc import nova.api.auth from nova.i18n import _ +from nova.openstack.common import jsonutils from nova.openstack.common.middleware import request_id from nova import test @@ -41,7 +40,7 @@ class TestNovaKeystoneContextMiddleware(test.NoDBTestCase): self.request = webob.Request.blank('/') self.request.headers['X_TENANT_ID'] = 'testtenantid' self.request.headers['X_AUTH_TOKEN'] = 'testauthtoken' - self.request.headers['X_SERVICE_CATALOG'] = json.dumps({}) + self.request.headers['X_SERVICE_CATALOG'] = jsonutils.dumps({}) def test_no_user_or_user_id(self): response = self.request.get_response(self.middleware) @@ -102,7 +101,7 @@ class TestKeystoneMiddlewareRoles(test.NoDBTestCase): self.request.headers['X_USER'] = 'testuser' self.request.headers['X_TENANT_ID'] = 'testtenantid' self.request.headers['X_AUTH_TOKEN'] = 'testauthtoken' - self.request.headers['X_SERVICE_CATALOG'] = json.dumps({}) + self.request.headers['X_SERVICE_CATALOG'] = jsonutils.dumps({}) self.roles = "pawn, knight, rook" diff --git a/nova/tests/integrated/test_api_samples.py b/nova/tests/integrated/test_api_samples.py index 0783abfa38eb..4707a7488956 100644 --- a/nova/tests/integrated/test_api_samples.py +++ b/nova/tests/integrated/test_api_samples.py @@ -17,7 +17,6 @@ import base64 import copy import datetime import inspect -import json import os import re import urllib @@ -2128,7 +2127,7 @@ class ConsoleAuthTokensSampleJsonTests(ServersSampleBase): "Console_auth_tokens") def _get_console_url(self, data): - return json.loads(data)["console"]["url"] + return jsonutils.loads(data)["console"]["url"] def _get_console_token(self, uuid): response = self._do_post('servers/%s/action' % uuid, @@ -2600,7 +2599,7 @@ class OsNetworksJsonTests(ApiSampleTestBaseV2): def test_delete_network(self): response = self._do_post('os-tenant-networks', "networks-post-req", {}) - net = json.loads(response.read()) + net = jsonutils.loads(response.read()) response = self._do_delete('os-tenant-networks/%s' % net["network"]["id"]) self.assertEqual(response.status, 202) diff --git a/nova/tests/integrated/v3/test_console_auth_tokens.py b/nova/tests/integrated/v3/test_console_auth_tokens.py index a7cc228a5719..100bc84da9c9 100644 --- a/nova/tests/integrated/v3/test_console_auth_tokens.py +++ b/nova/tests/integrated/v3/test_console_auth_tokens.py @@ -12,9 +12,9 @@ # License for the specific language governing permissions and limitations # under the License. -import json import re +from nova.openstack.common import jsonutils from nova.tests.integrated.v3 import test_servers @@ -23,7 +23,7 @@ class ConsoleAuthTokensSampleJsonTests(test_servers.ServersSampleBase): extra_extensions_to_load = ["os-remote-consoles"] def _get_console_url(self, data): - return json.loads(data)["console"]["url"] + return jsonutils.loads(data)["console"]["url"] def _get_console_token(self, uuid): response = self._do_post('servers/%s/action' % uuid, diff --git a/nova/tests/policy_fixture.py b/nova/tests/policy_fixture.py index 8f7e7206fdde..3da8cc7d8a08 100644 --- a/nova/tests/policy_fixture.py +++ b/nova/tests/policy_fixture.py @@ -12,12 +12,12 @@ # License for the specific language governing permissions and limitations # under the License. -import json import os import fixtures from oslo.config import cfg +from nova.openstack.common import jsonutils from nova.openstack.common import policy as common_policy import nova.policy from nova.tests import fake_policy @@ -56,7 +56,7 @@ class RoleBasedPolicyFixture(fixtures.Fixture): allow users of the specified role only """ super(RoleBasedPolicyFixture, self).setUp() - policy = json.load(open(CONF.policy_file)) + policy = jsonutils.load(open(CONF.policy_file)) # Convert all actions to require specified role for action, rule in policy.iteritems(): @@ -66,7 +66,7 @@ class RoleBasedPolicyFixture(fixtures.Fixture): self.policy_file_name = os.path.join(self.policy_dir.path, 'policy.json') with open(self.policy_file_name, 'w') as policy_file: - json.dump(policy, policy_file) + jsonutils.dump(policy, policy_file) CONF.set_override('policy_file', self.policy_file_name) nova.policy.reset() nova.policy.init() diff --git a/nova/tests/test_metadata.py b/nova/tests/test_metadata.py index 8ea9d3b1fa3f..7bf28e1131be 100644 --- a/nova/tests/test_metadata.py +++ b/nova/tests/test_metadata.py @@ -19,7 +19,6 @@ import base64 import hashlib import hmac -import json import re try: @@ -43,6 +42,7 @@ from nova.db.sqlalchemy import api from nova import exception from nova.network import api as network_api from nova import objects +from nova.openstack.common import jsonutils from nova import test from nova.tests import fake_block_device from nova.tests import fake_instance @@ -417,7 +417,7 @@ class OpenStackMetadataTestCase(test.TestCase): mdjson = mdinst.lookup("/openstack/2012-08-10/meta_data.json") mdjson = mdinst.lookup("/openstack/latest/meta_data.json") - mddict = json.loads(mdjson) + mddict = jsonutils.loads(mdjson) self.assertEqual(mddict['uuid'], self.instance['uuid']) self.assertIn('files', mddict) @@ -447,7 +447,7 @@ class OpenStackMetadataTestCase(test.TestCase): mdinst = fake_InstanceMetadata(self.stubs, inst, extra_md=extra) mdjson = mdinst.lookup("/openstack/2012-08-10/meta_data.json") - mddict = json.loads(mdjson) + mddict = jsonutils.loads(mdjson) for key, val in extra.iteritems(): self.assertEqual(mddict[key], val) @@ -485,20 +485,21 @@ class OpenStackMetadataTestCase(test.TestCase): # verify that 2013-04-04 has the 'random' field mdjson = mdinst.lookup("/openstack/2013-04-04/meta_data.json") - mddict = json.loads(mdjson) + mddict = jsonutils.loads(mdjson) self.assertIn("random_seed", mddict) self.assertEqual(len(base64.b64decode(mddict["random_seed"])), 512) # verify that older version do not have it mdjson = mdinst.lookup("/openstack/2012-08-10/meta_data.json") - self.assertNotIn("random_seed", json.loads(mdjson)) + self.assertNotIn("random_seed", jsonutils.loads(mdjson)) def test_no_dashes_in_metadata(self): # top level entries in meta_data should not contain '-' in their name inst = self.instance.obj_clone() mdinst = fake_InstanceMetadata(self.stubs, inst) - mdjson = json.loads(mdinst.lookup("/openstack/latest/meta_data.json")) + mdjson = jsonutils.loads( + mdinst.lookup("/openstack/latest/meta_data.json")) self.assertEqual([], [k for k in mdjson.keys() if k.find("-") != -1]) @@ -534,7 +535,7 @@ class OpenStackMetadataTestCase(test.TestCase): # verify that 2013-10-17 has the vendor_data.json file vdpath = "/openstack/2013-10-17/vendor_data.json" - vd = json.loads(mdinst.lookup(vdpath)) + vd = jsonutils.loads(mdinst.lookup(vdpath)) # the instance should be passed through, and our class copies the # uuid through to 'inst_uuid'. diff --git a/nova/tests/virt/libvirt/test_imagecache.py b/nova/tests/virt/libvirt/test_imagecache.py index 5c05f4a14e44..1045f8e3bbad 100644 --- a/nova/tests/virt/libvirt/test_imagecache.py +++ b/nova/tests/virt/libvirt/test_imagecache.py @@ -17,7 +17,6 @@ import contextlib import cStringIO import hashlib -import json import os import time @@ -26,6 +25,7 @@ from oslo.config import cfg from nova import conductor from nova import db from nova.openstack.common import importutils +from nova.openstack.common import jsonutils from nova.openstack.common import log as logging from nova.openstack.common import processutils from nova import test @@ -501,7 +501,7 @@ class ImageCacheManagerTestCase(test.NoDBTestCase): d = {'sha1': '21323454'} with open('%s.info' % fname, 'w') as f: - f.write(json.dumps(d)) + f.write(jsonutils.dumps(d)) image_cache_manager = imagecache.ImageCacheManager() image_cache_manager.unexplained_images = [fname] diff --git a/nova/virt/libvirt/imagecache.py b/nova/virt/libvirt/imagecache.py index cd17d11e2c1c..fa8e460ded11 100644 --- a/nova/virt/libvirt/imagecache.py +++ b/nova/virt/libvirt/imagecache.py @@ -21,7 +21,6 @@ http://wiki.openstack.org/nova-image-cache-management. """ import hashlib -import json import os import re import time @@ -206,7 +205,7 @@ def write_stored_info(target, field=None, value=None): d['%s-timestamp' % field] = time.time() with open(info_file, 'w') as f: - f.write(json.dumps(d)) + f.write(jsonutils.dumps(d)) write_file(info_file, field, value) diff --git a/nova/virt/storage_users.py b/nova/virt/storage_users.py index 58e7f58498fb..edb0215f8d56 100644 --- a/nova/virt/storage_users.py +++ b/nova/virt/storage_users.py @@ -13,13 +13,13 @@ # under the License. -import json import os import time from oslo.config import cfg from nova.i18n import _ +from nova.openstack.common import jsonutils from nova.openstack.common import log as logging from nova import utils @@ -59,7 +59,7 @@ def register_storage_use(storage_path, hostname): if os.path.exists(id_path): with open(id_path) as f: try: - d = json.loads(f.read()) + d = jsonutils.loads(f.read()) except ValueError: LOG.warning(_("Cannot decode JSON from %(id_path)s"), {"id_path": id_path}) @@ -67,7 +67,7 @@ def register_storage_use(storage_path, hostname): d[hostname] = time.time() with open(id_path, 'w') as f: - f.write(json.dumps(d)) + f.write(jsonutils.dumps(d)) return do_register_storage_use(storage_path, hostname) @@ -97,7 +97,7 @@ def get_storage_users(storage_path): if os.path.exists(id_path): with open(id_path) as f: try: - d = json.loads(f.read()) + d = jsonutils.loads(f.read()) except ValueError: LOG.warning(_("Cannot decode JSON from %(id_path)s"), {"id_path": id_path})