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
This commit is contained in:
Ihar Hrachyshka 2014-06-16 09:26:39 +02:00
parent d003a85520
commit 9f2658d8cf
15 changed files with 40 additions and 46 deletions

View File

@ -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:

View File

@ -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(

View File

@ -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(

View File

@ -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),
}

View File

@ -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 {}

View File

@ -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):

View File

@ -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):

View File

@ -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"

View File

@ -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)

View File

@ -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,

View File

@ -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()

View File

@ -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'.

View File

@ -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]

View File

@ -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)

View File

@ -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})